AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN


Saving and Restoring System Variables.

It is good practice (and manners) when writing Lisp routines to restore the system environment to the state that your program found it in on completion of your application. Most AutoLisp routines start and end like this :

(defun c:example ()
	(setq oldhigh (getvar "Highlight")
	      oldsnap (getvar "Osmode")
	      oldblip (getvar "BlipMode")
	      oldecho (getvar "Cmdecho")
	);setq

	(setvar "Highlight" 0)
	(setvar "Osmode" 517)
	(setvar "Blipmode" 0)
	(setvar "Cmdecho" 0)

	Programme statements.............
	.................................

	(setvar "Highlight" oldhigh)
	(setvar "Osmode" oldsnap)
	(setvar "Blipmode "oldblip)
	(setvar "Cmdecho" oldecho)
  (princ)
);defun
;******************************************************

I must have written statements like this a thousand times in my Lisp routines.
The following example is designed to act as a global routine that first stores, then changes specific system variables. On completion of the routine, the function is then called again and all system variables are returned to their previous settings.

(defun varget ()

	(setq lis '("HIGHLIGHT" "BLIPMODE" "CMDECHO"
		    "BLIPMODE" "OSMODE"))
	;store names of system variables

	(setq  var (mapcar 'getvar lis))
	;get the value of the system variables and
	;store them as a list

	(setq var1 '(0 0 0 0 517))
	;store the new values of the system variables

	(setq no 0)
	;set counter to zero

	(repeat (length lis)
	;get the number of variables in the list
	;to use as the counter control number

		(setvar (nth no lis) (nth no var1))
		;set the variables to their new values

		(setq no (1+ no))
		;move up one in the list

	);repeat

  (princ);finish quietly

);defun

;***************************************************************

(defun varset ()

	(setq no 0)
	;set counter to zero

	(repeat (length lis)
	;get the number of variables in the list

		(setvar (nth no lis) (nth no var))
		;reset the variables to their original values

		(setq no (1+ no))
		;move up one in the list

	);repeat

  (princ);finish quietly

);defun

;***************************************************************

(princ);load quietly

Our Autolisp routine could now look like this :

(defun c:example ()

	(varget)
	;store system variables and then reset them

	Programme statements.............
	.................................

	(varset)
	;restore system variables

  (princ)
);defun
;******************************************************

As you can see, we have reduced the size of our routine by a lot and saved ourselves quite a bit of typing. These two routines could both be loaded from our Acad.Lsp file so that they would be available to all of your routines.

The AutoLisp/Visual Lisp/VBA Resource Website

Copyright © 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website