Issue saving variable globally when variable name is the product of a function

Issue saving variable globally when variable name is the product of a function

Anonymous
Not applicable
809 Views
2 Replies
Message 1 of 3

Issue saving variable globally when variable name is the product of a function

Anonymous
Not applicable

Hi,

I asked about this in a previous thread but I figured I should make a new topic so the answer is easier for others to find.

I am working on a pair of routines that basically take coordinates and rotation from a block and saves those globally, where the other routine can access those in another drawing and based on the name use those values to create a viewport looking at the location of the corresponding block.

The issue I'm currently having is that vl-bb-set doesn't seem to work with the function I use to define the variable name. I was wondering if it's possible to have the variable name for vl-bb-set to be based on the evaluation of the strcat. If not, if there is some kind of clever workaround to get it to work.

To explain the goal result further, the intent is for this to create 3 global variables each loop, xx1 yy1 and r1 on the first loop, xx2 yy2 r2 2nd loop etc.

 

Any help is appreciated, thanks.

 

(vl-load-com)
(defun C:VPP (/ sx xv yv vv vvl)
	(setq sx 0)
	(while (setq vv (car (entsel)))
		(setq sx (1+ sx))
		(setq vvl (entget vv))
		(setq xv (rtos (car (cdr (assoc 10 vvl)))))
		(setq yv (rtos (car (cdr (cdr (assoc 10 vvl))))))
		(vl-bb-set (read (strcat "r" " (rtos sx)")) (rtos (cdr (assoc 50 vvl))))
		(vl-bb-set (read (strcat "xx" " (rtos sx)")) xv)
		(vl-bb-set (read (strcat "yy" " (rtos sx)")) yv)
	)
(princ)
)
0 Likes
Accepted solutions (1)
810 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Just try to evaluate the following extract from your code :

(setq sx 0)
(strcat "r" " (rtos sx)")

Then try:

(read (strcat "r" " (rtos sx)"))

Another thing, you should'nt use (rtos ) for the variable values because when converting a real number into a string, you might loose the accuracy of the real number.

 

You do not need to call (vl-load-com) because your code does not use COM/ActiveX.

 

(defun C:VPP (/ sx xv yv vv vvl)
  (setq sx 0)
  (while (setq vv (car (entsel)))
    (setq sx (1+ sx))
    (setq vvl (entget vv))
    (setq xv (cadr (assoc 10 vvl)))
    (setq yv (caddr (assoc 10 vvl)))
    (vl-bb-set (read (strcat "r" (itoa sx))) (cdr (assoc 50 vvl)))
    (vl-bb-set (read (strcat "xx" (itoa sx))) xv)
    (vl-bb-set (read (strcat "yy" (itoa sx))) yv)
  )
  (princ)
)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

That worked, thank you very much 🙂

And the advice is greatly appreciated as well, I should be testing the bits I'm not sure about on their own. That'll help me out a lot next time I get stuck.

Cheers

0 Likes