# 090918_WrkShp_01

Option Explicit
‘Script written by <MathewStaudt>
‘Script version Friday, September 18, 2009 3:00:15 PM

Call Main()

Sub Main()

‘Get Closed Polylines And declare flow control variable
‘4 Is a selection filter (see help files)
Dim Polylines : Polylines = rhino.GetObjects (“Select tiangle”,4)
Dim distLim : distLim = 5 ‘tells script To Stop subdividing once a curve Is shorter than disLim
Dim i  ‘a counter

‘Loop through all polylines And subdivide them over And over again
For i=0 To Ubound (Polylines)
‘Function has 2 inputs : the curve To subdivide And the limit To how small the subdivision can go
Call SubDivide (Polylines(i),distLim)
Next

End Sub

Function SubDivide (cell,lim) ‘(Function inputs)

‘Declare all variables used In the Function
Dim midpts,cntr,crvpts
Dim j,a
Dim lngth
Dim line

midpts = f_getMidsFromPoly (cell) ‘custom Function that returns an array of midpoint coordinates
cntr = rhino.CurveAreaCentroid (cell) ‘method returns an array of 2 elements, we are only interested In the first: cntr(0) see help files
crvpts = rhino.CurvePoints (cell) ‘method returns the endpts of the polyline as an array 3d coordinates

For j=0 To ubound (midpts)

‘the New cell Is drawn by looping through all the midpoints,
‘drawing a line from that midpoint To the cntr, To the midpoint In front of him, To the curvepoint In front of him, And back To himself
‘a problem arrises when you Get To the last midpoint In the array ( midpts(Ubound(midpts) ) there Is no midpoint In font of him!
‘ To solve this problem you tell the script that If you are at the last midpoint : j= Ubound (midpoints)
‘ Then look To the first midpoint In the array ‘ a=0 otherwise look In front of you a=j+1
If j = Ubound (midpts) Then
a=0
Else
a=j+1
End If

‘draw New cell Then measure the length of it To check If you should continue To subdivide
line = rhino.AddPolyline ( array  ( midpts(j),cntr(0),midpts(a),crvpts(a),midpts(j) ))
lngth = rhino.CurveLength (line)
‘If the length of the New cell Is long enough, subdivide it again
If lngth > lim Then
Call SubDivide (line,lim)
End If
Next

End Function

Function f_getMidsFromPoly (plyln)

‘Is custom To Null a Function first
f_getMidsFromPoly = Null

‘explode curves, Get their midpoints
Dim explode : explode = rhino.ExplodeCurves (plyln)
Dim mids()
ReDim mids (ubound (explode))
Dim i

For i=0 To ubound (explode)
mids(i) = rhino.CurveMidPoint (explode (i))
Next

‘Fuction output Is defined as such:
f_getMidsFromPoly = mids

‘Delete exploded curves
rhino.DeleteObjects explode

End Function