Revit .NET API provides quite some classes and methods to filter elements and parameters. FilterRule, ElementParameterFilter and FilteredElementCollector.WherePasses() are among those pretty important ones.
In the previous post, some general idea was provided about how to find elements with a particular built-in parameter no matter what value it may have for each. This time, let's share some code. The sample parameter is ALL_MODEL_COST, as suggested.
Here is the helper method:
ICollection<ElementId> GetElementsWithCost()
{
ElementId pId = new ElementId(BuiltInParameter.ALL_MODEL_COST);
FilterRule rule = ParameterFilterRuleFactory.CreateGreaterRule(pId, -1.0, 0.01);
ElementParameterFilter filter = new ElementParameterFilter(rule);
FilteredElementCollector fec = new FilteredElementCollector(CachedDoc);
fec = fec.WherePasses(filter);
return fec.ToElementIds();
}
Here is the test code:
var ids = GetElementsWithCost();
MessageBox.Show(ids.Count.ToString());
As can be seen, the code is pretty simple actually for the Double StorageType case such as ALL_MODEL_COST here. Last time, it's said that two opposite filter rules may be necessary, created by ParameterFilterRuleFactory.CreateGreaterOrEqualRule and CreateLessOrEqualRule for example. It also hinted that a LogicalOrFilter instead of LogicalAndFilter is also needed. In fact, as demonstrated above, one GreaterRule is good enough. It makes code simpler and more readable, and program faster and more reliable.
Please feel free to use the code or adjust it to meet your specific needs. Enjoy!
Recent Comments