Filter Rule data – where is it hiding?

It is in there, but you’ve got to dig into some sub-classes and static functions.

This doesn’t implement every case, hopefully is enough to shed some light on how it works.

public void ListFilters()
{
    Document doc = this.ActiveUIDocument.Document;
    foreach (ParameterFilterElement pfe in new FilteredElementCollector(doc).OfClass(typeof(ParameterFilterElement)).Cast<ParameterFilterElement>())
    {        
        string ruleData = "";                    
        string categories = "";
            
        foreach (ElementId catid in pfe.GetCategories())
        {
            categories += doc.Settings.Categories.get_Item(((BuiltInCategory)catid.IntegerValue)).Name + ",";
        }
                 
        foreach (FilterRule rule in pfe.GetRules())
        {
            string comparator = "";
            string ruleValue = "";

            if (rule is FilterDoubleRule)
            {
                FilterDoubleRule fdr = rule as FilterDoubleRule;
                
                if (fdr.GetEvaluator().GetType().Equals(typeof(FilterNumericLess)))
                    comparator = "<";
                else if (fdr.GetEvaluator().GetType().Equals(typeof(FilterNumericGreater)))
                    comparator = ">";
                
                ruleValue = fdr.RuleValue.ToString();

            }
            else if (rule is FilterStringRule)
            {
                FilterStringRule fsr = rule as FilterStringRule;
                
                if (fsr.GetEvaluator().GetType().Equals(typeof(FilterStringBeginsWith)))
                    comparator = "starts with";
                else if (fsr.GetEvaluator().GetType().Equals(typeof(FilterStringEndsWith)))
                    comparator = "ends with";
                else if (fsr.GetEvaluator().GetType().Equals(typeof(FilterStringEquals)))
                    comparator = "=";
                else if (fsr.GetEvaluator().GetType().Equals(typeof(FilterStringContains)))
                    comparator = "contains";
                
                ruleValue = fsr.RuleString;
            }
            else if (rule is FilterIntegerRule)
            {
                FilterIntegerRule fir = rule as FilterIntegerRule;
                
                if (fir.GetEvaluator().GetType().Equals(typeof(FilterNumericEquals)))
                    comparator = "=";
                else if (fir.GetEvaluator().GetType().Equals(typeof(FilterNumericGreater)))
                    comparator = ">";
                
                // some parameters store an integer but the UI shows a text string
                // this text comes from the value of an enum such as WallFunction
                if (((BuiltInParameter)ParameterFilterElement.GetRuleParameter(rule).IntegerValue) == BuiltInParameter.FUNCTION_PARAM)
                {
                    ruleValue = ((WallFunction)fir.RuleValue).ToString();
                }
                else
                    ruleValue = fir.RuleValue.ToString();
            }
            
            string paramName = "";
            if (ParameterFilterElement.GetRuleParameter(rule).IntegerValue < 0)
                paramName = LabelUtils.GetLabelFor((BuiltInParameter)ParameterFilterElement.GetRuleParameter(rule).IntegerValue);
            else
                paramName = doc.GetElement(ParameterFilterElement.GetRuleParameter(rule)).Name;

            
            ruleData += "'" + paramName + "' " +
                comparator + " " +
                "'" + ruleValue.ToString() + "'" +
                Environment.NewLine;
        }
        TaskDialog td = new TaskDialog("Rule");
        td.MainInstruction = "Filter name: " + pfe.Name;
        td.MainContent = "Categories: " + categories + Environment.NewLine + Environment.NewLine + ruleData;
        td.Show();
    }
}
Advertisements

5 thoughts on “Filter Rule data – where is it hiding?

  1. Hi Harry,

    Thanks for your post.

    I didn’t find a way to filter walls by “location line”. I used BuiltInParameter WALL_KEY_REF_PARAM and BuiltInCategory OST_WALLS to create a ParameterFilterElement, but it failed saying: “One of the given rules refers to a parameter that does not apply to this filter’s categories.
    Parameter name: rules”

    It seems impossible with the UI so I think we just cannot filter walls using their location lines…

    What do you think about that ?

    Thanks a lot for your ideas,
    Maxime

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s