View Filters

View Filters

Filters can be applied to Views using the ParameterFilterElement class. A ParameterFilterElement filters elements based on its category and a series of filter rules. One or more categories can be specified as allowable for the filter.

Once a filter has been defined (with one or more categories and one or more filter rules), it can be applied to a View using one of several methods. The View.AddFilter() method will apply the filter to the view, but with default overrides, meaning the view's display will not change. View.SetFilterOverrides() will set graphical overrides associated with a filter. And View.SetFilterVisibility() will set whether the elements that pass the filter are visible in the view or not. AddFilter() and SetFilterVisibility() will both apply the filter to the view if it is not already applied, making it unnecessary to call AddFilter() separately.

The following example creates a filter which includes all walls whose Comments property is set to "foo". The filter is then applied to the View so that any walls meeting this criteria are outlined in red.

Code Region: Applying a filter to a view

private void CreateViewFilter(Autodesk.Revit.DB.Document doc, View view)
{
    List<ElementId> categories = new List<ElementId>();
    categories.Add(new ElementId(BuiltInCategory.OST_Walls));
    ParameterFilterElement parameterFilterElement = ParameterFilterElement.Create(doc, "Comments = foo", categories);

    FilteredElementCollector parameterCollector = new FilteredElementCollector(doc);
    Parameter parameter = parameterCollector.OfClass(typeof(Wall)).FirstElement().get_Parameter("Comments");

    List<FilterRule> filterRules = new List<FilterRule>();
    filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(parameter.Id, "foo", true));
    parameterFilterElement.SetRules(filterRules);

            
    OverrideGraphicSettings filterSettings = new OverrideGraphicSettings();
    // outline walls in red            
    filterSettings.SetProjectionLineColor(new Color(255, 0, 0));
    view.SetFilterOverrides(parameterFilterElement.Id, filterSettings);
}

All filters applied to a view can be retrieved using the View.GetFilters() method which will return a list of filter ids. Filter visibility and graphic overrides can be checked for a specific filter using the View.GetFilterVisibility() and View.GetFilterOverrides() methods respectively. View.RemoveFilter will remove a filter from the view.