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


#1

Hello.

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);
        polyline1.Add(line1);
        polyline1.Add(line2);
        
        for (int i = 0; i < polyline1.Count; i++)
        {
            doc.Objects.AddLine(polyline1[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:


(Dale Fugier) #2

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


#3

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


(Luis Fraguada) #4

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);


#5

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


#6

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

// ---
			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 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)
      			{
        			doc.Views.Redraw();
        			rc = Rhino.Commands.Result.Success;
      			}
    		}
    		return rc;

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

Thanks,
«Randy


(Dale Fugier) #7

@rhinorudi

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)
    {
      doc.Views.Redraw();
      rc = Rhino.Commands.Result.Success;
    }
  }
  return rc;
}

– Dale


#8

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

«Randy


(Dale Fugier) #9

Here are lots of good samples here:

– Dale