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

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Print xref names, from the block table
« on: August 08, 2006, 01:24:24 PM »
Can anyone tell me what I'm doing wrong?  What I THINK is wrong, is the test to see if it's an xref, but I don't know how else to test.  Just incase it helps, the error is the little dialog box telling me I messed up.

Thanks in advance.
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("PrintXrefs")]
public void XrefList()
{

Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
BlockTable BlkTbl =(BlockTable) DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead);
foreach (BlockTableRecord btr in BlkTbl)
{
if (btr.IsFromExternalReference == true)
{
DocEd.WriteMessage(btr.Name);
}
}
}
}
}
}
I have tried this also
Code: [Select]
foreach (BlockTableRecord btr in BlkTbl)
{
if (btr.IsFromExternalReference)
{
DocEd.WriteMessage(btr.Name);
}
}
Thinking it would return true/null (which is how I think it could work coming from Lisp), but it didn't.
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: 5251
Re: Print xref names, from the block table
« Reply #1 on: August 08, 2006, 02:34:03 PM »
Found the answer.  I needed to get the ObjectId from the BlockTable, and then use that with the DocDbTrans (my variable) to get the BlockTableReference from the ObjectId.  This will take some getting used to.

Here are my two codes.  Is one better than the other?  I would think that PrintXrefs2 is better because it used the "using" type of calls, and that is supposed to dispose of them when they are done, at least that is my understanding.

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("PrintXrefs")]
public void XrefList()
{

Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
BlockTable BlkTbl =(BlockTable) DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead);
foreach (ObjectId ObjId in BlkTbl)
{
BlockTableRecord BlkTblRec = (BlockTableRecord) DocDbTrans.GetObject(ObjId, OpenMode.ForRead);
if (BlkTblRec.IsFromExternalReference)
{
DocEd.WriteMessage("\n " + BlkTblRec.Name);
}
}
}
}

[CommandMethod("PrintXrefs2")]
public void XrefList2()
{

Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
using (BlockTable BlkTbl =(BlockTable) DocDbTrans.GetObject(DocDb.BlockTableId, OpenMode.ForRead))
{
foreach (ObjectId ObjId in BlkTbl)
{
using (BlockTableRecord BlkTblRec = (BlockTableRecord) DocDbTrans.GetObject(ObjId, OpenMode.ForRead))
{
if (BlkTblRec.IsFromExternalReference)
{
DocEd.WriteMessage("\n " + BlkTblRec.Name);
}
}
}
}
}
}
}
}
Tim

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

Please think about donating if this post helped you.

LE

  • Guest
Re: Print xref names, from the block table
« Reply #2 on: August 08, 2006, 03:49:57 PM »
Hey Tim;

I do not see the call of "DocDbTrans.Commit();" - is that part not required? - How do we close the counterpart of the transaction? - my goodness I am now confused...  :-(

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #3 on: August 08, 2006, 04:03:00 PM »
Hey Tim;

I do not see the call of "DocDbTrans.Commit();" - is that part not required? - How do we close the counterpart of the transaction? - my goodness I am now confused...  :-(
I don't call it.  It is not needed when you use the "using" to call it.  Following the suggestion from Bobby, form this post and the couple that follow.  Hope that helps.  Will be back later, meeting to run to now.  :-)
Tim

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

Please think about donating if this post helped you.

LE

  • Guest
Re: Print xref names, from the block table
« Reply #4 on: August 08, 2006, 04:28:09 PM »
Orale! - he is da' man....

Anyway I did one function to work on AutoCAD 2005 - and still has the call to Commit()....

Code: [Select]
[CommandMethod("PrintXrefs")]
public void XrefList()
{
AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager tm = db.TransactionManager;
using (AcDb.Transaction tr = tm.StartTransaction())
{
AcDb.BlockTable bt = (AcDb.BlockTable)tr.GetObject(db.BlockTableId, AcDb.OpenMode.ForRead);
foreach (AcDb.ObjectId id in bt)
{
AcDb.BlockTableRecord btr = (AcDb.BlockTableRecord)tr.GetObject(id, AcDb.OpenMode.ForRead);
if (btr.IsFromExternalReference)
{
CommandLinePrompts.Message("\n " + btr.Name);
}
}
tr.Commit(); // no needed!
}
}

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #5 on: August 08, 2006, 05:12:54 PM »
You got me thinking now Luis.  I wonder if you only don't need the Dispose() but you still need the Commit() on the TransactionManager?  The things I have seen only refure to Dispose().  Maybe one of the Guru's will chime in a let us know for sure.

Thanks whomever does.
Tim

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

Please think about donating if this post helped you.

Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: Print xref names, from the block table
« Reply #6 on: August 08, 2006, 05:23:55 PM »
You got me thinking now Luis.  I wonder if you only don't need the Dispose() but you still need the Commit() on the TransactionManager?  The things I have seen only refure to Dispose().  Maybe one of the Guru's will chime in a let us know for sure.

Thanks whomever does.
You must commit the transaction prior to disposing it if you want the actions you took inside it to stick.  If you abort the transaction prior to disposing it, then the actions won't stick.  If a transaction is disposed without committ being called, then it is aborted.

For read only type code, like you're showing here Tim, it doesn't really matter if you commit or abort.
Bobby C. Jones

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #7 on: August 08, 2006, 05:32:45 PM »
You got me thinking now Luis.  I wonder if you only don't need the Dispose() but you still need the Commit() on the TransactionManager?  The things I have seen only refure to Dispose().  Maybe one of the Guru's will chime in a let us know for sure.

Thanks whomever does.
You must commit the transaction prior to disposing it if you want the actions you took inside it to stick.  If you abort the transaction prior to disposing it, then the actions won't stick.  If a transaction is disposed without committ being called, then it is aborted.

For read only type code, like you're showing here Tim, it doesn't really matter if you commit or abort.
Thank you very much Bobby.  I didn't think I needed the Commit() part, but I saw it in Luis's code, and it got me thinking.  So I figured I better ask before the thought leaves.   :-)
Tim

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

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Print xref names, from the block table
« Reply #8 on: August 08, 2006, 05:33:14 PM »
I'm no guru, but ...

Commit commits modified/new objects to the database. If the BlockTable and BlockTable Records are opened ForRead I dont believe commit is necessary.

... but I've been wrong previously .. :)


edit: ... this is what happens when I answer the phone .. everyone jumps in and posts before me ...
« Last Edit: August 08, 2006, 05:35:02 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #9 on: August 08, 2006, 05:54:41 PM »
I'm no guru, but ...

Commit commits modified/new objects to the database. If the BlockTable and BlockTable Records are opened ForRead I dont believe commit is necessary.

... but I've been wrong previously .. :)


edit: ... this is what happens when I answer the phone .. everyone jumps in and posts before me ...
I still appreciated it.

One more question.  Do you have to OpenMode.ForWrite all objects? or just the one you are going to edit?

Say you want to change the path of an Xref, you would have to open the BlockTableRecord to write, but do you have to open the BlockTable for write also?

Thanks.
Tim

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

Please think about donating if this post helped you.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Print xref names, from the block table
« Reply #10 on: August 08, 2006, 06:09:53 PM »
One more question.  Do you have to OpenMode.ForWrite all objects? or just the one you are going to edit?
...............

Just the ones that you expect to modify, is my understanding.


............ Say you want to change the path of an Xref, you would have to open the BlockTableRecord to write, but do you have to open the BlockTable for write also?

Thanks.

I think the block table just holds references to  the btr's, so I'd guess that the bt would not need writing ... but I'd suggest you prove that with testing.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #11 on: August 08, 2006, 06:16:33 PM »
One more question.  Do you have to OpenMode.ForWrite all objects? or just the one you are going to edit?
...............

Just the ones that you expect to modify, is my understanding.


............ Say you want to change the path of an Xref, you would have to open the BlockTableRecord to write, but do you have to open the BlockTable for write also?

Thanks.

I think the block table just holds references to  the btr's, so I'd guess that the bt would not need writing ... but I'd suggest you prove that with testing.
I haven't made changes yet like that.  Still working on the lingo, but I will try soon.  Maybe tomorrow, if work won't let me today.  Thanks Kerry.
Tim

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

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Print xref names, from the block table
« Reply #12 on: August 08, 2006, 10:59:48 PM »
That's definately one way of doing it, here is, in my opion, the best way to do it:

http://www.theswamp.org/index.php?topic=8299.msg106160#msg106160

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Print xref names, from the block table
« Reply #13 on: August 09, 2006, 11:09:59 AM »
That's definately one way of doing it, here is, in my opion, the best way to do it:

http://www.theswamp.org/index.php?topic=8299.msg106160#msg106160

Cheers,
Glenn.
Thanks Glenn.  I saw that one, and didn't understand how you got there.  I'm just starting out, and this is just a way for me to get better.

I did have a question though on you code (if you don't mind me asking).  How did you know that you had to use a counting system to step through the XrefGraph?  I was trying to find in the help somewhere that would tell me if I could step through it with a 'Foreach' loop, or if the only way is the counter method?  After I saw your code, I started looking through the help files (dll's, and one help file that Kerry found) and didn't see why you used it the way you did.
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: 5251
Re: Print xref names, from the block table
« Reply #14 on: August 09, 2006, 01:37:55 PM »
That's definately one way of doing it, here is, in my opion, the best way to do it:

http://www.theswamp.org/index.php?topic=8299.msg106160#msg106160

Cheers,
Glenn.
Thanks Glenn.  I saw that one, and didn't understand how you got there.  I'm just starting out, and this is just a way for me to get better.

I did have a question though on you code (if you don't mind me asking).  How did you know that you had to use a counting system to step through the XrefGraph?  I was trying to find in the help somewhere that would tell me if I could step through it with a 'Foreach' loop, or if the only way is the counter method?  After I saw your code, I started looking through the help files (dll's, and one help file that Kerry found) and didn't see why you used it the way you did.

No need to answer these questions.  I found what I needed in the help files, and a pdf file that Luis directed me towards that tells me how I can figure out what is able to use the 'foreach' on.
Here is the code.  Close to yours Glenn (I used it as a guide when I got stuck, Thanks for sharing it).  It's nice to know that you can use the properties from the 'base' (I think that would be the correct word) 'class' (type) on the children.
Example:
If you look at 'XrefGraph' it doesn't have the 'NumNodes' property, but the class that 'XrefGraph' is a child of is 'Graph' and that has the 'NumNodes'.  I know the Masters/Gurus know this, but I didn't.  :-)
Code: [Select]
[CommandMethod("PrintXrefs3")]
public void XrefList3()
{

Document ActDoc = acadApp.DocumentManager.MdiActiveDocument;
Database DocDb = ActDoc.Database;
Editor DocEd = ActDoc.Editor;
using (Transaction DocDbTrans = DocDb.TransactionManager.StartTransaction())
{
XrefGraph DocDbXrGraph = DocDb.GetHostDwgXrefGraph(false);
for (int i = 0; i<DocDbXrGraph.NumNodes; ++i)
{
XrefGraphNode XrGraphNode = DocDbXrGraph.GetXrefNode(i);
if (i != 0)
{
DocEd.WriteMessage("\n Xref name = " + XrGraphNode.Name);
DocEd.WriteMessage("\n   |-> Is nested? " + XrGraphNode.IsNested.ToString());
}
}
}
}
Now to see what else I can get into.
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: 5251
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

  • Guest
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.

T.Willey

  • Needs a day job
  • Posts: 5251
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: 5251
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

  • Guest
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.........

T.Willey

  • Needs a day job
  • Posts: 5251
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: 5251
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.