• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Active Member
    Posts: 8
    Registered: ‎11-19-2008
    Accepted Solution

    find if ExternalReferences Is Nested Reference

    425 Views, 3 Replies
    01-01-2012 06:16 AM
     
    Please use plain text.
    ADN Support Specialist
    Posts: 261
    Registered: ‎05-22-2006

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

     

     

    Cheers,

    Stephen Preston
    Autodesk Developer Network
    Please use plain text.
    Contributor
    lukasz.taraszka
    Posts: 13
    Registered: ‎10-07-2011

    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:

     

    MainDrawing.dwg

    |

    |---AttachedDrawing.dwg

    ||---AnotherAttachedDrawing.dwg

    ||---AttachedJpg.jpg

    ||---Picture.bmp

    |

    |---AnotherDrawing.dwg

     

    BlockTable in MainDrawing will look like this:

     

    MainDrawing-BlockTable

    -AttachedDrawing.dwg

    -AnotherAttachedDrawing.dwg

    -AnotherDrawing.dwg

    -ModelSpace

    -Picture.bmp

    - Attached.jpg

    -AttachedDrawing.dwg

    -AnotherAttachedDrawing.dwg

    -AnotherDrawing.dwg

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

     


    Please use plain text.
    Active Member
    Posts: 8
    Registered: ‎11-19-2008

    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

    Please use plain text.