You can use BlockTableRecord:: GetBlockReferenceIds API to get the object ids of the block references. You can get only direct reference (where the block is used directely, like in model space or inside another block) by passing true as first parameter.
[CommandMethod("BlockRefCount")]
static public void BlockRefCount()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptStringOptions opts =
new PromptStringOptions("Enter block name");
opts.AllowSpaces = true;
PromptResult blockName = ed.GetString(opts);
if (blockName.Status != PromptStatus.OK)
return;
using (Transaction tx =
db.TransactionManager.StartTransaction())
{
BlockTable blockTable = tx.GetObject(db.BlockTableId,
OpenMode.ForRead) as BlockTable;
if (blockTable.Has(blockName.StringResult))
{
BlockTableRecord block = tx.GetObject(
blockTable[blockName.StringResult],
OpenMode.ForRead) as BlockTableRecord;
//only direct reference
ObjectIdCollection ids =
block.GetBlockReferenceIds(true, true);
ed.WriteMessage("Number of reference is "
+ ids.Count.ToString() + "n");
}
tx.Commit();
}
}

Leave a Reply