In a previous post, Balaji showed how to find the name of a dynamic block from one of its block references. This post shows how to navigate in the other direction – finding all the references to a dynamic block.
As a reminder – when you manipulate a dynamic block, the changes are stored behind the scenes as anonymous blocks. An anonymous block is created for each different state of the dynamic block in the drawing. In ObjectARX, you use the AcDbDybBlockReference and AcDbDynBlockTableRecord classes to work with anonymous blocks. The extra functions they provided are rolled into the BlockReference and BlockTableRecord classes in .NET for simplicity.
Finding Dynamic Block References
[CommandMethod("selb")]
public void selectDynamicBlockReferences()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// Get the blockTable and iterate through all block definitions
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
// Get the block definition and check if it is dynamic
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);
if (btr.IsDynamicBlock)
{
// Get all anonymous blocks from this dynamic block
ObjectIdCollection anonymousIds = btr.GetAnonymousBlockIds();
ObjectIdCollection dynBlockRefs = new ObjectIdCollection();
foreach (ObjectId anonymousBtrId in anonymousIds)
{
// Get the anonymous block
BlockTableRecord anonymousBtr = (BlockTableRecord)trans.GetObject(anonymousBtrId, OpenMode.ForRead);
// And all references to this block
ObjectIdCollection blockRefIds = anonymousBtr.GetBlockReferenceIds(true, true);
foreach (ObjectId id in blockRefIds)
{
dynBlockRefs.Add(id);
}
}
// Output the results
ed.WriteMessage(String.Format("\nDynamic block \"{0}\" found with {1} anonymous blocks and {2} block references\n",
btr.Name, anonymousIds.Count, dynBlockRefs.Count));
}
}
trans.Commit();
}
}

Leave a Reply to Vivek KumarCancel reply