function that uses argument as a variable name

function that uses argument as a variable name

Anonymous
Not applicable
1,089 Views
3 Replies
Message 1 of 4

function that uses argument as a variable name

Anonymous
Not applicable

Hello,

 

Well, I have created a Visual Basic macro that reads filtered info from a range of tables in a Qlikview application database and then it writes a Lisp program. In this table I have coordinates and variable names that will be used many times in this created Lisp program. What I dont know how to do is a function to receive this coordinates and "variable names" and use it as a variable name and not just its content. Well this will be my first function. Why I need to do it is because sometimes this macro is creating a Lisp program with 28 thousand lines, so my idea is to replace this linear code that repeats many commands in a function. So instead repeating command lines I'll call this funcion to reduce the code size inside the Lisp program and make it faster to the Autocad run it.

 

Examples of some lines that are created by this macro in a linear code:
(setq a1 ' (-477.949 370.679 0))
(setq b1 ' (-477.949 322.249 0))
(command "Line" a1 b1 "")
(setq a1b1_line (entlast))
(setq offpt (osnap a1 "NEA"))

 

It´s possible to receive a1 as an argument and use it as an variable name.

 

(defun aCoord (ax ay az variable)
'  where ax -477.949 ay 370.679 az0 and variable a1

           (setq "variable" (ax ay az))

' I need that the variable received as an argument be replaced by its contents a1, b1, c1, etc...

 

If someone understand my question and could help me...
Thanks in advance

0 Likes
Accepted solutions (1)
1,090 Views
3 Replies
Replies (3)
Message 2 of 4

dbroad
Mentor
Mentor
Accepted solution

To use variable in that context, use the set function rather than the setq function and quote the argument in the function call.

 

 

(defun foo (a b c)

  ....

  (set c (somefun a b))

 ...

)

 

calling function

(foo arg1 arg2 'arg3)

 

 

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 4

SeeMSixty7
Advisor
Advisor

I'm not entirely sure what you are asking, but it sounds like variable is a string that you are trying to pass in and set as a variable name. If that is the case then use something like this

 

(defun aCoord (ax ay az variable)

    (set (read variable) (list ax ay az)) ; SET is a bit different that SETQ

   ; at this point your string that you passed into aCorod function would be your variable name and would have be a list containing the values of ax ay az

)

 

examples of above

(setq myval (aCoord 1.0 2.0 3.0 "MYCUSTVAR"))

;at this point mycustvar would have a list value of (1.0 2.0 3.0)

(setq myval (aCoord 2.0 4.0 5.0 "MYOTHERVAR"))

;at this point myothervar would have a list value of (2.0 4.0 5.0

 

 

If you simply want your function to return a value to your calling function, you could use something like this.

 

(defun myfunction()

  (setq a 1.0 b 2.0 c 3.0)

  (setq myvariable (aCoord a b c))

  ; at this point myvariable would equal a list (1.0 2.0 3.0)

)

(defun aCoord (ax ay az)

  (setq returnvalue (list ax ay az))

  returnvalue

)

 

Basically the last thing evaluated in LISP function is what is returned to the calling function.

Message 4 of 4

Anonymous
Not applicable

Thank you!

 

(defun foo (x y z var)

(set var (setq Pt1 (list x y z)))

)

(foo 10 20 0 'a1)

 

result a1
(10 20 0)

 

 

 

0 Likes