Region Centroid

Region Centroid

leaveAlone
Enthusiast Enthusiast
7,814 Views
6 Replies
Message 1 of 7

Region Centroid

leaveAlone
Enthusiast
Enthusiast

I need to find center of Region (refer to as Centroid in MassProperties of acad, also known as Centre of gravity if 'element' mass is unique).
After few days of googling, searchin i this forum... I still can not figure it out how to get centroid point from Region in VB.NET.
For Solid3D is easy while it exposes this property, but Region does not reveal any properties except Area.

Has anybody manage to solve this and how?

0 Likes
Accepted solutions (1)
7,815 Views
6 Replies
Replies (6)
Message 2 of 7

Alfred.NESWADBA
Consultant
Consultant
Accepted solution

Hi,

 

does that help? With the Interop/COM-functionality you should get the value in this way:

 

'tRegion is the AutoCAD-Region object

Dim tCent() As Double = CType(CType(tRegion.AcadObject, _

    Autodesk.AutoCAD.Interop.Common.AcadRegion).Centroid, Double())

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
Message 3 of 7

leaveAlone
Enthusiast
Enthusiast

Thanks Alfred

Thats it!!!!!

0 Likes
Message 4 of 7

BKSpurgeon
Collaborator
Collaborator

Hi Alfred,

 

I cannot seem to access Autodesk.AutoCAD.Interop.Common.AcadRegion via intellisense in Visual Studio.

 

Dim tCent() As Double = CType(CType(tRegion.AcadObject, _

 

CType isn't coming up in Intellisence either? 

 

I must be doig something wrong? 

0 Likes
Message 5 of 7

BKSpurgeon
Collaborator
Collaborator

HI folks

 

Worked out that Ctype doesn't work in C#.

 

And to get to the COM interop: http://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/AutoCAD-NET/files/GUID...

 

but is there a way to access the centroid properties without resorting to COM/interop and using simply native .NET?

 

 

0 Likes
Message 6 of 7

_gile
Consultant
Consultant

BKSpurgeon a écrit :
but is there a way to access the centroid properties without resorting to COM/interop and using simply native .NET?

 

 


You can use COM whitout adding references to the COM libraries by using late binding.

 

If you're using C# and targeting Framework 4.0 or higher (the Frameork 4.0 is installed with AutoCAD 2012), you can use the dynamic type which allows to write a code very similar to the one using early binding:

 

dynamic acadRegion = region.AcadObject;
double[] centroid = acadRegion.Centroid;

 

For prior Frameworks, you can use late binding with Reflection:

 

object acadRegion = region.AcadObject;
double[] centroid = (double[])acadRegion.GetType().InvokeMember(
    "Centroid", System.Reflection.BindingFlags.GetProperty, null, acadRegion, null);

 

With VB, I'm not certain but it seems to me that the late binding is implicit with the Object type while Option Strict is Off, so you should simply write (not tested):

 

Dim acadRegion As Object = region.AcadObject
Dim centroid() As Double = acadRegion.Centroid

 

 

If you really do not want to use COM.

 

Since AutoCAD 2014 the Region class offers a new method: AreaProperties() which returns a RegionAreaProperty object which has a Centroid property.

An example to get the centroid of a region lying on the WCS XY plane:

 

Point3d origin = Point3d.Origin;
Vector3d xAxis = Vector3d.XAxis;
Vector3d yAxis = Vector3d.YAxis;
Point2d centroid = region.AreaProperties(ref origin, ref xAxis, ref yAxis).Centroid;

 

For prior versions of AutoCAD, you can create a Solid3d by extruding the region, get the solid centroid and move it back to the region plane:

 

Point3d centroid;
using (Solid3d solid = new Solid3d())
{
    solid.Extrude(region, 2.0, 0.0);
    Point3d solidCentroid = solid.MassProperties.Centroid;
    centroid = solidCentroid.TransformBy(Matrix3d.Displacement(region.Normal.Negate()));
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 7

BKSpurgeon
Collaborator
Collaborator

Thank you sir this has been inordinately helpful. I appreciate it very much, and so will future readers, I am sure.

 

regards

 

BK

0 Likes