# List xrefs loaded in the drawing

**URL:** <https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088>\
**Category:** Civil 3D\
**Created:** [July 17, 2020, 8:16pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088 "2020-07-17T20:16:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![csanchez](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@csanchez](https://forum.dynamobim.com/u/csanchez)\
**Post date:** [July 17, 2020, 8:16pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/1 "2020-07-17T20:16:02Z")

</div>

Hi, is there a way to list all the xrefs loaded in the cad drawing?

---

<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 17, 2020, 8:42pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/2 "2020-07-17T20:42:58Z")

</div>

This is a bit of a workaround, but you can take advantage of two things:

1. XREFs are loaded to the current drawing as blocks.
2. All layers in an XREF come into the current drawing and are prefixed with the XREF name followed by a pipe symbol.

 ![XREF Names](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/5/e5fd3aeb69f78a6958866e0a34a3c47f413a39a5.png)

Obviously it would be better to achieve this via the AutoCAD API, but I don’t have time for that right now.

---

<div class="post-metadata">

**Author:** ![csanchez](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@csanchez](https://forum.dynamobim.com/u/csanchez)\
**Post date:** [July 20, 2020, 7:05pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/3 "2020-07-20T19:05:06Z")

</div>

Thank you Mzjensen, it works great filtering the names of the xref but i am not sure why is also listing a couple of layers too (“1” & “206”).

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

---

<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 20, 2020, 7:25pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/4 "2020-07-20T19:25:53Z")

</div>

@csanchez hard to tell without seeing the output of _String.Split_. But you actually probably don’t need the _List.FirstItem_ and _List.UniqueItems_ nodes at all. You should be able to just flatten everything after _String.Split_ and then intersect that list with the block names.

---

<div class="post-metadata">

**Author:** ![csanchez](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@csanchez](https://forum.dynamobim.com/u/csanchez)\
**Post date:** [July 20, 2020, 9:07pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/5 "2020-07-20T21:07:54Z")

</div>

I removed them but didn’t work. I did a filter to removed the names that are not xref names since they are just numbers and works.

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

---

<div class="post-metadata">

**Author:** ![keith.sowinski](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@keith.sowinski](https://forum.dynamobim.com/u/keith.sowinski)\
**Post date:** [July 21, 2020, 12:14pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/6 "2020-07-21T12:14:39Z")

</div>

Here one way to do it with python.

```
import clr

clr.AddReference('AcMgd')
clr.AddReference('AcDbMgd')

from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.DatabaseServices import *

adoc = Application.DocumentManager.MdiActiveDocument

xrefs = []

with adoc.LockDocument():
	with adoc.Database as db:
		with db.TransactionManager.StartTransaction() as t:
			bt = t.GetObject(db.BlockTableId, OpenMode.ForRead)
			btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead)

			for oid in btr:
				bl = t.GetObject(oid, OpenMode.ForRead)
				if isinstance(bl, BlockReference):
					if bl.BlockTableRecord.IsFromExternalReference:
						xrefs.append(bl.Name)
						
			
OUT = xrefs
```

---

<div class="post-metadata">

**Author:** ![csanchez](https://avatars.discourse-cdn.com/v4/letter/c/8491ac/32.png) [@csanchez](https://forum.dynamobim.com/u/csanchez)\
**Post date:** [July 21, 2020, 12:50pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/7 "2020-07-21T12:50:32Z")

</div>

This is great, thank you Keith. I would love to find some tutorial on Python for Civil 3D. Python is very powerful but hard to learn without good tutorial. Do you know of any?

---

<div class="post-metadata">

**Author:** ![keith.sowinski](https://avatars.discourse-cdn.com/v4/letter/k/d26b3c/32.png) [@keith.sowinski](https://forum.dynamobim.com/u/keith.sowinski)\
**Post date:** [July 21, 2020, 1:02pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/8 "2020-07-21T13:02:44Z")

</div>

I am by no means an expert with Python or a programmer, but was able to pick it up by watching some YouTube tutorials and by looking at the many examples that have been posted on this forum. It’s not as difficult as it might seem at first. Beyond Python, you’ll want to familiarize yourself with the APIs that you are dealing with.

I suggest bookmarking the [AutoCAD](https://help.autodesk.com/view/OARX/2020/ENU/?guid=GUID-C3F3C736-40CF-44A0-9210-55F6A939B6F2) and [Civil 3D](http://help.autodesk.com/view/CIV3D/2020/ENU/?guid=GUID-DA303320-B66D-4F4F-A4F4-9FBBEC0754E0) Developer’s Guides. You’ll be in there a lot looking through the API Reference Guides and the provided examples.

---

<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 21, 2020, 1:14pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/9 "2020-07-21T13:14:39Z")

</div>

Thanks for taking the time to code that up @keith.sowinski!

@Paolo_Emilio_Serra1 perhaps a nice addition to the toolkit.

---

<div class="post-metadata">

**Author:** ![bertrand.savarit](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/bertrand.savarit/32/103266_2.png) [@bertrand.savarit](https://forum.dynamobim.com/u/bertrand.savarit)\
**Post date:** [July 21, 2020, 1:21pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/10 "2020-07-21T13:21:23Z")

</div>

Thanks for this Python script, it certainly will be usefull for a lot of people.

---

<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 21, 2020, 6:31pm UTC](https://forum.dynamobim.com/t/list-xrefs-loaded-in-the-drawing/53088/11 "2020-07-21T18:31:39Z")

</div>

FYI for future readers:

> [@Civil 3D Toolkit Feedback thread](https://forum.dynamobim.com/t/civil-3d-toolkit-feedback-thread/43570/391):
>
