Adding To array?

Hey all ,

If i duplicate an object X time in loop , how can i store all these new objects in an array ?

I thought about Redim preserve , but i don’t find right way to write this.

In example below , i would like to store each new StrObject in an array , to be able to easily manipulate and identify Objects.

Any help ?

Thanks.


Option Explicit

Call Main()
Sub Main()
    
	Dim strObject, arrVect, arrXForm ,INum, arrLst
	Dim i
    
	strObject = Rhino.GetObject("Select object")
	
	If IsNull(strObject) Then Exit Sub
	
	INum = rhino.GetReal("number of copies ?", 10)
	
	If IsNull(INum) Then Exit Sub
	
	arrVect = Array(0, 10, 0)
	
	For i = 0 To INum - 1	
	
		arrXForm = Rhino.XformTranslation(arrVect)
		
		strObject = Rhino.TransformObject(strObject, arrXForm, True)
		
				
	Next
	
End Sub

Since you know exactly how long your list will be once you have gotten “INum” from the user input, you can simply Dim arrLst as a dynamic array of undefined length at the beginning and then ReDim it once before running your loop with the correct number.

You create an unsized dynamic array with this: Dim arrLst()
You redimension it like this: `ReDim arrLst(some_integer)

So your script looks like this:

Option Explicit
Call Main()
Sub Main()
    
    Dim strObject, arrVect, arrXForm ,INum, i
    Dim arrLst()
    
    strObject = Rhino.GetObject("Select object")
    
    If IsNull(strObject) Then Exit Sub
    
    INum = rhino.GetReal("number of copies ?", 10)
    
    If IsNull(INum) Then Exit Sub
    
    arrVect = Array(0, 10, 0)
    
    ReDim arrLst(INum-1)
    For i = 0 To INum - 1    
    
        arrXForm = Rhino.XformTranslation(arrVect)
        
        strObject = Rhino.TransformObject(strObject, arrXForm, True)
        
        arrLst(i) = strObject
                
    Next
    
End Sub

If you don’t know how long your list is going to be at the outset, then you are stuck using ReDim Preserve every time you want to add a position to your list. This is somewhat slow and resource consuming if you have very long lists.

Sample code:

Option Explicit
Call AddObjectsToListUntilDone()
Sub AddObjectsToListUntilDone()
    Dim strObj, n, arrJunk()
    n = -1

    Do
        strObj = Rhino.GetObject("Select an object or Enter when done")
        If IsNull(strObj) Then Exit Do
        n = n + 1
        ReDim Preserve arrJunk(n)
        arrJunk(n) = strObj        
    Loop    
End Sub

HTH, --Mitch

Hi Mitch,

Perfect .

Thanks !

Here are some miscellaneous array utilities you might find useful.

http://developer.rhino3d.com/guides/rhinoscript/array-utilities/

Hey,

Thanks , really intersting.