Author Topic: Print xref names, from the block table  (Read 6217 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Print xref names, from the block table
« Reply #15 on: August 09, 2006, 07:45:09 PM »
Version 3.  Soon I might have one that is worth something.
Code: [Select]
using System;

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.Xref))]

namespace Test
{
/// <summary>
/// Description of Xref.
/// </summary>
public class Xref
{
[CommandMethod("PrintXrefs3")]
public void XrefList3()
{

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 + " ]");
SearchBlockDefinition(BlkTblRec, "   ");
}
}
}
}
}
}
static void SearchBlockDefinition (BlockTableRecord BlkTblRec, string StringSpacer)
{
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 + " ]");
SearchBlockDefinition(NestedBlkTblRec, StringSpacer + "   ");
}
}
}
}
}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Print xref names, from the block table
« Reply #16 on: August 10, 2006, 01:26:03 AM »
Tim,

You're welcome.

Base class -> children - welcome to OOP inheritance and all it's glory :)

Version 3: I don't understand why you have SearchBlockDefinition???????? What's your aim? If it's to simply print out all the xrefs in the drawing, then the xrefgraph will give you all that without having to search the block def...or am I wrong in assuming your intent with this?

Cheers,
Glenn.
Me

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Print xref names, from the block table
« Reply #17 on: August 10, 2006, 11:53:12 AM »
Tim,

You're welcome.

Base class -> children - welcome to OOP inheritance and all it's glory :)

Version 3: I don't understand why you have SearchBlockDefinition???????? What's your aim? If it's to simply print out all the xrefs in the drawing, then the xrefgraph will give you all that without having to search the block def...or am I wrong in assuming your intent with this?

Cheers,
Glenn.
Glenn,

  With version 3 I was trying out the XrefGraph, and it does give a lot of information, but it doesn't tell you where the nested xrefs are nested in, at leat I didn't see it.  That is the reason why I did the SearchBlockDefinition.  If I missed something, please point it out.  It was a fun experience trying to code that.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Print xref names, from the block table
« Reply #18 on: August 10, 2006, 03:32:40 PM »
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).
Code: [Select]
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);
}

}
}
}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Print xref names, from the block table
« Reply #19 on: August 10, 2006, 09:21:11 PM »
This is very similar to how I've done it in ARX in the past, but it appears the implementation fails when trying to get 'outgoingNode':

Code: [Select]
// 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

}

This is annoying, unless I'm missing something obvious this morning.........
Me

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Print xref names, from the block table
« Reply #20 on: August 10, 2006, 11:17:34 PM »
Thanks for posting Glenn; I don't think I looked at the 'Out' of the xref graph node.  I will have to check it out tomorrow when I get to work.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5218
Re: Print xref names, from the block table
« Reply #21 on: August 11, 2006, 12:59:14 PM »
It doesn't look like it will assign the GraphNodes returned by the 'Out' method as XrefGraphNodes.  I will try to find out some way of doing it today, but figured I would post this in case someone has dealt with it already.

Thanks.
Code: [Select]
[CommandMethod("XrGraphTest")]
static public void GraphTest()
{
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);
DocEd.WriteMessage("\n Name {0}, In {1}, Out {2}", XrGraphNode.Name, XrGraphNode.NumIn, XrGraphNode.NumOut);
if (XrGraphNode.NumOut > 0)
{
OutNodePrint(XrGraphNode);
}
}
}
}
static public void OutNodePrint(XrefGraphNode XrGraphNode)
{
Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Editor DocEd = ActDoc.Editor;
for (int i = 1; i <= XrGraphNode.NumOut; ++i)
{
XrefGraphNode tmpNode = XrGraphNode.Out(i) as XrefGraphNode;
if (tmpNode != null)
{
DocEd.WriteMessage("\n  {0}", tmpNode.Name);
}
else
{
DocEd.WriteMessage("\n   Not found as an \"XrefGraphNode\"!");
}
}
}
Returned
Quote from:  Acad Command Line
Command: xrgraphtest

 Name XrefIsIn, In 0, Out 2
   Not found as an "XrefGraphNode"!
   Not found as an "XrefGraphNode"!
 Name XREF, In 1, Out 0
 Name XrefMain, In 1, Out 2
   Not found as an "XrefGraphNode"!
   Not found as an "XrefGraphNode"!
 Name Xref2, In 1, Out 1
   Not found as an "XrefGraphNode"!
 Name Xref1, In 1, Out 0
 Name Cube, In 1, Out 0
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.