Identifying block name from the reference becomes bit tricky when dynamic blocks are involved. When users modify the dynamic block, AutoCAD creates an anonymous block first and then creates a reference to it in the drawing. Below code takes care of this scenario and finds the name of the block on selection of block reference.
[CommandMethod("blockName")]
static public void blockName()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions options =
new PromptEntityOptions("nSelect block reference");
options.SetRejectMessage("nSelect only block reference");
options.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult acSSPrompt = ed.GetEntity(options);
using (Transaction tx =
db.TransactionManager.StartTransaction())
{
BlockReference blockRef = tx.GetObject(acSSPrompt.ObjectId,
OpenMode.ForRead) as BlockReference;
BlockTableRecord block = null;
if (blockRef.IsDynamicBlock)
{
//get the real dynamic block name.
block = tx.GetObject(blockRef.DynamicBlockTableRecord,
OpenMode.ForRead) as BlockTableRecord;
}
else
{
block = tx.GetObject(blockRef.BlockTableRecord,
OpenMode.ForRead) as BlockTableRecord;
}
if (block != null)
{
ed.WriteMessage("Block name is : "
+ block.Name + "n");
}
tx.Commit();
}
}

Leave a Reply to Igor FUJSCancel reply