How to add an existing filter to a view

How to add an existing filter to a view

Anonymous
Not applicable
3,144 Views
2 Replies
Message 1 of 3

How to add an existing filter to a view

Anonymous
Not applicable

I am trying to add filter to views. After I create the filter and add it to one view, I don't know how to add it 

to the other views.

I tried to isolate the Creation in  a try Catch but I still need "parameterFilterElement" to use.

 

How can I add the same filter to other views?

Thanks

 

 

 

 

  private void CreateViewFilter(  View view,string property, string filterName, string value, Color colo)
        {

            var col = new FilteredElementCollector(doc);

            var elems = col.OfClass(typeof(FillPatternElement));
            string dx = "";
            ElementId xx = null;
            foreach(Element i in elems)
            {
                dx += i.Name+"      "+i.Id+"\n";
                if (i.Name == "Solid fill")
                    xx = i.Id;
            }

          // TaskDialog.Show("pattern", dx);

            List<ElementId> categories = new List<ElementId>();
           
            categories.Add(new ElementId(BuiltInCategory.OST_Walls));
       
            Transaction colorIt = new Transaction(doc, "the elements");
            ParameterFilterElement parameterFilterElement=null;

            colorIt.Start();
            try
            {
                parameterFilterElement = ParameterFilterElement.Create(doc, filterName, categories);
            }
            catch(Exception ex)
            {
                TaskDialog.Show("Info", "Filter is already created ");
            }
            FilteredElementCollector parameterCollector = new FilteredElementCollector(doc);
            Parameter parameter = parameterCollector.OfClass(typeof(Wall)).FirstElement().LookupParameter(property);

            List<FilterRule> filterRules = new List<FilterRule>();
            filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(parameter.Id, value, true));
            parameterFilterElement.SetRules(filterRules);
             OverrideGraphicSettings filterSettings = new OverrideGraphicSettings();
            filterSettings.SetCutFillColor(colo);
            filterSettings.SetCutFillPatternId(xx);
            view.SetFilterOverrides(parameterFilterElement.Id, filterSettings);
            //


          

            colorIt.Commit();

          
        }

0 Likes
Accepted solutions (1)
3,145 Views
2 Replies
Replies (2)
Message 2 of 3

keith_white
Autodesk
Autodesk
Accepted solution

Hi oakam. 

 

First, you need to instantiate a parameter filter element object to use later in your code  

ParameterFilterElement pfe = null;

 

Next, you need to collect all filters in the model.

FilteredElementCollector coll = new FilteredElementCollector(doc).OfClass(typeof(ParameterFilterElement));

 

Next check for the parameter filter using its name.

IEnumerable<Element> paramFilters = from element in coll where element.Name.Equals(name) select element;

 

Next Iterate over the IEnumerable collection (there should only be one, but just in case)

foreach (Element element in paramFilters)
{
    if (element.Name.Equals(name))
    {
        pfe = element as ParameterFilterElement;
    }
}

 

Next check the pfe for null and create if not found in the model

if (pfe == null)

{

   ...you can use your code here to create the parameter filter element

  pfe = CreateParameterFilterElement(doc, parameterFilterName, categoryIds)

}

 

Next build out the Override Graphics Settings and add the parameter filter element to the view that was collected or created in the steps above

{

    ...you can use your code here to build out the override graphics settings

    OverrideGraphicSettings ogs = new OverrideGraphicSettings();

    ...

 

    ...you can use your code here to add the parameter filter element to the view using the pfe.id value

    uiDoc.ActiveView.SetFilterOverrides(pfe.Id, ogs);

}

 

You will need to wrap the above within a transaction and sub-transactions to commit the changes, but that should be easy enough.

 

I hope this helps.

 

Keith



Keith White

Principal Solution Architect, Autodesk Consulting
Message 3 of 3

DollyPortis-WPM
Participant
Participant
Thank you so much for your solution. It helped me to finally get my plugin to work right.