using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly: CommandClass(typeof(ScanXref.MyCommands))]
namespace ScanXref
{
public class MyCommands
{
[CommandMethod("XrefGraph")]
public static void XrefGraph()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction Tx = db.TransactionManager.StartTransaction())
{
ed.WriteMessage("\n---Находим все внешние ссылки ------------------");
db.ResolveXrefs(true, false);
XrefGraph xg = db.GetHostDwgXrefGraph(true);
ed.WriteMessage("\n---Граф внешних ссылок -------------------------");
ed.WriteMessage("\nТекущий чертеж ");
GraphNode root = xg.RootNode;
printChildren(root, "|-------", ed, Tx);
ed.WriteMessage("\n----------------------------------------\n");
}
}
// Рекурсивно печатаем информацию об иерархии внешних ссылок
private static void printChildren(GraphNode i_root, string i_indent,
Editor i_ed, Transaction i_Tx)
{
for (int o = 0; o < i_root.NumOut; o++)
{
XrefGraphNode child = i_root.Out(o) as XrefGraphNode;
if (child.XrefStatus == XrefStatus.Resolved)
{
BlockTableRecord bl =
i_Tx.GetObject(child.BlockTableRecordId, OpenMode.ForRead)
as BlockTableRecord;
i_ed.WriteMessage("\n" + i_indent + child.Database.Filename);
ObjectIdCollection idsBref = bl.GetBlockReferenceIds(false, true);
if (idsBref.Count > 0)
{
for (int i = 0; i < idsBref.Count; i++)
{
BlockReference bref =
i_Tx.GetObject(idsBref[i], OpenMode.ForRead)
as BlockReference;
if (bref != null)
{
i_ed.WriteMessage("\n {0} Origin = {1}, Scale = {2}", i, bref.Position, bref.ScaleFactors);
}
}
}
printChildren(child, "| " + i_indent, i_ed, i_Tx);
}
}
}
}
}