Rhinoscript:global control flow
From ksteinfeWiki
Overview
When developing a large or complex script, it's often useful to break off a discrete set of actions into it's own separate process. Using this technique, it's possible to generalize routines such that they can be used over and over again, without cluttering up your code.
For example, working in pseudocode, let's imagine that we had a script which draws a row of triangles, increasing in size. You might accomplish this with a simple loop, and a series of calls to the Rhino.AddLine function:
create variable 'size' and set equal to 1.0
loop n from 0 to 10
'' figure out point coords
point1 = (0,0,n)
point2 = (size,0,n)
point3 = (size,0,n)
'' draw triangle
addLine(point1,point2)
addLine(point2,point3)
addLine(point3,point1)
join lines
'' iterate
size = size + 1
next
You can see from the structure of our loop that in each cycle we perform three basic operations: First, we define three points which will make up the vertices of our triangle. Next, we draw lines to rhino at these locations and join the lines together. Finally, we iterate our size variable such that the size of each triangle in succession gets larger. These three logical steps aren't coded in as discrete routines... we're simply thinking of them as discrete. However, using a subroutine we can describe them to the computer as discrete routines, and in the process gain some efficiency.
Again working in pseudocode, let's try that same script again:
main subroutine
create variable 'size' and set equal to 1.0
loop n from 0 to 10
'' draw this triangle
addTriangle( (0,0,n) , (size,0,n) , (size,0,n) )
'' iterate
size = size + 1
next
end main subroutine
addTriangle subroutine ( first point, second point, third point )
'' draw triangle
addLine( first point , second point )
addLine( second point , third point )
addLine( third point , first point )
join lines
end drawTriangle subroutine
This kind of structure is accomplished in rhinoscript via two keywords: Function and Sub. Let's begin with the simpler of these two.
Subs
TODO: describe subs
Sub Main()
Dim size
size = 1.0
Dim i
For i=0 To 10
addTriangle( array(0,0,n) , array(size,0,n) , array(size,size,n) )
size = size + 1.0
Next
End Sub
Sub addTriangle( p1 , p2 , p3 )
Call Rhino.AddLine(p1,p2)
Call Rhino.AddLine(p2,p3)
Call Rhino.AddLine(p3,p1)
''TODO: figure out how to join lines
End Sub
Functions
TODO: describe functions
