You could try looking at the XrefGraph for the drawing - see Database.GetHostDwgXrefGraph(). Here's a blog post that shows how to use that function, although not for the same reason you want it - http://through-the-interface.typepad.com/through_t
Autodesk Developer Network
First You have to realize how does the xrefs works. The external references are placed in block table as a block table records. They are in a flat structure (opposite to tree structure) it means if we have attached drawing which have another external references, those references will be in top level in block table or they will be entity in model space.
Here is explaination:
BlockTable in MainDrawing will look like this:
- ... another block talbe records
We can check if attached drawing is external reference by property IsXref.
Dim AcadDocument As AcadDocument = Application.DocumentManager.MdiActiveDocument.Acad
Document For Each block As AcadBlock In AcadDocument.Blocks If block.IsXRef OrElse block.ObjectID = AcadDrawing.ModelSpace.ObjectID Then ' Your code here End If Next
If drawing is xref we can get its path, and open that document in background.
Dim drawing As Database = New Database(False, True) drawing.ReadDwgFile(pathToReference, FileOpenMode.OpenForReadAndAllShare, True, Nothing)
You can search for each references in opened drawing and check if there are external references by Property IsFromExternalReference. If drawing contain external reference it means that it's nested reference.
Unfortunatelly the best way for doing this is recursion because nested references can appear on different level. The worst information is in some case You can have the same reference on different lvl or duplicated references... . It's long and hard way...
Dim blockTable As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) For Each blockTableRecId As ObjectId In blockTable Dim blockTableRec = tr.GetObject(blockTableRecId, OpenMode.ForRead) If blockTableRec.IsFromExternalReference ' Is nested reference
Dim bt As Autodesk.AutoCAD.DatabaseServices.BlockTable
Dim btr As BlockTableRecord
Dim blockId As ObjectId
Dim iter As IEnumerator
Dim xg As XrefGraph = db.GetHostDwgXrefGraph(False)
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
iter = bt.GetEnumerator() Do While iter.MoveNext()
blockId = iter.Current btr = trans.GetObject(blockId, OpenMode.ForWrite)
If btr.IsFromExternalReference Then
If Not xg.GetXrefNode(btr.Name).IsNested Then
btr.IsUnloaded = True
If btr.IsUnloaded = True Then
Dim aa As New SiWeb.BaseFileVersion(btr.PathName)