4.10 Prompting UI Selection
Tell me what you need...

User-Selection Workflows

Sometimes we might be building a generic tool that requires the user to select Revit elements in the document (for instance, an element renumbering tool). It would require an infinite number of customisable filters to let a user select their chosen element(s) using logic, however we can skip the whole question by just prompting the user to select something.

'Select Model Elements' is Limited

Of course, there's already a node to let users select elements in Dynamo - Select Model Elements. It's useful as a catch-all; it will let users select elements of any possible category. But what if, instead, we wanted users to only be able to select walls, or detail lines? There is another way.
Revit's Selection class can be used to read the user's currently-selected elements in the active Revit document, or to prompt the user to select via various methods (by click or by dragging a rectangle).

The ISelectionFilter Interface

This interface can be implemented to restrict the kinds of elements a user can select, using any kind of coded logic. To do this, we need to create a new class which implements the interface.
An instance of this class it then fed as an argument to the PickElementsByRectangle() method. As an example, the code needed to prompt a user to select (only) Walls, would be:
1
#Boilerplate Code
2
​
3
#You'll also want to import the below namespace to get access
4
#to the ISelectionFilter interface
5
from Autodesk.Revit.UI.Selection import *
6
​
7
class MySelectionFilter(ISelectionFilter):
8
def __init__(self):
9
pass
10
def AllowElement(self, element):
11
if element.Category.Name == "Walls":
12
return True
13
else:
14
return False
15
def AllowReference(self, element):
16
return False
17
​
18
selection_filter = MySelectionFilter()
19
​
20
walls = uidoc.Selection.PickElementsByRectangle(selection_filter)
21
OUT = walls
Copied!
The above code shows an example whereby we can prompt the user to only select walls. Dragging a rectangle over non-walls will not select them and they do not get returned by the script.
The ISelectionFilter interface has an __init__ method, like any other Python class, and two methods called AllowElement() and AllowReference(). The AllowElement() method can be used to define custom logic, returning True when an element meets your criteria. The AllowReference() method can be used to return geometry references, such as edges and faces and, again, it can be built to return True or False as desired.
Last modified 1yr ago
Copy link