Here is the final version of Version 3. I has a dialog to show, with a tree view. I was thinking of other stuff, but I don't think I will mess around with this anymore (this topic that is).
using System;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass (typeof(Test.XrefDialog))]
namespace Test
{
/// <summary>
/// Description of XrefDialog.
/// </summary>
public class XrefDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TreeView treeView1;
public XrefDialog()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.treeView1 = new System.Windows.Forms.TreeView();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(512, 280);
this.treeView1.TabIndex = 1;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(420, 287);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Done";
this.button1.Click += new System.EventHandler(this.Button1Click);
//
// XrefDialog
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 313);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.DockPadding.All = 1;
this.MaximumSize = new System.Drawing.Size(500, 340);
this.MinimumSize = new System.Drawing.Size(250, 340);
this.Name = "XrefDialog";
this.Text = "XrefDialog";
this.Load += new System.EventHandler(this.XrefDialogLoad);
this.ResumeLayout(false);
}
#endregion
void Button1Click(object sender, System.EventArgs e)
{
Close();
}
void XrefDialogLoad(object sender, System.EventArgs e)
{
Document ActDoc = AcadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
BlockTable DocDbBlkTbl = (BlockTable)DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead);
XrefGraph DocDbXrGraph = DocDb.GetHostDwgXrefGraph(false);
for (int i = 0; i<DocDbXrGraph.NumNodes; ++i)
{
XrefGraphNode XrGraphNode = DocDbXrGraph.GetXrefNode(i);
if (i != 0)
{
if (!XrGraphNode.IsNested)
{
using (BlockTableRecord BlkTblRec = (BlockTableRecord)DocDbTrans.GetObject(XrGraphNode.BlockTableRecordId,OpenMode.ForRead))
{
string FilePath = HostApplicationServices.Current.FindFile (BlkTblRec.PathName, DocDb, FindFileHint.Default);
//DocEd.WriteMessage("\n Main Xref = " + XrGraphNode.Name + " [ " + FilePath + " ]");
TreeNode MainTreeNode = new TreeNode(XrGraphNode.Name);
MainTreeNode.Text = XrGraphNode.Name;
treeView1.Nodes.Add(MainTreeNode);
SearchBlockDefinition(BlkTblRec, " ", MainTreeNode);
}
}
}
}
}
}
static void SearchBlockDefinition (BlockTableRecord BlkTblRec, string StringSpacer, TreeNode ParentNode)
{
Document ActDoc = AcadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
foreach (ObjectId ObjId in BlkTblRec)
{
BlockReference BlkRef = DocDbTrans.GetObject(ObjId, OpenMode.ForRead) as BlockReference;
if (BlkRef != null)
{
BlockTableRecord NestedBlkTblRec = (BlockTableRecord) DocDbTrans.GetObject(BlkRef.BlockTableRecord, OpenMode.ForRead);
if (NestedBlkTblRec.IsFromExternalReference)
{
string FilePath = HostApplicationServices.Current.FindFile (NestedBlkTblRec.PathName, DocDb, FindFileHint.Default);
//DocEd.WriteMessage("\n" + StringSpacer + "Nested: " + NestedBlkTblRec.Name + " [ " + FilePath + " ]");
TreeNode SubNode = new TreeNode (NestedBlkTblRec.Name);
SubNode.Text = NestedBlkTblRec.Name;
ParentNode.Nodes.Add (SubNode);
SearchBlockDefinition(NestedBlkTblRec, StringSpacer + " ", SubNode);
}
}
}
}
}
[CommandMethod("XrefDialog")]
public void CallXrefDialog()
{
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
XrefDialog modalForm = new XrefDialog();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modalForm);
}
}
}
}