Boxes

Option Explicit
‘Script written by <Eric Tse>
‘Script copyrighted by <Eric Tse>
‘Script version Monday, October 19, 2009 9:11:38 PM

Dim numSegments : numSegments = 20
Dim segmentL : segmentL = 1.25
Dim numPts : numPts = 6
Dim fractScale : fractScale = 0.75
Dim fractNum : fractNum = 1
Dim boxScale : boxScale = 0.25
Dim fractalLim : fractalLim = 0.002

Call Main()
Sub Main()
 
 Dim strBox
 Dim arrCrv : arrCrv = rhino.GetObjects (“selcrv”,4)
 Dim arrCentroidNew
 Dim crv, divpts
 Dim i,j,k,l,m
 Dim pt, clpt,clparam
 ReDim arrpt(pt)
 Dim ptparam,plane
 Dim xvect,yvect,vect,zvect
 Dim Tvect, Tv, Zv, tangVect
 Dim Nvect : Nvect = array (0,1,0)
 Dim angle
 Dim newBox, xBox, arrBox
 Dim scale
 Dim vScale
 Dim fractal_output, vol
 
 rhino.EnableRedraw False
 
 For l = 0 To ubound (arrCrv)
  crv = arrCrv(l)
  divpts = rhino.DivideCurveLength (crv,segmentL*boxScale)
  For i = 0 To ubound(divpts)
   ptparam = rhino.CurveClosestPoint (crv,divpts(i))
   plane = rhino.CurvePerpFrame (crv,ptparam)
   For j = 0 To numPts
   
    xvect = rhino.Vectorrotate (plane(1),fx_Random (0, 360),plane(3))
    yvect = rhino.Vectorrotate (plane(1),fx_Random (0, 360),plane(3))
    zvect = rhino.VectorScale (plane(3),fx_Random (-0.15, 0.15))
    vect = rhino.VectorAdd (xvect,yvect)
    vect = rhino.VectorAdd (vect,zvect)
    vect = rhino.VectorScale(vect,boxScale)
    vect = rhino.vectorscale (vect,fx_Random (0.5,0.75))
   
    pt = rhino.PointAdd (divpts(i),vect)
    newBox = Rhino.InsertBlock (“box2”, pt)
    newBox = rhino.ScaleObject(newBox,pt,array(boxScale,boxScale,boxScale))
    ‘find closest point on curve’
    clparam = Rhino.CurveClosestPoint (crv, pt)
    clpt = rhino.EvaluateCurve (crv,clparam)
    ‘create vector’
    Tvect = Rhino.VectorCreate (pt, clpt)
    
    angle = 45
    tangVect = rhino.CurveTangent(crv,clparam)
    tangVect = rhino.VectorUnitize(tangVect)
    tangVect = rhino.VectorScale(tangVect,1)
    Tv = rhino.VectorUnitize (Tvect)
    Tv = Rhino.VectorAdd (Tv,tangVect)
    Tv = rhino.VectorUnitize (Tv)
    Tv = rhino.VectorScale(Tv,boxScale)
    
    scale = 1 * (numPts-(j/2))/numPts
    newBox = rhino.ScaleObject(newBox,pt,array(scale,scale,scale))
    
    ‘    For m = 0 To fractNum
    ‘     fractal_output = fractal (newBox,Tv,pt)
    ‘     newBox = fractal_output(0)
    ‘     Tv = fractal_output(1)
    ‘     pt = fractal_output(2)
    ‘     vol = fractal_output(3)
    ‘    Next
    
    arrBox = rhino.ExplodeBlockInstance(newBox)
    xBox = arrBox(0)
    
    vol = 1000
    Do
     fractal_output = fractal (xBox,Tv,pt)
     xBox = fractal_output(0)
     Tv = fractal_output(1)
     pt = fractal_output(2)
     vol = fractal_output(3)
    Loop While vol > fractalLim
   
   Next
  
  Next
 Next 

 rhino.EnableRedraw True
 
End Sub

Function fractal (xBox,Tv,cntr)

 Dim i
 Dim box, boxLength, newPt, arrVol, vol
 
 boxLength = 1
 
 box = rhino.CopyObject(xBox)
 box = rhino.MoveObject(box,Tv)
 newPt = rhino.PointAdd(cntr,Tv)
 box = rhino.ScaleObject (box,newPt,array(fractScale,fractScale,fractScale))
 
 Tv = rhino.VectorScale (Tv,fractScale)
 
 arrVol = rhino.MeshVolume(box)
 vol = arrVol(1)
 
 fractal = array(box,Tv,newPt,vol)
 
End Function

Function fx_Random (min, max)
 
 fx_Random = Rnd*(max-min) + min
 
End Function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s