What you can do with View Filters in the 2014 API

Here’s are the new Filter items for the View class in 2014:

  • AddFilter – The filter will be added with default overrides, so there will be no change in the view’s display.
  • GetFilterOverrides – Returns all graphic overrides of a specific filter in the view
  • GetFilters – Returns the ids of the filters applied to the view
  • GetFilterVisibility – Returns true if the elements associated with a specific filter are visible in the view
  • IsFilterApplied – Returns true if a specific filter is applied to the view
  • RemoveFilter
  • SetFilterOverrides – Set the OverrideGraphicSettings for a filter in a view. Adds the filter to the view if it hadn’t been previously added.
  • SetFilterVisibility – Set the visibility of a specific filter (true / false) for a view. Adds the filter to the view if it hadn’t been previously added.
public void Filter2014Sample()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    View view = doc.ActiveView;

    TaskDialog.Show("Boost Your BIM","# of filters applied to this view = " + view.GetFilters().Count);

    // create list of categories that will for the filter
    IList<ElementId> categories = new List<ElementId>();
    categories.Add(new ElementId(BuiltInCategory.OST_Walls));

    // create a list of rules for the filter
    IList<FilterRule> rules = new List<FilterRule>();
    // This filter will have a single rule that the wall type width must be less than 1/2 foot
    Parameter wallWidth = new FilteredElementCollector(doc).OfClass(typeof(WallType)).FirstElement().get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);
    rules.Add(ParameterFilterRuleFactory.CreateLessRule(wallWidth.Id, 0.5, 0.001));

    ParameterFilterElement filter = null;
    using (Transaction t = new Transaction(doc, "Create and Apply Filter"))
    {
        t.Start();
        filter =  ParameterFilterElement.Create(doc, "Thin Wall Filter", categories, rules);
        view.AddFilter(filter.Id);
        t.Commit();
    }

    string filterNames = "";
    foreach (ElementId id in view.GetFilters())
    {
        filterNames += doc.GetElement(id).Name + "\n";
    }
    TaskDialog.Show("Boost Your BIM","Filters applied to this view: " + filterNames);

    // Create a new OverrideGraphicSettings object and specify cut line color and cut line weight
    OverrideGraphicSettings ogs = new OverrideGraphicSettings();
    ogs.SetCutLineColor(new Color(255,0,0));
    ogs.SetCutLineWeight(9);

    using (Transaction t = new Transaction(doc, "Set Override Appearance"))
    {
        t.Start();
        view.SetFilterOverrides(filter.Id,ogs);
        t.Commit();
    }
}

17 thoughts on “What you can do with View Filters in the 2014 API

  1. So, I am curious if the Revit 2014 API has a “RemoveLineStyle” feature? I know I can control the color, weight, line pattern (from a list) but I have never been able to DELETE line styles (and trust me.. those can get out of hand quickly).

    • Line Styles are Revit elements, so you should be able to use these to find the styles and then Document.Delete to remove them.
      CurveElement..::..GetLineStyleIds Method – Retrieves the set of available line style Element Ids.
      CurveElement..::..LineStyle Property – The line style of this curve element. (returns the Line Style element)

  2. Hello!
    But if i want to create filter by “Uconnected Height”?
    How should i do it, because there is no exist in filter option sort field like that.

    • Hi,

      You can change this line to get whatever parameter you want.

      Harry

      Parameter wallWidth = new FilteredElementCollector(doc).OfClass(typeof(WallType)).FirstElement().get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);

      • Thanks for quick answer
        Ye i’ve thougth so too, for example i’ve wanted to use BuiltInParameter.WALL_USER_HEIGHT_PARAM parameter (Unconnected height) but i have null value parameter.
        or for examle COMMENTS i have the same situation.
        Parameter wallWidth = new FilteredElementCollector(doc).OfClass(typeof(WallType)).FirstElement().get_Parameter(BuiltInParameter.WALL_USER_HEIGHT_PARAM);
        And surely i cant create rules…

        • This code is getting a parameter from a wall type. If you want to get a parameter from a wall instance then you would need to change the OfClass statement.

          Parameter wallWidth = new FilteredElementCollector(doc).OfClass(typeof(WallType)).FirstElement().get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);

          • Thanks again Harry,
            for now i’ve got this parameter, but other problem is i cant add the rule with those parameter 😦
            rules.Add(ParameterFilterRuleFactory.CreateGreaterRule(wallWidth.Id, 5));
            i means that rule has been added but it cant be apply in model.
            View Filter show me all walls, but shoul show more than 5 meters.

            Is there way to implement filtering for elementId collection?

      • Thanks for your reply Harry
        These are the using statements I have:

        using System;
        using System.Diagnostics;
        using System.IO;
        using System.Linq;
        using System.Text;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using Autodesk.Revit.DB;
        using Autodesk.Revit.DB.Architecture;
        using Autodesk.Revit.DB.Events;
        using Autodesk.Revit.UI.Events;
        using Autodesk.Revit.UI;
        using Autodesk.Revit.UI.Selection;
        using Autodesk.Revit.ApplicationServices;

  3. Dear Harry,
    The above code shows Nullreferenec for new versions of Revit.
    I got it worked for new versions. This one works and does not show any error or exception
    public void Filter2014Sample()
    {
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;
    View view = doc.ActiveView;

    TaskDialog.Show(“Boost Your BIM”,”# of filters applied to this view = ” + view.GetFilters().Count);

    // create list of categories that will for the filter
    IList categories = new List();
    categories.Add(new ElementId(BuiltInCategory.OST_Walls));

    // create a list of rules for the filter
    IList rules = new List();
    // This filter will have a single rule that the wall type width must be less than 1/2 foot
    FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Wall));
    Wall wl = collector.FirstElement() as Wall;
    WallType wlType = wl.WallType;
    Parameter wallWidth = wlType.get_Parameter(BuiltInParameter.WALL_ATTR_WIDTH_PARAM);
    rules.Add(ParameterFilterRuleFactory.CreateLessRule(wallWidth.Id, 0.5, 0.001));

    ParameterFilterElement filter = null;
    using (Transaction t = new Transaction(doc, “Create and Apply Filter”))
    {
    t.Start();
    filter = ParameterFilterElement.Create(doc, “Thin Wall Filter”, categories, rules);
    view.AddFilter(filter.Id);
    t.Commit();
    }

    string filterNames = “”;
    foreach (ElementId id in view.GetFilters())
    {
    filterNames += doc.GetElement(id).Name + “\n”;
    }
    TaskDialog.Show(“Boost Your BIM”,”Filters applied to this view: ” + filterNames);

    // Create a new OverrideGraphicSettings object and specify cut line color and cut line weight
    OverrideGraphicSettings ogs = new OverrideGraphicSettings();
    ogs.SetCutLineColor(new Color(255,0,0));
    ogs.SetCutLineWeight(9);

    using (Transaction t = new Transaction(doc, “Set Override Appearance”))
    {
    t.Start();
    view.SetFilterOverrides(filter.Id,ogs);
    t.Commit();
    }
    }

Leave a reply to harrymattison Cancel reply