Jump to content

Recommended Posts

Posted

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

Posted

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
)

Posted
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?

Posted
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".

Posted

FWIW -

 

You may also find PRAGMA to be of interest.

 

Cheers

Posted

Does my code is writen correctly?

Posted

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.

Posted

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

Posted

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".

Posted
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?

Posted

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

Posted
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!

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...