Overriding Linked CAD Layer Lineweight in Drafting View

Hi everyone. New member here…and several months new to Python and pyRevit.

Does the Revit API allow overriding Linked CAD layer lineweights for either an individual drafting view or more globally, at the object styles level? My review of the API thus far has not uncovered anything.

I found a somewhat related post ([View Overrides base on linked Cad layers - #7 by clntnco] that deals with changing the line color, but again I am not seeing a way within the API to alter this code to override the lineweights.

Thanks in advance for any insights!

1 Like

Hi and Welcome to the party.

Since 2024.1+ and 2025 I think this is possible, not sure to what extent
https://thebuildingcoder.typepad.com/blog/2024/04/whats-new-in-the-revit-2025-api.html#4.1.9

You will have to go through the CHM file in the latest Revit API SCK to dig through or just dig through the API programmaticaly to get your answer using the dir method for example

or get some github search going

Many thanks Jean-Marc!

That is promising as I’m currently using 2023. I’ll continue the search in the latest API as you recommended…very helpful!

If you are new to Python: it’s useful to get the hang of the hierachie of revit api (install RevitLookup if you haven’t already)

You slowly will go down the rabbit hole, but the quest is usually which path to take and which doors you have to open in order to get there.

In the link you shared I gave a little explaination how to set layer colors. I wrote for our company a script that converts autocad layer colors to revit since revit converts rgb to the nearest index color.

So to give you some hints: you can “ask” what else is possible in python.

selections = revit.get_selection()
for selection in selections:                
    layers = sorted(selection.Category.SubCategories, key=lambda x: x.Name)                
    for layer in layers:
        lname = layer.Name

Basicly this loops through each layer. But be aware: you are looping through Revit’s Subcategories.

And it lname is set to layer.Name. Everything after the dot is something you can call. But “hey I’m new to python how do I know what to call?”

Use dir().

selections = revit.get_selection()
for selection in selections:                
    layers = sorted(selection.Category.SubCategories, key=lambda x: x.Name)                
    for layer in layers:
        lname = layer.Name
        print dir(layer) #this will show everything you can do with layer.

This will output something like this:
['AllowsBoundParameters', 'BuiltInCategory', 'CanAddSubcategory', 'CategoryType', 'Dispose', 'GetBuiltInCategory', 'GetBuiltInCategoryTypeId', 'GetCategory', 'GetGraphicsStyle', 'GetHashCode', 'GetLinePatternId', 'GetLineWeight', 'HasMaterialQuantities', 'Id', 'IsBuiltInCategory', 'IsBuiltInCategoryValid', 'IsCuttable', 'IsReadOnly', 'IsTagCategory', 'IsValid', 'IsVisibleInUI', 'LineColor', 'Material', 'Name', 'Parent', 'ReleaseManagedResources', 'ReleaseUnmanagedResources', 'SetLinePatternId', 'SetLineWeight', 'SubCategories', '__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

So to me SetLineWeight looks promising.

Hi Joris,

Very appreciative of the detailed explanation and code sample! I do have Revit Lookup installed and have been using it to guide my efforts when reviewing the API for options.

I didn’t know about the dir () method (outstanding!). Will definitely add that to my coding process. I will also post any progress I might have with the SetLineWeight property. Thanks again!

So my attempt at using the SetLineWeight method is resulting in a “AttributeError: attribute ‘SetLineWeight’ of ‘Category’ object is read-only”

This error is a bit confusing as I believe the ‘layer’ object is properly set to the Sub-Category (i.e. I am able to print the layer names).

#MAIN

data = {
    "FRAMING": 5,
    "WALL": 5,
    "GRADE": 7
}

# Get all linked CAD files
import_instances = DB.FilteredElementCollector(doc) \
    .OfClass(ImportInstance) \
    .ToElements()
if not import_instances:
    print("No ImportInstances found.")
    exit()

# Loop through each ImportInstance
for instance in import_instances:
    layers = sorted(instance.Category.SubCategories, key=lambda x: x.Name)
    for layer in layers:
        lname = layer.Name
        if lname not in data:
            continue
        # Get the layer lineweight
        cad_lineweight = data[lname]

        t = Transaction(doc, 'Override Layer LineWeight')
        t.Start()

        layer.SetLineWeight = cad_lineweight

        t.Commit

Additionally, when I use Revit Lookup on the imported CAD file and drill down to the Sub-Category level, I see no Lineweight property associated with the Layers. So I am wondering how the SetLineWeight method would work in this situation?

Wondering if I’m assessing things correctly…

Ah welcome in the rabbit hole!

Wrong turn / path as it turns out.
layer.SetLineWeight is indeed read only … don’t ask why, don’t know the answer :slight_smile:

BUT we’ll take a few steps back and take another approach (spoiler alert: this one does work; tested myself)

object_styles = revit.doc.Settings.Categories
for cat in object_styles:
    if cat.SubCategories:
        for subcat in cat.SubCategories:
            print subcat.Name
            with revit.Transaction("test"):
                if subcat.Name == "autocad_layer_testname":
                    subcat.SetLineWeight(4, revit.DB.GraphicsStyleType.Projection)

Don’t mind the messy code but basicly I did:

  1. list all the categories in Revit: the linked files are listed there too.
  2. we want the layers of the cad file … in Revit we call them subcategories … so subcategories we will loop.
    SetLineWeight requires an int not a string

Btw: this will set layer lineweights for ALL views.

Prefer one transaction to account for all modifications (cleaner revit actions history and much faster)

from pyrevit import revit
object_styles = revit.doc.Settings.Categories
with revit.Transaction("test"):
for cat in object_styles:
    if cat.SubCategories:
        for subcat in cat.SubCategories:
            print subcat.Name
            if subcat.Name == "autocad_layer_testname":
                subcat.SetLineWeight(4, revit.DB.GraphicsStyleType.Projection)
1 Like

yup yup, obviously, but thanks for that catch!

1 Like

Thank you gentlemen. Can’t wait to put this into practice.

Great to make the connection between the API and Object Styles.

Really like this approach as it will affect the CAD files in a global way.

1 Like