Finally functions to get you what you want!
If you are a Revit API enthusiast, then at certain point you wanted to get your elements or types just by writing Type or Family name as a string. 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 you will find snippets and a brief explanation on how to make a function to do that using ElementParameterFilter Class.
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 case
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."""
param_id = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), type_name,True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), type_name)
filter_type_name = ElementParameterFilter(f_rule)
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsNotElementType().ToElements()
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."""
param_id = ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name, True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name)
filter_type_name = ElementParameterFilter(f_rule)
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsNotElementType().ToElements()
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."""
param_id = ElementId(BuiltInParameter.ALL_MODEL_FAMILY_NAME)
f_param = ParameterValueProvider(param_id)
f_evaluator = FilterStringEquals()
if rvt_year < 2023:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name, True)
else:
f_rule = FilterStringRule(f_param, FilterStringEquals(), family_name)
filter_type_name = ElementParameterFilter(f_rule)
return FilteredElementCollector(doc).WherePasses(filter_type_name)\
.WhereElementIsElementType().ToElements()
types = get_elements_by_family_name('Family Name')
ElementParameterFilter
These are really good 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 😉