Get Elements with ElementParameterFilter
Filter Elements by parameter Value
If you are a Revit API enthusiast, then at certain point you wanted to get your elements or types with a parameter value. Foe example get all instance of a specific Type or Family Name. It sounds like a very logical thing that should be within Revit API, but it's not.
So let me help you with that! Below I will provide a brief explanation on how to make a function to get elements by Type or Family name using ElementParameterFilter Class.
And also you will learn how to modify it for any other parameter!
Get Element of a certain Type Name
We will use ElementParameterFilter to create a function to get elements by Type Names.
It needs:
Parameter Id to create ParameterValueProvider
Evaluator (FilterStringEquals) in this caseAlso notice that there were changes in Revit API for this class so I will account for that too
Also notice that there were changes in Revit API for this class so I will account for that too
Then we can provide all of this to create ElementParameterFiler
And we can use this filter for FilteredElementCollector with WherePasses
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = int(app.VersionNumber)
def get_elements_by_type_name(type_name):
"""Function to get Elements by Type Name."""
# CREATE RULE
param_id = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
# Revit API has changes
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), type_name,True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), type_name)
# CREATE FILTER
filter_type_name = ElementParameterFilter(f_rule)
# GET ELEMENTS
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsNotElementType().ToElements()
# Get Elements
elements = get_elements_by_type_name('Type Name')
print(elements)Get Elements with Family Name
We can also modify very little a get elements by Family name instead of type.
To do that we need to
- Change Parameter Id to BuiltInParameter.ALL_MODEL_FAMILY_NAME
- Rename function and variables
The rest stays the same, here is complete Snippet.
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = int(app.VersionNumber)
def get_elements_by_family_name(family_name):
"""Function to get Elements by Family Name."""
# CREATE RULE
param_id = ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
# Revit API has changes
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name, True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name)
# CREATE FILTER
filter_type_name = ElementParameterFilter(f_rule)
# GET ELEMENTS
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsNotElementType().ToElements()
# Get Elements
elements = get_elements_by_family_name('Filled Region')
print(elements)Get Types by Type or Family Name
You might also want to get types instead of instances and you can use exact duplicates of these functions but replace
collector.WhereElementIsNotElementType() -> collector.WhereElementIsElementType()Here is an example:
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = int(app.VersionNumber)
def get_types_by_family_name(family_name):
"""Function to get Elements by Family Name."""
# CREATE RULE
param_id = ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
# Revit API has changes
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name, True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name)
# CREATE FILTER
filter_type_name = ElementParameterFilter(f_rule)
# GET ELEMENTS
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsElementType().ToElements()
# Get Types
types = get_elements_by_family_name('Family Name')ElementParameterFilter
These are examples on how to use ElementParameterFilter.
You can adjust very little and it will work with other parameters, and evaluators.
There will be a post about it in more detail in the future 😉