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?
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to 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 -
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?
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?
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())); }
Thank you sir this has been inordinately helpful. I appreciate it very much, and so will future readers, I am sure.
regards
BK