Let us continue to talk about the particular slow filter in Revit API, ElementParameterFilter. Let’s look at how to use the FilterElementIdRule filter rule to filter element id type element parameters in this article. Supposing we’d like to find all rooms with the Phase Created parameter as a specific phase, what shall we do? The following code does so in C#: public static ICollection<ElementId> GetWallsInPhase(RvtDocument doc, Phase phase)
{
ParameterValueProvider provider = new ParameterValueProvider(new ElementId((int)BuiltInParameter.PHASE_CREATED));
FilterElementIdRule rule1 = new FilterElementIdRule(provider, new FilterNumericEquals(), phase.Id);
ElementParameterFilter filter1 = new ElementParameterFilter(rule1); return
(new FilteredElementCollector(doc))
.OfClass(typeof(Wall))
.WherePasses(filter1)
.ToElementIds();
}
A few highlights about the code:
• An ElementParameterFilter needs a filter rule, the FilterElementIdRule in this case.
• The FilterElementIdRule needs a parameter value provider (ParameterValueProvider) and a filter rule evaluator (FilterStringRuleEvaluator), specifically the FilterNumericEquals here.
• Do not feel surprised that the FilterNumericEquals evaluator also works with the FilterElementIdRule as the ElementId is effectively nothing more than an integer value.
• The ParameterValueProvider needs an argument of parameter, as the phase created parameter BuiltInParameter.PHASE_CREATED in this case.
• The parameter is represented by an ElementId, which is the numeric value of the specified BuiltInParameter.
• A fast filter, ElementClassFilter, represented by its shortcut method (OfClass), is also used to narrow down the FilteredElementCollector first. It not only speeds up the search but also makes sure only walls are returned. Curious people may ask: how did you figure out the PHASE_CREATED of the BuiltInParameter enumerator is the right one to use? Good question! Though we still have to make some guess and do some experiment most of times to sort things like this out, a few RevitAddinCoder coders can help make the task a lot easier: • Parameter Infoer Coder
• Parameter Categorizer Coder
• Parameter Retriever Coder To use the method is very straightforward. Here is some test code in C#: …
Phase phase = CachedDoc.Phases.get_Item(0);
ICollection<ElementId> ids = GetWallsInPhase(CachedDoc, phase);
TaskDialog.Show("ElementParameterFilter Test", string.Format("{0} walls are in the phase created {1}.", ids.Count, phase.Name));
…
|