Revit Parameters to AutoCAD Attributes using Dynamo

Ever since I started playing around with connecting Dynamo to AutoCAD my colleagues (well, one of them) have been complaining that I should be using this fancy Dynamo connection for something practical. Apparently creating all of Sweden in a DWG, creating labyrinths from the Internet or mathematical fractals is not practical enough.

But then a customer asked about an age old Swedish CAD-problem and I finally found Inspiration! When they are exporting Sheets from Revit (which I consider a silly idea) the information in Revit becomes simple Text in AutoCAD. Over the years they have become spoiled with AutoCAD files (referred to as “drawing definition files”) which have a Titleblock, a View Port and a “smart” AutoCAD Block with Attributes. Then you can double-click the block and edit the values, kind of similar to how it looks like in Revit. These can then be exported, uploaded into storage systems, databases, fullfill CAD-requirements and all kinds of mischief.

So they would like to export from Revit to AutoCAD and get those fancy attributes and Revit does not do it. There’s apparently an expensive add-on which can do it but I won’t mention its name since it used to be Free and now it’s not, which I consider a bit unfair. Besides, I wasn’t too impressed by it when I saw it last.

First step was remembering how to create an attribute block in AutoCAD and I was quite impressed with myself that I could still remember those ancient commands! Eventually I settled on creating a block with three attributes that I wanted filled from Revit – Sheet Number, Sheet Status, and Sheet Name. I made them in beautiful AutoCAD green to see them properly. Apparently my eyesight is worse than my memory.

Then I created a clean Title Block in Revit and exported a bunch of Sheets to DWG. Using the “short” naming system I could be sure that the file names would match the Sheet Number of my Sheets.

And then it was time for Dynamo! With quite a lot of Python of course. First steps were quite standard Dynamo stuff, getting the specific Sheets I exported to DWG, getting the parameter information and then specifying the paths to the exported DWGs and to the attribute block.

Within the Python box the first step is getting connected to a running AutoCAD in the background using the magic of the Interop. This doesn’t work in Revit 2023 with that new, fat and modern looking Dynamo and I still don’t really know why. It has something to do with IronPython vs CPython.

Then adding the inputs I was providing to the scripts and starting a simple loop. For every DWG path provided – open the DWG, activate it and switch to PaperSpace (hopefully the correct layout)…

Then comes the DWG magic – placing a block in AutoCAD! The API command is pretty basic – InsertBlock in a point, from a file, scale in three dimensions and rotate. The last ones I obviously didn’t need to do.

The final step was feeding the attributes of my newly created block – this assumes that the order of those attributes doesn’t change and I have really no idea if they ever will at some point in time. In Revit those kinds of things always jumps around to confuse you.

And that was magically all that was required – AutoCAD happily opened the DWGs, placed the block and filled the information and then carried on to the next DWG – just like that! I haven’t added any saving and closing of the DWGs but that’s of course possible as well.

The complete program is below – for it’s googleabilities and perhaps for someone else’s enjoyment. The code can obviously be improved, it lacks every trace of error handling, has no Quality Assurance whatsoever and probably doesn’t follow any good coding practice or Python standard in any civilized country. But that’s the way I code!

Thank you for reading and have a great day!

import clr

import System
from System import *

app = System.Runtime.InteropServices.Marshal.GetActiveObject("Autocad.Application")

attrib_path = IN[0]
dwg_paths = IN[1]
sheet_numbers = IN[2]
sheet_names = IN[3]
sheet_status = IN[4]

i=0

for dwg_path in dwg_paths:
	dwg = app.Documents.Open(dwg_path)
	dwg.Activate()
	
	app.ActiveDocument.ActiveSpace = 0 #model vs paper

	apunkt = Array[float]([-20,20,0])
	scale = 1
	rotation = 0
	
	blockref = app.ActiveDocument.PaperSpace.InsertBlock(apunkt,attrib_path,scale,scale,scale,rotation)	
	
	attribs = blockref.GetAttributes()
	attribs[0].TextString = sheet_numbers[i]
	attribs[1].TextString = sheet_names[i]
	attribs[2].TextString = sheet_status[i]
	
	i=i+1
	
OUT = 0

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top