How to get center of mass/center of gravity/centroid of a solid body ?

Dear all,

I'm trying to find the center of gravity of a solid body, mainly based on the geometry ( I don't consider the density or anything else at all).

I tried to get it through MeasureBodies, which, according to the documentation, didn't seem like a bad idea.


NXObject *nullNXObject(NULL);
MeasureBodyBuilder *measureBodyBuilder1;
measureBodyBuilder1 = workPart->MeasureManager()->CreateMeasureBodyBuilder(nullNXObject);

measureBodyBuilder1->SetAnnotationMode(MeasureBuilder::AnnotationTypeShowDimension);

std::vector bodies1(1);
Body *body1(dynamic_cast(workPart->Bodies()->FindObject("SPLIT BODY(192)1")));
BodyDumbRule *bodyDumbRule1;
bodyDumbRule1 = workPart->ScRuleFactory()->CreateRuleBodyDumb(bodies1, true);

std::vector rules1(1);
rules1[0] = bodyDumbRule1;
measureBodyBuilder1->BodyCollector()->ReplaceRules(rules1, false);

std::vector massUnits1(5);
Unit *unit1(dynamic_cast(workPart->UnitCollection()->FindObject("SquareMilliMeter")));
massUnits1[0] = unit1;
Unit *unit2(dynamic_cast(workPart->UnitCollection()->FindObject("CubicMilliMeter")));
massUnits1[1] = unit2;
Unit *unit3(dynamic_cast(workPart->UnitCollection()->FindObject("Kilogram")));
massUnits1[2] = unit3;
Unit *unit4(dynamic_cast(workPart->UnitCollection()->FindObject("MilliMeter")));
massUnits1[3] = unit4;
Unit *unit5(dynamic_cast(workPart->UnitCollection()->FindObject("Newton")));
massUnits1[4] = unit5;

MeasureBodies *measureBodies1;
measureBodies1 = workPart->MeasureManager()->NewMassProperties(massUnits1, 0.99, measureBodyBuilder1->BodyCollector());

Measure *measure1;
measure1 = measureBodies1->CreateFeature();

Point3d newBary = measureBodies1->Centroid();

But well, my newBary point is always null, while on the graphic, one can see that the centroid point created is well placed.
Is that a bug ? Does any of you had this problem before ?

Thanks

And also, I tried to do it manually. From the body, get the edges, from the edges the vertices ...
I want to have the centroid of each part of a tube that I splitted into 2 parts.
As there is no edge on the rounded side, the centroid of the arc is in the middle of the line ...

This appears to be a bug relating to the use of the body collector. I've reported it to GTAC, if you want to get updates on the status of this, contact GTAC and have them add you to PR7176007.

The code will work if you pass in an array of bodies rather than the body collector.

Another alternative is to use the wrapper function for: UF_MODL_ask_mass_props_3d().

I would recommend UF_MODL_ask_mass_props_3d. But beware: it returns answers in WCS coordinates, unlike most nxopen functions.

If you have SNAP, the code is just

centroid = Snap.Compute.MassProperties(myBody).Centroid

I would recommend UF_MODL_ask_mass_props_3d. But beware: it returns answers in WCS coordinates, unlike most nxopen functions.

If you have SNAP, the code is just

centroid = Snap.Compute.MassProperties(myBody).Centroid