Selection Basics

Learn the basics of how to select elements with Revit API. It's a great place to start as you will need to get elements to test your code.

Selection Basics

Learn the basics of how to select elements with Revit API. It's a great place to start as you will need to get elements to test your code.

Lesson Objectives

Lesson Objectives

  • Learn about Selection class in Revit API Docs

  • Learn the basics of Revit API Selection

  • Code Practical Examples

Are you Ready?

Lesson Summary

Lesson Summary

Selection Class

To control selection in Revit with python we need to use Selection Class.

It has plenty of helpful methods. So let's go through most used ones for selection and understand how to use them.

Methods:
GetElementIds
PickElementsByRectangle
PickObject
PickObjects
SetElementIds
PickBox
PickPoint

Get Selection Instance

To use any of these methods we need to get an instance of Selection Class.
Luckily, there is a property .Selection in our uidoc instance. So it's very simple to get it

💡We can also specify type of a variable so pyCharm can give us better autosuggestions by using '#type: …' syntax.

from Autodesk.Revit.UI.Selection import Selection

uidoc     = __revit__.ActiveUIDocument
selection = uidoc.Selection #type: Selection

👆 We will need that for all the snippet below!

1️⃣ GetElementIds Method

This method will return list of ElementId of your current selection in Revit UI. I love using it, as it can be a very quick approach to select elements.

# 1️⃣ .GetElementIds() - Get Currently selected elements
selection   = uidoc.Selection
elem_ids = uidoc.Selection.GetElementIds()
elems    = [doc.GetElement(el_id) for el_id in elem_ids]

for elem in elems:
    print(elem)
    # Extra:
    print('Category: {}'.format(elem.Category.Name))
    print('BuiltInCategory: {}'.format(elem.Category.BuiltInCategory))

Since this method returns us ElementIds, don't forget to turn them into actual elements, so we can read their properties and methods.

💡Optionally, we can also introduce very basic filter by checking type of elements.

2️⃣ PickObject Method

This is great for selecting a single element.

But be aware that it returns a Reference. Luckily, we can use the same doc.GetElement() method and provide reference instead of an ElementId.

# 2️⃣ .PickObject - Prompt user to select single element
from Autodesk.Revit.UI.Selection import ObjectType

try:
    ref  = uidoc.Selection.PickObject(ObjectType.Element, 'Select Wall')
    elem = doc.GetElement(ref)
    print(elem)
except:
    print('Selection Cancelled.')

💡 We can also create a for/while loop and ask user to select single element over and over and do something with it. For example we could change color overrides of each element one by one by clicking on them.

3️⃣ PickObjects Method

This method is similar to single PickObject. But this time we would need a list comprehension to convert all references to actual elements.

# 3️⃣ .PickObjects - Prompt user to select multiple elements
from Autodesk.Revit.UI.Selection import ObjectType

try:
    refs  = uidoc.Selection.PickObjects(ObjectType.Element)
    elems = [doc.GetElement(ref) for ref in refs]
    
    for el in elems:
        print(el)
    
except:
    print('Selection Cancelled.')
4️⃣ PickElementsByRectangle Method

This method is great, because it's a single line of code, and it already returns you list of Elements instead of ElementIds.

#🔷 PickElemnetsbyRectangle
selected_elements = selection.PickElementsByRectangle('Title Here')
print(selected_elements)

But unfortunately, there is no confirmation needed on your selection. So if you selected wrong elements, you would need to run your tool again to correct it. And if you want to fix that, PickObjects is going to solve that issue for you.

5️⃣PickBox Method

Alternatively, we can ask user to Pick a Box. PickedBox has properties to get 2 points:

.Min - Bottom left point
.Max - Top right point

#5️⃣ .PickBox() - Select Area on Screen
from Autodesk.Revit.UI.Selection import PickBoxStyle
pick_box = uidoc.Selection.PickBox(PickBoxStyle.Directional)
print(pick_box)
print(pick_box.Min)
print(pick_box.Max)
6️⃣ PickPoint Method

We can also ask user to PickPoint to get XYZ that represent points in Revit API. This might be useful for many tools in the future.

#6️⃣ .PickPoint() - Select Point on Screen
pt = uidoc.Selection.PickPoint() #type: XYZ
print(pt)

💡 Keep in mind that Revit API stores coordinates with Internal Coordinate System and uses Feet as internal units. So you might need to convert both systems and units when you work with them.

7️⃣ SetElementIds Method

Lastly, we can also modify the Revit UI selection by using SetElementIds Method. You will notice that it takes a single argument - ICollection[ElementId].

💡This is a special container from .NET framework that specifies the type of element that can be added. This provides better control since any other type would give an error if we try to add it to such container.

In python we can use System.Collection.Generic.List since it's based on the same parent class. Here is how to import it:

# .NET Imports
import clr
clr.AddReference("System")
from System.Collections.Generic import List

To use it, we need to use square brackets to specify allowed types and then provide python list in parenthesis to convert into List[T].

Here is an example to get all walls in the project and select them in Revit UI.

#7️⃣ .SetElementIds() - Set current selection in Revit UI
all_walls = FilteredElementCollector(doc).OfClass(Wall).ToElements()
wall_ids  = [wall.Id for wall in all_walls]

List_wall_ids = List[ElementId](wall_ids)
uidoc.Selection.SetElementIds(List_wall_ids)
💡 EXTRA: FEC Anatomy

I will go a little ahead of myself and mention FilteredElementCollector.

It's a class to get elements from Revit projects by using various filters. It's one of the most used classes and there is going to be a whole module about it. For now I just want to mention the Anatomy of it, so you get a little bit more familiar with it.

P.S.

It might look intimidating because it's very long, but it's one of the most repetitive classes. So don't be afraid, you can copy and modify very little to get different results. But be patient, I will teach you more about it soon.

Homework

Homework

Spent the next 10-20 minutes practicing selecting elements in Revit API. Try to use different methods, and it's best if you code it from scratch instead of copying the final solutions.

Try using these methods:

You need to practice writing code yourself.

⌨️ Happy Coding!

Questions

Questions

I got 'ObjectType' is not defined

I got 'ObjectType' is not defined

Legacy Tutorial

Legacy Tutorial

This is an older lesson that cover the same lesson materials. It might be useful if you'd like to understand this lesson better.

⌨️ Happy Coding!

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for the best experience.

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for the best experience.

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for the best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2025 EF Learn Revit API

© 2023-2025 EF Learn Revit API

© 2023-2025 EF Learn Revit API