Quick way to find number of entities in modelspace

<?xml encoding=”UTF-8″>By Balaji Ramamoorthy

LINQ provides an easy way to find the number of entities in modelspace without having to iterate on our own. The IEnumerator exposed by BlockTableRecord can be cast as IEnumerable<ObjectId> to find the count. Here is a code snippet :

 <span>using</span><span>  System.Linq;</span>
 
 Document doc 
 	= Application.DocumentManager.MdiActiveDocument;
 Database db = doc.Database;
 
 <span>using</span><span>  (Transaction tr </span>
 	= db.TransactionManager.StartTransaction())
 <span>{</span>
     BlockTable bt = tr.GetObject(
 			db.BlockTableId, 
 			OpenMode.ForRead) as BlockTable;
 
     ObjectId modelSpaceId 
 		= SymbolUtilityServices.GetBlockModelSpaceId(db);
 
     BlockTableRecord btr = tr.GetObject(
 			modelSpaceId, 
 			OpenMode.ForRead) as BlockTableRecord;
 
     System.Collections.Generic.IEnumerable<ObjectId> 
 		idCollection = btr.Cast<ObjectId>();
 
     doc.Editor.WriteMessage(
 		String.Format(<span>"<span>{</span>0<span>}</span> Model space count : <span>{</span>1<span>}</span>"</span><span> , </span>
 		Environment.NewLine, idCollection.Count<ObjectId>()));
 
     tr.Commit();
 <span>}</span>
 


Comments

5 responses to “Quick way to find number of entities in modelspace”

  1. I tried to adapt this for counting lines, or circle and so on.
    but using
    idCollection = btr.Cast();
    idCollection.Count();
    resulted in an error
    thank you

  2. edit:
    but using
    idCollection = btr.Cast;
    idCollection.Count();
    resulted in an error
    thank you

  3. can’t show “less then” and “greater then” symbols
    after “Cast” and “Count”, please imagine there’s “Line” within those two symbols

  4. With this code, you can only cast it as a collection of ObjectId, because that is the one the block table record enumerator lets you iterate.
    You can try using Editor.SelectAll with appropriate filter to identify the number of lines or circles.
    Regards,
    Balaji

  5. steeloncall Avatar
    steeloncall

    Steeloncall offering a wide range of steel materials online with the best prices in India. https://steeloncall.com/ms-unequal-angles https://steeloncall.com/brands/jsw-neo https://steeloncall.com/brands/KAY2

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading