How to get the Points of a Polyline in C#?

Hello all,

I am quite new to developing my own Grasshopper components. I have done a few so far in Visual Studio an C#, but nothing really fancy. Right now I’d like to write a component that calculates the aspect ratio of a triangle. The triangle is given as a Polyline. Thus, I put it into the component as a “Curve” Parameter, since it looks the best fitting data type to me.

However, I really don’t have a clue how I could acces the curve itself or its properties, and do things with it. I mean, every curve has to be some how defined. Some deep down in the underwood of my computer, there must be formula, or a collection of corner points or something similar. And I don’t know how to get - in this case - the corner points.

That’s basicly my problem. And I have a feeling, that there is some basic knowledge I miss, or something that I am understanding quite wrong.

This is my code so far:

I know, it is still almost empty, but what the component shall do is quite easy:

  1. Check if the polyline is a triangle (Is it closed? Are all segments lines? Has it 4 Points?)
  2. Check if triangle is not degenerated (calculate, sort and compare side lengths)
  3. Calculate the aspect ratio and return it.

Can somebody please help me? I already spent hours in googling but I couldn’t find anything. How do I get the points of the polyline?

Cheers, Henry

Hi @henry.langner,

Polyline.Item property gives access to each polyline point.

The problem is: The “triangle” I have in the “SolveInstance” has no such thing as an “Item”-property or method.

Curve curve = null
if(!DA.GetData(0, ref curve))
    return;
if(!curve.IsClosed)
   throw new Exception("Curve must be closed");

if(curve.IsPolyline() )
{
    if(curve.TryGetPolyline(out Polyline pline))
    {
       if(pline.Count == 4)
       {
          Point3d p0 = pline[0];
          Point3d p1 = pline[1];
          Point3d p2 = pline[2];
          Point3d p3 = pline[3];
          // Do something
       }
       else if(pline.Count == 3)
       {
          // Do something
       }
    }
}
4 Likes

Yay! It works! Much apprechiated!

So, the mistake I made was to choose the wrong data type. I have to use the Rhino.Geometry.xyz data types instead of the Grasshopper.Kernel.Types.xyz data types. What are these for anyway?

GH has wrappers for the data types, all inherited from:
https://developer.rhino3d.com/api/grasshopper/html/T_Grasshopper_Kernel_Types_IGH_Goo.htm

They support type conversion, data descriptions, visualizations, baking, transformations, serialization… the common things in GH data. As a wrapper, it has the real data inside. When you retrieve the data using DA.GetData or when you send it using DA.SetData, you can choose whether to use the real data
type or its GH_Goo, because the GH API will convert it to you if needed.