Get MPolygon centroid

tamara
Advocate

Get MPolygon centroid

tamara
Advocate
Advocate

Hello,

 

this is a question that I tend to return to every now and then. How can I get the centroid property of MPolygon using C#??

I have tried to find a solution to this, but at this moment I'm calculating the centroid point on my one. Is it possible that this is not exposed to .NET?

 

Greetings...

0 Likes
Reply
1,502 Views
7 Replies
Replies (7)

fieldguy
Advisor
Advisor

ObjectIdCollection mapcentroids = new acdb.ObjectIdCollection();
ObjectIdCollection polygons = new ObjectIdCollection() { mypline.ObjectId };  <--- i am only working with 1 pline
MapUtility.CreateCentroids(mapcentroids, polygons, "layer", "block name");

 

The Autodesk.Gis.Map namespace contains the MapUtility class. 

 

I use the above if the host is Map / Civil 3D.  There is a nice routine from Gilles Chanteau that I use for vanilla autocad

https://forums.autodesk.com/t5/net/get-the-area-midpoint-of-a-closed-polyline/m-p/2690567/highlight/...  

<edit> sorry.  I see you have mpolygon.  I don't remember trying that.  You might have to use explode.  Maybe only the outer ring is required?   

0 Likes

tamara
Advocate
Advocate

I already use Gille's example to calculate centroid on outer ring of the mpolygon, but it is slightly misplaced compared to the centroid point that AutoCAD gives me.

cent.jpg

1- CAD centroid;

2- my centroid;

 

0 Likes

fieldguy
Advisor
Advisor

I just tried using the mpolygon as the objectidcollection and it worked.  I can't explain the difference in position.  What is the distance between 1 and 2 in your pic?  

PromptEntityOptions peo = new PromptEntityOptions("\nSelect mpolygon: ");
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{

MPolygon mp = (MPolygon)tr.GetObject(per.ObjectId, OpenMode.ForRead);
MapApplication mapapp = HostMapApplicationServices.Application;
Project.ProjectModel mapprj = mapapp.ActiveProject;
ObjectIdCollection mapcentroids = new acdb.ObjectIdCollection();
ObjectIdCollection polygons = new acdb.ObjectIdCollection() { mp.ObjectId };
MapUtility.CreateCentroids(mapcentroids, polygons, "0", "block name");

}

0 Likes

tamara
Advocate
Advocate

I don't actually need centroids marked on dwg as points, I just need their locations so I can place labels there. Here is another image. It is an equilateral triangle where side length=1. cent2.jpg

pt 1   X= 0.5000 Y= 0.2887 Z= 0.0000

pt 2   X= 0.5000 Y= 0.4357 Z= 0.0000

pt 3   X= 0.5000 Y= 0.4330 Z= 0.0000

 

Pt 1 is my centroid location, that i calculated based on Gille's example.

Pt 2 is center grip point of mpolygon.

Pt 3 is Map centroid created through Topology-Create centroids function.

 

As it can be seen, all three are different. I'm wondering what the center grip point of mpolygon represents, and how centroid point are calculated in Map. 

 

Definition of centroid: 

The centroid of a triangle is the intersection of the three medians of the triangle (each median connecting a vertex with the midpoint of the opposite side).

 

Centroid that Map gives does not fit in this definition. 

0 Likes

fieldguy
Advisor
Advisor

I remember reading that map centroid generation is GIS "label position" friendly.  I was using Gilles method but sometimes the centroid is mathematically outside the boundary.  Map ensures that the centroid falls inside the boundary, so they are probably not accurate.

I can't find the source of this info now.  I guess the answer is it depends on what you need it for.  

0 Likes

tamara
Advocate
Advocate

Well, that being said, it seems to me like naming those points to be centroids is bit misleading. Thank you very much.

 

Best regards

0 Likes

O_Eckmann
Mentor
Mentor

Hi,

 

Because MPolygon is defined as a polyline and a hatch, grip is not centroid, but it's center of the hatch pattern.

image.png

If you draw your triangle and associative hatch to fill it, grip of hatch is the same as grip of mpolygon.

 

 

Olivier

Olivier Eckmann

EESignature

0 Likes

Type a product name