List xrefs loaded in the drawing

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

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.

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

4 Likes

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”).

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

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.

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
5 Likes

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?

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 and Civil 3D Developer’s Guides. You’ll be in there a lot looking through the API Reference Guides and the provided examples.

2 Likes

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

@Paolo_Emilio_Serra1 perhaps a nice addition to the toolkit.

2 Likes

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

FYI for future readers:

1 Like