How To Get Dynamic Block from Anonymous Block

<?xml encoding=”UTF-8″>By Madhukar Moogala

 A common request from developers is how to retrieve the original Dynamic Block reference from a given anonymous     block. This is useful when working with AutoCAD&rsquo;s Dynamic Blocks, as modifying or identifying the source Dynamic     Block definition can be critical in automation and customization workflows.      AutoCAD stores a reference to the Dynamic Block inside an anonymous block using extended entity data (XData). The     following C# function demonstrates how to extract this reference.   

public static Handle GetDynamicBlockHandleFromAnonymousBlock(BlockTableRecord btr)
{
if (!btr.IsAnonymous)
return ObjectId.Null.Handle;
Handle btrHand = btr.ObjectId.Handle;
ResultBuffer rb = btr.GetXDataForApplication("AcDbBlockRepBTag");
if (rb == null)
return ObjectId.Null.Handle;
foreach (TypedValue tv in rb)
{
if (tv.TypeCode == 1005 && tv.Value is string strValue)
{
long nHandle = Convert.ToInt64(strValue, 16);
return new Handle(nHandle);
}
}
return ObjectId.Null.Handle;
}


Comments

3 responses to “How To Get Dynamic Block from Anonymous Block”

  1. how is this different than using AcDbDynBlockTableRecord?
    Thanks

  2. It is just another way to get a DynamicBlock for a given anonymous block, if it exists, probably more efficiently.
    May be you can do a benchmark test ;)

  3. Thanks, I was just wondering, I’m all for multiple ways of doing things :)

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading