Site Tools


Splitting Curves into Multiple Segments

Developer: RhinoScript
Summary: Demonstrates how to split a curve into multiple segments using RhinoScript.

Question

I know that RhinoScript's SplitCurve function will split a curve into two pieces. But what if I need to split a curve into more than two pieces?

Answer

You can split a curve into multiple pieces by using the TrimCurve function. TrimCurve will trim a curve by removing portions of the curve outside of a specified interval. All you need to do is first divide the curve into the number of segments you want using DivideCurve. Then, loop through each division segment, create intervals from the results of CurveClosestPoint, and then trim the curve. For example:

 Option Explicit
 
 Sub CurveSplitter
 
   Const rhCurveObject = 4
 
   Dim sCurve
   sCurve = Rhino.GetObject("Select curve to split", rhCurveObject)
   If IsNull(sCurve) Then Exit Sub
 
   Dim nSegments
   nSegments = Rhino.GetInteger("Number of segments", 2, 2)
   If IsNull(nSegments) Then Exit Sub
 
   Dim aPoints
   aPoints = Rhino.DivideCurve(sCurve, nSegments)
   If Not IsArray(aPoints) Then Exit Sub
 
   Rhino.EnableRedraw False
 
   Dim i, t0, t1
   For i = 0 To UBound(aPoints) - 1
     t0 = Rhino.CurveClosestPoint(sCurve, aPoints(i))
     t1 = Rhino.CurveClosestPoint(sCurve, aPoints(i+1))
     Rhino.TrimCurve sCurve, Array(t0, t1), vbFalse
   Next
 
   Rhino.DeleteObject sCurve
   Rhino.EnableRedraw True
 
 End Sub


developer/scriptsamples/curvesplitter.txt ยท Last modified: 2015/09/14 (external edit)