LearnRevitAPI

Jan 10, 2023

Select/Pick Elements

Selection and Picking is often a good way for a user to provide a useful input for your custom tools. So let me break it down for you and give a few examples that you can start using today.

Selection Class

There is a Selection Class in Revit API which allows us to get user selection or activate picking tools. It is located in Autodesk.Revit.UI.Selection Namespace. 

Getting Selection instance is very easy, since uidoc has a property for that. Have a look at the images below to see how to get Selection instance and what methods are available.

Selection Class

You can read more about each method available in Revit API Documentation.

Imports & Variables

Imports and Variables that I will be using for my examples. 

Get Selected Elements

Let's start by getting user's selection. There is a self-explaining method called .GetElementIds() so it's going to be very simple. Have a look at the snippet 

👇

Set Selection

Setting selection is also easy. You just need provide a List[ElementId]() for new selection in .SetElementIds() method. 

👇

Pick Elements

Picking is also a great way for a user to provide input. There are multiple methods available to pick your elements .  

PickObject  -  Pick a single element
PickObjects - Pick multiple Elements and confirm your selection
PickElementsByRectangle - Select Elements with a rectangle

Notice in Revit API Documentation that:
PickObject/s return Reference
PickElementByRectangle returns List[Elements]

So we need to convert Reference into Elements by using doc.GetElement()

You might have noticed that one of the arguments is ObjectType.
It's an enumeration that contains the following options: [Nothing, Element, PointOnElement, Edge, Face, LinkedElement, Subelement ]

Pick Position

We can also ask user to provide a location input such as Point or PickBox. There are 2 methods in Selection class that can help us out:
- PickPoint() - Return XYZ Point
- PickBox()   - Return selected PickBox (You can extract .Min and .Max points )

ISelectionFilter

Lastly, If you look through these methods you might notice an argument: ISelectionFilter. It allows us to filter elements that user is allowed to select.

ISelectionFilter is an interface in Revit API to create our custom subclasses with rules for selection.

There are 2 methods that we can override:
- AllowElement(element) 
- AllowReference(reference, position) 
 

We can make any logical checks we want by using argument - element.
In the example below I've created ISelectionFilter that will make sure that only elements with allowed type will be selected, and then I passed it to PickObjects method.

Then I created an instance of my filter to filter Walls, Floors, Roofs and Ceilings. When PickObjects will be run, user will only be able to select these types of elements.

I hope you learnt something new about Selection. And if you have trouble with any of the snippet I provided, reach out and let me know. I will try to help you.

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 6700+ people!