.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Count entities list in drawing file using vb.net

5 REPLIES 5
Reply
Message 1 of 6
Anonymous
2423 Views, 5 Replies

Count entities list in drawing file using vb.net

Hi Guys,


I am Balaji, new for Autocad API (Vb.net) development. I have experience vb.net and not in Autocad API. I am trying to get the entities count list in the opened drawing file.


Like line, arc, circle, dimension, text and mtext count in the current opened drawing using vb.net application.


I have searched in AutoCAD .NET - Developer’s Guide and I couldn’t get clear idea for further process.


Can anyone help me to get the count using which properties/blocks have to use.


Thnks
Balaji

5 REPLIES 5
Message 2 of 6
_gile
in reply to: Anonymous

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


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6
Anonymous
in reply to: _gile

Hi Gile,

 

Thanks for your timely help and  reply.

 

I will check and update you.

 

Can you tell me the guide / tutorials for study,  bcoz i am just 1 month experience in autocad api (vb,net).  Here i have to develop more tools in autocad api oriented process(drawing / models / assembly).

 

Please help me.

 

Thnks,

Balaji

Message 4 of 6
Keith.Brown
in reply to: Anonymous

If you have not already then you should check out the Autodesk Developer's Network page and look at My First Autocad Plugin.  It is an excellent place to start your Autocad .NET journey.  The link below will take you to the web page.

 

http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112

Message 5 of 6
Anonymous
in reply to: _gile

Hi Gile,

 

Thanks lot for your help. Your given example worked and now i am generating the list. 

 

Thnks

Balaji

 

 

Message 6 of 6
Anonymous
in reply to: Keith.Brown

Hi keith,

 

 

Thanks for you provided URL. Its use full guide / info for automation oriented process.

 

 

Thanks,

Balaji.S

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost