I thought of blatantly reloading/repathing an xref too, but it could have been deliberate (the unload) so I decided the most sensible thing to do was get the program to complain loudly and stop.
Have a look at the attached image 'XrefDlg'. It's the most complex/convoluted xref nesting scenario I could come up with and is what I use to test all xref related things on.
As far as changing the path as you do, it won't work on nested xrefs as you can only change that in their immediate host file, which in the current drawing is essentially read-only.
You can change it with VBA and maybe .net, but it won't stick if you reopen the drawing. One solution might be to use TonyT's idea and class for temporarily switching the WorkingDatabase to the xref database, then switch back....maybe.
Attached also is a zip file with the relevant drawings from that image so you can test with it.
Try running this code over the attached "xref bind test.dwg":
// Define Command "LX"
[CommandMethod("LX")]
static public void ListXrefsCommand() {
// Get a pointer to the active document...
Document doc = acadApp.DocumentManager.MdiActiveDocument;
// From the active document, get a pointer to the doc's dbase...
Database db = doc.Database;
// Get a pointer to the editor...
Editor ed = doc.Editor;
// Get the xref graph for the current dbase...
XrefGraph xrefGraph = db.GetHostDwgXrefGraph(true);
if (xrefGraph.IsEmpty || xrefGraph.NumNodes == 1) {
ed.WriteMessage("\nNo xrefs found.");
return; // No xrefs...
}
for (int i = 0; i < xrefGraph.NumNodes; i++) {
XrefGraphNode xrefGraphNode = xrefGraph.GetXrefNode(i);
if (xrefGraphNode == null) {
ed.WriteMessage("\nError: Failed to get a node on the graph - aborting!");
return;
}
// Is it the root node, which is the current drawing?
// If it IS, then the number of incoming nodes will be 0...
if (xrefGraphNode.NumIn == 0)
continue;
// Continue if it's truly nested...
if (xrefGraphNode.IsNested)
continue;
ed.WriteMessage("\nXref name: {0}", xrefGraphNode.Name);
for (int j = 0; j < xrefGraphNode.NumOut; j++) {
XrefGraphNode outgoingNode = xrefGraphNode.Out(j) as XrefGraphNode;
if (outgoingNode != null)
ed.WriteMessage("-->{0}", outgoingNode.Name);
}//for
}//for
}
Tell me if it lists ALL xrefs.
Also, by looking at the attached image, which xrefs can be bound and which ones can't? I do have a solution for the bind, which I will post up in a bit, but first let's continue this discussion and see what others have to say.
Cheers,
Glenn.