Revit API: Get Elements or Types by Type/Family name

Here you will learn about getting Elements,Type or Family by providing a Type or Family Name.

Tags:  Revit API
Sep 2022

Finally functions to get you what you want!

I remember when I started with Revit API and information for python users was scarce. There were a few golden nuggets here and there, but mostly I had to go through multiple forums and blogs and assemble my Frankenstein's Code Monster. So I want to give you a few functions that can be very useful.

Below you will find a series of snippets to:

  • Get Elements of a certain Type Name
  • Get Elements of a certain Family Name
  • Get Element's Type by Name
  • Get Element's Family by name

Reusable Functions

code
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()
    f_rule      = FilterStringRule(f_param, f_evaluator  , type_name, True) 
    # Revit 2023 does not need last argument in f_rule!

    # CREATE FILTER
    filter_type_name = ElementParameterFilter(f_rule)

    # GET ELEMENTS
    return FilteredElementCollector(doc).WherePasses(filter_type_name)\
                          .WhereElementIsNotElementType().ToElements()
code
def get_elements_by_family_name(family_name):
    """Function to get Elements by Family Name."""

    # CREATE RULE
    param_id    = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
    f_param     = ParameterValueProvider(param_id)
    f_evaluator = FilterStringEquals()
    f_rule      = FilterStringRule(f_param, f_evaluator  , family_name, True) 
    # Revit 2023 does not need last argument in f_rule!

    # CREATE FILTER
    filter_family_name = ElementParameterFilter(f_rule)

    # GET ELEMENTS
    return FilteredElementCollector(doc).WherePasses(filter_family_name)\
                               .WhereElementIsElementType().ToElements()
code
def get_type_by_name(type_name):
    """Function to get Type by Name."""

    # CREATE RULE
    param_id    = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
    f_param     = ParameterValueProvider(param_id)
    f_evaluator = FilterStringEquals()
    f_rule      = FilterStringRule(f_param, f_evaluator  , type_name, True) 
    # Revit 2023 does not need last argument in f_rule!

    # CREATE FILTER
    filter_type_name = ElementParameterFilter(f_rule)

    # GET ELEMENTS
    return FilteredElementCollector(doc).WherePasses(filter_type_name)\
                           .WhereElementIsElementType().FirstElement()
code
def get_family_by_name(family_name):
    """Function to get Family by Name."""

    # CREATE RULE
    param_id    = ElementId(BuiltInParameter.ALL_MODEL_TYPE_NAME)
    f_param     = ParameterValueProvider(param_id)
    f_evaluator = FilterStringEquals()
    f_rule      = FilterStringRule(f_param, f_evaluator  , family_name, True) 
    # Revit 2023 does not need last argument in f_rule!

    # CREATE FILTER
    filter_family_name = ElementParameterFilter(f_rule)

    # GET ELEMENTS
    return FilteredElementCollector(doc).WherePasses(filter_family_name)\
                             .WhereElementIsElementType().FirstElement()

Let's Sum it up

code
# IMPORTS
from Autodesk.Revit.DB import *

# VARIABLES
doc   = __revit__.ActiveUIDocument.Document

# FUNCTIONS
#TODO Import functions from your file or place function here
from CustomFunctions import (get_elements_by_type_name, 
                             get_elements_by_family_name, 
                             get_type_by_name, 
                             get_family_by_name)

# MAIN
elements_by_type_name   = get_elements_by_type_name('TYPE NAME HERE')
elements_by_family_name = get_elements_by_family_name('FAMILY NAME HERE')

type_by_name   = get_type_by_name('TYPE NAME HERE')
family_by_name = get_family_by_name('FAMILY NAME HERE')

⌨️ Happy Coding!

— Erik Frits