AutoLISP Commands to Manage System Variables in AutoCAD

November 1, 2024 Jeff Morris

This is the third of three blogs explaining how to manage system variables 

  1. This final blog demonstrates a simple LISP program to change variables and how to load these simple programs into AutoCAD. 

AutoLISP 

Scripts and macros are quite convenient for a certain degree of automation; however, they have a serious drawback, which is that they do not take any user input, nor can they be paused. That is where AutoLISP comes in. AutoLISP is a variation of the LISP programming language, which is based on the concept of being able to process lists. It is a very powerful (and complicated) language. This blog will only skim the surface, demonstrating two very simple commands and the steps needed to have these commands automatically loaded from a trustworthy source into AutoCAD. 

The problem with the macro created in the previous blog is simply that one macro is needed to turn on a system variable (^C^CFiledia 1 ) and another is needed to turn it off (“^C^CFiledia 0 ) 

It would be much more convenient to have a toggle, which would check the state of the variable and turn it on or off accordingly. This is what AutoLISP can do. 

Here is a simple example: 

(defun c:filedia () 

 

  (if (= (getvar "filedia") 0) 

    (setvar "filedia" 1) 

 

    (setvar "filedia" 0) 

    ) 

  (princ) 

  ) 

Defines the function as filedia. The “c:” designates this as a command. 

Get the value of the “filedia” variable. If it is 0 

   Then set the value of the “filedia” variable to 1 

Otherwise 

   Set the value of the “filedia” variable to 0 

 

This negates anything from displaying at the command line 

as the program finishes. 

 

   

Here is a more refined version that informs you what has been done: 

(defun c:filedia () 

 

  (if (= (getvar "filedia") 0) 

    (progn 

      (setvar "filedia" 1) 

      (princ "\nSetting FileDia ON") 

      ) 

   (progn 

      (setvar "filedia" 0) 

      (princ "\nSetting FileDia OFF") 

      ) 

    ) 

  (princ) 

  ) 

Defines the function as “filedia. The “c:” designates this as a command. 

Get the value of the “filedia” variable. If it is 0 

  A series of more than one command 

 Then set the value of the “filedia” variable to 1 

Print to the console in a new line (\n)  

End of series of commands 

Otherwise - a series of more than one command 

 Set the value of the “filedia” variable to 0 

Print to the console in a new line (\n)  

End of series of commands 

 

Print to the console nothing - This negates anything from displaying at the command line as the program finishes. 

 

 

These can be typed in a simple text editor and saved with an .LSP extension.  

To load them, invoke the AppLoad command, which is found in the Manage tab>Application panel, as shown below. The keyboard shortcut is AP. 

 

In the Load/Unload Applications dialog box, you can browse to the folder and select the LISP file, as shown below 

 

When you load these files initially, a warning appears, as shown below. Since this is a simple text file you created, select Always Load. You can also assign the folder containing your LISP files as a Trusted Location in the Options dialog box. 

 

If you want these LISP routines loaded automatically, you can add them to the Startup Suite in the Load/Unload Applications dialog box, as shown below. Every time you open a drawing, the contents within the Startup Suite will be loaded. 

 

Now you have new commands and can add them to toolbars as demonstrated in my previous blog. 

Conclusion 

Simple AutoLISP programs are superior to macros in that they can make decisions and pause for user input. They can read the current state of variables and toggle them, even giving a simple message back to the user. This is just a very simple example of the power of AutoLISP. 

About the Author

Jeff Morris

Learning Content Developer<br><br>Jeff specializes in infrastructure tools such as Civil 3D and Infraworks, delivering training classes and contributing to the learning guides for these Autodesk software applications. Jeff has worked for several Autodesk resellers and has had roles of both CAD and BIM Manager with Civil and Architectural firms.

Follow on Linkedin More Content by Jeff Morris
Previous Article
Macros and Scripts
Macros and Scripts

Creating macros is a great way to streamline your AutoCAD workflows. Learn more in this blog.

Next Article
Linking Tables in AutoCAD
Linking Tables in AutoCAD

Learn how to use the Data Linking feature in AutoCAD with helpful tips in this post.

×

Sign up for email updates

First Name
Last Name
Country
Thank you!
Error - something went wrong!