Calculate Area in Rhino3dm.dll

Hello,
I need to calculate the Area and the Perimeter of some geometries from a Console app. I read properly the file and get all the geometries, but when i try to do some calculation i get stacked.

Is there any options like AreaMassGeometry of RhinoCommon that can be used? Or does anyone have some code to share?

I share you my idea and i explain why i got stacked. This is the code to get Area and Perimeter:

private Point3d[] GetAllPoints(Curve c ) 
        {
            Point3d[] points = new Point3d[c.SpanCount];

            for (int i = 0; i < c.SpanCount; i++)
            {
                points[i] = c.PointAt(c.SpanDomain(i).T0);
            }

            Circle circle;
            if (c.TryGetCircle(out circle)) //THIS PART IS ONLY FOR DEBUGGING
            {
                double circumference = circle.Circumference; // IT GIVES AROUND 75
                double perimeter = 0; 
                var n = c.ToNurbsCurve(); // curve or nurbs curve doesn't change, i've tried both
                
                for (int i = 0; i < n.SpanCount; i++)
                {
                    perimeter += n.SpanDomain(i).Length; // IT GIVES AROUND 78.
                }
                
                double circleArea = Math.Pow(circle.Radius, 2) * Math.PI; // IT GIVES AROUND 450
                double pointsArea = points.GetArea(); // IT GIVES AROUND 280
            }

            return points;
}

The problem is that when i try to get a circle, i see that my calculations are different than it should be. Especially in the perimeter part (because for the Area maybe there are some miscalculations)

Isn’t a good way to use the SpanDomain() ???

Thanks to all for the support

Hi @Samuele_Angeletti ,
To my knowledge rhino3dm has no AreaMassProperties class. This is exactly the additional feature which rhino3dm is not supposed to provide.
I didn’t understand what is not working in your code? The circle area and parameter should work as per your circleArea and circumference values.
Or are you talking about calculating these two for other non-circular curves?

Hello djordje.
The point is that i need to calculate the area for non regular shapes, filled with curves Joined during the use of Rhino.
I’ve taken some circles to understand if my calculates where correct for porting the same calculation in more complex shapes. I need it only for 2D shapes, so it is a bit easier and I understood that i’m actually doing it in the right way: the GetArea() is calculating the area by points. So for the circle I’m actually getting its Inside-Square area.
The problem is that in this way i don’t get the real full area of the circle (or more complex shapes).
So if for the Square i get a smaller area, this means that also for the other shapes i get a smaller area, but i need it strictly precise.

If you want to be precise without RhinoCommon, you need to make an area calculation yourself.
I’m not sure but I think Rhino3dm.dll should have conversion from NurbsCurve to BezierCurves.

Then you can convert each bezier curve to to polynomial expressions x(t) and y(t) (assuming that your curves are in the XY-plane). For these expressions you can find dx/dt and dy/dt by computing the derivative of the polynomial, and then compute x(t)*dy/dt to get x*dy then integrate x*dy over the domain [0,1] to give the area.

Does it sound complicated? Yes, I’m afraid that it is. But it works.
Note that this only works on non-rational curves. A circle is a rational curve.

1 Like

Thank you Menno.

This is the code i used in getArea():

public static double GetArea(this Point3d[] point3Ds)
        {
            if (point3Ds.Length <= 1) return 0;

            double area = 0;
            for (int i = 0; i < point3Ds.Length; i++)
            {
                int j = i + 1;
                if (j == point3Ds.Length) j = 0;
                Point3d pointA = point3Ds[i];
                Point3d pointB = point3Ds[j];
                area += pointA.X * pointB.Y - pointA.Y * pointB.X;
            }
            return Math.Abs(area / 2);
        }

Let’s assume i’ve this kind of Geometries. Does this code or yours be worthy?

image

Hi @Samuele_Angeletti ,
I would use the method Menno suggested.
If you like me, have no idea how to replicate it - then your method is precise enough.
Instead of SpanCount I would just use TryGetPolyline and then obtain polyline control points.