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.
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…
…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.