How to extract line segments from a curve?

Hi,

I have a surface and some lines on the surface, then I use a cutting plane to cut the surface and the lines. Points (ptList) are created when the cutting plane intersects lines, and curves (curves[]) are created when the cutting plane intersects the surface. I would like to create line segments passing through the points (ptList). I’m having trouble to get all intersection points (ptList) with the following code. Thanks, Ming

        var rc = Intersection.BrepBrep(brepPlane0, brep0, TOL, out Curve[] curves, out Point3d[] points);
       //Join curves
        var output = Curve.JoinCurves(curves, TOL);
        SortedDictionary<double, Point3d> t2Pt = new SortedDictionary<double, Point3d>();
        foreach (var curve in output)
        {
            foreach (Point3d pt in ptList.Reverse<Point3d>())
            {
                double t;

                if (curve.ClosestPoint(pt, out t, 0.5))
                {
                    t2Pt[t] = pt;
                    ptList.Remove(pt);
                }
            }
        }
        int k = 0;
        Point3d pointFrom = Point3d.Unset;
        foreach (KeyValuePair<double, Point3d> kvp in t2Pt)
        {

            Point3d pointTo = kvp.Value;
            if (k > 0)
            {
                Line l = new Line(pointFrom, pointTo);
                var object_id = doc.Objects.AddLine(l);
                var rhino_object = doc.Objects.Find(object_id);
                rhino_object?.Select(true);
            }
            pointFrom = pointTo;
            k++;
        }

Hi @ming.ma.usa,

Can you post some geometry that show an example of what you are trying to split and also what results you expect to achieve?

Thanks,

– Dale

pic2pic1

The first picture shows a line passing through all nodes, but didn’t break them up. The second picture shows two points on the curve were not found by ClosestPoint()

Hi @dale,

I’d prefer to examine a 3dm file. Can you up load one?

Thanks,

– Dale

Hi @ming.ma.usa,

I think this is what you want. Let me know if I am wrong.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  // Select cutting planes
  var go0 = new GetObject();
  go0.SetCommandPrompt("Select cutting planes");
  go0.GeometryFilter = ObjectType.Surface;
  go0.SubObjectSelect = false;
  go0.GetMultiple(1, 0);
  if (go0.CommandResult() != Result.Success)
    return go0.CommandResult();
      
  // Select stiffeners planes
  var go1 = new GetObject();
  go1.SetCommandPrompt("Select stiffening curves");
  go1.GeometryFilter = ObjectType.Curve;
  go1.SubObjectSelect = false;
  go1.EnablePreSelect(false, true);
  go1.DeselectAllBeforePostSelect = false;
  go1.GetMultiple(1, 0);
  if (go1.CommandResult() != Result.Success)
    return go1.CommandResult();

  var tolerance = doc.ModelAbsoluteTolerance;

  // Process each cutting plane
  for (var i = 0; i < go0.ObjectCount; i++)
  {
    var surface = go0.Object(i).Surface();
    if (null == surface)
      return Result.Failure;

    Plane plane;
    if (!surface.TryGetPlane(out plane, tolerance))
      continue;

    var points = new List<Point3d>(go1.ObjectCount);

    // Intersect the plane with each curve
    for (var j = 0; j < go1.ObjectCount; j++)
    {
      var curve = go1.Object(j).Curve();
      if (null == curve)
        return Result.Failure;

      var curve_intersections = Intersection.CurvePlane(curve, plane, tolerance);
      if (null != curve_intersections)
      {
        foreach (var intersection_event in curve_intersections)
        {
          if (intersection_event.IsPoint)
            points.Add(intersection_event.PointA);
        }
      }
    }

    if (points.Count >= 2)
    {
      // Orders a set of points so they will be connected in a "reasonable polyline" order.
      var sorted_points = Point3d.SortAndCullPointList(points, tolerance);
      var polyline_curve = new PolylineCurve(sorted_points);
      var object_id = doc.Objects.AddCurve(polyline_curve);
      doc.Objects.Select(object_id);
    }
  }

  doc.Views.Redraw();

  return Result.Success;
}

– Dale

Hi, Dale,
Thanks, but that’s still not what I want. I want to get a collection of line segments, each line segment passes only two points. See picture below! Ming
line-seg

Hi @ming.ma.usa,

Have you tried iterating through the intersection points and creating linet segments between every set of two?

– Dale

Hi, Dale,
I made the following changes from your code, and it almost works. I’m just missing a few corner points. What did I do wrong? (see picture below)
if (points.Count >= 2)
{
// Orders a set of points so they will be connected in a “reasonable polyline” order.
var sorted_points = Point3d.SortAndCullPointList(points, tolerance);
//var polyline_curve = new PolylineCurve(sorted_points);
//var object_id = doc.Objects.AddCurve(polyline_curve);
//doc.Objects.Select(object_id);
Point3d pointFrom = Point3d.Unset;
for (var k = 0; k < sorted_points.Length; k++)
{
Point3d pointTo = sorted_points[k];
doc.Objects.AddPoint(pointTo);
if (k > 0)
{
Line l = new Line(pointFrom, pointTo);
var object_id = doc.Objects.AddLine(l);
var rhino_object = doc.Objects.Find(object_id);
rhino_object?.Select(true);
}
pointFrom = pointTo;
}
}

Capture

Hi, Dale,
I got what I want. Many Thanks!
Ming