How do I get a centroid of a brep using RhinoCommon?

Preferably with Python

if you need a proper volume centroid, use AreaMassProperties:

Rhino.Geometry.AreaMassProperties.Compute(x).Centroid

Or if it doesn’t have to be the centroid exactly, just a point near the center, use the faster

x.GetBoundingBox(False).PointAt(0.5,0.5,0.5)

(x is the Brep)

4 Likes

:slight_smile: I was looking at this namespace expecting to see `Rhino.Geometry.AreaMassProperties.Centroid(x)

How did you know you have to place Centroid after Compute?

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_AreaMassProperties_Compute.htm

1 Like

It is so intuitive right? :smiley:

1 Like

Per the documentation, it is a property of the return value, which is an object of type AreaMassProperties:

Regards
Jeremy

2 Likes

Constructs a Brep using the trimming information of a brep face and a surface.

1 Like

Hi @jeremy5,

If it is your first visit to MassAreaProperties class in the API documentation. And you do not look into the Compute method the intuition tells you to get the centroid by Rhino.Geometry.MassAreaProperties.Centroid.

It is how we (noobs) think because it is there in the autocompletion. There has to be perhaps some help when you type this that you have to call the Compute method. Or maybe MassAreaProperties.Centroid should be a method instead of a property.

1 Like

Hi @ivelin.peychev,

Yeah, but autocomplete is only dealing with syntax, not semantics. for the latter you have to RTFM.

Assuming I’m smart enough to find the MassAreaProperties class in the API (a big ask), then I want to know how to associate an instance with my Brep instance. I start by looking at the Constructors: uh, there isn’t one, so I probably nip off to the Brep class to see if M-A-P is one of its properties. Uh-uh, so back to M-A-P and look at the methods to find a way of making the association. Compute(Brep) looks promising (because there are lots of Compute overloads, so it’s probably pretty important): yep, I’m hot on the trail now. And that brings me to the point where I posted.

I admire your intent to use RhinoCommon, rather than RhinoScript. But by moving further away from the actual Rhino commands, intuition becomes weaker. Then you need to have an examining mindset.

(Oh, and a centroid is a property of an area or volume, and giving a method a property name is counter-intuitive :wink: - getCentroid() maybe.)

Best
Jeremy

2 Likes

Yes, now, this I call intuitive having getCentroid() method but not inside the M-A-P class but rather the appropriate Rhino.Geometry.someBrep(or surface).getCentroid()

@Helvetosaur flag? It’s impolite!

“Read the flippin’ manual”, nothing wrong with that :innocent:

1 Like

But an a M-A-P object could relate to all sorts of things (look at all the Compute overloads). You would have to scatter the method across all of them. By making it the responsibility of the M-A-P, you avoid that duplication and your code is easier to maintain.

Regards
Jeremy

Mitch knows exactly why I do this. I hope he learns his lesson. I didn’t flag, I don’t flag figures of speech. Or taking a word out of context, thus helping remove/hide a post. This is normal and if it is a targeted insult then it should be removed.

Yeah, you’re probably right, I don’t argue, just for a novice programmer it is difficult to comprehend and counter-intuitive.

Do you also know how to get the lengths of the edges of a brep?

problem%20get%20edge%20lenghts

You don’t use Compute for that, just get the edges and
https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_BrepEdge.htm
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Curve_GetLength.htm

At least that’s what I would do :slight_smile:

2 Likes

What can I do? Compute?
I have a rotated brep from which, starting from the middle, I want to make a point grid.
First, I need to know what the dimensions and the orientation of the brep are, then I can make the point grid.

try this. it is untested

my_brep = ?

list_of_edges = my_brep.Edges

for my_edge in list_of_edges:
    print my_edge.GetLength()
1 Like

Thank you :slight_smile:
Do you might know how to determine which edge is x,y, and z.?
With that, I know what direction is which length.