Loop over all entities in a drawing

Top  Previous  Next

[CommandMethod("ModelSpaceIterator")]

public static void ModelSpaceIterator_Method()

{

  Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

  Database database = HostApplicationServices.WorkingDatabase;

  using (Transaction tr = database.TransactionManager.StartTransaction())

   {

      BlockTableRecord btRecord = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(database), OpenMode.ForRead);

      foreach (ObjectId id in btRecord)

       {

          Entity entity = (Entity)transaction.GetObject(id, OpenMode.ForRead);

 

          //Access to the entity

          string sMsg = string.Format("\nHandle: {0}", entity.Handle);

          ed.WriteMessage (sMsg);

       }

 

       // Save the changes and dispose of the transaction

       tr.Commit();

   }

}