Region Centroid 面域的质心


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()));
}
 
https://forums.autodesk.com/t5/net/region-centroid/td-p/3193544

http://www.keanw.com/2015/08/getting-the-c ... ocad-region-using-net.html



Modifying AutoCAD support Path through .NET

ByCAD工具箱--材料表粘贴至Excel表格

欢迎关注微信公众账号ByCAD