Using Dynamo to Insert a DWG Into a Revit Family

Another question that I’d rather not have the answer left to email

I have a project on which HVAC is using AutoCAD MEP for their ductwork (they don’t know Revit…yet). They’re designing in 3D based off my Revit-to-dwg exports. But the only way I can import their 3D ductwork from AutoCAD MEP into Revit, AND have it respect my cut planes in my sheet views, is to bring it into a Revit Generic Family first.


Any ideas on how to improve this workflow (aside from making HVAC work in Revit)? Maybe some sort of Dynamo script to help automate this process?

This is a workflow that I’ve only had to follow a few times when working with a bridge team that was still working in AutoCAD. I guess I’ve had the luxury entire project teams working in either Revit or ArchiCAD and working with this files is a straight forward process.

There is a Dynamo solution that works well, but my first iteration of this doesn’t automatically batch process all the files, but rather it’s run on an as-needed basis. This isn’t such a bad thing though as every DWG file usually isn’t issued all at the same time.

My Dynamo solution requires that you have

  • Dynamo 2.x
  • GeniusLoci package
  • Excel list of  families and their corresponding DWG files

First we start off with reading our Excel file, it provides the validation between our RFA names and the required DWG to be inserted to that RFA. I have a dummy list of files, but it shows how the concept works.

Our Excel file has headers included, so we split our our headers with a List.Deconstruct node and we transpose that list so we split out to a list of RFA files and a list of DWG files. Then using a List.GetItemAtIndex node we take the name of the RFA file from the list to validate that against our open document.

Next, we take the name of our open document. Sometimes this node won’t refresh to pick up the current document name, if this is the case, just close and re-open the graph.

The nodes are reporting back not just the file name, but the location that it is saved. To be able to break the string down into it’s separate pieces, we use a String.Split node and split by the string “\\”, once we’ve split the string the split elements will be provided in a list. We simply want to take the last list item as that will be our file name.

Now that we have our file name from our current document as well as the file names from our Excel list, we want to use a List.IndexOf to search our Excel list for the name of our currently open document (final node in the above screenshot)

From there, we can take the list of DWG files from our Excel sheet and using the value produced from our List.IndexOf node we can pull the corresponding DWG name from our Excel list.

We then need to put together a path string. You could hard code this into the code block or you can use the Directory Path node to feed into the code block.

Next is the bulk of the work, with the help of the GeniusLoci package we need to enumerate the DWG import settings available to us. We then take those lists of settings and using a List.GetItemAtIndex node we select our import settings to then feed into our Python script.


The Python script is a modification of the existing Import DWG node from the GeniusLoci package, we just need to change a single word within the script as although the existing node is called Import DWG it actually links the DWG files.

So on line 60 of the script we need to change doc.Link

doc.Link(filePaths[view], options, views[view], linkedElem)

to doc.Import

doc.Import(filePaths[view], options, views[view], linkedElem)

Rather than modify the GeniusLoci node itself, I have created a new Python script node and put the modified code within that node.

Complete Import DWG Python Code
# By Konrad Sobon
# By Joseph_Peel
#By Alban de Chasteigner
import clr
from Autodesk.DesignScript.Geometry import *

# Import DocumentManager and TransactionManager
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

# Import RevitAPI
import Autodesk
from Autodesk.Revit.DB import *

#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

filePaths = IN[0]

if not hasattr(filePaths, '__iter__'):
	filePaths = [filePaths]

views = UnwrapElement(IN[1])

if not hasattr(views, '__iter__'):
	views = [views]

customscale = IN[2]
colormode = IN[3]
placement = IN[4]
unit = IN[5]
viewsplaced = []
outName = []
CADlinktype = []
importinst = []

options = DWGImportOptions()
options.AutoCorrectAlmostVHLines = True
options.OrientToView = True
options.ThisViewOnly = True
options.VisibleLayersOnly = True
options.CustomScale = customscale
options.ColorMode = colormode
options.Placement= placement
options.Unit = unit

linkedElem = clr.Reference[ElementId]()

for view in range(len(views)):
	#doc.Link(filePaths[view], options, views[view], linkedElem)
	doc.Import(filePaths[view], options, views[view], linkedElem)
ImportInstances = FilteredElementCollector(doc).OfClass(ImportInstance).ToElements()

for importinstance in ImportInstances :
	if importinstance.IsLinked:
		for importins in importinst :
			CADLink = doc.GetElement(importins.GetTypeId())
		name = Element.Name.GetValue(CADLink)

#Assign your output to the OUT variable.
OUT = viewsplaced,outName,CADlinktype,importinst


And that is it. Once all the required data is fed into the Python script, the DWG is placed on the view and our job is done.


If you have a need to import DWGs into family files and want to get started a bit quicker, you can download my Dynamo graph below

2 thoughts on “Using Dynamo to Insert a DWG Into a Revit Family

  1. Great website and great workflow !
    Thank you for the remark on the wrong name of the Import DWG node. It’s now corrected.

  2. avatar Ryan Lenihan says:

    Hey thanks for the great Dynamo package Alban! It has come in incredibly handy. When I was working through the solution I was actually going to contact you to see what I was doing wrong and then I realised the node was linking rather than importing.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.