Hi,
AFAIK there's no other way than iterating the model space.
The Database.Handseed will be much greater than the number of entities in model space: every non graphical object (symbol tables, symbol table records, dictionaries, xrecords, etc. also do have a handle.
Editor.SelectAll (used with a filter to get only model space entities) does iterate the modelspace (how could it be otherwise?) and is, from the tests I did slower than iterating the model space to fill an ObjectIdCollection.
Testing commands:
[CommandMethod("CountBySelection")]
public void CountBySelection()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
var sw = new Stopwatch();
sw.Start();
ObjectId[] ids = null;
for (int i = 0; i < 10; i++)
{
ids = ed.SelectAll(new SelectionFilter(new[] { new TypedValue(410, "Model") })).Value.GetObjectIds();
}
sw.Stop();
ed.WriteMessage($"\nFound {ids.Length} entities in {sw.ElapsedMilliseconds / 10} milliseconds");
}
[CommandMethod("CountByIteration")]
public void CountByIteration()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var sw = new Stopwatch();
sw.Start();
ObjectIdCollection ids = null;
for (int i = 0; i < 10; i++)
{
ids = new ObjectIdCollection();
using (var tr = db.TransactionManager.StartTransaction())
{
var ms = (BlockTableRecord)tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in ms)
{
ids.Add(id);
}
tr.Commit();
}
}
sw.Stop();
ed.WriteMessage($"\nFound {ids.Count} entities in {sw.ElapsedMilliseconds/ 10} milliseconds");
}
Results:
Commande: COUNTBYSELECTION
Found 106375 entities in 198 milliseconds
Commande: COUNTBYITERATION
Found 106375 entities in 31 milliseconds