Revit API Selection

VIDEO COMING LATER…

Imports & Variables

Imports & Variables

Imports & Variables

This is the base imports and variables that you might need for code snippets below…

This is the base imports and variables that you might need for code snippets below…

This is the base imports and variables that you might need for code snippets below…

# -*- coding: utf-8 -*-

#⬇️ IMPORTS
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ISelectionFilter, Selection, ObjectType

#📦 VARIABLES
#==================================================
doc       = __revit__.ActiveUIDocument.Document #type:Document
uidoc     = __revit__.ActiveUIDocument          

# Class for selection methods...
selection = uidoc.Selection                     #type: Selection
# -*- coding: utf-8 -*-

#⬇️ IMPORTS
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ISelectionFilter, Selection, ObjectType

#📦 VARIABLES
#==================================================
doc       = __revit__.ActiveUIDocument.Document #type:Document
uidoc     = __revit__.ActiveUIDocument          

# Class for selection methods...
selection = uidoc.Selection                     #type: Selection
# -*- coding: utf-8 -*-

#⬇️ IMPORTS
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ISelectionFilter, Selection, ObjectType

#📦 VARIABLES
#==================================================
doc       = __revit__.ActiveUIDocument.Document #type:Document
uidoc     = __revit__.ActiveUIDocument          

# Class for selection methods...
selection = uidoc.Selection                     #type: Selection

1. Get Selected Elements

1. Get Selected Elements

1. Get Selected Elements

Get elements that are currently selected in Revit UI. It also includes selection of views and sheets in ProjectBrowser menu.

#1️⃣ Get Selected Elements
sel_elem_ids = uidoc.Selection.GetElementIds()
sel_elems    = [doc.GetElement(e_id) for e_id in sel_elem_ids]

# Quick and Simple Filter (Optionally)
filtered_elements = [el for el in sel_elems if type(el) == Wall]
#1️⃣ Get Selected Elements
sel_elem_ids = uidoc.Selection.GetElementIds()
sel_elems    = [doc.GetElement(e_id) for e_id in sel_elem_ids]

# Quick and Simple Filter (Optionally)
filtered_elements = [el for el in sel_elems if type(el) == Wall]
#1️⃣ Get Selected Elements
sel_elem_ids = uidoc.Selection.GetElementIds()
sel_elems    = [doc.GetElement(e_id) for e_id in sel_elem_ids]

# Quick and Simple Filter (Optionally)
filtered_elements = [el for el in sel_elems if type(el) == Wall]
  1. Pick Elements by Rectangle

Prompt user selection by clicking and dragging a rectangular area in Revit space.

#2️⃣ Pick Elements by Rectangle
selected_elements = uidoc.Selection.PickElementsByRectangle('Select some Elements.')
print(selected_elements)
#2️⃣ Pick Elements by Rectangle
selected_elements = uidoc.Selection.PickElementsByRectangle('Select some Elements.')
print(selected_elements)
#2️⃣ Pick Elements by Rectangle
selected_elements = uidoc.Selection.PickElementsByRectangle('Select some Elements.')
print(selected_elements)
  1. Pick Object

Prompt user to pick and element.

from Autodesk.Revit.UI.Selection import ObjectType

#3️⃣ Pick Object
ref           = uidoc.Selection.PickObject(ObjectType.Element) #type: Reference
picked_object = doc.GetElement(ref)
print(picked_object)
from Autodesk.Revit.UI.Selection import ObjectType

#3️⃣ Pick Object
ref           = uidoc.Selection.PickObject(ObjectType.Element) #type: Reference
picked_object = doc.GetElement(ref)
print(picked_object)
from Autodesk.Revit.UI.Selection import ObjectType

#3️⃣ Pick Object
ref           = uidoc.Selection.PickObject(ObjectType.Element) #type: Reference
picked_object = doc.GetElement(ref)
print(picked_object)
  1. Pick Objects (Multiple)

Prompt user to pick multiple elements. User have to confirm selection by clicking 'Finish' in top left corner of UI.

from Autodesk.Revit.UI.Selection import ObjectType

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

for el in picked_objects:
    print(el)
from Autodesk.Revit.UI.Selection import ObjectType

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

for el in picked_objects:
    print(el)
from Autodesk.Revit.UI.Selection import ObjectType

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

for el in picked_objects:
    print(el)
  1. Pick Point

Prompt user to pick a point in Revit space.

#5️⃣ Pick Point
selected_pt = uidoc.Selection.PickPoint()
print(selected_pt, type(selected_pt))
#5️⃣ Pick Point
selected_pt = uidoc.Selection.PickPoint()
print(selected_pt, type(selected_pt))
#5️⃣ Pick Point
selected_pt = uidoc.Selection.PickPoint()
print(selected_pt, type(selected_pt))
  1. Pick Box

Prompt user to pick a box area in Revit space.

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)
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)
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)
  1. Set Selection in Revit UI

Modify user's selection in Revit UI.
Notice that you have to create a List[ElementId] instead of python list to provide in SetElementIds method.

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

#7️⃣ Set Selection in Revit UI
wall_ids      = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
List_wall_ids = List[ElementId](wall_ids)

uidoc.Selection.SetElementIds(List_wall_ids) 
#⚠️ Ensure to input List[ElementId] not regular python list!
#.NET Imports
import clr
clr.AddReference('System')
from System.Collections.Generic import List

#7️⃣ Set Selection in Revit UI
wall_ids      = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
List_wall_ids = List[ElementId](wall_ids)

uidoc.Selection.SetElementIds(List_wall_ids) 
#⚠️ Ensure to input List[ElementId] not regular python list!
#.NET Imports
import clr
clr.AddReference('System')
from System.Collections.Generic import List

#7️⃣ Set Selection in Revit UI
wall_ids      = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
List_wall_ids = List[ElementId](wall_ids)

uidoc.Selection.SetElementIds(List_wall_ids) 
#⚠️ Ensure to input List[ElementId] not regular python list!

BONUS: Advanced Selection Filtering

To improve user experience you can pre-define what elements are allowed to be selected using ISelectionFilter interface. Just create a class and specify rules inside AllowElement method that return True to allow selection.

#💪 Advanced Selection Filtering with ISelectionFilter

# Create Custom Filter Rules
from Autodesk.Revit.UI.Selection import ISelectionFilter, ObjectType
class custom_filter(ISelectionFilter):
    def AllowElement(self, element):
        # 👇 Provide Filtering Logic Here...
        if type(element) == Wall:
            return True
        
# Prompt Selection
el_ids    = uidoc.Selection.PickObjects(ObjectType.Element, custom_filter())
sel_elems = [doc.GetElement(el_id) for el_id in el_ids]
print(sel_elems)
#💪 Advanced Selection Filtering with ISelectionFilter

# Create Custom Filter Rules
from Autodesk.Revit.UI.Selection import ISelectionFilter, ObjectType
class custom_filter(ISelectionFilter):
    def AllowElement(self, element):
        # 👇 Provide Filtering Logic Here...
        if type(element) == Wall:
            return True
        
# Prompt Selection
el_ids    = uidoc.Selection.PickObjects(ObjectType.Element, custom_filter())
sel_elems = [doc.GetElement(el_id) for el_id in el_ids]
print(sel_elems)
#💪 Advanced Selection Filtering with ISelectionFilter

# Create Custom Filter Rules
from Autodesk.Revit.UI.Selection import ISelectionFilter, ObjectType
class custom_filter(ISelectionFilter):
    def AllowElement(self, element):
        # 👇 Provide Filtering Logic Here...
        if type(element) == Wall:
            return True
        
# Prompt Selection
el_ids    = uidoc.Selection.PickObjects(ObjectType.Element, custom_filter())
sel_elems = [doc.GetElement(el_id) for el_id in el_ids]
print(sel_elems)

© 2023-2026 EF Learn Revit API

© 2023-2026 EF Learn Revit API

© 2023-2026 EF Learn Revit API