We talked about filtering the whole document before based on kinds of different criteria, e.g. class type, category name, parameter type and value, etc. In this article, let us see how to filter selected elements in the current document.
So we care about post filtering element selections rather than pre filtering. In terms of pre filtering, the ISelectionFilter interface is supposed to do it as we discussed and demonstrated before.
In fact, there is nothing new about post filtering element selections. The FilteredElementCollector and related ElementFilter classes can do this work as well. A different signature of the FilteredElementCollector can address the case easily though not directly. The signature has an extra ElementId collection argument besides the Document one:
public FilteredElementCollector(
Document document,
ICollection<ElementId> elementIds
)
It may be a bit challenge to convert the Selection of UIDocument to the wanted ElementId collection but clearly very possible. As demonstrated many times before, the LINQ can help us achieve the goal with few code:
public static ICollection<ElementId> GetWallsInSelection(RvtDocument doc)
{
return (new FilteredElementCollector(doc, (from Element e
in new UIDocument(doc).Selection.Elements
select e.Id).ToList()))
.OfClass(typeof(Wall))
.ToElementIds();
}
The following test code,
ICollection<ElementId> wallsFound1 = GetWallsInSelection(CachedDoc);
MessageBox.Show(string.Format("There are {0} walls in selected elements.", wallsFound1.Count), "Selection, Collector And Filter");
yields a good result something like the following for a test model:
RevitAddinWizard provides various coders to filter Revit elements.
Recent Comments