Author Topic: Print xref names, from the block table  (Read 11998 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.