pythonrevitrevitpythonshell

OverrideGraphicSettings in active view with RevitPythonShell


I am trying to override graphics of a wall in a 3D view in Revit using RevitPythonshell. I managed to make this work in Dynamo with a Python Node.

So far I have the following code;

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager 

from System.Collections.Generic import List


doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.EnsureInTransaction(doc)

walls = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
elements = walls.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType()


color = Autodesk.Revit.DB.Color(255,50,50)
ogs = OverrideGraphicSettings().SetProjectionFillColor(color)


for i in elements:
    doc.ActiveView.SetElementOverrides((i.Id), ogs)


TransactionManager.Instance.TransactionTaskDone()

When I run this in RevitPythonShell it does nothing. I get no error or anything. When I print doc.ActiveView.SetElementOverrides((i.Id), ogs) it returns None.

What am I missing here? I am in a 3D View in Revit which is the active view. I am starting and ending a transaction.

Here is somewhat the same code in a Python node in Dynamo node.


Solution

  • With a few tweaks your code works:

    import clr
    
    clr.AddReference('RevitAPI') 
    clr.AddReference('RevitAPIUI') 
    from Autodesk.Revit.DB import * 
    from Autodesk.Revit.UI import *
    
    app = __revit__.Application
    doc = __revit__.ActiveUIDocument.Document
    
    elements = list(FilteredElementCollector(doc, doc.ActiveView.Id))
    
    color = Color(255,50,50)
    ogs = OverrideGraphicSettings().SetProjectionFillColor(color)
    
    t = Transaction(doc, 'Color Walls')
    t.Start()
    try:
        for i in elements:
            if i.Category.Name == 'Walls':
                doc.ActiveView.SetElementOverrides((i.Id), ogs)
                print 'element overridden'
    except Exception as e:
        print '- Failed to override -'
        print '- ' + str(e) + ' -'
    t.Commit()