How To Find Elements

A reader asked how to count the number of doors in a model. Here is a 5 line macro:

  1. Get the document
  2. Create a FilteredElementCollector for this document
  3. Create a list of doors using this collector. There are 3 methods applied to the collector
    1. OfClass restricts the collector to only FamilyInstance elements
    2. OfCategory restricts the collector to doors
    3. ToList converts the output of the collector into a list
  4. Create an integer variable to store the count of the list
  5. Display the count to the user.
public void CountDoors()
    Document doc = this.ActiveUIDocument.Document;
    FilteredElementCollector collector = new FilteredElementCollector(doc);
    List<Element> doorList = collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).ToList();
    int doorCount = doorList.Count;
    TaskDialog.Show("Door Count", "Your projects has " + doorCount + " doors");

He also asked how to count the number of doors in a view. This should be possible to do for the active view by changing the 2nd line to:

collector = new FilteredElementCollector(doc, doc.ActiveView.Id);

This version of the constructor constructs a new FilteredElementCollector that will search and filter the visible elements in a view. But the documentation is a bit vague about what exactly will be found. It says:

“Elements that will be passed by the collector have graphics that may be visible in the input view. Some elements may still be hidden because they are obscured by other elements.”

When I run make this change and run the macro in a ceiling plan where the doors are not visible, it correctly says there are 0 doors.

But when the active view is Level 2 Plan or East Elevation, where no doors are visible, the collector finds them anyway. So, specifying the view in the FilteredElementCollector constructor will not always produce the desired result.

4 thoughts on “How To Find Elements

  1. The term ‘collector’ in FilteredElementCollector makes me wonder if it’s possible to save a selection, with a name, and call upon it later.

    Thinking through the possible approach, you’d begin by activating the command. Once active, the element id of any element the user selects would be stored. Once the selection process ends, maybe by hitting enter, the user is prompted for a name to give the list. Calling upon that list might be a different macro? Either the user types in a name (prone to mor errors i’d assume?) or a pull down of all saved selections?

  2. I understand that this method doesn’t work for Curtain Wall Panels. What options are there for counting them?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s