Author Topic: "Cleanest" Method For Selecting All Blocks in PaperSpace  (Read 13113 times)

0 Members and 1 Guest are viewing this topic.

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #15 on: July 06, 2009, 02:57:03 PM »
Thanks for the confirmation Sinc.

Luis,

That would just get all the inserts of that specific block definition, not all the blocks that are inserted ( nested ) within the definition.


dang!.... sorry

lately I'm way out of my miserable C# skills (== -0)

4 slaps on my face.   :oops:

T.Willey

  • Needs a day job
  • Posts: 5251
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #16 on: July 06, 2009, 03:01:56 PM »
For most instances the Id will be fine to work with.  In rare cases the Handle will have to be used, but here I think you can stick with the Id.

As for testing if it's a BlockReference, what you have works, and would most likely be what I used ( take that for what it's worth ).  If you wanted another way, you could just cast it as an entity, then test it with the ' is ' keyword ( I think below is how it works ).  Just another option.

Code: [Select]
Entity Ent = tr.GetObject( id, OpenMode.ForRead ) as Entity
if ( Ent is BlockReference )
    result.Add( id );
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: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #17 on: July 06, 2009, 03:03:13 PM »
Thanks for the confirmation Sinc.

Luis,

That would just get all the inserts of that specific block definition, not all the blocks that are inserted ( nested ) within the definition.


dang!.... sorry

lately I'm way out of my miserable C# skills (== -0)

4 slaps on my face.   :oops:

Not a problem Luis.   :-)
Tim

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

Please think about donating if this post helped you.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #18 on: July 06, 2009, 03:11:33 PM »
Thanks Tim.

My C# 'stuff' isn't much more than hacking some snippets here and there :-(
Speaking English as a French Frog

T.Willey

  • Needs a day job
  • Posts: 5251
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #19 on: July 06, 2009, 03:46:46 PM »
Thanks Tim.

My C# 'stuff' isn't much more than hacking some snippets here and there :-(

You're welcome Gile.  I'm learning just like you, and I see you with some good stuff, and I'm sure you will keep at it and develop some good stuff.
Tim

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

Please think about donating if this post helped you.

wannabe

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #20 on: July 07, 2009, 07:30:31 AM »
Thanks Tim.

My C# 'stuff' isn't much more than hacking some snippets here and there :-(

The guys on here recommended Pro C# 2008 and the .NET platform. It was a great read, but I already had a good foundation of C# by then. Headfirst C# is a great entry-level hands-on book, though (in my opinion).

What level are you currently at?

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #21 on: July 07, 2009, 10:52:17 AM »
Well, I have some time to play today with little c# and here it is Gile's function with some touch ups.... HTH-maybe :)

Code: [Select]
public ObjectIdCollection GetBlockRefInLayouts()
{
    ObjectIdCollection ids = new ObjectIdCollection();
    Database db = Application.DocumentManager.MdiActiveDocument.Database;
    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
        foreach (DBDictionaryEntry entry in layoutDict)
        {
            if (entry.Key == "Model") continue;
            Layout layout = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
            BlockTableRecord layoutBtr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead, false);
            if (!layoutBtr.IsAnonymous)
            {
                foreach (ObjectId id in layoutBtr)
                {
                    BlockReference blk = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
                    if (blk != null) ids.Add(id);
                }
            }
        }
        tr.Commit();
    }
    return ids;
}

[CommandMethod("PSBLOCKS")]
public void psblocks()
{
    Document doc = acadApp.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;
    ObjectIdCollection ids = new ObjectIdCollection();
    ids = GetBlockRefInLayouts();
    ed.WriteMessage("\nBlocks on paper-space: {0}", ids.Count.ToString());
}

wannabe

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #22 on: July 07, 2009, 12:26:25 PM »
That's a touch closer to the methodology I used. Rather than going through the NOD I directly grabbed the LayoutDictionaryID from each database I readDWg()'ed.

The only part I would like to query is the .IsAnonyMous property. I think I've read somewhere, or deduced from someone else's  code that this means is not in an Xref? Close? :-)


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #23 on: July 07, 2009, 12:29:03 PM »
Una vez más, muchas gracias Luis.

He apprendido el uso de ObjectIdCollection, db.LayoutDictionaryId y continue, pero nunca he visto de anonymous layout block.
¿Cuáles son esos objetos?

I learned the using of ObjectIdCollection, db.LayoutDictionaryId and continue but I never saw any anonymous layout block.
What kind of objects are there ?
Speaking English as a French Frog

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #24 on: July 07, 2009, 12:43:09 PM »
Una vez más, muchas gracias Luis.

He apprendido el uso de ObjectIdCollection, db.LayoutDictionaryId y continue, pero nunca he visto de anonymous layout block.
¿Cuáles son esos objetos?

I learned the using of ObjectIdCollection, db.LayoutDictionaryId and continue but I never saw any anonymous layout block.
What kind of objects are there ?

:)

For example, remove the IsAnonymous condition, and then in the drawing draw one dimension... it will count the anonymous block.

Also, on this line:

BlockTableRecord layoutBtr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead, false);

notice the false argument, this will ignore the recent erased blocks.

And also about the continue, we can use that here too:

 if (layoutBtr.IsAnonymous) continue;

To skip the anonymous blocks.

EDIT:.... --- I need to finish some work, and if someone can insert an anonymous block and tested... I'll be back.

« Last Edit: July 07, 2009, 12:56:17 PM by LE »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #25 on: July 07, 2009, 01:04:16 PM »
OK, I know anonymous blocks, but AFAIK, they're stored in the blocks collection, not in the layout block.

As far as I understand, layoutBtr refers to the layout block definition as the object got with the vlisp expression (I'm more comfortable with LISP):
(vla-get-Block (vla-item (vla-get-Layouts ...) "Layout1")).

If I'm true, the false parameter should have been here:

BlockReference blk = tr.GetObject(id, OpenMode.ForRead, false) as BlockReference;

EDIT: I tried both an exploded dimension and a dynamic bloc.
Only the dynamic bloc is founded whatever the IsAnonymous condition, even I found "*D2" and "*U4" in the block collection (BlockTable).
And it seems there's no need to the false parameter if a block reference have been erased.
« Last Edit: July 07, 2009, 01:11:52 PM by gile »
Speaking English as a French Frog

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #26 on: July 07, 2009, 01:07:42 PM »
OK, I know anonymous blocks, but AFAIK, they're stored in the blocks collection, not in the layout block.

As far as I understand, layoutBtr refers to the layout block definition as the object got with the vlisp expression (I'm more comfortable with LISP):
(vla-get-Block (vla-item (vla-get-Layouts ...) "Layout1")).

If I'm true, the false parameter should have been here:

BlockReference blk = tr.GetObject(id, OpenMode.ForRead, false) as BlockReference;

I need another coffee... and need to play a little more with C#.

Have been lately in the miserable ATL COM stuff... and that might be my excuse...

T.Willey

  • Needs a day job
  • Posts: 5251
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #27 on: July 07, 2009, 01:24:15 PM »
I don't think you need to check if the block is anonymous, because I don't think a layout could have an anonymous block associated with it.

I'm not sure if you would need to check if the block has been erased either, as it might not be in the layout dictionary, but a quick test could be done.  Just create a layout, and run the code.  Then delete the layout, and run the code.  Then you should be able to tell if the layout information is still in the dictionary.
Tim

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

Please think about donating if this post helped you.

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #28 on: July 07, 2009, 01:25:03 PM »
OK, I know anonymous blocks, but AFAIK, they're stored in the blocks collection, not in the layout block.

As far as I understand, layoutBtr refers to the layout block definition as the object got with the vlisp expression (I'm more comfortable with LISP):
(vla-get-Block (vla-item (vla-get-Layouts ...) "Layout1")).

If I'm true, the false parameter should have been here:

BlockReference blk = tr.GetObject(id, OpenMode.ForRead, false) as BlockReference;

EDIT: I tried both an exploded dimension and a dynamic bloc.
Only the dynamic bloc is founded whatever the IsAnonymous condition, even I found "*D2" and "*U4" in the block collection (BlockTable).
And it seems there's no need to the false parameter if a block reference have been erased.

Yep... did that too.... I'm glad that I don't code in C# for a living....  :-P

Spike Wilbury

  • Guest
Re: "Cleanest" Method For Selecting All Blocks in PaperSpace
« Reply #29 on: July 07, 2009, 01:26:47 PM »
I don't think you need to check if the block is anonymous, because I don't think a layout could have an anonymous block associated with it.

I'm not sure if you would need to check if the block has been erased either, as it might not be in the layout dictionary, but a quick test could be done.  Just create a layout, and run the code.  Then delete the layout, and run the code.  Then you should be able to tell if the layout information is still in the dictionary.

We (me) did that already Tim.... and you right.

thanks! to refresh my 3 brain cells... left