Identify the number of reference to a block

By Virupaksha Aithal

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();
 }
}

Comments

2 responses to “Identify the number of reference to a block”

  1. How do you get in an objectID collection the objectID of each unique block in modelspace? This is only blocks (i.e. not layouts etc).
    Thanks

  2. Thanks for the comment.
    I think best is to use the editor.SelectAll() API, as shown in blog http://adndevblog.typepad.com/autocad/2012/06/counting-number-of-unique-blocks-used-in-modelspace.html
    Thanks
    Viru

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading