In addition to the AutoLISP Refer ence, several other AutoCAD publications may be required by users building applications with AutoLISP: AutoCADCustomization Guide contains basic information on creating customized AutoCAD applications. For example, it includes information on creating customized user interface elements, linetypes, and hatch. Lisp is a programming language, that is been around longer than AutoCAD. Thousands of Lisp routines written over the last 30 years, can still be used in AutoCAD which you can find in books and on the Internet.
Issue:
How to have AutoLISP routines load automatically when starting AutoCAD. Each time running the program the files must be manually reloaded.
Solution:
To have AutoLISP routines load automatically, either add them to the AutoCAD Startup Suite or use the acad.lsp file.Add to the Startup Suite
- Run the APPLOAD command.
- Under Startup Suite, click the Contents button.
- Click the Add button.
- Browse to the location of the LISP file, select it, then click the Open button.
- Once all of the LISP routines have been added to the Startup Suite, click the Close button.
- Click Close again to close the Load/Unload Applications dialog.
The acad.lsp file is always loaded when AutoCAD starts up. If a special function S::STARTUP is defined in the acad.lsp,
it will be executed.
For example, there are two AutoLISP routines named stair.lsp and wall.lsp to be loaded each time AutoCAD is run. Create an acad.lsp file that contains the following lines of code and place it in the AutoCAD support paths.
(defun s::startup ()
(load 'STAIR.LSP')
(load 'WALL.LSP')
)
If wall.lsp and stair.lsp are in the AutoCAD search path, they will automatically load. If the AutoLISP routines are not in the AutoCAD support paths, include the full path within the acad.lsp file. Use either '/' or ' as path delimiters. With the same example, the acad.lsp file would look like this:
(defun s::startup ()
(load 'C:/PROG/LISP/STAIR.LSP')
(load 'C:PROGLISPWALL.LSP')
)
Creating Lisp Routines In Autocad 2017
If the S::STARTUP function is defined like this, problems could arise if other applications use the S::STARTUP function as well (for example, a third-party plug-in). To ensure compatibility, append the code in case there is an existing S::STARTUP function. To do this, add the following code to:
(defun mystartup ()
(load 'C:/PROG/LISP/STAIR.LSP')
(load 'C:PROGLISPWALL.LSP')
)
(if s::startup
(setq s::startup (append s::startup (quote ((mystartup)))))
(defun s::startup () (mystartup))
)
Note: Using an acad.lsp file runs each time AutoCAD is launched. An acaddoc.lsp file runs each time a drawing is open.
Versions:
any_version;AutoLISP has been a popular customization for AutoCAD. Many people use it to extend AutoCAD capabilities, do things that vanilla AutoCAD can’t. AutoLISP can also be useful to automate several process that usually need you use several tools, but with AutoLISP you may only need a few clicks. I’m not good with AutoLISP, but if you are interested to learn it with we, you can keep reading this tutorial. If you are an AutoLISP guru, I will be happy if you correct any mistakes or if you suggest better solution. So let us start to learn slowly, by creating simple program. You can also refer to AfraLISP for more AutoLISP tutorial. It’s an excellent resource!
Creating AutoLISP Application
An AutoLISP program can be created in notepad. It is a plain text, you only have to save it with file extension .lsp. However, AutoCAD itself has provided a Visual LISP editor. There are many functionalities you can use here, more useful than notepad.
Let us use visual lisp editor instead of notepad. You can access visual lisp editor from manage tab, applications panel.
AutoCAD will open visual lisp window. This window might not look fancy, and and the icons remind me of good old Windows 3.1 icons. However, it has many specific AutoLISP programming tools that can help us.
Click new or access file>new to create a new AutoLISP program.
AutoLISP Structure
Before we start, let us see the common program structure below.
(defun c:YourProgramCommand ()
WhateverYouWantAutoCADtoDo
(princ)
)
Define a Function (defun ())
AutoLISP will start with (defun c:ProgramCommand ()). Defun stands for define for a function. If you find this:
(defun c:ZO ())
It means that we are defining ZO as a command. AutoCAD will run your program when you type ZO then enter at command line.
Most programmer will put the close parenthesis below, parallel to open parenthesis. This will be easier for us to find the parenthesis pair. Most programming language will not care where is the close parenthesis, as long as they can find it. So make sure it will be EASY FOR YOU to find it.
Inside the parenthesis, you define whatever you want AutoCAD to do. That is the tricky part. We will do a simple exercise here, to see how it works.
Your First AutoLISP Program: Zoom to Origin
We are going to create our very first program. This program will zoom to the drawing origin, no matter which part of drawing we currently see. AutoCAD tool that can do this is zoom to center. Examine what we do when we use the command.
Command: ‘_zoom
Specify corner of window, enter a scale factor (nX or nXP), or
[All/Center/Dynamic/Extents/Previous/Scale/Window/Object] <real time>: _c
Specify corner of window, enter a scale factor (nX or nXP), or
[All/Center/Dynamic/Extents/Previous/Scale/Window/Object] <real time>: _c
Make A Lisp For Autocad
Specify center point: 0,0
Creating Lisp Routines In Autocad Free
Enter magnification or height <1753.5398>: 2000
The red fonts are the command we input during the zoom process. This is what we do manually:
- First, we type _zoom to activate the zoom tool.
- Then AutoCAD will ask us which method you want to use. We type _c to use zoom center.
- Then AutoCAD will ask us the center point. Because we want to zoom to origin, we type 0,0.
- The last thing AutoCAD will ask is the magnification. Type the distance you want to see on your screen. I use 2000. It means AutoCAD will show 1000 to the left, and 1000 to the right. With 0,0 at the screen center. If you use imperial, you may want to change it to smaller number.
Each time we give the input, we press enter. So we type _zoom [enter] _c [enter] 0,0 [enter] 2000 [enter].
Now in your Visual LISP editor, try to type like below. Type it, do not copy and then paste it. It will not work.
(defun c:ZO ()
(command “_ZOOM” “_C” “0,0” “2000”)
(princ)
)
(command “_ZOOM” “_C” “0,0” “2000”)
(princ)
)
You know what’s written in red means right? Command will load internal AutoCAD command, then you give the parameters. Each input in quote.
This time, you can use (princ) or not using it. Adding (princ) will end the program gracefully, but without it, it will also work.
Now save this program.
Load and Run Your Program
In visual LISP editor, click load active edit window.
Now move to AutoCAD window, and try to type ZO then [enter]. Does it work?
Congratulations! You have just created your first program!
We will modify this program further. So save it and don’t loose it.
Go see how we can use variable and ask for user input here.