Autolisp is the grand daddy of AutoCAD programming tools and youâ€™d be amazed at the amount of Autolisp programming tools you can find on the Internet. Given a little knowledge you can integrate existing Autolisp routines into your own and gain tremendous power over your AutoCAD based installation.
The first thing to understand is that Autolisp has a couple of key files and a key function that perform startup operations for you. The key files are called ACAD.LSP and ACADDOC.LSP and the key function is called S::STARTUP and their operations are as summarized here:
ACAD.LSP â€“ This file loads when AutoCAD starts up. Any programming you place within this file will be automatically loaded every time AutoCAD starts. The ACAD.LSP file is normally located in the SUPPORT subdirectory of the AutoCAD installation.
ACADDOC.LSP â€“ This file loads every time a new drawing session is started in AutoCAD 2000, 2000i or 2002 based products. Therefore any programming you place in this file will be loaded automatically every time a drawing is opened or started. Note that while the ACAD.LSP file would load in the FIRST drawing of the AutoCAD 2000 type products only the ACADDOC.LSP file will load with subsequent drawings. Since AutoCAD R14 doesnâ€™t support multiple drawing sessions you wonâ€™t have to worry about the ACADDOC.LSP file with R14. Like the ACAD.LSP file, ACADDOC.LSP is normally located in the SUPPORT subdirectory of the AutoCAD installation.
S::STARTUP function â€“ This function is typically within the ACADDOC.LSP file (or ACAD.LSP file for AutoCAD R14 installations) and its sole job is to execute customized commands you need to initialize your new drawing environment. This function is the perfect place to set system variables like DIMSCALE, VIEWRES parameters, current layers, etc. The most powerful aspect of the S::STARTUP function is that it invokes automatically and it lets you control exactly how the AutoCAD environment is initialized.
If you created an ACADDOC.LSP file in the SUPPORT subdirectory of your AutoCAD installation and placed the following text in it what do you think would happen?
Well, the DEFUN statement simply DEfines a FUNction (see where the DEFUN comes from) called S::STARTUP which we already know will run every time AutoCAD starts/opens a drawing. The contents of the function are simply an ALERT box notification which will say â€œHello there!â€ followed by a PRINC statement which is traditionally the last line of a function.
Save your new ACADDOC.LSP file checking the syntax carefully to be sure that itâ€™s right and startup AutoCAD. Do you understand the result you get?
Letâ€™s carry the example a bit further by trying this:
This example sets the default text size and dimension text size to a value of 0.125 (or 1/8â€ if you prefer) every time a drawing opens or starts. The reason I used the decimal value is that your drawing may not always be in the architectural or fractional coordinate system and thus the decimal value is the most generic way to input a value. This example shows how the SETVAR statement can be used to set AutoCADâ€™s system variables and also shows how to set values using a generic coordinate system.
Letâ€™s continue the example to a logical conclusion by saying that the TEXTSIZE value really should be 0.125 times the value of the DIMSCALE that is already set in the drawing. To complete this task weâ€™ll need a new command called GETVAR that allows us to get variables (rather than set them) and a multiplication function (the normal * operator) within a single statement. Using a bit of imagination yields the following program:
Note that the statement that sets the text size first gets the DIMSCALE value then multiplies it by 0.125 and only then sets the TEXTSIZE value. The order of the operations occurs from the inner most set of parenthesis out. Please also note there are three sets of parenthesis and that they always balance.
LSP files and functions
As you begin to write your own programs it becomes logical to keep your program code stored in files that can be edited individually. These files always have the .LSP file suffix and are typically edited in a Notepad/WordPad session or in the Visual Lisp editor (which weâ€™ll talk about more in an upcoming issue). The location of the LSP files can be in any directory you want but the easiest way to assure the files will be located is to place them in the SUPPORT folder of your AutoCAD installation.
Within the LSP files you create you may store functions, which are simply programming routines that you can run over and over. A specific type of function, the C: function, allows you to actually add commands to AutoCADâ€™s vocabulary. By adding new commands, or C: functions, to AutoCADâ€™s command set your users simply think they have a new AutoCAD command they can use. Only you will know that they are actually using a custom AutoCAD function.
As you create your custom functions and save them as LSP files youâ€™ll need to load in the files to be sure that the functions work. Once debugged the functions can be loaded from the ACAD.LSP file we talked about in Newsletter 61.
A Real World Example
I love keyed in shortcuts for commands like L for line, or E for erase. Some while back I wanted to create a command called FZ that would always give me a zero radius fillet command (for creating sharp corners) yet would still leave the default F fillet command at whatever value I last set it at.
Therefore the program I would write would have to do the following things:
remember the old fillet radius (set in the FILLETRAD system variable)
set the new fillet radius to zero
invoke the fillet command and let me select the objects
set the fillet radius back to itâ€™s prior setting
The way I programmed this function looks like this:
(defun c:fz () ; line 1
(setq old_radius (getvar â€œfilletradâ€)) ; line 2
(setvar â€œfilletradâ€ 0) ; line 3
(command â€œ.filletâ€ pause pause) ; line 4
(setvar â€œfilletradâ€ old_radius) ; line 5
) ; line 6
In the above example Iâ€™ve added comments at the end of each line to assign line numbers. Youâ€™ll note that each comment is preceded by a semi-colon character which means that the Autolisp interpreter will ignore everything after the semicolon. This method of commenting the program function allows you to store programming notes.
Now Iâ€™ll summarize what each line of the program is doing:
Defines a command function called FZ using the DEFUN statement.
Holds the default fillet radius value in a variable called OLD_RADIUS using the SETQ and GETVAR statements.
Sets the fillet radius in the AutoCAD session to 0 using the SETVAR statement.
Invokes the FILLET command and has PAUSE statements to wait for input from the user in the form of selecting two lines. Please note that AutoCAD commands are encased in quotation marks and that PAUSE instructions do not.
Sets the fillet radius back to its old value using the OLD_RADIUS value using the SETVAR statement.
A final parenthesis closes out the DEFUN statement begun in line 1.
Letâ€™s Test It
Now you can create a file called FZ.LSP that contains the programming code from above and save it into AutoCADâ€™s support directory. Be very sure youâ€™ve typed everything correctly before saving the file (or get the code from my web site which I mention at the end of the newsletter).
Now start your AutoCAD session and type the following at your command prompt:
Once the file is loaded into AutoCAD you should receive confirmation at the command line that will say C:FZ which is the name of the function.
You may now test the function by typing in FZ at the command line. Before you try the new FZ command though use the standard fillet command and set the radius to some non-zero value and fillet a couple of lines to be sure the command has been set properly. Now you should be able to fillet with normal radius values by using the normal fillet command but using FZ will always invoke a zero radius fillet.
That wasnâ€™t so hard was it?
If youâ€™d like you can now add the LOAD instruction line of (load â€œfz.lspâ€) in your S::STARTUP function (mentioned earlier) and your new FZ function will load automatically upon starting AutoCAD. By loading the function from S::STARTUP youâ€™ll take care of your users without them even knowing where the new FZ command came from.
The â€“Command Set
Some AutoCAD commands will require an addition of a leading â€œ-â€œ character to invoke them from Autolisp commands because the command is normally manipulated via a dialog box. The layer command is a prime example of this concept that you can verify simply by typing in â€“LAYER at the command line. Now type in LAYER at the command line and youâ€™ll get a very different result right?
Since AutoCAD commands pass through the Autolisp command interpreter with TYPED input youâ€™ll need to always invoke the â€“LAYER command in Autolisp code like this:
(command â€œ-layerâ€) instead of (command â€œlayerâ€)
If you donâ€™t believe me try it at the AutoCAD command line and see how it works. Other commands I frequently use the â€œ-â€œ prefix with are â€“STYLE for text style manipulation and â€“PLOT for issuing plot commands via Autolisp. Youâ€™ll want to examine these commands simply by keying them in at the command line then making some notes about the prompts you can enter into the command line.
In almost all cases with Autolisp commands youâ€™ll need to know how the AutoCAD commands operate and understand the sequence of how to input parameters into the command to write successful functions. If you already type in commands in AutoCAD you should be all set, if not youâ€™ve got some exploring to do. A nice way to document the various keyed in information you use in AutoCAD is to simply copy the text out of the text history window which you access using the F2 key. Then simply use the COPY function to copy the history text into your Notepad programming session.
When you inadvertently forget parenthesis, quote marks or some other key piece of code in your LSP file you will experience a variety of problems. To see these problems demonstrated try loading in the following programming examples and see what happens.
The error here is an extra L in the word FILLLET. You’ll notice that the function will actually run but will halt when the fillet command should run.
Input and Selection Sets
In the first two installments of our programming series I covered how to store variables, manipulate system variables and use the COMMAND statement to pass instructions to the AutoCAD editor. While these skills are key to writing basic programs youâ€™ll find that to write truly flexible programs youâ€™ll need to be able to:
Prompt your users for input values (numbers, text, points)
Prompt your users for sets of data (using selection set identification)
Filter the AutoCAD database for certain entity types
Work with entity points (line endpoints, circle centers, etc)
The commands weâ€™ll be utilizing to achieve these goals are as follows:
GETREAL (to GET a user defined REAL number)
GETINT (to GET a user defined INTeger number)
GETSTRING (to GET a user defined STRING of text)
GETPOINT (to GET a user defined POINT)
SSGET (to GET a user defined Selection Set)
SSGET with the â€œXâ€ option (to filter the database for objects)
CAR, CADR and CADDR (to break points into X, Y and Z components)
As in past installments Iâ€™ll use short pieces of programming code to illustrate the concepts and ramp up the level of complexity gradually until you can understand even complex examples.
Getting Values from the User
Getting user supplied values is straightforward in Autolisp as can be seen with these statements:
(setq var1 (getreal â€œ\nPlease input a REAL number: â€œ))
(setq var2 (getint â€œ\nPlease input an INTEGER number: â€œ))
(setq var3 (getstring â€œ\nPlease input a piece of text: â€œ))
Try pasting these examples into your AutoCAD command line to run the statements interactively and youâ€™ll see immediately whatâ€™s happening. The interesting thing to note is that when using the GETINT statement that you canâ€™t input a real number or a piece of text. Youâ€™ll also notice that a prompt is generated by the command so the user knows what information to input.
Note: The \n character is simply a line feed that assures you that the prompt to the user will be placed on a new command prompt line in the AutoCAD session!
The result of these statements will be a stored variable VAR1, VAR2 or VAR3 that can be used in other programs. Hereâ€™s a simple example of a program you can run interactively that prompts the user then makes use of the stored variable:
(setq dimtxt_height (getreal â€œ\nDimension text height: â€œ))
(setvar â€œdimtxtâ€ dimtxt_height)
While this is a very simple example it illustrates the concept perfectly. Just acquire a value from the user then use the variable name in other commands instead of fixed values. (See examples from the previous newsletter issues if you have any questions on the SETVAR statement.)
Getting and Working with Points
While getting a point from the AutoCAD user is very simple using this form:
(setq point1 (getreal â€œ\nPlease select a POINT location: â€œ))
Working with the points isnâ€™t easy because the point is actually three pieces of data that must be picked apart. Point data is really a list of X, Y and Z coordinates lumped together which can be decomposed using CAR, CADR and CADDR functions as follows:
Note that an intermediate variable called POINT1 stored the point while variables POINT1_X, POINT1_Y and POINT1_Z contact the actual values for the x, y and z coordinates, respectively.
This CAR, CADR, CADDR approach requires equal components of memorization and getting used to, but you will find that youâ€™ll grow accustomed to its usage faster than you think.
Working with Sets
Yet another component of AutoCAD programming is that AutoCAD tends to operate on sets (AutoCAD calls them selection sets) of objects. AutoCAD users provide selection sets in commands like MOVE, COPY, ROTATE, ERASE, etc. The rule of thumb is that if a command ever asks you to SELECT OBJECTS that command is making use of a selection set.
To make your programs acquire selection sets youâ€™ll need to use the SSGET command. The most basic form of the command works like this:
(setq set1 (ssget))
(setq set2 (ssget â€œ\nPlease select objects for your set: â€œ))
the only difference being a user prompt in the second example.
The resulting set will be stored in a variable (SET1 or SET2 in the above examples) that you can manipulate with a COMMAND style instruction. A simple program illustrates the trick:
This example illustrates how a selection can be obtained, stored and processed using only two lines of code!
Note: The final â€œâ€ is simply an extra ENTER which youâ€™d ordinarily have to hit manually to complete the erase command in AutoCAD.
Lets say that you wanted to author a program that would erase all text in a drawing automatically. You will need to use the SSGET command but not for user input, rather youâ€™ll need to filter the AutoCAD drawing for TEXT objects. The example code here looks pretty nasty but youâ€™ll have to trust me when I tell you there isnâ€™t a better way in standard Autolisp:
Iâ€™ll admit that this syntax isn’t very user friendly, but you will get used to it. Now simply combine one of the above statements with an ERASE command and you have a program that deletes a give entity type from the drawing automatically:
As you can see youâ€™ve gained a lot of power in this example using only a few lines of code. For more information on how to filter for more complex sets of objects consult the Developerâ€™s Help section of AutoCADâ€™s electronic help files and search on the SSGET Autolisp function – youâ€™ll find syntax examples a plenty there.
March 24th, 2009 |
Comments are closed.
Robert has provided CAD management consulting, programming and training services for clients throughout the United States and Canada since 1991. Reach Robert at email@example.com