Element Selection Filter


How Can You Apply a Filter When Selecting Elements?
Have you ever considered that during the selection process, you might want to choose only a specific type of element? For instance, your Revit model may contain various elements, but you only want to select the pipes.
There is an Interface in the Revit API specifically designed to address this scenario.
: ISelectionFilter
Here is a snippet demonstrating how to implement the ISelectionFilter interface.


This is how the ISelectionFilter interface body looks like 👇
Now the question is how to write the logic so that the filter is applied only to the Pipe element? 🤔


In the snapshot below, there are two methods in the interface: AllowElement and AllowReference. To filter the element type, we need to take two steps:
1. Check whether the element belongs to a valid category (e.g., Pipe, Duct, or Cable Tray).
2. Apply the required element type class within the if statement.
The AllowElement method in the SelectionFilter class checks if an Element object can be permitted based on specific criteria. It ensures the Category. Id is not null and the element is of type Pipe. If both conditions are satisfied, it returns true; otherwise, it returns false.
Similarly, in a corporate email filter, the method verifies that the Email object has a valid domain (non-null Domain.Id) and is marked as important (type HighPriority). If both criteria are met, it flags the email for review; if not, the email remains unflagged.