The interface ISelectionFilter can be implemented to filter the element selection 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 the implementation of the ISelectionFilter:
#region Namespaces
using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
#endregion
namespace RevitAddinCSProject
{
public class SelectionFilter : ISelectionFilter
{
#region ISelectionFilter Members
public bool AllowElement(Element elem)
{
if (elem is Wall) return true;
if (elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_WallsDefault) return true;
if (elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_WallsStructure) return true;
if (elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_WallsInsulation) return true;
return false;
}
public bool AllowReference(Reference refer, XYZ pos)
{
if (refer.GeometryObject == null) return false;
if (refer.GeometryObject is Face) return true;
if (refer.GeometryObject is Edge) return true;
return false;
}
#endregion
}
}
If the ISelectionFilter derivative is associated with a PickObject call, for example:
......
Reference reference = CachedUiApp.ActiveUIDocument.Selection
.PickObject(ObjectType.Face, new SelectionFilter(), "Select a wall face");
MessageBox.Show(string.Format("Reference Type: {0}\nElement 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, the MessageBox will look like:
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.
Links to some related articles:
Use RevitAddinWizard to Create IUpdater Derivatives of Revit API
Implement IFailuresPreprocessor of Revit API
Use RevitAddinWizard to Implement IFailuresPreprocessor of Revit API
Implement An IFailuresProcessor of Revit API
Use RevitAddinWizard to Implement IFailuresProcessor of Revit API
Command Availability And Revit Flavors/Categories of Revit API
Use RevitAddinWizard to Implement IExternalCommandavailability of Revit API
Implement ISelectionFilter of Revit API
Use RevitAddinWizard to Implement ISelectionFilter of Revit API
Recent Comments