Rs Find Closest Curve
Last changed: dale@mcneel.com-204.177.179.144

.
DeveloperRhinoScript
SummaryDemonstrates how to find the closest curve to test point using RhinoScript.

The following RhinoScript sample demonstrates how to find the closest curve to test point using RhinoScript.

  Sub FindClosestCurve


    Const rhPoint = 1
    Const rhCurve = 4


    'Dim arrCurves : arrCurves = Rhino.ObjectsByType(rhCurve)
    Dim arrCurves: arrCurves = Rhino.GetObjects("Select curves to test", rhCurve)
    If Not IsArray(arrCurves) Then Exit Sub


    Dim strPoint : strPoint = Rhino.GetObject("Select test point", rhPoint)
    If IsNull(strPoint) Then Exit Sub


    Dim arrPoint : arrPoint = Rhino.PointCoordinates(strPoint)


    Dim dblDistance : dblDistance = Null
    Dim strCurve : strCurve = Null
    Dim dblParameter : dblParameter = Null
    Dim arrPt : arrPt = Null


    Dim i, b, t, pt, d
    For i = 0 To UBound(arrCurves)
      b = vbFalse
      t = Rhino.CurveClosestPoint( arrCurves(i), arrPoint )
      If Not IsNull(t) Then
        pt = Rhino.EvaluateCurve( arrCurves(i), t )
        If IsArray(pt) Then
          d = Rhino.Distance(pt, arrPoint)
          If IsNull(dblDistance) Then
            b = vbTrue
          ElseIf (d < dblDistance) Then
            b = vbTrue
          End If


          If (b = vbTrue) Then
            dblDistance = d
            strCurve = arrCurves(i)
            dblParameter = t
            arrPt = pt
          End If
        End If
      End If
    Next


    If Not IsNull(dblDistance) Then
      Rhino.Print "Closest curve = " & CStr(strCurve)
      Rhino.Print "Curve parameter = " & CStr(dblParameter)
      Rhino.Print "Point = " & Rhino.Pt2Str(arrPt)
      Rhino.Print "Distance = " & CStr(dblDistance)


      Rhino.SelectObject strCurve
      Rhino.SelectObject Rhino.AddPoint(arrPt)
    End If


  End Sub