Jump to content

Global Variable Not So Global?


GregGleason

Recommended Posts

I have a acaddoc.lsp routine with this code snippet:

 

(princ "\nHey!")
(load mypatlsp)
ReadCSVForInsu
(princ "\nInsulation After: ")
(princ *insulationtyp*)

The result in the Text Window is:

 

Hey!

Insulation After: nil

The code used in the calling file to define the global variable is as follows:

 

 (defvar *insulationtyp* "QQQ")

When I try to run the routine by itself I get the following result:

 

; error: no function definition: DEFVAR
What should I do to make this work?

 

Greg

Link to comment
Share on other sites

I think your looking for the SetEnv function? Tho this adds a custom variable to your register and therefore its global accessable. But thats something diferent than a global autolisp variable...

Edited by Aftertouch
wrong variable name
Link to comment
Share on other sites

I think your looking for the SetVar function? Tho this adds a custom variable to your register and therefore its global accessable. But thats somerhing diferent than a global autolisp variable...

 

 

Ahhh. Ok.

 

So, will this work to set a variable that can be changed in other routines?

 

(setvar "insulationtyp" "QQQ")

Greg

Link to comment
Share on other sites

Ahhh. Ok.

 

So, will this work to set a variable that can be changed in other routines?

 

(setvar "insulationtyp" "QQQ")

Greg

 

When in doubt try it yourself. Paste that into the command line to see what it does.

Link to comment
Share on other sites

Sorry, i made a mistake in my last post...

 

When you use (setenv "MYVARIABLENAME" "1") (only string allowed)

Then the variable MYVARIABLENAME is stored in your register.

With (getenv "MYVARIABLENAME") you can get the value again. In this case "1".

Since this variable is stored in the register, it can be called by all other lisp routines.

When restarting AutoCAD, the variable will remain there. It is also accesable in all DWG files.

When changed in one file, its changed for ALL files.

 

When you use a AutoLisp 'global variable' the following is usualy ment:

For example:

(defun C:TEST ( / )
(setq testvariable "blablabla")
(princ)
)

 

the variable 'testvariable' is now a global variable wich can be accepted by other lisps WITHIN the samen DWG file.

When you restart AutoCAD, this variable is gone since its not stored for ever.

 

When you use:

(defun C:TEST ( / testvariable )
(setq testvariable "blablabla")
(princ)
)

Now testvariable is set to a local variable, wich is removed when the function is finished, and thus, not accesable anymore by other lisp programmes...

 

Hope this makes sense. :-)

Link to comment
Share on other sites

Ok. I don't think setvar is what I want since that sets a sets a system variable.

 

Here's what I want to do:

 

 

  1. Set a global variable in acaddoc.lsp
  2. Call "Routine X" from acaddoc.lsp that changes the global variable
  3. princ the global variable after "Routine X" runs

 

I think I'm close but I still have the syntax wrong.

 

Greg

Link to comment
Share on other sites

Read my post better. Im talking about setENV, and not setVAR.

 

Your wish..

 

Method one:

In acaddoc.lsp:

(setq globalvariable "1") ; Sets the variable, its global since its outside a function.

(defun RoutineX ( / )      ;Define the function RoutineX
(setq globalvariable "2") ; Change the value of the global variable.
(princ (strcat "The global variable is set to: " globalvariable " by the RoutineX function"))     ; Princ the variable in the command line.
(princ)         ; Silent exit.
)

(ROUTINEX) ; Execute the RoutineX function.

 

 

Or method 2:

(setenv "GlobalVariable" "1")

(defun RoutineX ( / )      ;Define the function RoutineX
(setenv "GlobalVariable" "2") ; Change the value of the global variable.
(princ (strcat "The global variable is set to: " (getenv "GlobalVariable") " by the RoutineX function"))     ; Princ the variable in the command line.
(princ)         ; Silent exit.
)

(ROUTINEX) ; Execute the RoutineX function.

 

With method one, the global variable is lost when the DWG is closed.

With method two, the global variable is stored in the register, and stored for the next session..

Link to comment
Share on other sites

Again, IMO 'defvar' is a custom defined function copied from somewhere so GregGleason posted a question with code fragments without any further knowledge.

Function names cannot be that easily mistaken, like Aftertouch suggested about setenv. Next question could be "did you mean defun?".

So for basic questions just post the complete code, or the source link.

Link to comment
Share on other sites

Ok. I got it to work how I would like.

 

I think where I got lost was on my understanding of global variables.

 

It "looks" like variables are global unless you define them to be local to your program with the defun declaration. That is somewhat of a paradigm shift as I was of the notion that all variables were local unless explicitly made global. Is my thinking correct?

 

Greg

Link to comment
Share on other sites

It "looks" like variables are global unless you define them to be local to your program with the defun declaration.

Correct. Thats where 'Localising Variables' term got from, you might want to check Lee Mac's tutorial. ;)

 

 

That is somewhat of a paradigm shift as I was of the notion that all variables were local unless explicitly made global.

Incorrect thought. As far I'm aware variables in LISP or Visual Basic for Applications (VBA) must be localised, no idea for the other languages.

Hope I'm not wrong, but (localising) decalring variables to be local occurs also in C++/C# via functions like dim / using.

Link to comment
Share on other sites

The one downfall with using the setenv is its local to only your PC so if the guy next to you works on the dwg he does not have the current setting.

 

A better way may be to store the value in the dwg there are a couple of ways you can use USERS1,USERS2 - USERS5 string value, Useri1 is integer these are a setvar. The other way is to use xdata and have a user dictionary you can save lots of variables this way.

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