BIM World
A Professional BIM Learning Platform


Revit API: How to Retrieve Filter Colors in View Visibility Settings


In Revit’s View > Visibility/Graphics settings, you can change the color of a specific component type by applying filters, as illustrated in the image below:Revit Secondary Development - Obtaining Filter Colors in View Visibility

But how can we retrieve or set the colors of these filters programmatically? The Revit API offers the GetFilterOverrides() method, which requires passing an ElementId and returns an OverrideGraphicSettings object. This object allows us to access or modify filter colors and other graphical settings.

Here is a sample code demonstrating how to obtain filter colors applied to the active view:

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System.Collections.Generic;
using System.IO;

namespace SettingFillPatternByCate
{
 [Transaction(TransactionMode.Manual)]
 class Command : IExternalCommand
 {
  public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
  {
   Document doc = commandData.Application.ActiveUIDocument.Document;
   View v = doc.ActiveView;
   StreamWriter sw = new StreamWriter(@"C:UsersAdministratorDesktopFilterColor.txt", false);
   string names = null;

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

   ICollection filterIds = v.GetFilters();
   Color c = new Color(255, 255, 255);

   foreach (ElementId id in filterIds)
   {
    Element filter = doc.GetElement(id);
    OverrideGraphicSettings ogs2 = v.GetFilterOverrides(id);
    c = ogs2.ProjectionFillColor;

    if (c.IsValid)
    {
     names += filter.Name + " " + id.ToString() + " (" + c.Red + "," + c.Green + "," + c.Blue + ")n";
    }
   }

   sw.Write(names);
   sw.Close();

   return Result.Succeeded;
  }
 }
}

The content of the generated FilterColor.txt file looks like this:Revit Secondary Development - Obtaining Filter Colors in View Visibility

A key point to note here is the difference between two approaches used to retrieve filters from the document:

1. Using a FilteredElementCollector to gather all filters in the document:

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

2. Directly getting all filters applied to the active view:

ICollection filterIds = v.GetFilters();

The first method returns all filters present in the document, regardless of whether they are applied to the current view. The second method retrieves only those filters that are actively applied to the view.

If you use the first approach and then call:

OverrideGraphicSettings ogs2 = v.GetFilterOverrides(id);

you might encounter an exception stating “The filter was not applied to the view”. This happens because some filters retrieved by the first method are not applied to the current view.

Please feel free to correct any inaccuracies. Thank you very much!

xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit API: How to Retrieve Filter Colors in View Visibility Settings

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up