Dynamo Python Primer
  • Take Dynamo Further 🚀
  • 1 Hello Python 🐍
    • 1.1 Why Should I Learn to Code?
    • 1.2 Python Introduction
    • 1.3 What is IronPython?
  • 2 Seeing The Bigger Picture 🔭
    • 2.1 Context Matters
    • 2.2 What is an API?
    • 2.3 The .NET Framework
    • 2.4 Object-Oriented Programming
  • 3 Getting Started 🛴
    • 3.1 Dynamo's Python Node
    • 3.2 Boilerplate Setup Code
    • 3.3 Basics: Input and Output
    • 3.4 Common Errors
  • 4 Revit-Specific Topics 🏡
    • 4.1 Introduction to Revit's API
    • 4.2 How to Read Revit's API Documentation
    • 4.3 Doc, UIDoc, App, UIApp
    • 4.4 Unwrapping Revit Elements
    • 4.5 The FilteredElementCollector
    • 4.6 Geometry Conversion Methods
    • 4.7 Working With Parameters
      • Family Parameters
      • Global Parameters
      • Project Information Parameters
      • Group Parameters
      • Built-In Parameters
    • 4.8 Working With Transactions
    • 4.9 Opening & Closing External Files
    • 4.10 Prompting UI Selection
    • 4.11 Working With Units
    • 4.12 Built-In Categories
    • 4.13 Family Acrobatics
    • 4.14 Feedback: TaskDialogs
  • 5 Glossary 📚
  • About This Primer 👋
Powered by GitBook
On this page

Was this helpful?

  1. 4 Revit-Specific Topics 🏡

4.10 Prompting UI Selection

Tell me what you need...

Previous4.9 Opening & Closing External FilesNext4.11 Working With Units

Last updated 5 years ago

Was this helpful?

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 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 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 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.

Selection
PickElementsByRectangle()
ISelectionFilter