Link DWGs in Drafting Views?

revit
#1

Anyone know a way to link DWGs programmatically? In Revit’s SDK there is “ImportDWGData.cs”, but it does not do links, just imports.

0 Likes

#2

http://revitapisearch.com/html/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm

0 Likes

#3

That is a good clue, thanks. Any Dynamo versions around?

0 Likes

#4

Been attempting to get this to work by adapting a similar example for images.
Despite finding all the right stuff in the API my coding is letting me down :stuck_out_tongue:

Having problems with doc.Link(path1, DWGio, view1, newElement) because ‘name newElement is not defined’
But what should this be? and isnt this an output if link is succesful?

Also struggling to set import options like ImportColorMode. This can be Preserved, Inverted or BlackAndWhite but trying to set this with a string doesnt work.

(Dynamo version is 0.9)

DWG Link.dyn (38.6 KB)

0 Likes

#5

Try this:

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

filePath = IN[0]
view = UnwrapElement(IN[1])

options = DWGImportOptions()
options.AutoCorrectAlmostVHLines = True
options.ColorMode = ImportColorMode.BlackAndWhite
options.OrientToView = True
options.ThisViewOnly = True
options.VisibleLayersOnly = True

linkedElem = clr.Reference[ElementId]()

TransactionManager.Instance.EnsureInTransaction(doc)
doc.Link(filePath, options, view, linkedElem)
TransactionManager.Instance.TransactionTaskDone()

#Assign your output to the OUT variable.
OUT = 0

5 Likes

Batch link multiple DWG files
Export to DXF
#6

Thanks, had to wait til the next project to fix it but this works great : )
Im trying to get people to stop using autocad for 2D details so being able to import all their standard dwgs in one click should help.
Modified it slightly to handle a list of views/paths and to output a list of the views created;

"# Link DWGs
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]
views = UnwrapElement(IN[1])
viewsplaced=list()

options = DWGImportOptions()
options.AutoCorrectAlmostVHLines = True
options.ColorMode = ImportColorMode.BlackAndWhite
options.OrientToView = True
options.ThisViewOnly = True
options.VisibleLayersOnly = True
options.CustomScale = 100
#options.Placement = Origin

LinkedElem = clr.Reference[ElementId]()

for view in range(len(views)):
	TransactionManager.Instance.EnsureInTransaction(doc)
	doc.Link(filePaths[view], options, views[view], linkedElem)
	TransactionManager.Instance.TransactionTaskDone()
	viewsplaced.append(views[view])


#Assign your output to the OUT variable.
OUT = viewsplaced;
";

Is there a good way to make code 'list or single item proof"? I prefer to use code in a string where it can be edited directly rather than putting it in a custom node every time.

0 Likes

Creat link DWG file and IFC file
#7

@Joseph_Peel
Sorry to revive this old topic but I could not find any solution. I get an error when I use this code: “EOL while scanning single-quoted string”, do you know why?

0 Likes

#8

have no dynamo running. but this error message is for the last line in the code.
remove that and the error will be gone. doubt yet the code will run. in[1] hands over a single view to views and
doc.Link(filePaths[view], options, views[view], linkedElem)

expects a list of views: views[view] for iteration.
will not work

0 Likes

#9

Indeed it does not work. I removed the last line and the warning is still the same. Couldn’t it be related to the backslashes in file paths strings? (https://stackoverflow.com/questions/501187/eol-while-scanning-single-quoted-string-backslash-in-string).

0 Likes

#10

Don’t know for sure, but check for funny characters (like commas) in your filenames.

0 Likes

#11

No filenames issues here: it works if I use Konrad’s code (with one file)

0 Likes

#12

Could you make a custom node of it and use lacing to get there?

1 Like

#13

Great, this works, thanks a lot! :slight_smile: (sorry but I will have to leave the solution of this thread where it was initially)

0 Likes

#14

Totally fine. Im just happy you’re all set. :slight_smile:

0 Likes

#15

Hey @Konrad_K_Sobon I am trying to understand this line of code:
linkedElem = clr.Reference[ElementId]()
I tried googling and it seems that in some cases the variables must be initialized to set some sort of “value field” (in this case an ElementId). But why and when does this happen?
Furthermore what should be done with “linkedElem” to show as a “revit element” output in dynamo?Does it have to do with wrapping?
Thanks

0 Likes

#16

@GregX
This is often confusing because it has to do with a language feature that exists in C#/.Net but not in Python.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref
Ref values (or out, which is similar) is explained above. It basically means that instead of the function getting an argument and returning a value,
you first create a reference, then pass it to the function. A value for the reference object is then set_within_ the function, instead of returned. The function may return other values, but the reference (ref parameter) will always be set within it and not returned.
Since this is.net specific, you must use the clr module to create this type of object (typed reference object)

The returned value and ref value are explained here:
http://www.revitapidocs.com/2016/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm

Details on the Ironpython implementation of ref and out (similar to ref) here:
http://ironpython.net/documentation/dotnet/dotnet.html#ref-and-out-parameters

Edit: clarity

3 Likes

#17

http://revitapisearch.com/html/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm24

Working Link
http://www.revitapidocs.com/2016/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm

0 Likes

#18

Thanks for the explanation! I am trying to get a hold of python but everytime there is (luckily?) something new to learn and understand!

0 Likes

#19

I tried this in the simplified version provided above and in the latest version by Konrad and it doesn’t work. The later version will create the drafting views but that’s it. What are you all doing to make this script work?

0 Likes

#20

Hmm. I dont know… If your lists of views and paths are the same length it should work fine.
Check what errors the python nodes give.
Maybe something changed in the latest api?

0 Likes