avatarEduardo Di Loreto

Summary

The web content provides a technical guide on using the Revit API with Python to select and manipulate elements within a Revit model, including both the active and linked models.

Abstract

The article is a comprehensive guide for using Python with the Revit API to interact with elements in a Revit model. It begins by emphasizing the necessity of having the Revit Python Shell installed to execute the provided code snippets. The guide covers various methods for element selection, such as retrieving all elements, those in the active view, owned by a view, of a specific category like "Floors," or of a specific class like "ViewSheet." It also distinguishes between element instances and types. The article explains how to combine multiple selection criteria within a FilteredElementCollector instance. Additionally, it demonstrates graphical selection techniques, including obtaining selected elements and picking elements by rectangle, with or without a custom selection filter. The guide concludes by addressing the selection of elements from linked models, highlighting the need to access the linked document within the Revit application.

Opinions

  • The author assumes the reader has a basic understanding of Python and the Revit API, as evidenced by the direct use of code examples without extensive explanations.
  • The article suggests that the Revit Python Shell is a valuable tool for running Python scripts directly within the Revit environment, streamlining the development process.
  • By providing multiple examples and use cases, the author conveys the versatility and power of the FilteredElementCollector for precise element selection in Revit models.
  • The inclusion of graphical selection methods indicates the importance of user interaction in automating tasks within Revit, enhancing the user experience by allowing for more intuitive selections.
  • The guide's focus on both model elements and linked model elements underscores the comprehensive approach to element management in complex Revit projects.
  • The author's use of comments in the code snippets suggests a pedagogical intent to educate readers on best practices for coding with the Revit API.

Revit + Python — Selecting elements from Model

There are several ways to get elements from the model using the Revit API. Here we are going to explore some of them.

Before starting, make sure you have installed Revit Python Shell.

Please note that Revit Python Shell already has imported all the necessary classes and objects from the Revit API, so you can run directly in your RPS Editor all the code used in this story.

API Reference

Using FilteredElementCollector

Getting all elements in the model

# The variable doc is already instantiated in Revit Python Shell
elements = FilteredElementCollector(doc).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Getting all elements in the active view

Here we are using the active view, but you can pass the Id of any view to the FilteredElementCollector constructor.

# The variable doc is already instantiated in Revit Python Shell
active_view = doc.ActiveView

elements = FilteredElementCollector(doc, active_view.Id).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Getting all elements owned by a view

For example, an annotation element.

# The variable doc is already instantiated in Revit Python Shell
active_view = doc.ActiveView

elements = FilteredElementCollector(doc).OwnedByView(active_view.Id).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Getting all elements of a specific category

In this example, we are getting all the elements from the model that have the category “Floors”.

# The variable doc is already instantiated in Revit Python Shell
elements = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Getting all elements of a specific class

In this example, we are getting all the elements from the model that are instance of the class ViewSheet (Sheets in the model)

# The variable doc is already instantiated in Revit Python Shell
elements = FilteredElementCollector(doc).OfClass(ViewSheet).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Getting all type elements of a specific category

In this example, we are getting all the type elements of category “Floors”

# The variable doc is already instantiated in Revit Python Shell
elements = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors).WhereElementIsElementType()
for element in elements:
    print(element.Name)

Combining methods in FilteredElementCollector

Let’s get all the elements of category floors from the current view. We can combine method inside the same FilteredElementCollector instance.

# The variable doc is already instantiated in Revit Python Shell
active_view = doc.ActiveView

elements = FilteredElementCollector(doc, active_view.Id).OfCategory(BuiltInCategory.OST_Floors).WhereElementIsNotElementType()
for element in elements:
    print(element.Name)

Graphical Selection

Get selected elements in model

# The variables uidoc and doc already instantiated in Revit Python Shell

# Getting ids from selected objects
element_ids = uidoc.Selection.GetElementIds()
for id in element_ids:
    
# Getting element from id 
    element = doc.GetElement(id)
    print(element.Name)

Pick elements by rectangle

# The variable uidoc is already instantiated in Revit Python Shell

# You can specify any prompt message
elements = uidoc.Selection.PickElementsByRectangle("Select elements")
for element in elements:
    print(element.Name)

Pick elements by rectangle and a filter

from Autodesk.Revit.UI.Selection import ISelectionFilter

# Custom filter that takes a category name and filter the elements
# Alternatively you can create any type of filter
class CategoryISelectionFilter(ISelectionFilter):
    def __init__(self, category_name):
        self.category_name = category_name

    def AllowElement(self, e):
        if e.Category.Name == self.category_name:
            return True
        else:
            return False

    def AllowReference(self, ref, point):
        return False

# Instantiating the filter to allow only floors
# Alternatively you can instantiate it with other categories
floors_filter = CategoryISelectionFilter("Floors")

elements = uidoc.Selection.PickElementsByRectangle(floors_filter, "Select floors")
for element in elements:
    print(element.Name)

Getting elements from linked models

The procedure is the same, the only difference is we have to use the “Document” from the linked model, for example:

# __revit__ is a instance of Autodesk.Revit.UI.UIApplication

for linked_doc in __revit__.Application.Documents:
    if linked_doc.IsLinked:
        elements = FilteredElementCollector(linked_doc).OfCategory(BuiltInCategory.OST_Floors).WhereElementIsNotElementType()
for element in elements:
        for element in elements:
            print(element.Name)
Architecture
Python
Revit
Bim
Engineering
Recommended from ReadMedium