TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Cider on February 17, 2004, 02:08:39 AM

Title: is it clumsy?
Post by: Cider on February 17, 2004, 02:08:39 AM
hi everyone, i had a problem of defining the upper-limits of a
safearray at runtime, so i worked this code out:

Code: [Select]
(Setq p_data
"(setq pos_data (vlax-make-safearray vlax-vbstring '(0 . 5) '(0 . zappa)))"
  )
  (Setq p_data (vl-string-subst
(rtos (length liste))
"zappa"
p_data
      )
   )
(eval (read p_data))


the problem was that the length of "liste" changed depending on user input. so i wanted to tie the upper-limts of one dimension to the length of "liste".

my question: is there a more "elegant" way to get the same result? iīm a bit new to
this vl/vlax-stuff, maybe iīve overseen something in the vlisp documentation?

btw, this seems to be a great bb, i just discovered it a few days ago and gathered a
wealth of information already.

S! Cider
Title: is it clumsy?
Post by: SMadsen on February 17, 2004, 06:38:46 AM
Hi Cider,
You are aware that you're creating a two-dimensional array with this call, right?

You can simply let the LENGTH function return the upper limits directly to VLAX-MAKE-SAFEARRAY:

(setq p_data (vlax-make-safearray vlax-vbstring '(0 . 5) (cons 0 (1- (length liste)))))

Because you're starting at index zero and LENGTH returns the entire number of elements in the list, you'll have to decrement the upper bounds by one.


Say liste is a five element list:

(setq liste (list 1 2 3 4 5))

Starting lower bounds at zero and merely setting upper bounds to LENGTH of liste will create a 6x6 array:

Code: [Select]
(setq p_data (vlax-make-safearray vlax-vbstring '(0 . 5) (cons 0 (length liste))))
(vlax-safearray->list p_data)
(("" "" "" "" "" "") ("" "" "" "" "" "") ("" "" "" "" "" "")
 ("" "" "" "" "" "") ("" "" "" "" "" "") ("" "" "" "" "" ""))


That's 1 more element than you really want in order to fit it with liste. Here's a little something you can test the array on. Try changing the parameters so it fits your purpose.

Code: [Select]
(defun safeArrayTest (/ liste p_data i j)
  (setq liste '(1 2 3 4 5))
  (setq p_data (vlax-make-safearray vlax-vbstring
                 '(0 . 5) (cons 0 (1- (length liste))))
  )
  (setq i (vlax-safearray-get-u-bound p_data 1)
        j (vlax-safearray-get-u-bound p_data 2)
  )
  (repeat i
    (repeat j
      (vlax-safearray-put-element p_data i j
        (nth (1- j) liste))
      (setq j (1- j))
    )
    (setq i (1- i))
  )
  (print (vlax-safearray->list p_data))
  (princ)
)
Title: is it clumsy?
Post by: Keith™ on February 17, 2004, 06:48:27 AM
I knew Stig would have a good answer to this....
Title: is it clumsy?
Post by: Anonymous on February 17, 2004, 06:52:15 AM
thx, smadsen!

now thatīs what i call elegant :) .
had a similar approach last night but it didnīt work. now i know why: i qouted (cons 0 (1- length liste)). this, of course, would never work.

thx again, this is one great bb.

S!Cider
Title: is it clumsy?
Post by: SMadsen on February 17, 2004, 07:02:06 AM
Cider, you're welcome. Quoting it will work but only if you stay in love with EVAL :)