View Filters

View Filters

Filters are elements independent of views. They can be applied to Views using the ParameterFilterElement class or a SelectionFilterElement class.

ParameterFilterElement

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 new view filter matching multiple criteria and then hides those elements in the view.

Code Region: Applying a parameter filter to a view

public static void CreateViewFilter(Document doc, View view)
{
    List<ElementId> categories = new List<ElementId>();
    categories.Add(new ElementId(BuiltInCategory.OST_Walls));
    List<FilterRule> filterRules = new List<FilterRule>();

    using (Transaction t = new Transaction(doc, "Add view filter"))
    {
        t.Start();

        // Create filter element associated to the input categories
        ParameterFilterElement parameterFilterElement = ParameterFilterElement.Create(doc, "Example view filter", categories);

        // Criterion 1 - wall type Function is "Exterior"
        ElementId exteriorParamId = new ElementId(BuiltInParameter.FUNCTION_PARAM);
        filterRules.Add(ParameterFilterRuleFactory.CreateEqualsRule(exteriorParamId, (int)WallFunction.Exterior));

        // Criterion 2 - wall height > some number
        ElementId lengthId = new ElementId(BuiltInParameter.CURVE_ELEM_LENGTH);
        filterRules.Add(ParameterFilterRuleFactory.CreateGreaterOrEqualRule(lengthId, 28.0, 0.0001));

        // Criterion 3 - custom shared parameter value matches string pattern
        // Get the id for the shared parameter - the ElementId is not hardcoded, so we need to get an instance of this type to find it
        Guid spGuid = new Guid("96b00b61-7f5a-4f36-a828-5cd07890a02a");
        FilteredElementCollector collector = new FilteredElementCollector(doc);
        collector.OfClass(typeof(Wall));
        Wall wall = collector.FirstElement() as Wall;

        if (wall != null)
        {
            Parameter sharedParam = wall.get_Parameter(spGuid);
            ElementId sharedParamId = sharedParam.Id;

            filterRules.Add(ParameterFilterRuleFactory.CreateBeginsWithRule(sharedParamId, "15.", true));
        }

        parameterFilterElement.SetRules(filterRules);

        // Apply filter to view
        view.AddFilter(parameterFilterElement.Id);
        view.SetFilterVisibility(parameterFilterElement.Id, false);
        t.Commit();
    }
}

SelectionFilterElement

A SelectionFilterElement is a special view filter not based on rules, but on a group of possibly unrelated elements. Specific elements can be added to the filter as required and the resulting selection can be overridden just like ParameterFilterElement.

The following example creates a new selection filter and applies an override to it.

Code Region: Applying a selection filter to a view

public static void CreateSelectionFilter(Document doc, View view)
{
    // find room tags in this view
    FilteredElementCollector collector = new FilteredElementCollector(doc, view.Id);
    collector.WherePasses(new RoomTagFilter());

    // collect tags whose room number matches criteria
    List<ElementId> tagIds = new List<ElementId>();

    foreach (RoomTag tag in collector.Cast<RoomTag>())
    {
        int number = Int32.Parse(tag.Room.Number);
        if (number % 3 == 0)
        {
            tagIds.Add(tag.Id);
        }
    }

    using (Transaction t = new Transaction(doc, "Create SelectionFilterElement"))
    {
        t.Start();

        // Create selection filter and assign ids
        SelectionFilterElement filterElement = SelectionFilterElement.Create(doc, "Room tags filter");
        filterElement.SetElementIds(tagIds);

        ElementId filterId = filterElement.Id;

        // Add the filter to the view
        view.AddFilter(filterId);

        doc.Regenerate();

        // Use the existing graphics settings, and change the color to Blue
        OverrideGraphicSettings overrideSettings = view.GetFilterOverrides(filterId);

        overrideSettings.SetProjectionLineColor(new Color(0x00, 0x00, 0xFF));

        view.SetFilterOverrides(filterId, overrideSettings);

        t.Commit();
    }
}

Modifying filters

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.

The following example demonstrates how to get the filters in a view and then modifies the overrides associated with any filter currently setting the cut color to red.

Code Region: Modify existing filter

public static void ModifyExistingFilter(Document doc, View view)
{
    // Find any filter with overrides setting cut color to Red
    Dictionary<ElementId, OverrideGraphicSettings> filterIdsToChange = new Dictionary<ElementId, OverrideGraphicSettings>();

    foreach (ElementId filterId in view.GetFilters())
    {
        OverrideGraphicSettings overrideSettings = view.GetFilterOverrides(filterId);

        Color lineColor = overrideSettings.CutLineColor;

        if (lineColor == Color.InvalidColorValue)
            continue;

        // Save overrides setting the cut color to green
        if (lineColor.Red == 0xFF && lineColor.Green == 0x00 && lineColor.Blue == 0x00)
        {
            overrideSettings.SetCutLineColor(new Color(0x00, 0xFF, 0x00));
            filterIdsToChange[filterId] = overrideSettings;
        }
    }

    // Make the change to all found filters
    using (Transaction t = new Transaction(doc, "Change override filters"))
    {
        t.Start();

        foreach (ElementId filterId in filterIdsToChange.Keys)
        {
            view.SetFilterOverrides(filterId, filterIdsToChange[filterId]);
        }
        t.Commit();
    }
}