LearnRevitAPI

Nov 7, 2023

Filter Elements with Parameters


If you are a Revit API enthusiast, then at certain point you wanted to get your elements based on a parameter value.

We can get elements with OfClass or OfCategory and then make a for loop... 

But what if you want to make it more efficient?

💡Let's learn how to use ElementParameterFilter

😉Learn in Action

By the end of this email you will understand how ElementParameterFilter works and you will have a function to get elements by Type/Family Name. 

👀 Isn't it what you always wanted ?

🧩 ElementParameterFilter

ElementParameterFilter allows us to create a filter that targets specific parameter. We can provide a value and choose evaluator.

💡 And it's not that complicated once you make it work once!

There are 4 constructors and they are very similar. They all need FilterRule

💡 Let's use the first one as it's the most common and simple one.

🤔What is FilterRule?

FilterRule class allows us to provide Parameter, Evaluator and Value to check. And there are different classes depending on the StorageType of the parameter.

In our case, we will be checking Type/Family Names, so we need to choose FilterStringRule for that.

✨How to create FilterRule

If you compared constructors above, you might have noticed that all of them require similar arguments.

valueProvider   |   evaluator   |   ruleValue

Before I explain how to create them, let's look at Filters in Revit UI.
When you create View Filters we need to define Filter Rules, and it's very similar to Revit API.

We need to:

 1️⃣ Define Parameter 
2️⃣ Choose Evaluator
3️⃣ Provide Value

Let's do the same with Revit API.

1️⃣ Define Parameter

To provide a parameter for a rule we need to use ParameterValueProvider.
It has very simple constructor that takes ElementId of a Parameter. And we can get it by using BuiltInParameter Enumeration.

2️⃣ Evaluator

Next argument is - Evaluator.
There are many classes to choose from, but it's actually quite simple.

First of all you need to decide is your parameter Text or Numbers based?
Secondly, you need to choose evaluator type.
Do you want Equals, Less, Greater, Contains... 

💡 I will choose FilterStringEquals because I want the exact match!

3️⃣ Value

Lastly, we need a value.
This should match StorageType of your parameter. Since we are dealing with Type and Family names, we want to provide a string.

✅Create Filter

We have all the arguments so we can put them to use.
1️⃣ Let's create FilterStringRule
2️⃣ then we can use it to create ElementParameterFilter
3️⃣ and lastly we can use it with WherePasses() in FEC 

💡 Also note that Revit API had some changes in 2022, so FilterStringRule has removed caseSensetive parameter after 2022. 

👉Reusable Function

Lastly, let's make it a function so you can reuse it in your own script by just providing correct Type Name. 

And if you want to change it to Family Name then change BuiltInParameter to ALL_MODEL_FAMILY_NAME

Now you can modify very little to make it work for other uses.

1️⃣ Change BuiltInParameter
2️⃣ Choose Evaluator
3️⃣ Choose FilterRule Class
4️⃣ Change Value
5️⃣ Bonus: extend FIlteredElementCollector methods. 

💪 Practice it for your own use-cases. 

Once you adjust it to your own needs it will click and you won't have issues using it anymore! Now you know how to use ElementParameterFilter

🔥FREE PDF Guide

P.S.

Make sure that you have my FREE PDF guide on FilteredElementCollectors. Half of this guide is about Revit API filters, so you will learn even more!
There are plenty of examples you can use!

Get FREE PDF GUIDE

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!