Let us continue to talk about the particular slow filter in Revit API, ElementParameterFilter.
Let’s look at how to use the SharedParameterApplicableRule filter rule to filter out elements which have some project parameter associated with. Supposing we’d like to find all elements which have a specific project parameter, what shall we do?
The following code does so in C#:
public static List<IGrouping<string, Element>> GetWallsHavingProjectParameter(RvtDocument doc, string paramName)
{
SharedParameterApplicableRule rule = new SharedParameterApplicableRule(paramName);
ElementParameterFilter filter = new ElementParameterFilter(rule);
return (new FilteredElementCollector(doc)).
WherePasses(filter).
GroupBy(e => e.GetType().Name).
ToList();
}
A few highlights about the code:
• An ElementParameterFilter needs a filter rule, the SharedParameterApplicableRule in this case.
• The SharedParameterApplicableRule only needs a parameter name.
• The parameter should be a project parameter or bound shared parameter instead of a BuiltInParameter.
• No fast filter like ElementClassFilter is used here as it does not make sense to do so since project parameters can only be bound to some Revit whole categories or element classes rather than to a few particular element instances.
To use the method is very straightforward. Here is some test code in C#:
…
string sharedParameterName = "PrjParameter1";
List<IGrouping<string, Element>> list = GetWallsHavingProjectParameter(CachedDoc, sharedParameterName);
string msg1 = string.Format("{0} types of elements have the shared parameter {1}\n", list.Count, sharedParameterName);
foreach (IGrouping<string, Element> group in list)
{
msg1 += string.Format("\tCount in type {0}: {1}\n", group.Key, group.Count());
}
MessageBox.Show(msg1);
…
So in the test model, four element classes have the project parameter PrjParameter1 associated with and they are WallType, Wall, FamilyInstance, and FamilySymbol. The element count in each class is also reported.
ElementParameterFilter Creator of RevitAddinCoder can create the code automatically in no time.
How would i add another filter that filters by one of "PrjParameter1" values?
PrjParameter1.Equals("something");
Posted by: RevitRyan | 03/17/2015 at 07:43 PM
Hi RevitRyan, using a filter rule along with the ElementParameterFilter looks what you are looking for. In terms of which filter rule to apply, it depends on the project parameter value type (int, double, string, or something else) and the need (equality comparing or range checking). We have a few other posts to cover some such scenarios. Here are a few:
ElementParameterFilter: Using FilterStringRule to Filter Element Parameters in C#
http://spiderinnet.typepad.com/blog/2011/07/elementparameterfilter-using-filterstringrule-to-filter-element-parameters-in-c.html
ElementParameterFilter: Using FilterIntegerRule to Filter Element Parameters in C#
http://spiderinnet.typepad.com/blog/2011/07/elementparameterfilter-using-filterintegerrule-to-filter-element-parameters-in-c.html
ElementParameterFilter: Using FilterDoubleRule to Filter Element Parameters in C#
http://spiderinnet.typepad.com/blog/2011/07/elementparameterfilter-using-filterdoublerule-to-filter-element-parameters-in-c.html
Posted by: Spiderinnet | 03/18/2015 at 12:14 PM