Sunday, September 30, 2007

Pattern Generation


What was seemingly a simple script took me all night to figure out. It takes any curve and divides it up into alternating sets of cut lines to be pulled out into perforated structures.





Option Explicit

Call CutPattern ()

Sub CutPattern ()

Dim blspace, varcut, arrOddCrv, arrEvenCrv, offset, temp

'get User Input

blspace = Rhino.GetReal ("size of space",.1, 0, .5)
varCut = Rhino.GetInteger ("Number of cuts",20)
offset = Rhino.GetReal ("Offset Amount", 1, 0, 1)
arrOddCrv = Rhino.GetObjects ("Pick Odd Curves", 4)
arrEvenCrv = Rhino.GetObjects ("Pick Even Curves", 4)
temp = 0

Dim k

For k = 0 To UBound(arrOddCrv)
Call Generate(arrOddCrv(k), blspace, varcut, temp)
Next

For k = 0 To UBound(arrEvenCrv)
Call Generate(arrEvenCrv(k), blspace, varcut, offset)
Next

End Sub

Function Generate(strCrv, BetweenCuts, CutNum, offset)

Dim Counter : Counter = 0
Dim Dist : Dist = 0
Dim ArrPoints(), ArrDomain, currentDist, adjSpace, adjCut
arrDomain = Rhino.CurveDomain(strCrv)
adjSpace = 1/CutNum*BetweenCuts
adjCut = ((1/CutNum)-adjSpace)*.5
dist = dist + (adjcut*offset)
'Rhino.Print "Curve domain: " & CStr(arrDomain(0)) & " to " & CStr(arrDomain(1))


Do While Dist < 1
ReDim Preserve arrPoint(counter)
currentDist = adjPar(arrDomain, dist)
arrPoint(counter) = Rhino.EvaluateCurve(strCrv, currentDist)
dist = dist + adjCut
Counter = counter + 1

ReDim Preserve arrPoint(counter)
currentDist = adjPar(arrDomain, dist)
arrPoint(counter) = Rhino.EvaluateCurve(strCrv, currentDist)
dist = dist + adjCut
Counter = counter +1

ReDim Preserve arrPoint(counter)
currentDist = adjPar(arrDomain, dist)
arrPoint(counter) = Rhino.EvaluateCurve(strCrv, currentDist)
dist = dist + adjSpace
Counter = counter +1
Loop

Dim i : i = 0
Dim LineArray(2)

'Add Lines
Do While i < UBound(arrPoint)
LineArray(0) = arrPoint(i)
i=i+1
LineArray(1) = arrPoint(i)
i=i+1
LineArray(2) = arrPoint(i)
i=i+1
Rhino.AddCurve LineArray
Loop

End Function

Function adjPar(crvDomain, dist)
adjPar = crvDomain(0) + dist * (crvDomain(1) -crvDomain(0))
End Function