Do anonymous blocks need to be handled specially?

Do anonymous blocks need to be handled specially?

nshupeFMPE3
Advocate Advocate
662 Views
6 Replies
Message 1 of 7

Do anonymous blocks need to be handled specially?

nshupeFMPE3
Advocate
Advocate

I have some old code that I did not write that I am trying to understand. The basic idea is

Select blocks in model space

Recursively go through each block reference and look for a block that matches a name

count it

there is a part after the selection has been made where they do this

BlockTableRecord Abtr = new BlockTableRecord();
                                try
                                {Abtr = (BlockTableRecord)Trans.GetObject(br.AnonymousBlockTableRecord, OpenMode.ForRead);}
                                catch
                                {Abtr = (BlockTableRecord)Trans.GetObject(br.BlockTableRecord, OpenMode.ForRead);}

 

I'm wondering how neccessary it is to handle anonymous blocks seperately, as when I place a block and select it to process I see that it's BlockTableRecord property is the same as the AnonymousBlockTableRecord property?

What exactly are anonymous blocks, and do I need to treat them special in the code?

0 Likes
663 Views
6 Replies
Replies (6)
Message 2 of 7

Ed__Jobe
Mentor
Mentor

Anonymous blocks are blocks that are used by acad in special cases, like dimensions and dynamic blocks. Their name begins with an astersisk "*". In a dimension, the  geometry for the dimension is stored in an anonymous block named *U###. In dynamic blocks, each configuration that differs from the original block definition is contained in an anonymous block and it's EffectiveName property has the parent BlockTableRecord's Name. In reverse, the BlockReference.AnonymousBlockTableRecord property returns the anonymous block. You can also create your own anonymous blocks. Note that you provide the prefix and acad generates the number portion.

 

In the code sample you provided, the var name Abtr apparently stands for Anonymous Block Table Record. Since that's all you have shown, I can only assume that they wanted to process anonymous blocks somehow. You would have to show more code.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 7

norman.yuan
Mentor
Mentor

Firstly, the code you showed is bad code: I believe variable "br" is an BlockReference, and the purpose of the code is to find out the BlockReference's block definition (BlockTableRecord). So, the first line

BlockTableRecord Abtr = new BlockTableRecord();

 is simply wrong: why instantiate a new BlockTableRecord? The following lines of the code gets the BlockTableRecord in interest.

 

As for your question, it looks like you need a bit of study to know what is anonymous blocks (block definition/BlockTableRecord) and why AutoCAD create them. AutoCAD creates anonymous blocks for composite entities, such as Dimensions, dynamic blocks, as needed, and we should not try to mess with them in general.

 

In your case, you especially need to understand what a dynamic block is, and why BlockReference.BlockTableRecord and BlockReference.DynamicBlockTableRecord can be different, if the BlockReference is a dynamic one. That is, if you want to get block references by its name (the name of the block definition), you would use BlockReference.DynamicBlockTableRecord to trace back to its "designed" block definition and then its name, because BlockReference.Name property is the direct BlockTableRecord's name, which can be an anonymous block when the block is dynamic. It all sounds confusing until you fully understand the relationship between a dynamic BlockReference and its defining BlockTableRecord.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 7

nshupeFMPE3
Advocate
Advocate

Thank you for your replay @norman.yuan. I agree the code is bad! Thankfully I did not write it, but I am in charge of it...
I think the person who wrote it was a bit more confused about how what you described works. Thankfully I understood most of what you described. I think it will help me rewrite this to be much better.

0 Likes
Message 5 of 7

nshupeFMPE3
Advocate
Advocate
@norman.yuan one follow up I have is whether the description of dynamic blocks and how they relate to BlockTableRecords etc. is documented anywhere? Or is this just knowledge you have from working with these things?
0 Likes
Message 6 of 7

Ed__Jobe
Mentor
Mentor

One way to learn about these is to study the drawing structure of a dwg you are familiar with. Try @_gile Inspector app.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 7 of 7

nshupeFMPE3
Advocate
Advocate
I was not familiar with this @Ed__Jobe. Thanks for sharing, I'll look into it
0 Likes