How to Calcuate the area of a layer, through .NET API ?
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
Hi,
a layer (LayerTableRecord) is no geometrey object, so it has no area as property or to calculate.
Can you try to declare it in other words?
- alfred -
ya, or can you please guide me, how to calculate all entity/object wise of a layer ?
Hi,
>> or can you please guide me, how to calculate all entity/object wise of a layer ?
What objecttypes are you working with? Not every object has an area, e.g. Node, Text, Dimensions, BlockReference, also 3D-Solids and a lot more.
The easiest way would be to create a selectionset that is filtered to the layername you like. Then you can scan through the entities within the selectionset and verify if the objecttype is derived from type DatabaseServices.Curve and then if it's property "closed" = true, if so you can read the area-property.
That's the theory, but there are also a lot of obstacles to get passed like 3d-polyline or self-crossing polylines or how to work with solids, with polylines in blocks, objects not oriented plane to XY, ....
Can you describe how your drawings are structures (or upload a typical sample drawing you have to test your app) and what your goal is with this area-calculation?
- alfred -
We have a floor drawing file which contains few layers like Exterior Wall, Interior Wall, etc.
We have to calculate the square feet of the area covered by each layer.
Could you please guide us how to calculate the total area covered by these layers?
Hi,
>> We have to calculate the square feet of the area covered by each layer.
And again I don't know what object-types you are speaking from. Are these walls polylines or lines or walls created by Autodesk-Architecture?
BTW: do you really want to calculate the area of the wall(s)? ... or is it the area of the rooms you want to get?
- alfred -
The object types are polylines.
Here is the attached sample dwg file screen shot.
I have shown the layers that are present in the dwg file and the spaces that are present in those layers.
I want to calculate the area of the spaces that are associated with the layers.
Hi,
without island-calculation and to verify the results!
''' <summary>calcAreaPLinesByLayer scans through the modelspace and calculates areas of closed polylines (dxf-typename like *POLYLINE*)</summary> ''' <remarks></remarks> <Autodesk.AutoCAD.Runtime.CommandMethod("calcAreaPLinesByLayer")> _ Public Shared Sub calcAreaPLinesByLayer() Dim tCumArea As SortedList(Of String, Double) = New SortedList(Of String, Double) Dim tEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor Dim tDb As Database = Application.DocumentManager.MdiActiveDocument.Database Dim tTrAct As Transaction = Nothing Try tTrAct = tDb.TransactionManager.StartTransaction 'get BlockTable Dim tBlTab As BlockTable = CType(tTrAct.GetObject(tDb.BlockTableId, OpenMode.ForRead), BlockTable) 'get ModelSpace Dim tModSp As BlockTableRecord = CType(tTrAct.GetObject(tBlTab("*MODEL_SPACE"), OpenMode.ForRead), BlockTableRecord) 'scan through ModelSpace For Each tObjID As ObjectId In tModSp If (tObjID.IsValid) AndAlso (Not tObjID.IsErased) AndAlso (tObjID.ObjectClass.DxfName Like "*POLYLINE*") Then Dim tCurve As Curve = CType(tTrAct.GetObject(tObjID, OpenMode.ForRead), Curve) If tCurve.Closed OrElse (tCurve.StartPoint.DistanceTo(tCurve.EndPoint) < 0.0000001) Then 'ok, it's a poly and it's closed Try Dim tArea As Double = tCurve.Area Dim tLayer As String = tCurve.Layer If tCumArea.ContainsKey(tLayer) Then tCumArea.Item(tLayer) += tArea Else tCumArea.Add(tLayer, tArea) End If Catch ex2 As Exception tEd.WriteMessage(vbNewLine & "Error calculating area for Polyline Handle = &h" & Hex(tCurve.Handle.Value) & ", EX2 = " & ex2.Message) End Try End If End If Next 'now write the areas counted/cumulated For Each tItem As KeyValuePair(Of String, Double) In tCumArea tEd.WriteMessage(vbNewLine & tItem.Key & ": " & tItem.Value.ToString) Next Catch ex As Exception tEd.WriteMessage(vbNewLine & "Unknown Error occured, EX = " & ex.Message) Finally If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing End Try End Sub
- alfred -
I am having a similar kind of doubt,
How can we find the area of particular objectID for the same dwg file?
Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection() Dim acSSet As SelectionSet = acSSPrompt.Value Dim acObjId As ObjectId For Each acObjId In acSSet.GetObjectIds() acObjIdColl.Add(acObjId) Next
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 -
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'~