1+ using Autodesk . AutoCAD . ApplicationServices ;
2+ using Autodesk . AutoCAD . DatabaseServices ;
3+ using Autodesk . AutoCAD . Runtime ;
4+ using Autodesk . AutoCAD . EditorInput ;
5+ using Application = Autodesk . AutoCAD . ApplicationServices . Core . Application ;
6+
7+ namespace Test ;
8+
9+ /// <summary>
10+ /// https://adndevblog.typepad.com/autocad/2012/06/finding-all-xrefs-in-the-current-database-using-cnet.html
11+ /// http://www.theswamp.org/index.php?topic=22884
12+ /// https://forums.autodesk.com/t5/net-forum/get-list-of-xrefs-in-net/td-p/4553291
13+ /// </summary>
14+ public class XrefPathExtractor
15+ {
16+ [ CommandMethod ( "XrefGraph" ) ]
17+ public static void XrefGraph ( )
18+
19+ {
20+ Document doc = Application . DocumentManager . MdiActiveDocument ;
21+
22+ Database db = doc . Database ;
23+
24+ Editor ed = doc . Editor ;
25+
26+ using ( Transaction Tx = db . TransactionManager . StartTransaction ( ) )
27+
28+ {
29+ ed . WriteMessage ( "\n ---Resolving the XRefs------------------" ) ;
30+
31+ db . ResolveXrefs ( true , false ) ;
32+
33+ XrefGraph xg = db . GetHostDwgXrefGraph ( true ) ;
34+
35+ ed . WriteMessage ( "\n ---XRef's Graph-------------------------" ) ;
36+
37+ ed . WriteMessage ( "\n CURRENT DRAWING" ) ;
38+
39+ GraphNode ? root = xg . RootNode ;
40+
41+ PrintChildren ( root , "|-------" , ed , Tx ) ;
42+
43+ ed . WriteMessage ( "\n ----------------------------------------\n " ) ;
44+ }
45+ }
46+
47+
48+ // Recursively prints out information about the XRef's hierarchy
49+
50+ private static void PrintChildren ( GraphNode ? iRoot , string iIndent ,
51+ Editor iEd , Transaction iTx )
52+
53+ {
54+ for ( int o = 0 ; o < iRoot ! . NumOut ; o ++ )
55+
56+ {
57+ XrefGraphNode ? child = iRoot . Out ( o ) as XrefGraphNode ;
58+
59+ if ( child . XrefStatus == XrefStatus . Resolved )
60+
61+ {
62+ BlockTableRecord ? bl =
63+ iTx . GetObject ( child . BlockTableRecordId , OpenMode . ForRead )
64+ as BlockTableRecord ;
65+
66+ iEd . WriteMessage ( "\n " + iIndent + child . Database . Filename ) ;
67+
68+ // Name of the Xref (found name)
69+
70+ // You can find the original path too:
71+
72+ //if (bl.IsFromExternalReference == true)
73+
74+ // i_ed.WriteMessage("\n" + i_indent + "Xref path name: "
75+
76+ // + bl.PathName);
77+
78+ PrintChildren ( child , "| " + iIndent , iEd , iTx ) ;
79+ }
80+ }
81+ }
82+ }
0 commit comments