Dynamo Python Primer
  • Take Dynamo Further 🚀
  • 1 Hello Python 🐍
    • 1.1 Why Should I Learn to Code?
    • 1.2 Python Introduction
    • 1.3 What is IronPython?
  • 2 Seeing The Bigger Picture 🔭
    • 2.1 Context Matters
    • 2.2 What is an API?
    • 2.3 The .NET Framework
    • 2.4 Object-Oriented Programming
  • 3 Getting Started 🛴
    • 3.1 Dynamo's Python Node
    • 3.2 Boilerplate Setup Code
    • 3.3 Basics: Input and Output
    • 3.4 Common Errors
  • 4 Revit-Specific Topics 🏡
    • 4.1 Introduction to Revit's API
    • 4.2 How to Read Revit's API Documentation
    • 4.3 Doc, UIDoc, App, UIApp
    • 4.4 Unwrapping Revit Elements
    • 4.5 The FilteredElementCollector
    • 4.6 Geometry Conversion Methods
    • 4.7 Working With Parameters
      • Family Parameters
      • Global Parameters
      • Project Information Parameters
      • Group Parameters
      • Built-In Parameters
    • 4.8 Working With Transactions
    • 4.9 Opening & Closing External Files
    • 4.10 Prompting UI Selection
    • 4.11 Working With Units
    • 4.12 Built-In Categories
    • 4.13 Family Acrobatics
    • 4.14 Feedback: TaskDialogs
  • 5 Glossary 📚
  • About This Primer 👋
Powered by GitBook
On this page

Was this helpful?

  1. 4 Revit-Specific Topics 🏡

4.5 The FilteredElementCollector

One of the most powerful tools in Revit API fetches elements - fast!

Previous4.4 Unwrapping Revit ElementsNext4.6 Geometry Conversion Methods

Last updated 5 years ago

Was this helpful?

Introduction

As its name implies, Revit's FilteredElementCollector lets you rapidly search through the Revit document's database for elements, using a custom-defined set of filter rules. You can stack search filters and modifiers together, making the number of custom searches you can undertake practically infinite.

The subject of FilteredElementCollectors is extensive and is thoroughly-covered in the ever-excellent .

A Quick Example

FilteredElementCollectors will return a list, either of Element or ElementIds. It's fairly simple to throw a quick one together. For example:

#Boilerplate code
all_furniture = FilteredElementCollector(doc)
all_furniture.OfCategory(BuiltInCategory.OST_Furniture)
all_furniture.WhereElementIsNotElementType()
all_furniture.ToElements()
OUT = all_furniture
  • This code starts by creating a new FilteredElementCollector instance, which takes the as the argument in its constructor.

  • The collector is then furniture-refined by specifying a filter - in our example, we only want to return elements of the Furniture category. This is specified as an option in the .

  • Line 4 adds in a further filter - we don't want to include Furniture Family Types in our returned elements, only instances.

  • In Line 5 we specify we want the FilteredElementCollector to return us the actual Revit elements (instead of their ElementIds).

  • Finally, Line 6 uses to output the collected elements from the node.

It's also possible to condense the above script down to a single line, like so:

#Boilerplate code
all_furniture = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Furniture).WhereElementIsNotElementType().ToElements()
OUT = all_furniture

Both scripts will return exactly the same object, so it's down to you to use whichever you prefer - the longer version or the more verbose version.

Make a Basic Filter: A Step-By-Step Guide

We've put together a rough step-by-step guide on how to assemble basic filters for elements:

  1. Do you want to return element instances or their types? For instance, are you looking for all TextNotes in a document (each might have different text), or all TextNoteTypes (e.g. Arial 2.0mm)? If you want instances, use the .WhereElementIsNotElementType() filter. If you want only types, use the .WhereElementIsElementType() filter. If you want both, don't either of these filters!

  2. Do you want to return Revit elements, or the elements' IDs? Finish off your FilteredElementCollector query with either .ToElements() or .ToElementIds().

Advanced Filtering

The above is a rough-and-ready guide to constructing a basic FilteredElementCollector. However, there are many more custom filters you can build:

  • You can use a ElementLevelFilter to filter for elements hosted at a level, or an ElementWorksetFilter to filter only for elements on a specific workset.

  • You can build up custom evaluation rules. Looking only for elements who have a 'Ext' in their Mark parameter value or a Height parameter greater than 3000mm? This is possible.

Other Notes

In no particular order, some of the quirks or oddities of the collector:

  • Worksets can be filtered, but came as a later addition to the API and they have their own special filter method known as the FilteredWorksetCollector.

  • You can filter just down to the elements in a specific view if you add in the view's ID as a second argument to the constructor.

Know what you're looking to filter for. If that's elements of a Revit category, find the equivalent in the . Otherwise, if you're looking for elements of a particular API class, you can use the .OfClass() filter, adding in the Class name as an argument.

This page just scratches the surface of FilteredElementCollectors. We've not even covered QuickFilters and SlowFilters here. Make sure to check out for extensively-documented details!

The Building Coder blog
Revit document
BuiltInCategory enumeration
OUT
BuiltInCategory
BuiltInCategory enumeration
The Building Coder