Revit API provides a variety of ways to pick objects including whole elements and some visible geometry components such as edges and faces/surfaces.
They are wrapped in the class Autodesk.Revit.UI.Selection and the most useful and typical ones are PickObject, PickObjects and PickElementsByRectangle. The PickElementsByRectangle one will prompt users to drag a window to select elements of some certain types.
It has four signatures two of which have a String argument and the other two do not:
PickElementsByRectangle()
PickElementsByRectangle(ISelectionFilter)
PickElementsByRectangle(String)
PickElementsByRectangle(ISelectionFilter, String)
The two without String argument ones may be good for testing purposes as they save one input but it’s unlikely that some final code will use them in a commercial and professional application, needless to say, in localizable software products. None of them require an ObjectType, and this is the big difference between the PickElementsByRectangle and the PickObject or the PickObjects that we introduced a bit earlier.
Another common feature all the signatures share is the return type IList<Element>.
If we’d like to select some walls through a window, the folllowing code will achieve it supposing the SelectionFilter1 is the same as we introduced before:
public static IList<Element> SelectWallsThroughWindow(Selection selection)
{
IList<Element> picked = selection.PickElementsByRectangle(new SelectionFilter1(), "Select walls through dragging a window");
MessageBox.Show(string.Format("{0} objects have been picked back.", picked.Count));
return picked;
}
The test calling code is also appended for convenience:
public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
{
_cachedCmdData = cmdData;
try
{
SelectWallsThroughWindow(CachedUiApp.ActiveUIDocument.Selection);
return Result.Succeeded;
}
catch (Exception ex)
{
msg = ex.ToString();
return Result.Failed;
}
}
In case necessary for new readers, the whole SelectionFilter1 has also been provided here:
public class SelectionFilter1 : ISelectionFilter
{
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_WallsInsulation) return true;
if (elem.Category.Id.IntegerValue == (int)BuiltInCategory.OST_WallsStructure) return true;
return false;
}
public bool AllowReference(Reference refer, XYZ pos)
{
if (refer.GeometryObject == null) return false;
if (refer.GeometryObject is Edge) return true;
if (refer.GeometryObject is Face) return true;
return false;
}
As demonstrated before the Selection Filter can help create various different ISelectionFilter implementations.
Object Picker of RevitAddinWizard can help take care of the cases regarding the PickElementsByRectangle and many more.
Our Software http://netspiderstudio.com/Software.html
Support: mailto:[email protected]
Query: mailto:[email protected]
Blog: http://spiderinnet.typepad.com
More: http://netspiderstudio.com
Links to some related articles:
Object Picking of Revit API - PickObject And ObjectType
Object Picking of Revit API - PickObjects And ObjectType
Recent Comments