I have a number of user-selected planar curves and I would like to sort them (in an array) based on their position along Z, as given by CurveAreaCentroid, for example.

How would I do it please? I'd really appreciate seeing it both in VBScript and in Python, for learning purposes.

Thank you.

Views: 354

Replies to This Discussion

There are many ways to do this depending on what you want. 

First thing to consider is what feature about the curve are you going to identify as being above the other.  I recommend using the bounding box method which encompasses the curve with a virtual box which returns and array of points to use as extremities.  The centroid is unreliable for this because a vertical curve which is only a millimeter above a horizontal curve will evaluate as being lower than the horz crv.  But if you use a bounding box and evaluate the corners max/min you can determine if the curve is above the other, above and overlapping, within a curve, below and overlapping , or below the curve.

Once you know the hierachy of the curves you can them switch them in the array accordingly.   Heres How I do it for curves which are on the x-axis where I test the y-coord of the start point for each curve. (For me, this worked fine because my curves are all vertical and only deviate on the z-axis so I only need to test y coords). 

[FYI - This toolpath was generated with offset of the model surface using the

Rhino.AddSrfSectionCrvs. But the problem is that it only works on each

of the exploded faces of the model so I had a lot of curves that weren't in order.

I used this function to sort them so they could be connected in a chain ]


You can use this same approach with bounding box corners as your start point.


Hope that helps...first time responding.


Function sortCrv2 (ByRef arrCurves, ByRef dblTolerance)

     Dim j
     Dim ptS1, ptE1, ptS2, ptE2 ' curve points
     Dim arrCrv : arrCrv = Rhino.JoinCurves(arrCurves, True, dblTolerance)

     Dim uB : uB = Ubound(arrCrv)
     Dim blnOrder : blnOrder = True
     Dim tempCrv

If isNull(arrCrv) Then
          sortCrv2 = arrCurves
     End If

If(uB >= 0) Then

For j=0 To uB
               ptS1 = Rhino.CurveStartPoint(arrCrv(j))
               ptE1 = Rhino.CurveEndPoint(arrCrv(j))

                    If(ptS1(1) < ptE1(1)) Then

                         ' if y start is > then y end - flip it as some curves are   backwards
                         Rhino.ReverseCurve arrCrv(j)
                    End If


Do While blnOrder
          blnOrder = False

For j=0 To uB - 1
                    tempCrv = arrCrv(j)
                    ptS1 = Rhino.CurveEndPoint(arrCrv(j))
                    ptS2 = Rhino.CurveEndPoint(arrCrv(j + 1))

                         If(ptS1(1) < ptS2(1)) Then

                              'Swap if greater than other - the magic
                               arrCrv(j) = arrCrv(j + 1)
                              arrCrv(j + 1) = tempCrv

                              blnOrder = True
                         End If



End If

SortCrv2 = arrCrv


End Function



© 2014   Created by McNeel Admin.

Badges  |  Report an Issue  |  Terms of Service