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

how to get number of objects in model space

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

how to get number of objects in model space

Hello

 

I have some code in a VBA program I'm trying to convert to .net that loops through all the entities in model space as in the code snip below.  What I can't figure out is how to get the total number of objects in model space at the start so I can use this info to advance a progress bar.  ie the equivilent of VBA's "ThisDrawing.ModelSpace.Count"

 

Thanks for any help

 

 

               Dim bt As BlockTable = tr.GetObject(DB.BlockTableId(), OpenMode.ForRead)
                Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                ' Loop for each entity in the modelspace
                For Each entId As ObjectId In btr
                    '<code sniped>
                    'advance progress bar
                Next

 

5 REPLIES 5
Message 2 of 6
Alfred.NESWADBA
in reply to: Anonymous

Hi,

 

there is no equivalent in dotNET for a BlockTableRecord, so you can enumerate through it (is that a correct english?) ... let's say scan through the BlockTableRecord to count it; or you have also access to the COM-based functions also in dotNET. As long as you don't access it to often within one function it will also be about no time-delay problem.

 

Here you have 3 versions, it you use 1 or 2 you have to add the references for COM-objects:

Autodesk.AutoCAD.Interop

Autodesk.AutoCAD.Interop.Common

 

Plus on top of the vb-file you need (for running the code below:

Imports Autodesk.AutoCAD

 

   <Runtime.CommandMethod("ADESK_CountModelSpace1")> _
   Public Shared Sub ADESK_CountModelSpace1()
      'using Document as COM-object
      Dim tAcadDoc As ApplicationServices.Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
      Dim tAcadDocCOM As Interop.AcadDocument = CType(tAcadDoc.AcadDocument, Interop.AcadDocument)
      MsgBox(tAcadDocCOM.ModelSpace.Count.ToString)
   End Sub
   <Runtime.CommandMethod("ADESK_CountModelSpace2")> _
   Public Shared Sub ADESK_CountModelSpace2()
      'using Modelspace-BlockTableRecord as COM-object
      Dim tAcadDoc As ApplicationServices.Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
      Dim tTrAct As DatabaseServices.Transaction = Nothing
      Try
         tTrAct = tAcadDoc.TransactionManager.StartTransaction
         Dim tBlTab As DatabaseServices.BlockTable = CType(tTrAct.GetObject(tAcadDoc.Database.BlockTableId, DatabaseServices.OpenMode.ForRead), DatabaseServices.BlockTable)
         Dim tModSp As DatabaseServices.BlockTableRecord = CType(tTrAct.GetObject(tBlTab(DatabaseServices.BlockTableRecord.ModelSpace), DatabaseServices.OpenMode.ForRead), DatabaseServices.BlockTableRecord)
         'using the BlockTableRecord as COM-object
         Dim tModSpCOM As Interop.Common.AcadModelSpace = CType(tModSp.AcadObject, Interop.Common.AcadModelSpace)
         MsgBox(tModSpCOM.Count.ToString)
      Catch ex As Exception
         MsgBox("Some exception occured" & vbNewLine & ex.Message)
      Finally
         If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing
      End Try
   End Sub
   <Runtime.CommandMethod("ADESK_CountModelSpace3")> _
   Public Shared Sub ADESK_CountModelSpace3()
      'counting by scan
      Dim tAcadDoc As ApplicationServices.Document = ApplicationServices.Application.DocumentManager.MdiActiveDocument
      Dim tTrAct As DatabaseServices.Transaction = Nothing
      Try
         tTrAct = tAcadDoc.TransactionManager.StartTransaction
         Dim tBlTab As DatabaseServices.BlockTable = CType(tTrAct.GetObject(tAcadDoc.Database.BlockTableId, DatabaseServices.OpenMode.ForRead), DatabaseServices.BlockTable)
         Dim tModSp As DatabaseServices.BlockTableRecord = CType(tTrAct.GetObject(tBlTab(DatabaseServices.BlockTableRecord.ModelSpace), DatabaseServices.OpenMode.ForRead), DatabaseServices.BlockTableRecord)
         Dim tEnum As DatabaseServices.BlockTableRecordEnumerator = tModSp.GetEnumerator
         Dim tCounter As Integer = 0
         Do While tEnum.MoveNext
            tCounter += 1
         Loop
         MsgBox(tCounter.ToString)
      Catch ex As Exception
         MsgBox("Some exception occured" & vbNewLine & ex.Message)
      Finally
         If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing
      End Try
   End Sub

 

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2024
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 3 of 6
augusto.goncalves
in reply to: Anonymous

hi David,

 

As you are using VB.NET, you can implicit call the AcadObject using LateBinding:

 

Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
Dim count as Integer = btr.AcadObject.Count

 

Hope this help.

 

Augusto Goncalves

Autodesk Developer Network

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 4 of 6
Anonymous
in reply to: Anonymous

thanks for the help

 

Message 5 of 6

This did not work for me. Visual studio is suggesting that the "operator '>' cannot be applied to operands of type 'method group' and 'int'. below is the relevant ode.

 

BlockTableRecord btrLayout = tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead) as BlockTableRecord;

                    if (btrLayout.AcadObject.Count > 0)
                    {
                        
                    }
Message 6 of 6
SENL1362
in reply to: BKSpurgeon

I am not sure about VB but for C# this can be used:
int btrjCnt = btr.Cast<ObjectId>().Count();

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

Post to forums  

Forma Design Contest


Autodesk Design & Make Report