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

Calculate the Area of a layer

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
AakashChopra
7999 Views, 11 Replies

Calculate the Area of a layer

How to Calcuate the area of a layer, through .NET API ?

11 REPLIES 11
Message 2 of 12

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 12

ya, or can you please guide me, how to calculate all entity/object wise of a layer ?

Message 4 of 12

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 12

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?

Message 6 of 12

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 12

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.

Message 8 of 12

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 -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 9 of 12

Thank you very much for the response and the code
Looking forward to seek help from you in the future...
Message 10 of 12

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

 

Message 11 of 12

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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 12 of 12
Hallex
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

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