How get the original block name from a Dynamic block

How get the original block name from a Dynamic block

oneMSN
Advocate Advocate
5,603 Views
4 Replies
Message 1 of 5

How get the original block name from a Dynamic block

oneMSN
Advocate
Advocate

Experimenting with dynamic blocks and I discovered an issue that has me stumped.

My code relies on identifying the type of block I am dealing with by looking at the BlockReference.Name

 

This works great for standard/regular blocks.

 

I thought I would test against a dynamic block, and discovered when one of the dynamic elements is changed AutoCAD's slight of hand replaces the original block with a clone but with a different Name value.

 

for example say I have a block named "MY_BLOCK"  after I adjust any of it's dynamic elements, If I then inspect the adjusted block I find that the Block Name is now "*U22"  and no longer "MY_BLOCK"

 

Is there any way to get the Original Block Name back from this new clone?

0 Likes
Accepted solutions (1)
5,604 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can get it from the BlockReference.DynamicBlockTableRecord.Name:

 

string name = blockRef.IsDynamicBlock ?
    ((BlockTableRecord)blockRef.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name :
    blockRef.Name;


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 5

oneMSN
Advocate
Advocate

Thanks I figured this out and created an extension method:

public static string EffectiveName(this BlockReference block)
{
    if (block == null)
        throw new ArgumentNullException(nameof(block));

    if(block.DynamicBlockTableRecord.Database.TransactionManager.TopTransaction == null)
        throw new Runtime.Exception(ErrorStatus.NoActiveTransactions);

    return ((BlockTableRecord)block.DynamicBlockTableRecord.GetObject(OpenMode.ForRead)).Name;
}
Message 4 of 5

Chuong.Ho
Advocate
Advocate

Do you know best way to can get with AutoCAD Interop ?

Chuong Ho

EESignature

0 Likes
Message 5 of 5

norman.yuan
Mentor
Mentor

It seems your question has nothing to do with the topic of this thread. You really should post your question in a new discussion thread.

 

Also, it is really a vague question: what does "to get AutoCAD interop" mean? Do you mean to use AutoCAD COM API in your AutoCAD .NET plugin project?

 

If it is really necessary to use COM API in the .NET API project, you can simply add references to COM API interop assembly/assemblies, then off you go. Better yet, if you only need very few COM API calls, you might want to use late binding (dynamic type in C#), no need to add references to COM API interop and your code is likely work OK with multiple AutoCAD version, as opposed to be tied to certain AutoCAD version, if you do add reference to COM API interop assembly to your project.

 

 

Norman Yuan

Drive CAD With Code

EESignature