EF-Selection Basics
Description
Learn and copy most used selection code snippets for prompting user selection.
Code
pyRevit# -*- 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
# SELECTION EXAMPLES BELOW
#███████████████████████████████████████████████████████████████████████████
#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]
#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
ref = uidoc.Selection.PickObject(ObjectType.Element) #type: Reference
picked_object = doc.GetElement(ref)
print(picked_object)
#4️⃣ Pick Objects (Multiple)
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.UI.Selection import ObjectType
refs = uidoc.Selection.PickObjects(ObjectType.Element)
picked_objects = [doc.GetElement(ref) for ref in refs]
for el in picked_objects:
print(el)
#5️⃣ Pick Point
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
selected_pt = uidoc.Selection.PickPoint()
print(selected_pt, type(selected_pt))
#6️⃣ PickBox
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.UI.Selection import PickBoxStyle
picked_box = uidoc.Selection.PickBox(PickBoxStyle.Directional)
print(picked_box)
print(picked_box.Min)
print(picked_box.Max)
#7️⃣ Set Selection in Revit UI
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\
# .NET List import
import clr
clr.AddReference('System')
from System.Collections.Generic import List
# Get Elements and convert to List[ElementId]
wall_ids = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
List_wall_ids = List[ElementId](wall_ids)
# Sel Element Selection
uidoc.Selection.SetElementIds(List_wall_ids) #⚠️ Ensure to input List[ElementId] not regular python list!
# -*- coding: utf-8 -*-
#⬇️ IMPORTS
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ISelectionFilter, ObjectType
#📦 VARIABLES
#░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
doc = __revit__.ActiveUIDocument.Document #type:Document
uidoc = __revit__.ActiveUIDocument
# ISelectionFIlter - Advanced Seleciton
#███████████████████████████████████████████████████████████████████████████
# Create Custom Filter Rules
class custom_filter(ISelectionFilter):
def AllowElement(self, element):
# 👇 Provide Filtering Logic Here...
if type(element) == Wall:
return True
# During selection AllowElements is the logic for allowing selection.
# Return True - selectable. False or no return - not selectable.
# Use Example
el_ids = uidoc.Selection.PickObjects(ObjectType.Element, custom_filter())
sel_elems = [doc.GetElement(el_id) for el_id in el_ids]
print(sel_elems)