Grasshopper c# trim/split one curve with multiple cutting curves

Hi all,

I am trying to split one curve with a list of “trimming” curves. I’m having a bit of trouble wrapping my head around how to structure the code in c# without it becoming an endless loop…

Here is a quick screenshot of what I am trying to achieve.
Red = the curve to split, and Green = trimming curves.

Here is my code so far (this is part of a much longer c# script) :

  Curve[] intComplexCurve(Curve curveToSplit, List<Curve> trimmingCurves){
    
    List<Curve> workingCurves = new List<Curve>();
    workingCurves.Add(curveToSplit); // start with the input curve in the list
    
    for( int i = 0; i < trimmingCurves.Count; i++){
      
      for( int j; j < workingCurves.Count; j++){
        
        double tA;
        workingCurves[j].ClosestPoint(trimmingCurves[i].PointAtStart, out tA);
        
        double tB;
        workingCurves[j].ClosestPoint(trimmingCurves[i].PointAtEnd, out tB);
        
        Curve trimmedCurve = curveToSplit.Trim(tB, tA);
        
      }
    }
    
    // return an array of curves;
  }

Any ideas or help would be greatly appreciated!!

Do you want the general case of this ? BTW: that’s classic Recursion:

static double tol = RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
  public List<Curve> splitted, splitters;

  public void Split(){ // Recursion means that Peter calls Peter (who calls Peter (...)) ...
    List<Curve> tmp = new List<Curve>();

    foreach(Curve crv in splitted){// Recursion starts with one (or many: self ccx events) crv(s)
      crv.Domain = new Interval(0, 1);
      List<double> t = new List<double>();

      foreach(Curve splitter in splitters){
        var events = Rhino.Geometry.Intersect.Intersection.CurveCurve(crv, splitter, tol, tol);
        if(events != null && events.Count > 0){
          for(int i = 0; i < events.Count;i++){
            t.Add(events[i].ParameterA);
          }
        }
      }

      if(t.Any()){
        t.Sort();
        if(!t.Contains(0)) t.Insert(0, 0); // why? answers: The Lord, district 666, North Pole
        if(!t.Contains(1)) t.Add(1);       // ditto

        for(int i = 0; i < t.Count - 1;i++){
          Curve piece = crv.Trim(t[i], t[i + 1]);
          tmp.Add(piece);
        }
      }
      else tmp.Add(crv);
    }

    // ... until something happens and Peter calls Maria (a far better obption)
    if(tmp.Count == splitted.Count) return; // this means no result: adios amigos

    splitted = tmp;  // swap and continue until the End of Days (is nigh anyway).
    
    Split(); // A Method that calls itself is rather freaky (but life sucks)
  }

I.e. any Curve [ self intersecting or not, open/closed] VS any collection of splitters [see above].

Or do you want collections of Curves VS collections of splitters? (where Curves may - or may not - act as splitters as well). See for instance: all VS all:


Hey Peter, I was looking for the one above - thank you!

Curve_Split_EntryLevel_V1.gh (120.3 KB)

Hi Peter,

Would you mind sharing the second script which creates closed planar? I have a similar script that does similar things, but I feel like it’s not so robust. I’m curious about how you address the problem. Thank you!