# Insert and XREF

**URL:** <https://forum.dynamobim.com/t/insert-and-xref/57965>\
**Category:** Civil 3D\
**Created:** [December 3, 2020, 10:28pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965 "2020-12-03T22:28:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MSchille-1994](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@MSchille-1994](https://forum.dynamobim.com/u/MSchille-1994)\
**Post date:** [December 3, 2020, 10:28pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/1 "2020-12-03T22:28:54Z")

</div>

I want to create a script for inserting an AutoCAD XREF into an empty drawing and then selecting all its layers and changing them to color 251. My question though, is there a way to insert an XREF with Dynamo? All I see is the option to insert a block.

Thanks,  
Michael

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [December 3, 2020, 11:44pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/2 "2020-12-03T23:44:15Z")

</div>

Hi @MSchille-1994,

There isn’t a dedicated node for this yet, so I put together this Python script for you to try. It doesn’t have any error checking, so it’s more of a proof of concept - use at your own risk.

IN[0] = the path to the DWG (as a string)  
IN[1] = the name for the XREF (I just used the file name in this example)  
IN[2] = a boolean toggle to set the XREF as Overlay or Attach (true is Overlay, false is Attach)

The output is the name of the XREF, which you can then feed into _Document.BlockByName_ to create the block wrapper and then get the objects and their layers. At the end of it all, _Layer.SetColor_ will be your friend.

FYI @Paolo_Emilio_Serra1 maybe something to add to the wish list.

![AddXref](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/b/ab3982a5d7aa27a476e235db3c3133df1b4884f4.gif)

```
import clr

# Add Assemblies for AutoCAD
clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

def add_xref(path,blockName,switch):
	
	global adoc
	
	with adoc.LockDocument():
		with adoc.Database as db:
			with db.TransactionManager.StartTransaction() as t:
				# Determine whether to overlay or attach
				if switch==True:
					oid = db.OverlayXref(path, blockName)
				else:
					oid = db.AttachXref(path, blockName)
				
				if not oid.IsNull:
					insPt = Point3d(0, 0, 0)					
					# Create block reference
					blkRef = BlockReference(insPt,oid)
					# Add block table record
					bt = t.GetObject(db.BlockTableId, OpenMode.ForWrite)
					btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
					btr.AppendEntity(blkRef);
					t.AddNewlyCreatedDBObject(blkRef, True)		
				t.Commit()			
	
	return blkRef.Name
	
OUT = add_xref(IN[0],IN[1],IN[2])

```

---

<div class="post-metadata">

**Author:** ![francois.picard](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@francois.picard](https://forum.dynamobim.com/u/francois.picard)\
**Post date:** [February 18, 2021, 6:02pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/3 "2021-02-18T18:02:26Z")

</div>

Hi that is a nice Python script!

Is there a way to make it work with multiple Xref? now when i’m feeding a list of path and name it return a error.

Any help will be geat!

thanks

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [February 18, 2021, 6:13pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/4 "2021-02-18T18:13:26Z")

</div>

You’d need to change the path and blockName inputs to be lists and then add a loop to iterate through each item in the list.

---

<div class="post-metadata">

**Author:** ![francois.picard](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@francois.picard](https://forum.dynamobim.com/u/francois.picard)\
**Post date:** [February 18, 2021, 7:35pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/5 "2021-02-18T19:35:07Z")

</div>

ok thanks, unfortunately im not very good (read no expentience at all) in Python ☹

---

<div class="post-metadata">

**Author:** ![mzjensen](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@mzjensen](https://forum.dynamobim.com/u/mzjensen)\
**Post date:** [July 1, 2022, 6:27pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/6 "2022-07-01T18:27:40Z")

</div>

FYI there are nodes for this in the Camber package starting in v4.0.0.

 ![xrefs](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/6/76dfc1130154193b50ec546a2442f0866c9b1188.png)

---

<div class="post-metadata">

**Author:** ![ddrennen2QDK2](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@ddrennen2QDK2](https://forum.dynamobim.com/u/ddrennen2QDK2)\
**Post date:** [December 15, 2023, 11:02pm UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/7 "2023-12-15T23:02:19Z")

</div>

did a quick image edit of the node above…  
the ability to insert XREF into an external document would be pretty RAD

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/4/34f26731cce783b18ac2667b2499bb9f9a5d2401.png)

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [December 16, 2023, 10:01am UTC](https://forum.dynamobim.com/t/insert-and-xref/57965/8 "2023-12-16T10:01:45Z")

</div>

Not sure if it possible to add information into external files but add this to the thread for Camber Feedback instead  
[Camber Feedback Thread - Civil3D - Dynamo (dynamobim.com)](https://forum.dynamobim.com/t/camber-feedback-thread/68942)
