# Link DWGs in Drafting Views?

**URL:** <https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127>\
**Category:** Developers\
**Tags:** revit\
**Created:** [February 8, 2016, 8:17pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127 "2016-02-08T20:17:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [February 8, 2016, 8:17pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/1 "2016-02-08T20:17:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Organon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/organon/32/161404_2.png) [@Organon](https://forum.dynamobim.com/u/Organon)\
**Post date:** [February 8, 2016, 8:26pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/2 "2016-02-08T20:26:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [February 8, 2016, 11:20pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/3 "2016-02-08T23:20:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joseph\_Peel](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Joseph\_Peel](https://forum.dynamobim.com/u/Joseph_Peel)\
**Post date:** [July 20, 2016, 10:19pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/4 "2016-07-20T22:19:59Z")

</div>

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 😛

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](https://forum.dynamobim.com/uploads/short-url/eFnstVcikHq2kYZQ31JG87mGTkD.dyn) (38.6 KB)

---

<div class="post-metadata">

**Author:** ![Konrad\_K\_Sobon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/konrad_k_sobon/32/8838_2.png) [@Konrad\_K\_Sobon](https://forum.dynamobim.com/u/Konrad_K_Sobon)\
**Post date:** [July 20, 2016, 10:48pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/5 "2016-07-20T22:48:55Z")

</div>

Try this:

```auto
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

```

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/2X/d/da99ba38815f0da1acced3470105ff254cf9c72d.png)

---

<div class="post-metadata">

**Author:** ![Joseph\_Peel](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Joseph\_Peel](https://forum.dynamobim.com/u/Joseph_Peel)\
**Post date:** [August 30, 2016, 2:10pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/6 "2016-08-30T14:10:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [September 2, 2017, 2:03pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/7 "2017-09-02T14:03:14Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![Peter\_Kompolschek](https://avatars.discourse-cdn.com/v4/letter/p/dec6dc/32.png) [@Peter\_Kompolschek](https://forum.dynamobim.com/u/Peter_Kompolschek)\
**Post date:** [September 2, 2017, 2:43pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/8 "2017-09-02T14:43:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [September 2, 2017, 3:18pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/9 "2017-09-02T15:18:43Z")

</div>

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](https://stackoverflow.com/questions/501187/eol-while-scanning-single-quoted-string-backslash-in-string)).

---

<div class="post-metadata">

**Author:** ![truevis](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/truevis/32/8634_2.png) [@truevis](https://forum.dynamobim.com/u/truevis)\
**Post date:** [September 2, 2017, 3:43pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/10 "2017-09-02T15:43:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [September 2, 2017, 3:52pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/11 "2017-09-02T15:52:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [September 2, 2017, 4:24pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/12 "2017-09-02T16:24:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yna\_Db](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/yna_db/32/30641_2.png) [@Yna\_Db](https://forum.dynamobim.com/u/Yna_Db)\
**Post date:** [September 2, 2017, 4:31pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/13 "2017-09-02T16:31:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [September 2, 2017, 4:39pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/14 "2017-09-02T16:39:04Z")

</div>

Totally fine. Im just happy you’re all set. 🙂

---

<div class="post-metadata">

**Author:** ![GregX](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gregx/32/56837_2.png) [@GregX](https://forum.dynamobim.com/u/GregX)\
**Post date:** [October 2, 2017, 4:41pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/15 "2017-10-02T16:41:15Z")

</div>

> [@Konrad\_K\_Sobon](#):
>
> linkedElem = clr.ReferenceElementId

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

---

<div class="post-metadata">

**Author:** ![Gui\_Talarico](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gui_talarico/32/10887_2.png) [@Gui\_Talarico](https://forum.dynamobim.com/u/Gui_Talarico)\
**Post date:** [October 2, 2017, 5:15pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/16 "2017-10-02T17:15:04Z")

</div>

@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](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](http://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](http://www.revitapidocs.com/2016/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm)

 ![dynamo](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/e/ce663eba9a15df2462ce9fc23c6807061f787a8c.png)

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

Edit: clarity

---

<div class="post-metadata">

**Author:** ![Gui\_Talarico](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gui_talarico/32/10887_2.png) [@Gui\_Talarico](https://forum.dynamobim.com/u/Gui_Talarico)\
**Post date:** [October 2, 2017, 5:17pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/17 "2017-10-02T17:17:13Z")

</div>

> [http://revitapisearch.com/html/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm24](http://revitapisearch.com/html/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm24)

Working Link

> **[Link Method (String, DWGImportOptions, View, ElementId)](https://www.revitapidocs.com/2016/f3112a35-91c2-7783-f346-8f21d7cb99b5.htm)**
>
> Online Documentation for Autodesk's Revit API: 2015, 2016, 2017, 2017.1, 2018

---

<div class="post-metadata">

**Author:** ![GregX](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gregx/32/56837_2.png) [@GregX](https://forum.dynamobim.com/u/GregX)\
**Post date:** [October 2, 2017, 5:22pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/18 "2017-10-02T17:22:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DJ\_949](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@DJ\_949](https://forum.dynamobim.com/u/DJ_949)\
**Post date:** [December 19, 2017, 6:09pm UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/19 "2017-12-19T18:09:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joseph\_Peel](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Joseph\_Peel](https://forum.dynamobim.com/u/Joseph_Peel)\
**Post date:** [January 8, 2018, 11:21am UTC](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127/20 "2018-01-08T11:21:39Z")

</div>

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?

[Next page](https://forum.dynamobim.com/t/link-dwgs-in-drafting-views/3127.md?page=2)
