Author Topic: is it clumsy?  (Read 3154 times)

0 Members and 1 Guest are viewing this topic.

Cider

  • Newt
  • Posts: 60
is it clumsy?
« 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
due to budget cuts, the lights at the end od the tunnel willbe turne d off.   

Win10 , Bricscad 10-18

SMadsen

  • Guest
is it clumsy?
« Reply #1 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)
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
is it clumsy?
« Reply #2 on: February 17, 2004, 06:48:27 AM »
I knew Stig would have a good answer to this....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Anonymous

  • Guest
is it clumsy?
« Reply #3 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

SMadsen

  • Guest
is it clumsy?
« Reply #4 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 :)