.NET

Reply
*Expert Elite*
Alfred.NESWADBA
Posts: 8,516
Registered: ‎06-29-2007
Message 11 of 12 (108 Views)

Re: Calculate the Area of a layer

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

Hi,

 

>> 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 -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 12 of 12 (99 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
            Try
                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)
                    Next
                    ' Commit transaction
                    tr.Commit()
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(vbLf & "Error: " & ex.Message & vbLf & "Trace: " & ex.StackTrace)
            Finally
                ed.WriteMessage(vbLf & "Pokey!")
            End Try
        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community