View Full Version : Global Variables
ccowgill
2008-11-21, 05:29 PM
could someone please explain how global variables work. Or more specifically, if I specify a global variable in acad.lsp, and have it set to only load once per session. Will other programs be able to access global variables set in acad.lsp in subsequent drawings that are opened? So, are global variables global only in the current drawing, or do they expand to every drawing that is open?
I may have answered my own question, but I would like confirmation from someone more knowledgeable than me. I think that globals are only available for that current drawing, if it is established in acad.lsp, it is only available to the first drawing of that session.
kennet.sjoberg
2008-11-21, 06:16 PM
. . .if I specify a global variable in acad.lsp, and have it set to only load once per session. . . then it is STOP.
Let acad.lsp loads into every drawing.
: ) Happy Computing !
kennet
T.Willey
2008-11-21, 06:42 PM
If you want a global variable loaded into all drawings, then look at ' vl-propagate '. Or like Kennet said, load the acad.lsp into all drawings.
Global variable, by default, are only in the drawings that establishes them, which is how Lisp works.
Why wouldn't you set them in an acaddoc.lsp that gets loaded in every drawing anyway?
(just curious)
kennet.sjoberg
2008-11-21, 11:51 PM
To be honest, I do not know why this option is necessary :
[ empty ] Load acad.lsp with every drawing
To set a global static variable to all sessions use acad.lsp or acaddoc.lsp.
to send variables between sessions use vl-propagate
: ) Happy Computing !
kennet
irneb
2008-11-24, 08:12 AM
Any variable in lisp is only "global" to its local namespace. By default AutoCAD creates a local namespace for each drawing. If you do lisps inside FAS / VLX files you could even have namespaces inside only the VLX set of lisps.
Unfortunately AutoLisp doesn't have a static / global statement like VB (or other languages). Now you've got 2 options, depending on your needs:
If you simply have a variable set to a predefined value (in other languages you'd have made this a constant), then simply have it load from the acad.lsp or acaddoc.lsp file. One copy of which is loaded into each open drawing.
If you change the value during the course of the rest of your lisps - then use the (vl-propagate ...) function to copy the change to the other drawings as well. This you need to do each time after you've changed the value.Another posibility could be to use the so called "black-board" namespace with the (vl-bb-set ...) and (vl-bb-ref ...) functions. This works for VLX / FAS namespaces as well.
And then finally to even have changes propagated to new instances of AutoCAD you can save the values into a file with (open ... "w") (write-line ...) (prin1 ...) (close ...) and then (open ... "r") (read-line ...) (read ...) (close ...).
Or preferably into registry using (vl-registry-write ...) and (vl-registry-read ...).
kennet.sjoberg
2008-11-24, 09:03 AM
So to not get lost in the forest ccowgill, just checkmark this option box
Options | Systems | [ v ] Load acad.lsp with every drawing
: ) Happy Computing !
kennet
ccowgill
2008-11-24, 02:23 PM
So to not get lost in the forest ccowgill, just checkmark this option box
Options | Systems | [ v ] Load acad.lsp with every drawing
: ) Happy Computing !
kennet
In the past I have only used the acaddoc.lsp file, we stopped using the acad.lsp, because it seemed pointless to load it in every drawing, and it doesn't seem to make much sense to load it only in the first drawing opened. I decided with this new program to define a startup routine, so I figured that acad.lsp would be a good place to put it. I am using it to verify that a mapped drive exists, and if it doesn't, after everything is completely loaded, AutoCAD creates the mapped drive, telling it to be persistent, so the user hopefully doesn't have to mess with it ever again. I have decided to avoid the necessity of a global variable by not using a variable at all, just using straight code to obtain a current setting.
irneb
2008-11-24, 02:56 PM
In the past I have only used the acaddoc.lsp file, we stopped using the acad.lsp, because it seemed pointless to load it in every drawing, and it doesn't seem to make much sense to load it only in the first drawing opened.Probably best. The only things I can think of which is more efficient in ACAD.LSP (if you didn't do what kennet suggested) are those which change Registry saved SysVars, load (or ensure loaded) special menus, etc. If you have something which needs to happen for each drawing - by all means place it in ACADDOC.LSP, that's why it's there :mrgreen:
As to your problem, it's probably not needed to do this for every single drawing. If you've already checked and set (if necessary) the drive - it should at least be there for the session (unless the user unmaps it manually). So this I'd simply (as you did) place in ACAD.LSP so it only runs once for each time you start ACad. If you place it in ACADDOC.LSP (or set kennet's suggestion) you're going to check if the drive exists every time a drawing is opened - which depending on how you're doing it, could be quite timeconsuming. In which case, I'd also want a global variable to test if the check has already been done - for that I'd use the vl-bb-set and vl-bb-ref functions since they are saved to true "global" variables, but still volatile (unlike file / registry).
kennet.sjoberg
2008-11-24, 08:09 PM
. . . I am using it to verify that a mapped drive exists. . .
(if (vl-file-directory-p "K:" )
( ) ; K: is present
( ) ; Map the drive
)
: ) Happy Computing !
kennet
ccowgill
2008-11-24, 08:23 PM
(if (vl-file-directory-p "K:" )
( ) ; K: is present
( ) ; Map the drive
)
: ) Happy Computing !
kennetyup, that's exactly how I have it set up.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.