Friday, January 5, 2007

FitCurvetoLength Modified

























Here's Yuki's earlier script modified to identify the centerpoint of the curve with the method Rhino.BoundingBox



Option Explicit
'title: FitCurveToLength
'this script is to loop scaling a curve until it fits certain length

FitCurveToLength ()
Sub FitCurveToLength ()
Dim strCurveID
strcurveID = Rhino.GetObject("Select a curve to fit to length", 4, True, True)
If IsNull(strCurveID) Then Exit Sub

Dim arrBoundingBox
arrBoundingBox = Rhino.BoundingBox(strCurveID)

Dim arrCtr
Dim dblx, dbly, dblz
dblx =0.5*(arrBoundingBox(0)(0)+arrBoundingBox(1)(0))
dbly =0.5*(arrBoundingBox(1)(1)+arrBoundingBox(2)(1))
dblz =0.5*(arrBoundingBox(1)(2)+arrBoundingBox(5)(2))

arrCtr = array(dblx, dbly, dblz)

Dim dblLength
dblLength = Rhino.CurveLength(strCurveID)

Dim dblLengthLimit
dblLengthLimit = Rhino.GetReal("Length limit", 0.5*dblLength, 0.01*dblLength, dblLength)
If IsNull(dblLengthLimit) Then Exit Sub

Do
If Rhino.CurveLength(strCurveID) <= dblLengthLimit Then Exit Do
strCurveID = Rhino.ScaleObject(strCurveID, arrCtr, Array(0.95,0.95,0.95),True)
If IsNull(strCurveID) Then
Rhino.Print "Something went wrong..."
Exit Sub
End If
Loop

Rhino.Print "New curve length: " & Rhino.CurveLength(strCurveID)

End Sub