Monday, October 15, 2007

Population Script

This Script takes a bunch of lines and populates another curve to them. Recommended for lasercut.

Option Explicit

Call Populate()
Sub Populate()

Dim Object, ArrLines, i, Popstart, PopEnd, RefPoints, strScale, strOptions, RefObject

strOptions = Array ("y", "n")

Object = Rhino.GetObject ("Select Populate Object", 4)
RefObject = Rhino.GetObject ("Select Reference line for Population", 4)
ArrLines = Rhino.GetObjects ("Select target Curves", 4)
strScale = Rhino.GetString ("Do you want to Scale", "y", strOptions)

Popstart = Rhino.CurveStartPoint(RefObject)
PopEnd = Rhino.CurveEndPoint(RefObject)
RefPoints = Array(Popstart, Popend)

For i = 0 To UBound(arrLines)
Call Copypop(Object, arrlines(i), Refpoints, strScale)
Call Rhino.DeleteObject(arrlines(i))

End Sub

Function CopyPop(Object, Target, Refpoints, strScale)

Dim startPoint, endPoint, TargetPoint, PopCurve, tempCrv, refDist

startPoint = Rhino.CurveStartPoint(target)
endPoint = Rhino.CurveEndPoint(target)
TargetPoint = Array(startPoint, endPoint)
refDist = Rhino.Distance (Refpoints(0), RefPoints(1))

If strScale = "n" Then
Dim targetDist, scalefactor
targetDist = Rhino.Distance(TargetPoint(0), TargetPoint(1))
scalefactor = TargetDist/Refdist
tempCrv = Rhino.ScaleObject(Object, RefPoints(0),Array(scalefactor,1,1) ,vbTrue)
PopCurve = Rhino.OrientObject(TempCrv, RefPoints, TargetPoint)

PopCurve = Rhino.OrientObject(Object, RefPoints, TargetPoint, 3)
End If

End Function

Thursday, October 4, 2007

Still having a bug

Ok Sky, I removed the bug, no more error message, but when I try hiding objects, command cannot grab anything. Array after sorting is name-string array, not object-string array.

Another problem is that since SortStrings reads CurveLength number as string...
Array (32, 9,12,11, 25)
Sorted Array (11, 12, 25, 32, 9)

I guess I'll go back to my studio now...

Option Explicit

ShowCurve ()
Sub ShowCurve ()

Dim arrCurve, arrSorted, strTemp, i

'Get The Curves
arrCurve = Rhino.GetObjects ("select curves to rename", 4)

'Assign Curves to new array and rename objects
For i = 0 To UBound(arrCurve)
If Rhino.IsCurve(arrCurve(i)) Then

strTemp = Rhino.CurveLength(arrCurve(i))
arrCurve(i) = Rhino.ObjectName (arrCurve(i), "Curve" & CStr(strTemp))
Rhino.Print (arrCurve(i))
Rhino.Sleep 300

End If

'Sort lengths in ascending order
arrSorted = Rhino.SortStrings(arrCurve, True)

'Hide all Objects
'Rhino.Command ("SelAll")
'Rhino.Command ("Hide")
Rhino.HideObjects (arrSorted)

'Show the Objects in curve length order
For i = 0 To UBound(arrSorted)

strTemp = arrSorted(i)
Rhino.ShowObject (strTemp)
Rhino.Print "sorted:" & (arrSorted(i))
Rhino.Sleep 300


End Sub


Today's Script: Generate a sine wave, create spheres and scale them in order.

Option Explicit
'draw a sine wave using points

Sub SineWave()

Dim x, y, dblA, dblB, arr, dblStep, arrPoint(), arrSphere(), i, A, B, C
dblA = 0
dblB = 12
dblStep = 0.25
i = 0

For x = dblA To dblB Step dblStep
y = 2*Sin(x)

A = 0.02*i
B = 0.02*i
C = 0.03*i
arr = Array(A, B, C)

Call Rhino.AddPoint (Array (x,y,0))
ReDim Preserve arrPoint(i)
arrPoint(i) = Array (x,y,0)

ReDim Preserve arrSphere(i)
arrSphere(i) = Rhino.AddSphere (Array (x,y,0), 0.25)

Call Rhino.ScaleObject (arrSphere(i), arrPoint(i), arr)

Call Rhino.Sleep(30)
i = i+1

End Sub

Tuesday, October 2, 2007

Show Curves in Order of Length

For some reason I can't debug this code. Has problems with sortnumber variable type. Can anyone figure this out?

Option Explicit

ShowCurve ()

Sub ShowCurve ()

Dim arrCurve, i, arr(), temp, arr2()

'Get The Curves
arrCurve = Rhino.GetObjects ("select a curves to evaluate", 4)

'Assign Curves to new array and rename objects
For i = 0 To UBound(arrCurve)
If Rhino.IsCurve(arrCurve(i)) Then
temp = Rhino.CurveLength(arrCurve(i))
Rhino.Print (temp)
ReDim Preserve arr(i)
arr(i) = temp
Rhino.ObjectName arrCurve(i), "Curve" & CStr(temp)
End If

'Sort lengths in ascending order
arr2 = Rhino.SortNumbers(arr, vbTrue)

'Hide all Objects
Rhino.Command ("SelAll")
Rhino.Command ("Hide")

'Show the Objects in curve length order
For i = 0 To UBound(arr2)
Temp = CStr(arr2(i))
Rhino.ShowObject "Curve" & Temp
Rhino.Sleep 1000

End Sub

Monday, October 1, 2007


On the right is a script I wrote to scale down a curve to fit in certain area. The original is CurveToFitLength in Rhino scripting tutorial. Knowing both INPUTs

Rhino.CurveLength (strObject)
Rhino.CurveArea (strObject)

were available in Scripting Method, I thought it would be easy to write the same script but Area-based scaling instead of Length-based scaling. But I just realized Scripting Method also requires me to refer the OUTPUT. Unfortunately, CurveLength has dbl OUTPUT while CurveArea has arr OUTPUT.

Checkout the difference in above jpg.