GregGleason Posted November 29, 2017 Share Posted November 29, 2017 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: DEFVARWhat should I do to make this work? Greg Quote Link to comment Share on other sites More sharing options...
Grrr Posted November 29, 2017 Share Posted November 29, 2017 Funciton "defvar" is not defined. Among the code snippets you got, there should be a (defun defvar ...) code. Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted November 29, 2017 Share Posted November 29, 2017 (edited) 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 November 29, 2017 by Aftertouch wrong variable name Quote Link to comment Share on other sites More sharing options...
GregGleason Posted November 29, 2017 Author Share Posted November 29, 2017 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 Quote Link to comment Share on other sites More sharing options...
ronjonp Posted November 29, 2017 Share Posted November 29, 2017 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. Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted November 29, 2017 Share Posted November 29, 2017 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. :-) Quote Link to comment Share on other sites More sharing options...
GregGleason Posted November 29, 2017 Author Share Posted November 29, 2017 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: Set a global variable in acaddoc.lsp Call "Routine X" from acaddoc.lsp that changes the global variable princ the global variable after "Routine X" runs I think I'm close but I still have the syntax wrong. Greg Quote Link to comment Share on other sites More sharing options...
Aftertouch Posted November 29, 2017 Share Posted November 29, 2017 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.. Quote Link to comment Share on other sites More sharing options...
Grrr Posted November 29, 2017 Share Posted November 29, 2017 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. Quote Link to comment Share on other sites More sharing options...
GregGleason Posted November 29, 2017 Author Share Posted November 29, 2017 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 Quote Link to comment Share on other sites More sharing options...
Grrr Posted November 29, 2017 Share Posted November 29, 2017 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. Quote Link to comment Share on other sites More sharing options...
BIGAL Posted November 30, 2017 Share Posted November 30, 2017 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. Quote Link to comment Share on other sites More sharing options...
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.