Python script graphic override

Hi is there anyone that could help me with this script. I am trying to get this type of effect (picture with red override) But i dont have any coding experience so i dont get what im doing wrong or how i should fix it (sadly chat gpt isnt helping much)


image

Hi Marki,

A bit more information would assist getting to a solution - what is the error that the script is giving?
Add the code to a post using the </> button in the editor menu

ChatGPT is only going to get you so far and in my experience it does not create logical or efficient code, if it works at all. YMMV

A quick review of your code:
When you operate on the Revit document actions have to be wrapped inside a transaction
Add from RevitServices.Transactions import TransactionManager after line 6. Then wrap your main loop with TransactionManager.Instance.EnsureInTransaction(doc) at line 20 and TransactionManager.Instance.TransactionTaskDone() at line 29

The standard python way of getting the index of an item while iterating is using the builtin enumerate() function which creates a tuple of (index, item). So line 21 can be for wall_index, wall in enumerate(walls): removing the need for line 22

The OverrideGraphicsSettings() constructor does not take a color as input only nothing or another instance of the settings. So line 11 is now override = OverrideGraphicSettings() creating the default instance

The two methods you are looking for to override the surface colour are SetSurfaceForegroundPatternColor() and SetSurfaceForegroundPatternId(). You will need to get the solid fill id for the second one - archilab have a nodes to assist with that. Try it without changing the pattern first as solid is most likely the default. You may have to set the pattern to visible with SetSurfaceForegroundPatternVisible(True) method

Good places to get info are Dynamo Python Primer and apidocs.co. There is also revitapidocs.com which is similar however the colour scheme does my head in

1 Like

Thank you very muc (i am not sure if i did the right way, i probably did the fill pattern part/override wrong)

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import FilteredElementCollector, View, OverrideGraphicSettings, Color
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

def kleurwijzigen(color, wall, fill):
    Override = OverrideGraphicSettings()
    override.SetSurfaceForegroundPatternColor(color)
    override.SetSurfaceForegroundPatternId(fill.Id)
    view.SetElementOverrides(wall.Id, override) 

# Input: list walls and view
walls = UnwrapElement(IN[0])
view = UnwrapElement(IN[1])
fill = UnwrapElement(IN[2])

TransactionManager.Instance.EnsureInTransaction(doc)
# Run throught every wall and draw it with a coloroverride
for wall_index, wall in enumerate(walls)
    kleurwijzigen(Color(0, 102, 204), wall, fill)
    if wall_index >= 1:
        kleurwijzigen(Color(51, 153, 255), walls[wall_index-1], fill)
    if wall_index >= 2:
        kleurwijzigen(Color(102, 178, 255), walls[wall_index-2], fill)
    view.Redraw()
 
TransactionManager.Instance.TransactionTaskDone() 

# Resultaat: Het aantal verwerkte wanden
OUT = len(walls)

Also thank you for the links! i tried understanding the revit api docs before but my head just wont comprehend hha because i dont know any basics so the dynamo python primer does look very promising thanks once again!!

Connect the Fill Pattern directly to the IN[2], currently you are inputting a True/False

Also very helpful on apidoc.co you can search for script examples - click the < > link - look for .py extension code in the result and click the file name to go to the code (.cs is C# code)

Screen Shot 2023-11-04 at 11.30.47 am
Screen Shot 2023-11-04 at 11.40.10 am

2 Likes