Storing and Setting AutoCAD’s System Variables with Autolisp

AutoCAD’s SETVAR function, when used in conjunction with a system variable name, will display the current value and permit the user to input a new value. In AutoLISP, the user can read the value of any AutoCAD system variable using the (getvar) function, and can set any non-read-only system variable using the (setvar) function.

Explanation: the (getvar) function

(getvar) reads the value of any AutoCAD system variable: (getvar variable_name)

  • The variable name is always a string.
  • You should store the system variable information in a (setq).

(setq CURRENT_LAYER (getvar “clayer”))

Explanation: the (setvar) function

(setvar) allows you to change the value of any non-read-only system variable:

(setvar variable_name new value)

  • The variable name is always a string
  • The new value is an AutoLISP expression that evaluates to an appropriate value.

(setvar “CLAYER” “WALLS”)

  • Both of these functions return the value of the variable name.
  • A list of AutoCAD system variables can be found in the Command Reference section of AutoCAD’s on-line help.

Examples

Command: (setq SNAP_ANGLE (getvar “snapang”))
0.0

Command:  (setvar “snapang” (/ PI 4.0))
0.785398

Command: (setvar “snapang” SNAP_ANGLE)
0.0

  • It is a good idea to follow proper programming protocol and reset any variables you change in your program.

At the beginning…

(setq OBJECT_SNAP_MODE (getvar “osmode”))
(setvar “osmode” 0)

At the end…

See also  why AutoLISP in AutoCAD?

(setvar “osmode” OBJECT_SNAP_MODE)

PRACTICE

Try the following statements that use (getvar) and (setvar). Estimated time for completion: 5 minutes.

1. Use AutoLISP to erase all objects within the extents of the current drawing.

(setq LOWLEFT (getvar “EXTMIN”))
(setq UPRIGHT (getvar “EXTMAX”))
(command “erase” “crossing” LOWLEFT UPRIGHT “”)

2. Set the current settings for LTSCALE to half of the value of DIMSCALE.

(setvar “LTSCALE” (/ (getvar “DIMSCALE”) 2))

3. (Extra Practice) Create a program to reset all your favorite system variables. We will learn how to save it in the next section.

Self Check:

Basic AutoLISP Functions

1. What function sets a variable equal to a value?

2. What AutoLISP function issues AutoCAD commands?

3. What is the purpose of the (setvar) and (getvar) functions?

Back to top button