.NET

Reply
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 1 of 12 (1,046 Views)
Accepted Solution

Calculate the Area of a layer

1046 Views, 11 Replies
10-07-2012 04:34 AM

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

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 -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 2 of 12 (1,043 Views)

Re: Calculate the Area of a layer

10-07-2012 04:36 AM in reply to: AakashChopra

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
-------------------------------------------------------------------------
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 3 of 12 (1,040 Views)

Re: Calculate the Area of a layer

10-07-2012 04:41 AM in reply to: Alfred.NESWADBA

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

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 4 of 12 (1,031 Views)

Re: Calculate the Area of a layer

10-07-2012 04:49 AM in reply to: AakashChopra

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
-------------------------------------------------------------------------
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 5 of 12 (989 Views)

Re: Calculate the Area of a layer

10-07-2012 11:56 PM in reply to: Alfred.NESWADBA

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?

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 6 of 12 (979 Views)

Re: Calculate the Area of a layer

10-08-2012 01:30 AM in reply to: AakashChopra

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
-------------------------------------------------------------------------
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 7 of 12 (972 Views)

Re: Calculate the Area of a layer

10-08-2012 02:31 AM in reply to: Alfred.NESWADBA

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.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,608
Registered: ‎06-29-2007
Message 8 of 12 (964 Views)

Re: Calculate the Area of a layer

10-08-2012 03:31 AM in reply to: AakashChopra

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
-------------------------------------------------------------------------
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 9 of 12 (951 Views)

Re: Calculate the Area of a layer

10-08-2012 04:57 AM in reply to: Alfred.NESWADBA
Thank you very much for the response and the code
Looking forward to seek help from you in the future...
Contributor
AakashChopra
Posts: 11
Registered: ‎10-07-2012
Message 10 of 12 (907 Views)

Re: Calculate the Area of a layer

10-08-2012 09:04 PM in reply to: Alfred.NESWADBA

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

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
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.