First off, I'm new to .NET. Pretty well versed in AutoLisp and VBA, but I haven't done any customization in a few years.
Anyway, I am creating a routine for collapsing one of our drawings into a single file that can be sent to an outside consultant.
We utilize layers of nested xrefs, then load/unload the various parts as needed for that one configuration.
Xref Hierarcy is organized like so
Master_Xref
a. Category_Xref
*Sub_Xref1
*Sub_Xref2
b. Category_Xref
*Sub_Xref1
There are a lot more than that, but just to give a general idea.
So far, I've got it to create the list of loaded vs unloaded xrefs, load all unloaded xrefs, and bind all xrefs into the current drawing as inserts.
What I'm on the fence about now, is how to handle the removal of the blocks of the previously unloaded xrefs.
Is it better to iterate through the drawing over and over again, exploding any block that has blocks nested inside of it, then deleting any with names that match the unloaded list?
Is there a way to go into the block definition of the parent block, and just remove the ones matching the unloaded list?
Is there a third way that someone with more experience doing this might suggest?
Code so far:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Namespace GADWGCollapser
PublicClassCommands
'define the command
<CommandMethod("gacollapse")>
PublicSharedSub CollapseDrawing()
Dim doc AsDocument = Application.DocumentManager.MdiActiveDocument
Dim xrUNLID AsNewObjectIdCollection
Dim xrPARID AsNewObjectIdCollection
Dim db AsDatabase = doc.Database
Dim xg AsXrefGraph = db.GetHostDwgXrefGraph(True)
Dim root AsGraphNode = xg.RootNode
Dim numOfnodes AsInteger = xg.NumNodes
Dim ed AsEditor = doc.Editor
Dim xrUnLoaded AsNewList(OfString)
Dim xrLoaded AsNewList(OfString)
Dim xrParents AsNewList(OfString)
'Iterate through all xrefs in the active drawing
For n AsInteger = 0 To numOfnodes - 1
Dim child AsXrefGraphNode = TryCast(xg.GetXrefNode(n), XrefGraphNode)
'Determine if this is a parent xref.
If child.IsNested = FalseThen
'if parent, add xref file name to list
xrParents.Add(child.Name)
'if parent, add ID for later binding
xrPARID.Add(child.BlockTableRecordId)
EndIf
'Determine if the xref is loaded or unloaded
'if loaded, add name to list for later use
If child.XrefStatus = XrefStatus.Resolved Then
xrLoaded.Add(child.Name)
'if not loaded, add name to list for later purging
ElseIf
child.XrefStatus = XrefStatus.Unloaded Then
xrUnLoaded.Add(child.Name)
'add ID for loading all unloaded xrefs
'since unloaded xrefs cannot be bound
xrUNLID.Add(child.BlockTableRecordId)
EndIf
Next
'load all unloaded xrefs
db.ReloadXrefs(xrUNLID)
'bind all parent xrefs
db.BindXrefs(xrPARID,True)
EndSub
EndClass
End Namespace