computational craft

solutions iteration exercises

back to tutorial

solution – exercise 01

Copy an Object 20 times, moving it further to the left each time:

Option Explicit

Call Main

Sub Main

     'variable declarations
     Dim strMessage, i, strObject, arrStart, arrEnd

     'user input
     strObject   = Rhino.GetObject("give me an object to copy around")

     'loop from 0 to 19
     For i = 0 To 19
          arrStart = array(-1,0,0)
          arrEnd   = array(i,0,0)

          strObject  = rhino.CopyObject(strObject,arrStart,arrEnd)

     Next 'end the loop	

End Sub

solution – exercise 02

Do the same with two objects:

Option Explicit

Call Main

Sub Main

     Dim strMessage, i, strObject, strObject2, arrStart, arrEnd

     strObject   = Rhino.GetObject("give me an object to copy around")
     strObject2  = Rhino.GetObject("give me another object to copy around")

     rhino.Print strObject
     rhino.Print strObject2

     'loop from 0 to 19
     For i = 0 To 19
	  arrStart = array(-1,0,0)
	  arrEnd   = array(i,0,0)

	  strObject  = rhino.CopyObject(strObject,arrStart,arrEnd)
	  strObject2 = rhino.CopyObject(strObject2,arrStart,arrEnd)

     Next 'end the loop	

End Sub

solution – exercise 03

Make the spacing between the second set of objects twice as great as the spacing between the first set of objects:

Option Explicit

Call Main

Sub Main

     Dim strMessage, i, strObject, strObject2, arrStart, arrEnd, arrEnd2

     strObject   = Rhino.GetObject("give me an object to copy around")
     strObject2  = Rhino.GetObject("give me another object to copy around")

     'loop from 0 to 19
     For i = 0 To 19
	  arrStart   = array(-1,0,0)
          arrEnd     = array(0,0,0)
          arrEnd2    = array(2*i,0,0)

          strObject  = rhino.CopyObject(strObject,arrStart,arrEnd)
          strObject2 = rhino.CopyObject(strObject2,arrStart,arrEnd2)

     Next 'end the loop

End Sub

solution – exercise 04

Make a grid of objects copied in both x and y axes rather than a single line of objects copied only along the x axis.

Option Explicit

Call Main

Sub Main

     Dim strObject, i, j, arrEnd

     strObject  = Rhino.GetObject("give me an object to copy around")

     'loop from 0 to 5 for x
     For i = 0 To 5

          'loop from 0 to 5 for y
          For j = 0 To 5

	       Dim arrStart : arrStart = array(-1,-1,0)
	       arrEnd     = array(i,j,0)

               Call rhino.CopyObject(strObject,arrStart,arrEnd)	

          Next 'end the j loop

     Next 'end the i loop

End Sub

solution – exercise 05

Now add a further nested loop to complete a grid of objects of a size determined by user inputs.

Option Explicit

Call Main

Sub Main()

	Dim intXSize_, intYSize_, intZSize_, strObject
        Dim i, j, k, arrStart, arrEnd

	intXSize_ = Rhino.GetInteger("Input the number of copies in X, please",5,2,50)
	If IsNull(intXSize_) Then Exit Sub

	intYSize_ = Rhino.GetInteger("Input the number of copies in Y, please",5,2,50)
	If IsNull(intYSize_) Then Exit Sub

	intZSize_ = Rhino.GetInteger("Input the number of copies in Z, please",5,2,50)
	If IsNull(intZSize_) Then Exit Sub

	strObject  = Rhino.GetObject("give me an object to copy around")
	If IsNull(strObject) 	Then Exit Sub

	'loop from 0 to 9
	For i = 0 To intXSize - 1

		For j = 0 To intYSize - 1

			For k = 0 To intZSize - 1

				arrStart = array(-1,-1,-1)
				arrEnd   = array(i,j,k)

				Call rhino.CopyObject(strObject,arrStart,arrEnd)

			Next 'end the k loop	

		Next 'end the j loop

	Next 'end the i loop

End Sub