C#: Create a polyline and copy it to different location


I am trying to create a polyline (line with 2 or more segments) and copy it (translate it) to a different location.

This is what I have so far:

        Point3d p1;
        Point3d p2;

        p1 =new Point3d(0,0,0);
        p2 = new Point3d(0,0,10);
        Line line1 = new Line(p1, p2);

        p1 = p2;
        p2[2] *= 2;
        Line line2 = new Line(p1, p2);

        List<Line> polyline1 = new List<Line>(5);
        for (int i = 0; i < polyline1.Count; i++)

So far so good. The lines are drawn in Rhino. Now I would like to take polyline1 and copy it to a different location, for example at x=20, x=30, etc.

Thank you very much :slight_smile:

To more or copy something, you will need to create a translation transformation

var xform = Rhino.Geometry.Transform.Translation(direction);

where direction is a vector that defines the magnitude and direction.

Tnd then apply this transformation to the geometry by calling it’s Transform member;

var new_line = line.Transform(xform);

Does this help?

– Dale

Please accept my apologies for not replying earlier.
This was very helpful. Thank you very much!

Just a small comment on your code. You’ve actually created two lines, not a polyline. If you want two, not joined lines, that is fine. If you want a polyline with two segments, you should take a look at the Polyline Class. If you just want to add it to the document, you can do that by calling doc.Objects.AddPolyline(YourPointList);

Yes Yes! Thank you for the comment! It is more than welcome :slight_smile:

1 Like

I was playing with this the other day, Can you explain how it works here?

// ---
			Rhino.Collections.Point3dList points = new Rhino.Collections.Point3dList(11);
			Rhino.Geometry.Polyline nc = Rhino.Display.DisplayPipeline.DrawPolyline(points);
			Rhino.Commands.Result rc = Rhino.Commands.Result.Failure;
    		if (nc != null && nc.IsValid)
      			if (doc.Objects.AddPolyline(nc) != Guid.Empty)
        			rc = Rhino.Commands.Result.Success;
    		return rc;

I was starting with this … http://developer.rhino3d.com/wip/samples/rhinocommon/add-nurbs-curve/



This code should make a bit more sense, as you really shouldn’t create a Polyline by calling .DisplayPipeline.DrawPolyline.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
  Result rc = Rhino.Commands.Result.Failure;

  Rhino.Collections.Point3dList points = new Rhino.Collections.Point3dList(11);
  points.Add(-7.090, 0, 5.86);
  points.Add(-6.00, 0, 5.86);
  points.Add(-6.00, 0, 4.02);
  points.Add(-3.84, 0, 4.02);
  points.Add(-3.84, 0, 3.03);
  points.Add(-4.234, 0, 3.03);
  points.Add(-4.234, 0, 0);
  points.Add(-4.835, 0, 0);
  points.Add(-4.835, 0, 3.0034);
  points.Add(-7.00, 0, 3.0034);
  points.Add(-7.00, 0, 5.86);

  Rhino.Geometry.Polyline polyline = new Rhino.Geometry.Polyline(points);
  if (polyline != null && polyline.IsValid)
    if (doc.Objects.AddPolyline(polyline) != System.Guid.Empty)
      rc = Rhino.Commands.Result.Success;
  return rc;

– Dale

Hi @dale thanks. That makes more sense. I am trying to learn by playing with the samples.


Here are lots of good samples here:

– Dale

1 Like