Change Imported CAD layer color

Hi all,
We link CAD files from our standard detail packages into Revit as drafting views. I need to change the color of a specific layer (hatch) to gray. I was able to find all the layers from the CAD files and filtered down to the hatch1 layer I want to change. I have no idea how where to go from here.

You could use this simple Python script. It changes the Layer found in the Object Styles dialog, not any view override. Note that the view needs to be refreshed (simply hide/unhide the DWG) to see the change. Forcing the refresh via Python is currently a limitation. I’ll get something more robust created and added to my package (bimorphNodes - hope its useful!):

#Copyright 2016. All rights reserved. Bimorph Consultancy LTD, 5 St Johns Lane, London EC1M 4BH www.bimorph.co.uk
#Written by Thomas Mahon @Thomas__Mahon info@bimorph.co.uk package: bimorphNodes
#GitHub: https://github.com/ThomasMahon/bimorphNodes/

import clr

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

dwgImport = UnwrapElement(IN[0])
name = IN[1]
col = IN[2]
# "Start" the transaction
TransactionManager.Instance.EnsureInTransaction(doc)

catSub = dwgImport.Category.SubCategories
catSub.Item[name].LineColor = Color(col[0], col[1], col[2])

TransactionManager.Instance.TransactionTaskDone()

Also the CADLayerNames node returns strings (text) not the Revit element which is partly why you’ve been unable to find a way to change them. I’ll look to add a new output to these nodes that also includes the Revit object for the next release

3 Likes

Tomas, thank you so much for your help. I added the python script and I got this warning. I made sure that it was exactly as you posted without any tabs, but I was still getting this warning.

As for the CADLayerNames node, I didn’t realize it returns a string. That completely make sense why the way I was using it didn’t work.

You need to add the additional inputs to the python node by clicking the + button. That script looks for three inputs using the IN variable. Each input is indexed and that’s what’s throwing the exception. If you match that example graph I posted then test it should work as expected

Crazy coincidence I was just on Dynamo Primer doing the tutorial on the exact thing you just explained. So now I don’t have errors but the layer still did not change color. Even after I refreshing like you explained above (hide/unhide the DWG).

its the huge black patch in the background that should change.

Its most likely an issue with your DWG rather than the script. Firstly, check the obvious things: ensure you are selecting the correct layer. Once confirmed then its a DWG issue. You can try this and it should work: open in Microstation, select all objects in the DWG, then set the color of all object to ByCell. Link back into Revit and now it will work (tested successfully). I dont know what the equivalent setting is in AutoCAD before you ask:

Thomas,
That did it! I really thank you for all your help. In AutoCAD its called ByLayer.

Is there a way to have the CADLayerNames node except more than one instance? For example, I would like to find all the CAD imported files and have it find the layers causing the issue and change them. Similar to what I tried to do in my first example.

Yes, just use List.Map:

Also, I’ve found a hack to get Python to force the line colour update without you having to hide/unhide the reference:

It’s pretty strightforward really; I’ve simply automated the hide/unhide step.

#Copyright 2016. All rights reserved. Bimorph Consultancy LTD, 5 St Johns Lane, London EC1M 4BH www.bimorph.co.uk
#Written by Thomas Mahon @Thomas__Mahon info@bimorph.co.uk package: bimorphNodes
#GitHub: https://github.com/ThomasMahon/bimorphNodes/

    import clr

    # Import RevitAPI
    clr.AddReference("RevitAPI")
    import Autodesk
    from Autodesk.Revit.DB import *

    clr.AddReference("RevitAPIUI")
    import Autodesk
    from Autodesk.Revit.UI import *

    # Import DocumentManager and TransactionManager
    clr.AddReference("RevitServices")
    import RevitServices
    from RevitServices.Persistence import DocumentManager
    from RevitServices.Transactions import TransactionManager
    from System.Collections.Generic import *

    doc = DocumentManager.Instance.CurrentDBDocument

    dwgImport = UnwrapElement(IN[0])
    name = IN[1]
    col = IN[2]
    # "Start" the transaction
    TransactionManager.Instance.EnsureInTransaction(doc)

    catSub = dwgImport.Category.SubCategories
    catSub.Item[name].LineColor = Color(col[0], col[1], col[2])

    vId = List[ElementId]( )
    vId.Add(dwgImport.Id)

    v = Autodesk.Revit.UI.UIDocument(doc).ActiveView
    v.HideElements(vId)
    v.UnhideElements(vId)

    TransactionManager.Instance.TransactionTaskDone()
5 Likes

Thomas,
That was an awesome hack. It works beautifully without having to do it manually.

Is it possible to have the Python script use the actual layer name instead of the numbered list? In your example, your showing tow files, one with 3 layers and the other with 4. Passing #4 into the script will not work in this case since the first one does not have it.

Like this, but keep in mind that Python script is very basic and only accepts scalars (i.e. single items). You’ll need to create multiple instances of the Python node for now. I am going to add this node to my package soon with the ability to take lists for the next release, so keep your package regularly updated:

Thomas,
Ones again, thank you for all the help. This was an awesome experience. I’m looking forward to the update to your package.

@bizz3d SetCADObjectStyles is avaialble in BimorphNodes v2.0!

2 Likes

Hi all,

Just want to say thank you for such a clear solution to the above problem – it has been extremely helpful in getting me this far. Apologies for resurrecting this feed but I feel that my issue does lie within the realm of this original ask.

I am working with multiple sub-contractor’s CAD backgrounds per level and I am trying to do the following:

  1. Select all Linked CAD throughout the entire Revit Project
  2. Filter the CAD by file name, thus grouping the Linked CAD by trade
  3. Change all the CAD layers within those Linked CAD files to the same Color, i.e. Electrical = Blue, HAC = Red and so on…

I have been able to get through steps 1 and 2 but cannot figure out how to get the elements/CAD layers within the Linked CAD to change the object style once I filter by trade. I would really appreciate any thoughts on the matter.

@kirsten.crock I think you could replace your current graph using the GetItemAtIndex node and List.Map with the bimorph node CAD.ReportImportInstances, then use your String.Contains and FilterByBoolMask similarly to shown below (I quickly just imported some dwgs that I could run this on).

3 Likes

Thanks so much @awilliams! That did the trick!! And with such a fast reply - I truly appreciate your help!

Greetings,

I’ve got the setup as shown (as far as I can tell), but the node doesn’t seem to actually change anything in my Object Styles.

I have a couple of instances of the same linked DWG in different views and they appear to be filtered correctly, but nothing seems to change.

Any feedback would be appreciated!

Thanks again,
Thom K.

Hi @Thom_Krejci you need to set your cad layer colours to ‘By Level’ (or similar, i don’t know the exact phraseology in AutoCad as I barely use it) in Autocad. If you override the colour of your DWG elements (in AutoCad) it cant be changed in Revit.

Thank you for the quick reply: I haven’t tried to change the color, it was the weight that I was trying to change.

In the Object Styles dialog, I can change the weight and the lines in that link change throughout the model.

Image%203

I guess I’m looking for Dynamo to be change that “4” highlighted above to let’s say “8”. Am I looking at the wrong dyn for this?

Thanks again!
Thom K.

@Thomas_Mahon : I am trying to update nearly 1020 Cad files in revit with the help of BIMorph tool. The only issue I am facing is that I can not edit only the color or line weight separately for a specific layer. As you know some of the elements only line width needed to be changed and some only color. Would you shed some light on it?
Thanks for such an awesome job with Bimorph by the way.