Jump to content

Passing variables between functions


nzgeocad

Recommended Posts

Gidday,

 

Basically what I am looking to do is run one Autolisp function that will define a global variable and then have another function use that variable. Both functions need to be able to be run from the command line or from a button on the ribbon at any time (the second will be error trapped so, if the first hasn't been run and the variable hasn't been defined, it will return a sensible error).

 

What I am trying is something like this:

 

(defun c:CreateVariable ( / localVariable1 localVariable2)
 (setq localVariable1 50.0)
 (setq localVariable2 3.0)
 (setq globalVariable (* localvariable1 localVariable2)
) ; end defun

(defun c:UseVariable ()
 (setq newVariable (/ 1234.0 globalVariable))
 (princ (rtos newVariable))
) ; end defun

I know that if you define a function using c: xxxx it wont let you use arguments but I don't know how to get that global variable into the function...

 

I'm sure it's simple, but at the moment it's very frustrating.

 

Any help would be appreciated.

Link to comment
Share on other sites

Introduce a test that will run the CreateVariable if the globalVariable has not been defined

 

 (if
 (not globalVariable) ;test expression
 (c:CreateVariable) ;preform the function
 )

 

See the test expressions COND, IF, AND OR in the AutoLISP help file for more info

 

(defun c:CreateVariable ( / localVariable1 localVariable2)
 (setq localVariable1 50.0)
 (setq localVariable2 3.0)
 (setq globalVariable (* localvariable1 localVariable2))
) ; end defun

(defun c:UseVariable ()
  
 ;Either the variable has a value or assign one
 (or globalVariable (c:CreateVariable))

 (setq newVariable (/ 1234.0 globalVariable))
 (princ (rtos newVariable))
) ; end defun

Link to comment
Share on other sites

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