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.

..snip..

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
[python] # By Konrad Sobon
# By Joseph_Peel
#By Alban de Chasteigner
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

# Import DocumentManager and TransactionManager
clr.AddReference(‘RevitServices’)
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
clr.AddReference(‘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)):
TransactionManager.Instance.EnsureInTransaction(doc)
#doc.Link(filePaths[view], options, views[view], linkedElem)
doc.Import(filePaths[view], options, views[view], linkedElem)
TransactionManager.Instance.TransactionTaskDone()
viewsplaced.append(views[view])

ImportInstances = FilteredElementCollector(doc).OfClass(ImportInstance).ToElements()

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

#Assign your output to the OUT variable.
OUT = viewsplaced,outName,CADlinktype,importinst
[/python]

 

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

6 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.
    Cheers,
    Alban

  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.

  3. avatar Gerardo Ruiz-King says:

    This is a really great workflow. I’m noticing though that this only imports the first file listed that matches the family name and then stops. Is this how it’s supposed to work or should it be repeating for all dwgs that match the family type?

  4. avatar Ryan Lenihan says:

    you may need to change your lacing for the node that picks up the family names. if you are using shortest lacing it will only pick up the first match, if you use longest it should pick up all. just make sure you are on top of your list management when you change your lacing.

  5. avatar Kahn Siridej says:

    Hi, Ryan. Thanks for your great explanation. However, your linked is dead. I can download but cannot extract the file. Could you please upload again or send it to me. I need this idea to import more than 80 cad files into a project.

  6. avatar Ryan Lenihan says:

    Hi, I apologise for not picking up on this sooner. I have just fixed all downloads that are available on the site, can you please check that they work now. I have tested each individually on my machine, it appears the problem was server-side.

Leave a Reply

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