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:

#Boilerplate Code

#You'll also want to import the below namespace to get access
#to the ISelectionFilter interface
from Autodesk.Revit.UI.Selection import *

class MySelectionFilter(ISelectionFilter):
	def __init__(self):
		pass
	def AllowElement(self, element):
		if element.Category.Name == "Walls":
			return True
		else:
			return False
	def AllowReference(self, element):
		return False

selection_filter = MySelectionFilter()

walls = uidoc.Selection.PickElementsByRectangle(selection_filter)
OUT = walls

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 updated