*Expert Elite*
Posts: 9,606
Registered: ‎06-29-2007
Message 11 of 12 (147 Views)

Re: Calculate the Area of a layer

10-08-2012 09:23 PM in reply to: AakashChopra



>> How can we find the area of particular objectID

That is already visible in my code above, if you have a given ObjectID you can start at this line:

Dim tCurve As Curve = CType(tTrAct.GetObject(tObjID, OpenMode.ForRead), Curve)

But make sure that the ObjectID is from an object that can be casted to a curve. Maybe the better syntax is then to use TryCast instead of CType:

Dim tCurve As Curve = TryCast(tTrAct.GetObject(tObjID, OpenMode.ForRead), Curve)
If tCurve isNot nothing then
   'if you land here you know that the object
   'can be casted to a Curve and so you can
   'get an area from it


- alfred -

Ingenieur Studio HOLLAUS ... www.hollaus.at
*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 12 of 12 (138 Views)

Re: Calculate the Area of a layer

10-09-2012 01:38 AM in reply to: AakashChopra

Try this code using Linq query:

     ' Add to Imports :
       ' Imports System.Linq
        <CommandMethod("tar")> _
        Public Shared Sub SubTotalAreasByLayer()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tr As Transaction = db.TransactionManager.StartTransaction
                Using tr

                    ' Get the BlockTable
                    Dim tb As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                    ' Get the current space block
                    Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead), BlockTableRecord)

                    ' Loop through the current space block
                    ' Create Group By Linq query
                    Dim cummareas = From id As ObjectId In btr _
                        Where (id.IsValid And id.IsResident And id.ObjectClass.DxfName Like "*POLYLINE*") _
                        Let curv = DirectCast(tr.GetObject(id, OpenMode.ForRead), Curve) _
                        Where (curv.Closed) _
                        Group curv By key = curv.Layer Into Group, ar = Sum(curv.Area) _
                        Select layer = key, subtotal = ar, name = Group

                    ' Get the query results  by group                  
                    For Each grouparea In cummareas
                        ' Get the grouping value and the total area
                        ed.WriteMessage(vbLf & "Layer: {0} ---> Total Area: {1}", grouparea.layer, grouparea.subtotal)
                    ' Commit transaction
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(vbLf & "Error: " & ex.Message & vbLf & "Trace: " & ex.StackTrace)
                ed.WriteMessage(vbLf & "Pokey!")
            End Try
        End Sub



Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.