samifox Posted May 12, 2014 Posted May 12, 2014 Hi i use lots of predefined global variables that control the program flow. i wonder how autolisp reffer the variable and function with the same name? (setq REBARLOWERDIA "3£16" ;_REBAR UPPER DIAMETER ) (defun c:REBARLOWERDIA () (setq REBARLOWERDIA (getString (strcat "Set rebar diameter and frequncy [" REBARLOWERDIA "]" ) ) ) ) is should be like this? Thanks Shay Quote
MSasu Posted May 12, 2014 Posted May 12, 2014 A good programming practice will be to use asterisks to build global variable names to easily distinguish them: (setq [color=blue]*[/color]REBARLOWERDIA[color=blue]*[/color] "3£16" ;_REBAR UPPER DIAMETER ) Quote
samifox Posted May 12, 2014 Author Posted May 12, 2014 A good programming practice will be to use asterisks to build global variable names to easily distinguish them: (setq [color=blue]*[/color]REBARLOWERDIA[color=blue]*[/color] "3£16" ;_REBAR UPPER DIAMETER ) thanx i wonder how autolisp reffer the variable and function with the same name? Quote
cwake Posted May 12, 2014 Posted May 12, 2014 i wonder how autolisp reffer the variable and function with the same name? In a nutshell, if you try to use the same symbol name for a function and a global variable it won't work (CAVEAT - that comment is in the context of the drawing document namespace, which is what you are referring to). The symbol will contain whatever you last set it as, eg. a string, a real, an integer, or a function. It can't be two things simultaneously. If you are experiencing ANY confusion understanding this, perhaps you haven't realised that "C:REBARLOWERDIA" does NOT refer to the same symbol as "REBARLOWERDIA". Quote
BlackBox Posted May 12, 2014 Posted May 12, 2014 FWIW - You may also find PRAGMA to be of interest. Cheers Quote
Bhull1985 Posted May 12, 2014 Posted May 12, 2014 I'm not sure your question samifox, are you asking if you did that correctly? It appears so.... If you were trying to see if autolisp DISTINGUISHES between "C:REBARLOWERDIA" and "REBARLOWERDIA", then as clint mentioned yes it does. They are two different atoms, if I understand correctly. Quote
samifox Posted May 14, 2014 Author Posted May 14, 2014 hi im writing a function (like setVar()) , for my own global variable for easily updating them as needed. my problem is to access the global variables without actually call them, this the code i manage to write so far, thanks Shay (defun setValue (var val) (if (= (type var) 'STR) (set var (getstring (strcat "Enter new value for ["var"]<"'existValue">"))) ) ) (setq *upperRebarTitle* "3£12" ;_REBAR UPPER DIAMETER ) ;_ end of setq (defun C:UPRREBARTLT () (setValue "UPRREBARTLT" "3x20") ) ;_ end of defun Quote
MSasu Posted May 14, 2014 Posted May 14, 2014 I will write it like this: (defun setValue( var val / tmp1 tmp2 ) (if (not val) (progn (or (setq tmp1 (eval (read var))) (setq tmp1 "3£12")) (set (read var) (if (/= (setq tmp2 (getstring (strcat "Enter new value for [" var "] <" tmp1 ">"))) "") tmp2 tmp1)) ) (set (read var) val) ) (princ) ) Call to set a value directly: (setValue "*upperRebarTitle*" "3x20") Call to set a value by user input : (setValue "*upperRebarTitle*" nil) If the variable wasn't initialized, as default value will use "3£12". Quote
samifox Posted May 14, 2014 Author Posted May 14, 2014 I will write it like this: (defun setValue( var val / tmp1 tmp2 ) (if (not val) (progn (or (setq tmp1 (eval (read var))) (setq tmp1 "3£12")) (set (read var) (if (/= (setq tmp2 (getstring (strcat "Enter new value for [" var "] <" tmp1 ">"))) "") tmp2 tmp1)) ) (set (read var) val) ) (princ) ) Call to set a value directly: (setValue "*upperRebarTitle*" "3x20") Call to set a value by user input : (setValue "*upperRebarTitle*" nil) If the variable wasn't initialized, as default value will use "3£12". hi MSasu, trying to understand how this magic was done, this is the evaluation : (set (read var) val) (set (read var) "3x20") (set *upperRebarTitle* "3x20") now if im using (set var val) without (read) i get error, why? Quote
MSasu Posted May 15, 2014 Posted May 15, 2014 Maybe this will help you understand: (setq var1 "VAR2" var2 "eggs") (print var1) ;a string (print (read var1)) ;a symbol (print (eval (read var1))) ;value stored in above symbol Quote
samifox Posted May 15, 2014 Author Posted May 15, 2014 Maybe this will help you understand: (setq var1 "VAR2" var2 "eggs") (print var1) ;a string (print (read var1)) ;a symbol (print (eval (read var1))) ;value stored in above symbol Thanks you very much Msasu you help me understand the eval much better! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.