PolylineCurve to Polyline

Hi,
i am sometimes confused about the different kinds of curves rhino commands return from functions. Specifically - mesh-plane intersection gives me PolylineCurves - and applying the ToPolyline method again returns a PolylineCurve. What is the best way to convert it to a simple Polyline?
Thanks!

If you are using RhinoCommon, feel free to use these Extension Methods for this purpose.

// usage
PolyLineCurve plc; // defined elsewhere
Point3dList list = plc.To<Point3dList>();
List<Point3d> anotherList = plc.To<List<Point3d>>();
Polyline polyline = plc.To<Polyline>();

Point3d[] arr = plc.ToArray();

public static class PolylineCurveExtensions
{

    /// <summary>
    /// Convert a polyline curve to a list of points
    /// </summary>
    /// <param name="plc"></param>
    /// <returns></returns>
    public static T To<T>(this PolylineCurve plc)
        where T : IList<Point3d>, new()
    {
        if (plc == null) throw new ArgumentNullException("plc");
        T list = new T();
        for (int i = 0; i < plc.PointCount; ++i)
        {
            list.Add(plc.Point(i));
        }
        return list;
    }

    /// <summary>
    /// Convert a polyline curve to an array of points
    /// </summary>
    /// <param name="plc"></param>
    /// <returns></returns>
    public static Point3d[] ToArray(this PolylineCurve plc)
    {
        if (plc == null) throw new ArgumentNullException("plc");
        Point3d[] pts = new Point3d[plc.PointCount];
        for(int i = 0; i < plc.PointCount; ++i)
        {
            pts[i] = plc.Point(i);
        }
        return pts;
    }
}

A Rhino.Geometry.Polyline object is nothing more than a collection of Rhino.Geometry.Point3d objects.

A Rhino.Geometry.Polyline is also a data member of a Rhino.Geometry.PolylineCurve object.

As mentioned above, if you want to create a Rhino.Geometry.Polyline object from a Rhino.Geometry.Polyline object, you can do the following:

PolylineCurve polyline_curve = ...; 
int point_count = polyline_curve.PointCount;
Polyline polyline = new Polyline(point_count);
for (int i = 0; i < polyline_curve.PointCount; ++i)
{
  polyline.Add(polyline_curve.Point(i));
}
1 Like

thats basically what i did, just was curious if there is any method to do it in one step.
thanks for the info / code!

I think there should be a method. So I’ve added this as a wish list item.

http://mcneel.myjetbrains.com/youtrack/issue/RH-30969

Hi Dale,

How does this differ from the [curve].TryGetPolyline() Method?
http://4.rhino3d.com/5/rhinocommon/?topic=html/M_Rhino_Geometry_Curve_TryGetPolyline.htm

-Willem

Hi Willem,

Thanks for pointing this out, and may be an option for the developer.

Several types of Curve can have the form of a polyline including a degree 1 NurbsCurve, a PolylineCurve, and a PolyCurve all of whose segments are some form of polyline. The Curve.IsPolyline method tests a curve to see if it can be represented as a polyline and, if so, returns the polyline form.

In this thread, the developer knows he has a PolylineCurve and just want to quickly get the polyline form.

– Dale

i am not sure if i follow correctly, but in case of my mesh-plane intersection, any result can only be a polyline, therefore a tryGetPolyline method should always work?

PolylineCurve polyline_curve = ...; 
Polyline polyline;
if (polyline_curve.TryGetPolyline(out polyline))
{
  // TODO...
}

This should be faster than using a for loop to get each point one at a time.