View Graphic Overrides

View Graphic overrides are very commonly used in Revit API. It's a great way to mark elements without a need of a View Filter. Let's learn how to override graphics.

View Graphic Overrides

View Graphic overrides are very commonly used in Revit API. It's a great way to mark elements without a need of a View Filter. Let's learn how to override graphics.

Download Files

Summary

Graphic Overrides in Revit

As you know, we can select any element in a view and apply custom graphic overrides. And using the graphic overrides with Revit API is great.

Often, we need to highlight some elements based on a specific parameter. While view filters are a great option, we don't always want to create hundreds of them just for a single view.

So, Graphic Overrides are the perfect solution to that, and it's very easy with Revit API.

How to Change Graphic Overrides?

Graphic Overrides are stored in a specific view, so we need to check the View class to find out how to get and set an element's overrides. There are two methods:

💡Also, we would need to check the OverrideGraphicSettings Class.

👈This menu from the Revit UI represents OverrideGraphicSettings.

It contains all overrides for Projection/Cut Lines, Surface/Cut Patterns, and a few extra settings in a view.

So, to override elements with Revit API, we need to create an instance of a OverrideGraphicSettings and then change overrides as we want by using methods such as:

SetCutBackgroundPatternColor, SetCutBackgroundPatternId, SetCutBackgroundPatternVisible, SetProjectionLineColor, SetProjectionLinePatternId, and so on…

Preparations

Let's start by defining our script for the button and picking an element with PickObject, so we have something to override.

#⬇️ IMPORTS
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType

#📦 VARIABLES
uidoc  = __revit__.ActiveUIDocument
doc    = __revit__.ActiveUIDocument.Document #type:Document

# 👉 PICK AN ELEMENT
ref_object = uidoc.Selection.PickObject(ObjectType.Element)
elem       = doc.GetElement(ref_object)
Define Colors and Patterns

To override graphics, we also need to define the colors and patterns which should be used in OverrideGraphicSettings. We need to use the following classes to define them:

#🟠 Define Color
color = Color(255,200,0)

#🧱 GET FILL PATTERNS
all_patterns  = FilteredElementCollector(doc).OfClass(FillPatternElement).ToElements()
solid_pattern = [i for i in all_patterns if i.GetFillPattern().IsSolidFill][0]

#➖ GET LINE PATTERNS AND WEIGHT
line_patterns       = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements()
random_line_pattern = line_patterns[0]
lineweight          = 5

Now we can create a default instance of OverrideGraphicSettings and then modify its overrides.

We will use the following methods:

There are many more methods to override cut patterns, lines and colors… They are used exactly the same way.

Just choose the settings that you want to override and apply colors and patterns as you want.

#🖌️ Create Override Settings
override_settings = OverrideGraphicSettings()

#⚙️ Modify Override Settings (COLOR, COLOR PATTERN, LINE PATTERN, LINEWEIGHT)
override_settings.SetSurfaceForegroundPatternId(solid_pattern.Id)
override_settings.SetSurfaceForegroundPatternColor(color)
override_settings.SetProjectionLinePatternId(random_line_pattern.Id)
override_settings.SetProjectionLineColor(color)
override_settings.SetProjectionLineWeight(lineweight)

#👀 More Settings
override_settings.SetSurfaceTransparency(45)
override_settings.SetHalftone(True)

Lastly, we can get our ActiveView and SetElementOverrides for the selected element.


# Transaction 🔓
t = Transaction(doc, 'Override Graphics')
t.Start()

active_view = doc.ActiveView
active_view.SetElementOverrides(elem.Id, override_settings)

t.Commit()

Complete Code: Set Graphic Overrides

Here is the full code to avoid any confusions.

#⬇️ IMPORTS
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import ObjectType


#📦 VARIABLES
uidoc  = __revit__.ActiveUIDocument
doc    = __revit__.ActiveUIDocument.Document #type:Document


# 👉 PICK AN ELEMENT
ref_object = uidoc.Selection.PickObject(ObjectType.Element)
elem       = doc.GetElement(ref_object)

#🖌️ Create Override Settings
override_settings = OverrideGraphicSettings()

#🟠 Define Color
color = Color(255,200,0)

#🧱 GET FILL PATTERNS
all_patterns  = FilteredElementCollector(doc).OfClass(FillPatternElement).ToElements()
solid_pattern = [i for i in all_patterns if i.GetFillPattern().IsSolidFill][0]

#➖ GET LINE PATTERNS AND WEIGHT
line_patterns       = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements()
random_line_pattern = line_patterns[0]
lineweight          = 5


#⚙️ Modify Override Settings (COLOR, COLOR PATTERN, LINE PATTERN, LINEWEIGHT)
override_settings.SetSurfaceForegroundPatternId(solid_pattern.Id)
override_settings.SetSurfaceForegroundPatternColor(color)
override_settings.SetProjectionLinePatternId(random_line_pattern.Id)
override_settings.SetProjectionLineColor(color)
override_settings.SetProjectionLineWeight(lineweight)

#👀 More Settings
override_settings.SetSurfaceTransparency(45)
override_settings.SetHalftone(True)

# Transaction 🔓
t = Transaction(doc, 'Override Graphics')
t.Start()

active_view = doc.ActiveView
active_view.SetElementOverrides(elem.Id, override_settings)

t.Commit()

How To Clear Overrides?

You know how to set your colors, but what if you want to clear the overrides?

It's actually very simple, as we just need to create a default OverrideGraphicSettings, and set it as the override. This will have the same effect as if we clicked the Reset button in the Revit UI.

Here is the code:

# Transaction 🔓
t = Transaction(doc, 'Clear Override Graphics')
t.Start()

override_settings = OverrideGraphicSettings()

active_view = doc.ActiveView
active_view.SetElementOverrides(elem.Id, override_settings)

t.Commit()

How to Match Graphic Overrides?

Let's also have a look at how to match graphic overrides.

Obviously, we need to select at least 2 elements and then use GetElementOverrides from one element, and SetElementOverrides on another element.

Here is a simple code:

def pick_el():
    ref_object = uidoc.Selection.PickObject(ObjectType.Element)
    return doc.GetElement(ref_object)


#📦 Variables
active_view = doc.ActiveView
main_el  = pick_el()
match_el = pick_el()

# Transaction 🔓
t = Transaction(doc, 'Clear Override Graphics')
t.Start()

#🎨 Override Settings
overrides = active_view.GetElementOverrides(main_el.Id)
active_view.SetElementOverrides(match_el.Id, overrides)

t.Commit() #🔒

It works! But only for one element at a time… Let's have a look at how to make it work for more than one element.

Match Graphic Elements (Multiple)

To make this code work for multiple elements, we need to create a loop. Since the number of elements can be different, we can use While True loop, and then control when to stop execution.

💡Whenever you use while loops, start by writing a break statement! You don't want to create an infinite loop and restart Revit.

Also, keep in mind that when we use PickObject method and we cancel the selection, it will prompt an error message to the user, saying the selection was aborted… So we can use that as a trigger to stop our loop.

We can use try/except and make changes in the try statement, and if it fails during selection, then we can break out of our loop.

💡Also, keep in mind that if you want changes to be visible after each element, we should place the Transaction inside the loop as well, so each element will have its own transaction.

But since we place and start a transaction inside the loop, we also need to make sure we RollBack the Transaction whenever try statement fails (due to aborting selection).

So here is the updated code to do that:

#🧬 FUNCTION
def pick_el():
    ref_object = uidoc.Selection.PickObject(ObjectType.Element)
    return doc.GetElement(ref_object)

#📦 Variables
active_view = doc.ActiveView
main_el  = pick_el()

#🎨 Override Settings
while True:
    try:
        # Transaction 🔓
        t = Transaction(doc, 'Clear Override Graphics')
        t.Start()
        match_el = pick_el()
        overrides = active_view.GetElementOverrides(main_el.Id)
        active_view.SetElementOverrides(match_el.Id, overrides)
        t.Commit()

    except:
        t.RollBack()
        break
Summary

Now you know how to create, apply, clear, and match graphic overrides using Revit API.

This can help you highlight elements based on various parameters, allowing you to create awesome overviews of your projects.

There will be another lesson in this module, where we will create a tool to Isolate and Override element colors in a 3D View.

It's a great use of GraphicOverrides to create specific plans.

HomeWork

How about trying Graphic Overrides in action?

I want you to create a simple tool based on this lesson. You will need to get some elements, and override their colors based on a parameter.

Here is an example for this Tool Idea:

  • Get Elements (Walls)

  • Sort by Parameter (Type Name)

  • Create a unique Color for each TypeName ( dict{TypeName: Color} )

  • Override all walls in a view based on a the Type Name

Adjust this to something that you might find useful and give it a try.

⌨️ Happy Coding!

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API