Basics: Selection

How Selection Works?

To do anything with selection in Revit API you need to use Selection Class. It's very straight-forward, but first of all you need to get an instance of Selection related to your project. This is how you do that:

uidoc     = __revit__.ActiveUIDocument
selection = uidoc.Selection

Once you know how to get selection instance, you can easily use any of its methods that you can see here in the documentation.

Code Examples

I'm going to leave here code snippets that you can simply copy-paste in your own code to test and use for your own tools.


🟠 1. Get Selected Elements

#🟠 1. Get Selected Elements
selected_element_ids = uidoc.Selection.GetElementIds()
selected_elements    = [doc.GetElement(e_id) for e_id in selected_element_ids]
print(selected_elements)

# Filter Selection (Optionally)
filtered_elements = [el for el in selected_elements if type(el) == Wall]
print(filtered_elements)

🟠 2. Pick Elements by Rectangle

#🟠 2. Pick Elements by Rectangle
selected_elements = uidoc.Selection.PickElementsByRectangle('Select some Elements.')
print(selected_elements)

🟠 3. Pick Object

from Autodesk.Revit.UI.Selection import  ObjectType

#🟠 3. Pick Object
ref_picked_object = uidoc.Selection.PickObject(ObjectType.Element)
picked_object     = doc.GetElement(ref_picked_object)
print(picked_object)

🟠 4. Pick Objects (Multiple)

from Autodesk.Revit.UI.Selection import  ObjectType

#🟠 4. Pick Objects (Multiple)
ref_picked_objects = uidoc.Selection.PickObjects(ObjectType.Element)
picked_objects     = [doc.GetElement(ref) for ref in ref_picked_objects]

for el in picked_objects:
    print(el)

🟠 5. Pick Point

#🟠 5. Pick Point
selected_pt = uidoc.Selection.PickPoint() #type: XYZ
print(selected_pt)

🟠 6. PickBox

from Autodesk.Revit.UI.Selection import  PickBoxStyle

#🟠 6. PickBox
picked_box = uidoc.Selection.PickBox(PickBoxStyle.Directional)
print(picked_box)
print(picked_box.Min)
print(picked_box.Max)

🟠 7. Set Selection in Revit UI

#⬇️ Imports
from Autodesk.Revit.DB import FilteredElementCollector


#📦 Variables
uidoc     = __revit__.ActiveUIDocument
doc     = __revit__.ActiveUIDocument.Document


#🎯 Set Selection in Revit UI
new_selection = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
selection.SetElementIds(new_selection) #🔓 No need for transaction! It's just UI change

Also keep in mind that to use SetElementIds you need to use IList from .NET.

For that you would need to reference System namespace and import IList. Then you can either convert your existing python list by providing it as an argument, or add elements one by one.

💡Make sure you add elements with the same Type as you declared for the List[Type]

# #💡 P.S. If You have a python list you need to turn it into List[ElementId] ;)\

list_element_ids = #... Here you list with element Ids.

#⬇️ Import .NET List
import clr
clr.AddReference('System')
from System.Collections.Generic import List
List_el_ids = List[ElementId](list_element_ids) 
# or use List_el_ids.Add()

🟠 8. ISelectionFilter Example

#⬇️ Imports
from Autodesk.Revit.UI.Selection import ISelectionFilter, Selection, ObjectType

#📦 Variables
uidoc     = __revit__.ActiveUIDocument

#🟠 ISelectionFilter
class ISF_wall_filter(ISelectionFilter):
    def AllowElement(self, element):
        if type(element) == Wall:
            return True

#🎯 Apply ISelectionFilter to PickElementsByRectangle
custom_filter     = ISF_wall_filter()
selected_elements = uidoc.Selection.PickElementsByRectangle(custom_filter, "Select Walls")

print(selected_elements)

Want More?

I hope it was really helpful to understand Revit API Selection.

If you want to learn more and get more examples, check under Selection group in the sidebar menu.

⌨️ Happy Coding!
Erik Frits