The interface ISelectionFilter can be implemented to filter element selections when objects are being picked either through a window or a single click.
The ISelectionFilter has two methods, AllowElement() and AllowReference(). The AllowElement() indicates which elements are allowed to pass through the picking to the selection and it can be extended to allow some particular categories to go through as well when needed. The AllowReference() indicates which GeometryObject references are allowed to be picked and it can be extended as well to limit the references to some positions.
For example, we need an ISelectionFilter to pick Wall elements or categories of WallsDefault, WallsStructure or WallsInsulation, and we allow either Edge or Face references to be picked.
Here is a sample implementation of the ISelectionFilter in VB.NET:
#Region "Namespaces"
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Reflection
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Media.Imaging
Imports System.Windows.Forms
Imports System.IO
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.DB.Events
Imports Autodesk.Revit.DB.Architecture
Imports Autodesk.Revit.DB.Structure
Imports Autodesk.Revit.DB.Mechanical
Imports Autodesk.Revit.DB.Electrical
Imports Autodesk.Revit.DB.Plumbing
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection
Imports Autodesk.Revit.UI.Events
Imports Autodesk.Revit.Collections
Imports Autodesk.Revit.Exceptions
Imports Autodesk.Revit.Utility
Imports RvtApplication = Autodesk.Revit.ApplicationServices.Application
Imports RvtDocument = Autodesk.Revit.DB.Document
#End Region
Public Class SelectionFilter
Implements ISelectionFilter
#Region "ISelectionFilter Members"
Public Function AllowElement(ByVal elem As Autodesk.Revit.DB.Element) As Boolean Implements Autodesk.Revit.UI.Selection.ISelectionFilter.AllowElement
If TypeOf elem Is Wall Then
Return True
End If
If elem.Category.Id.IntegerValue = CInt(BuiltInCategory.OST_WallsDefault) Then
Return True
End If
If elem.Category.Id.IntegerValue = CInt(BuiltInCategory.OST_WallsStructure) Then
Return True
End If
If elem.Category.Id.IntegerValue = CInt(BuiltInCategory.OST_WallsInsulation) Then
Return True
End If
Return False
End Function
Public Function AllowReference(ByVal refer As Reference, ByVal pos As Autodesk.Revit.DB.XYZ) As Boolean Implements Autodesk.Revit.UI.Selection.ISelectionFilter.AllowReference
If refer.GeometryObject Is Nothing Then
Return False
End If
If TypeOf refer.GeometryObject Is Autodesk.Revit.DB.Face Then
Return True
End If
If TypeOf refer.GeometryObject Is Autodesk.Revit.DB.Edge Then
Return True
End If
Return False
End Function
#End Region
End Class
If the ISelectionFilter derivative is associated with a PickObject call, for example:
Dim reference As Reference = CachedUiApp.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Face, New SelectionFilter(), "Select a wall face")
MessageBox.Show(String.Format("Reference Type: {0}" & vbLf & "Element Type: {1}", reference.ElementReferenceType, reference.Element.[GetType]()))
Revit will prompt us to select a wall face and only allow wall faces to be highlighted and selected. For any other elements such as doors or geometries such as edges the pick cursor will look like a stop sign and do not allow them to be picked.
We can allow wall edge geometries to be selectable by changing the ObjectType to Edge and using the same ISelectionFilter derivative instance since it allows both Face and Edge to go through.
After a wall face is properly selected, an MessageBox will show up tell us what have been selected.
With Selection Filter of RevitAddinWizard, an ISelectionFilter interface will be implemented in a moment and it can address any combinations of Element sub-classes, Category values, and/or Reference geometry types in VB.NET.
Recent Comments