Hi,
In AutoCAD graphical objects (objects derived from Entity type) belong to BlockTableRecord instances such as model and paper spaces and block definition (see this topic).
To get all entities in model space, for instance, you have to open the model space BlockTableRecord (which can be seen as an ObjectIds collection) and iterate through it.
You can get directly get some informations from the ObjectId.ObjectClass property (as the entity type), but if you need more, you have to open the Entity from its ObjectId typically using a transaction.
That said, here's an example to count all entities by type in a drawing model space.
C#
[CommandMethod("CMSE")]
public void CountModelSpaceEntities()
{
// get the database and editor of the active document
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
// start a transaction
using (var tr = db.TransactionManager.StartTransaction())
{
// open the model space block table record
var ms = (BlockTableRecord)tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
// iterate through the model space to count all objectIds grouped by their ObjectARX (C++) name
foreach (var item in ms
.Cast<ObjectId>()
.ToLookup(id => id.ObjectClass.Name))
{
ed.WriteMessage($"\n{item.Key}: {item.Count()}");
}
tr.Commit();
}
}
VB
<CommandMethod("CMSE")>
Public Sub CountModelSpaceEntities()
' get the database and editor of the active document
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
' start a transaction
Using tr As Transaction = db.TransactionManager.StartTransaction()
' open the model space block table record
Dim ms As BlockTableRecord = DirectCast(tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead), BlockTableRecord)
' iterate through the model space to count all objectIds grouped by their ObjectARX (C++) name
For Each item As IGrouping(Of String, ObjectId) In ms _
.Cast(Of ObjectId)() _
.ToLookup(Function(id) id.ObjectClass.Name)
ed.WriteMessage(vbLf & item.Key & ": " & item.Count())
Next
tr.Commit()
End Using
End Sub