A simple polyline generated in Grasshopper and rendered in Rhino

Get points from polyline in RhinoCommon/Grasshopper C# component

Polylines are highly lightweight forms of curves that have many useful applications in Rhino and Grasshopper.

What is a polyline?

A polyline is a list of points, nothing more. When Rhino reads in a polyline, it creates the curve by doing a dot-to-dot of the points.

It can also be thought of as an interpolation of points with degree = 1. The curve will always pass through the inputted points.

polyline

Extracting the points from a polyline

Let’s say you have a polyline as input in Grasshopper. You want to extract the list of points used to create that polyline within a C# script.

The most intuitive method is to look for a property of the polyline called ‘Points’ or something like that. But if we try and find it…

polyline_grasshopper_code

…there’s nothing there! So if there’s no property containing the points, where is this data?

Method 1: Get the whole list of points

As shown in the RhinoCommon SDK documentation, PolyLine inherits from the Point3dList, which itself inherits from the RhinoList class.

We can use the ToList() method to get the list of points:

  private void RunScript(Polyline pline, object y, ref object A)
  {
    var pts = new List<Point3d>();

    pts = pline.ToList();

    A = pts;
  }

Method 2: Get a single point

We can extract a single point from the ToList() method:

  private void RunScript(Polyline pline, object y, ref object A)
  {
    var pt = new Point3d();

    pt = pline.ToList()[0];

    A = pt;
  }

Or, even shorter, we can treat the polyline like a list itself and access the points directly:

  private void RunScript(Polyline pline, object y, ref object A)
  {
    var pt = new Point3d();

    pt = pline[0];

    A = pt;
  }

Thanks to Fraser Greenroyd for our joint effort in unpicking this deceptively unintuitive challenge – he also wrote about it here.

One thought on “Get points from polyline in RhinoCommon/Grasshopper C# component”

  1. First, love your site! Thank you for the effort of sharing your knowledge. Don’t know many places where to find info on how to use RhinoCommon and the C# Scripting component of Grasshopper.
    What about doing the same with a Brep? Extracting the vertices of a trimmed surface?

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: