Active Member
Posts: 9
Registered: ‎11-19-2008
Message 1 of 4 (737 Views)
Accepted Solution

find if ExternalReferences Is Nested Reference

737 Views, 3 Replies
01-01-2012 06:16 AM
Board Manager
Posts: 368
Registered: ‎05-22-2006
Message 2 of 4 (729 Views)

Re: find if ExternalReferences Is Nested Reference

01-01-2012 08:29 AM in reply to: Danny.isr

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_the_interface/2009/05/importing-autocad-layers-from...




Stephen Preston
Autodesk Developer Network
Posts: 13
Registered: ‎10-07-2011
Message 3 of 4 (711 Views)

Re: find if ExternalReferences Is Nested Reference

01-02-2012 04:53 AM in reply to: Danny.isr

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:








- Attached.jpg




- ... another block talbe records


We can check if attached drawing is external reference by property IsXref.

Dim AcadDocument As AcadDocument = Application.DocumentManager.MdiActiveDocument.AcadDocument

For Each block As AcadBlock In AcadDocument.Blocks
    If block.IsXRef OrElse block.ObjectID = AcadDrawing.ModelSpace.ObjectID Then
        ' Your code here
    End If

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


Active Member
Posts: 9
Registered: ‎11-19-2008
Message 4 of 4 (702 Views)

Re: find if ExternalReferences Is Nested Reference

01-02-2012 11:57 AM in reply to: StephenPreston

Thank you

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)

                End If

         End If

End If

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community