CAD Color Extraction For Layer / Sizing

Hello Everyone.

I would like to extract the CAD Color from the layer that was created from BiMorph. I am not much into python coding as much as I know I should be but was wondering if there is any way to do it without coding?

Let me know what you think! Thank you guys.
Kevin

Hi Kevin,

I have tried a few things for you but I couldn’t find a solution with custom nodes so I had to do it in Python.

What my graph does is, it reads the name of the Category of the linked CAD file. Then my Python node searches for the corresponding ImportedCategory and its Subcategories (Which have the same name as the CAD Layers). Then it creates a Dictionary with the name of the layer en the Dynamo Color. You can then use the BiMorph node to see which Layers are actually used and query the color of that layer from the Dictionary.

I hope this will help and if you have any question, let me know!

ColorsFromLinkedCad.dyn (17.2 KB)

4 Likes

Hey Joelmick,

This will totally work for what I need it for! So, what I plan on doing is taking it 1 step further. So I am going to have my AutoCAD file colors, determine the lineweight for the detail lines. For example, Red may be lineweight 4 vs Yellow at Lineweight 2. I just could not figure out a way to read the AutoCAD colors for the extracted data. Thank you so much and hope you have a wonderful holiday season!

Sincerely,
Kevin

Hi Kevin,

Are you going to detiremine the Line weights based on the colors? Because you can also query the Line weight and the Line Pattern from the CAD File.
ColorsAndWeightFromLinkedCad.dyn (20.1 KB)

1 Like

Hey Joelmick,

This is pure gold! I noticed from the icons that I believe this script works for 2.6 Dynamo. I am currently running 2.1 and got the following error when it came to the dictionary. It seems the python code is coming back as Empty Dictionary and not entirely sure why. I tried it both on a CAD Link and CAD Import method. Any idea?

Thank you,
Kevin

Hmm weird, works fine for me with Revit 2019 and Dynamo. Have you run the latest script?
ColorsAndWeightFromLinkedCad.dyn (20.1 KB)

This is quite phenomenal. I’ve been searching for a way to pull color information from AutoCAD layers for months and I just happened to come across this post today.

I’m able to get it to run, but I get an empty dictionary output from the Python script just like the OP was. I don’t really know much about python scripting, but I’ll try running it by my BIM Manager who is a real whiz at this sort of thing and maybe he can figure out what we’re doing wrong.

Thanks!

1 Like

Joel,

Running the python script from your latest .dyn file, I’m finding the line:

ids = ExternalFileUtils.GetAllExternalFileReferences(doc);

is selecting “KeynoteTable” and “AssemblyCodeTable” id’s, not the Import Instances that are present in several views, nor the one I selected in the Active View. Since it’s not selecting any Imports, it stops at the first IF. This causes the script to return an “Empty Dictionary” which is causing others’ issues.

-EDIT-
It appears this only works with Linked DWG’s, not Imported DWG’s, this is a distinct difference. Additionally, the line above only returns linked DWG’s in 2019 and above, not previous version.

Finally, there is another issue specific to Revit 2019:

'string' does not contain a definition for 'Key'

This is because pre-2.3 versions of Dynamo cannot handle Dictionaries properly. I’m still digging how to remedy this.

I have made a few simple changes to enable this to run in Revit 2019 and it’s working for us right now.

We have no need for Lineweight at this moment, so I did not spend the time to wrap it into the dictionary.

ColorsAndWeightFromLinkedCad.dyn (23.3 KB)

Didn’t like leaving it half-baked so I separated Color and Lineweight into useable outputs. This works in 2018 and 2019.


RevisedColorsAndWeightFromLinkedCad.dyn (32.5 KB)

and I forgot to share the Python (starting at line 52):

#Define Input (IN)
name = IN[0].Name
outName = []
outColor = []
outWeight = []

# Start Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

ids = ExternalFileUtils.GetAllExternalFileReferences(doc);
for id in ids:
	elem = doc.GetElement(id)
	c = elem.Category
	if c != None:
		if c.Name == name:
			for sc in c.SubCategories:
				outName.append(sc.Name)
				outColor.append(toDynamoColor(sc.LineColor))
				outWeight.append(sc.GetLineWeight(GraphicsStyleType.Projection))
				
#End Transaction
TransactionManager.Instance.TransactionTaskDone()

#Define Output (OUT)
OUT = [outName,outColor,outWeight]
3 Likes

I didn’t try it for Imported DWG’s because we get a slap on the wrist if we do that, gives you alot of junk in the project ;).

I only put the output in a dictionary so it was more organized, but for pre 2.3 versions you gave a good example for with the lists. Nice to see that you left your versions aswell! Does your work for imported CAD files?

1 Like

I agree that import is undesired, though it was necessary for a very specific workflow that we were trying to abandon; the reason we were using this graph.

My example only works with linked dwg’s as well.

It doesn’t seem to output all the colours?
I have 13 colours in a linked CAD file but it’s only telling me I have 2. Any idea why?

Ah, it’s one colour per layer? Someone’s gone and overridden the colours in CAD. Several colours per layer.

Any idea how to extract these?