SetElementOverrides with Revit API + python

You will learn how to override graphics of your elements in a view with Revit API + python. It's actually quite simple, even though we need to use many methods to create settings!

Revit API

Override Graphics

To override graphics with Revit API we need to use .SetElementOverrides(element_id, settings) that is located inside of View Classes. It's not a complicated method but it has a lot of methods that you might need to use to create Override Settings that you want to apply to Elements.

Revit API - SetElementOverrides .png

As you can see we just need to provide ElementId and OverrideGraphicsSettings

OverrideGraphicSettings

First of all we need to create OverrideGraphicSettings and then we can start modifying it.

settings = OverrideGraphicSettigns()

There are a lot of different methods for OverrideGraphicSettings, but yet it's quite simple to use them.
Just think of the menu in Revit UI where you manually have to assign all of the overrides.

Revit UI - GraphicOverrides Menu


You will find methods in Revit API Documentation for each of the controls you have in that UI menu.
πŸ‘‡ Check them out here

Method Description
.SetCutBackgroundPatternColor Sets the override color of the background pattern of cut faces.
.SetCutBackgroundPatternId Sets the ElementId of the cut face background pattern override. The fill pattern must be a drafting pattern. A value of InvalidElementId means no override is set.
.SetCutBackgroundPatternVisible Sets the visibility of the cut face background fill pattern.
.SetCutForegroundPatternColor Sets the override color of the foreground pattern of cut faces.
.SetCutForegroundPatternId Sets the ElementId of the cut face foreground pattern override. The fill pattern must be a drafting pattern. A value of InvalidElementId means no override is set.
.SetCutForegroundPatternVisible Sets the visibility of the cut face foreground fill pattern.
.SetCutLineColor Sets the cut surface line color.
.SetCutLinePatternId Sets the ElementId of the cut surface line pattern.
.SetCutLineWeight Sets the cut surface line weight.
.SetDetailLevel Sets the detail level.
.SetHalftone Sets the halftone value.
.SetProjectionLineColor Sets the projection surface line color.
.SetProjectionLinePatternId Sets the ElementId of the projection surface line pattern.
.SetProjectionLineWeight Sets the projection surface line weight.
.SetSurfaceBackgroundPatternColor Sets the override color of the surface background pattern.
.SetSurfaceBackgroundPatternId Sets the ElementId of the surface background pattern override. The fill pattern must be a drafting pattern. A value of InvalidElementId means no override is set.
.SetSurfaceBackgroundPatternVisible Sets the visibility of the surface background fill pattern.
.SetSurfaceForegroundPatternColor Sets the override color of the surface foreground pattern.
.SetSurfaceForegroundPatternId Sets the ElementId of the surface foreground pattern override. The fill pattern must be a drafting pattern. A value of InvalidElementId means no override is set.
.SetSurfaceForegroundPatternVisible Sets the visibility of the surface foreground fill pattern.
.SetSurfaceTransparency Sets the projection surface transparency.

OverrideGraphicSettings Methods

How to use OverrideGraphicSettings Methods?

By looking at each method you can see arguments that you need to provide. It can be Color, LinePattern, FillPattern,Number...

πŸ‘‡ Here is a simple snippet to get everything we need and only change Surface Color of the elements.

code
#⬇️ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ Variables
view = doc.ActiveView
walls_in_view = FilteredElementCollector(doc, doc.ActiveView.Id) \
        .OfCategory(BuiltInCategory.OST_Walls) \
        .WhereElementIsNotElementType() \
        .ToElements()


#🎨 Create a Color
color = Color(255, 255, 0)

#βš™οΈ Create Override Graphic Settings
override_settings = OverrideGraphicSettings().ToElements()

#βš™οΈ Apply new Graphic Setting
override_settings.SetSurfaceForegroundPatternColor(color)


#πŸ–ŒοΈ Override Elements
with Transaction(doc, 'Transaction Name') as t:
    t.Start()
    for wall in walls_in_view:
        view.SetElementOverrides(wall.Id, override_settings )
    t.Commit()

Modify to your needs

The snippet above shows you the principle, but it will do just fine to start with. Then go through RevitAPI Documentation and look for methods that you most likely want to use.

I prepared another snippet for you where you can see more examples including getting FillPatterns and FillPatterns.

πŸ‘‡ Here is a snippet with more examples

code
#⬇️ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ Variables
doc = __revit__.ActiveUIDocument.Document
view = doc.ActiveView

walls_in_view = FilteredElementCollector(doc, doc.ActiveView.Id) \
        .OfCategory(BuiltInCategory.OST_Walls) \
        .WhereElementIsNotElementType() \
        .ToElements()


#🎨 COLORS
color_yellow = Color(255,255,0)
color_red    = Color(255,0,0)
color_blue   = Color(0,0,255)

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

#〰️ LINES
line_patterns       = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements()
random_line_pattern = line_patterns[0]
lineweight          = 5


#βš™οΈ CREATE OVERRIDE SETTINGS
override_settings = OverrideGraphicSettings()

#βš™οΈ SURFACE FOREGROUND (PATTERN + COLOR)
override_settings.SetSurfaceForegroundPatternId(solid_pattern.Id)
override_settings.SetSurfaceForegroundPatternColor(color_yellow)

#βš™οΈ CUT FOREGROUND ( PATTERN + COLOR)
override_settings.SetCutForegroundPatternId(solid_pattern.Id)
override_settings.SetCutForegroundPatternColor(color_yellow)

#βš™οΈ PROJECTION LINES (Extra)
override_settings.SetProjectionLineColor(color_red)
override_settings.SetProjectionLinePatternId(random_line_pattern.Id)
override_settings.SetProjectionLineWeight(lineweight)

#βš™οΈ CUT LINES (Extra)
override_settings.SetCutLineColor(color_red)
override_settings.SetCutLinePatternId(random_line_pattern.Id)
override_settings.SetCutLineWeight(lineweight)

#βš™οΈ SET TRANSPARENCY
override_settings.SetSurfaceTransparency(50)


#🎯 MAIN
with Transaction(doc, 'Override Colours') as t:
    t.Start()

    #πŸ‘‰ OVERRIDE ELEMENT GRAPHICS IN ACTIVE VIEW
    for wall in walls_in_view:
        view.SetElementOverrides(wall.Id, override_settings)

    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

Match Graphic Overrides

This is even easier. To match Graphic Overrides from another element we just need to get GraphicOverrideSettings and apply it to another element with or without modifying it.

code
# ⬇️ IMPORTS
from Autodesk.Revit.DB import *
from pyrevit.revit import pick_element

#πŸ“¦ Variables
doc     = __revit__.ActiveUIDocument.Document
view    = doc.ActiveView
element = pick_element()

#βš™οΈ Get Graphics
graphics = view.GetElementOverrides(element.Id)

#πŸ‘‰ Pick elements to match Overrides.
match_element = pick_element()

#🎯 Match Graphics
with Transaction(doc, 'Match') as t:
    t.Start()
    view.SetElementOverrides(elem.Id, graphics)
    t.Commit()

⌨️ Happy Coding!

— Erik Frits