Revit-API-05

/

/

/

revit-api/05-revit-api-selection

Element Selection with Revit API

Finally, we're gonna begin this Revit API journey, and we're gonna start with the selection. After all, you need to select something before you can test other Revit API concepts.

Revit-API-05

/

/

/

revit-api/05-revit-api-selection

Element Selection with Revit API

Finally, we're gonna begin this Revit API journey, and we're gonna start with the selection. After all, you need to select something before you can test other Revit API concepts.

Summary

Beginning the Revit API Journey

To get or modify user's selection we will be using Selection Class from Revit API. And it's not a complicated one.

Let's look at this class in Revit API Docs.

Revit API Docs: Selection Class

Once you start exploring Selection Class, there is not so much you need. As I've mentioned previously, you will often find code examples in Revit API docs. They are not always great, but you can often extract something useful from them.

In this case, the example is a little confusing, however there are 2 very important lines we could use.

Notice that first of all we need to get Selection related to our Revit project.

And only then, we can use it to Get or Set current selection and prompt various selection methods.

In this example GetElementIds method will return a list of Ids from selected elements.

selection = uidoc.Selection
selectedIds = uidoc.Selection.GetElementIds()
Selection Examples

Let's go over all simple Selection methods and I will provide you simple code snippets that you can copy and try out yourself.

Also, keep in mind that some methods might take multiple arguments, but I will use each with the simplest option. You will figure out the rest when you need it.

Also, keep in mind that these methods might return you different things (Ids, References, XYZ points and so on…)

For example GetElementIds() will return ICollection[ElementId], which means a list of ids.

You can see what is being return in front of the method name in the Docs.

Let's look into these methods one by one.

Selection.GetElementIds()

Returns the ids of the elements that are currently selected within the project.

#🟠 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)
Selection.PickElementsByRectangle()
Prompts the user to select multiple elements by drawing a rectangle.
#🟠 2. Pick Elements by Rectangle
selected_elements = uidoc.Selection.PickElementsByRectangle('Select some Elements.')
print(selected_elements)
Selection.PickObject(ObjectType)
Prompts the user to select one object.

⚠️Make sure to import ObjectType to use it as an argument.

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)
💡ObjectType Enumeration

ObjectType Enumeration is a of allowed types to select from for selection operations.

It's used for PickObject and PickObjects methods.

Selection.PickObjects()
Prompts the user to select multiple objects.

⚠️Make sure to import ObjectType to use it as an argument.

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)
Selection.PickPoint()
Prompts the user to pick a point on the active work plane.
#🟠 5. Pick Point
selected_pt = uidoc.Selection.PickPoint() #type: XYZ
print(selected_pt)
Selection.PickBox()
Invokes a general purpose two-click editor that lets the user to specify a rectangular area on the screen.


⚠️Make sure to import PickBoxStyle to use it as an argument.

from Autodesk.Revit.UI.Selection import  PickBoxStyle

#🟠 6. PickBox
picked_box = uidoc.Selection.PickBox(PickBoxStyle.Directional)
print(picked_box)
print(picked_box.Min) # XYZ Point of Bottom Left Corner
print(picked_box.Max) # XYZ Point of Top RIght Corner
Selection.SetElementIds()

Change selection in Revit UI.


⚠️You need to use .NET typed list with ElementId elements inside of it.

List[ElementId]()

Notice that you need to provide ICollection[ElementId]. It just means that you need to use a typed list from .NET framework. And don't worry it's just extra line of code.

Firstly, look at this example where I get all walls with FEC class (you will learn about it later). In this case, it returns typed list of Ids, so I can use it directly.

#⬇️ 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

# This works because ToElementIds methods returns List[ElementId]
Selection.SetElementIds() - Example 2

However, if you create your list of element ids differently, then you would need to convert it into typed list. And it's just a few steps.

  • Get python list of ElementIds

  • Import .NET List

  • Convert Python list in .NET List

You will notice an unusual syntax of using List[SpecifiedTypeName](). That's how you create typed list and specify what kind of elements can go inside. If you try to add anything else, it will give you a warning.

Think of it as a bouncer that checks you elements before the join the party.

# Python List
el_ids = [ElementId(12345), ElementId(12345)] # List of ElementIds

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

# Convert to .NET List
List_el_ids = List[ElementId](el_ids)


#🎯 Set Selection in Revit UI
selection.SetElementIds(List_el_ids) 
#🔓 No need for transaction! It's just UI change

HomeWork

Now it's your turn to select elements with Revit API.

Scroll through the code examples and try to use them yourself:

  • Get User Selection

  • PickObject

  • PickObjects

Never assume that just by looking at someone doing it will be enough to learn something. You need to get your hands dirty and actually execute code and see what you get.

Practice is the best way to learn and mistakes are the greatest teachers.

⌨️ Happy Coding!

Questions:

Why we need Typed list ? (List[ElementId])

Why we need Typed list ? (List[ElementId])

Discuss the lesson :

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

Use Discord App for best experience.

Discuss the lesson :

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

Use Discord App for best experience.

Discuss the lesson :

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

Use Discord App for 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-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API