How to buy
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © 2025 Autodesk Inc. All rights reserved
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...
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
<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?
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.
1- CAD centroid;
2- my centroid;
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");
}
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.
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.
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.
Well, that being said, it seems to me like naming those points to be centroids is bit misleading. Thank you very much.
Best regards
Hi,
Because MPolygon is defined as a polyline and a hatch, grip is not centroid, but it's center of the hatch pattern.
If you draw your triangle and associative hatch to fill it, grip of hatch is the same as grip of mpolygon.
Olivier
Olivier Eckmann
How to buy
Privacy | Do not sell or share my personal information | Cookie preferences | Report noncompliance | Terms of use | Legal | © 2025 Autodesk Inc. All rights reserved
Type a product name