After you create an array using vlax-make-safearray, you can use either vlax-safearray-fill or vlax-safearray-put-element to populate the array with data.
The vlax-safearray-fill function requires two arguments: the variable containing the array you are populating and a list of the values to be assigned to the array elements. You must specify as many values as there are elements in the array.
For example, the following code populates a single-dimension array of three doubles:
(setq point (vlax-make-safearray vlax-vbDouble '(0 . 2))) (vlax-safearray-fill point (list 100 100 0)) #<safearray...>
The contents of the array can be displayed in list form with the vlax-safearray->list function:
(vlax-safearray->list point) (100.0 100.0 0.0)
If a value is not specified for every element in the array, vlax-safearray-fill results in an error.
When working with a multi-dimensional array, you must pass a list of lists to vlax-safearray-fill, with each list corresponding to a dimension. For example, the following statement assigns values to a two-dimension array of strings that contains three elements in each dimension:
(setq mat2 (vlax-make-safearray vlax-vbString '(0 . 1) '(1 . 3))) (vlax-safearray-fill mat2 '(("a" "b" "c") ("d" "e" "f"))) #<safearray...>
Use vlax-safearray->list to confirm the contents of mat2:
(vlax-safearray->list mat2) (("a" "b" "c") ("d" "e" "f"))
The vlax-safearray-put-element function can be used to assign values to one or more elements of a safearray. The number of arguments required by this function depends on the number of dimensions in the array. The following rules apply to specifying arguments to vlax-safearray-put-element:
For example, the following code populates a single-dimension array of three doubles:
(setq point (vlax-make-safearray vlax-vbDouble '(0 . 2))) #<safearray...> (vlax-safearray-put-element point 0 100) (vlax-safearray-put-element point 1 100) (vlax-safearray-put-element point 2 0)
If you need to change a value of an element in the array, you can make a call to vlax-safearray-put-element again. The following changes the second element of the array to a value of 50:
(vlax-safearray-put-element point 1 50)
The following example creates and populates a two-dimension array of strings. The first dimension of the array starts at index 0, while the second dimension starts at index 1:
(setq mat2 (vlax-make-safearray vlax-vbString '(0 . 1) '(1 . 3))) #<safearray...> (vlax-safearray-put-element mat2 0 1 "a") (vlax-safearray-put-element mat2 0 2 "b") (vlax-safearray-put-element mat2 0 3 "c") (vlax-safearray-put-element mat2 1 1 "d") (vlax-safearray-put-element mat2 1 2 "e") (vlax-safearray-put-element mat2 1 3 "f")
You can use vlax-safearray->list to confirm the contents of the array:
(vlax-safearray->list mat2) (("a" "b" "c") ("d" "e" "f"))