Content area

Abstract

AutoLISP is based on the common LISP language, which was defined back in the late 1950's and is widely used in artificial intelligence and expert systems. AutoLISP was first introduced as part of AutoCAD in Release 9, sometime in the early 1990's. AutoLISP differs from other programming languages in that it evaluates each line of a program as it is introduced to the computer. Other languages, such as Visual Basic and C++, compile the program code into machine code that the computer recognizes.

Full text

Turn on search term navigation
 

Application: Autodesk AutoCAD 2002 / 2004 / 2005 / 2006

Download: http: / / download.elementkjournals.com / autocad / 200601 / mybox.zip

AutoLISP is a powerful tool for automating AutoCAD tasks. However, you may have looked at the articles in this column and felt that they looked too complicated. Perhaps you wanted to start using and even writing AutoLISP programs to automate tasks that are unique to your situation, but didn't know how to get started. In this month's article, we'll open the door to the AutoLISP mystery and show you some simple techniques for getting started. It's not as difficult as you think.

Start the journey

In this article, we'll briefly discuss the history of AutoLISP. Then, we'll show you how to get started using AutoLISP. We'll define a one-line program and then guide you through the steps to create an AutoLISP program with more features. Finally, we'll discuss how to use the program in AutoCAD.

A brief history of AutoLISP

AutoCAD has become the leader in the CAD software industry with over four million users in 160 countries. A major reason for the huge success and popularity of AutoCAD is its open architecture design. This gives users the ability to customize AutoCAD to fit their own needs.

Though AutoCAD includes a wide variety of commands right out of the box, the capability of customization makes it more valuable. A customized AutoCAD environment gives you the ability to accomplish industry-specific tasks that save time, increase productivity, and improve quality. There are different levels of customization in AutoCAD. You can create custom menus and toolbars and you can create command line scripts; however AutoLISP offers some of the most powerful customizing tools.

AutoLISP is based on the common LISP language, which was defined back in the late 1950's and is widely used in artificial intelligence and expert systems. AutoLISP was first introduced as part of AutoCAD in Release 9, sometime in the early 1990's. AutoLISP differs from other programming languages in that it evaluates each line of a program as it is introduced to the computer. Other languages, such as Visual Basic and C++, compile the program code into machine code that the computer recognizes. The compiled code does execute faster. However, since AutoLISP interprets each line of code as it is introduced, you can type AutoLISP code on the AutoCAD command line and the AutoLISP interpreter gives you a result immediately. You can't type VBA code (another programming language that AutoCAD understands) on the command line.

Create new AutoCAD commands

With its open architecture design, AutoCAD allows you to add new commands and redefine existing commands. You can add new commands to AutoCAD using the (defun C:XXX) AutoLISP function. To create a new LINE command, you would type the following on the AutoCAD command line.

View Image -

The new line command name is my line and you can now type it on the AutoCAD command line. This example executes the existing AutoCAD line command, but it shows you how simple it is to create a new AutoCAD command. Executing an AutoCAD command in your AutoLISP program is a useful tool in many programs that you create.

Notice the parentheses in the code. Parentheses are an ever-present feature of AutoLISP. They surround the entire body of code and separate elements within the code.

Enter AutoLISP on the command line

As mentioned before, you can type AutoLISP functions on the command line. An example of this might be to add some numbers together. Type the following on the AutoCAD command line:

View Image -

This example adds the numbers together and prints the result 14 on the command line. Note that the plus sign (+) comes first. In AutoLISP, the operator or function comes first.

You can execute other AutoLISP functions on the command line, however this isn't a practical way of writing a program, since you may have many lines of code. Instead, create a program using a text editor application such as Notepad or WordPad.

Create your first program

In this example, we'll use Notepad to write and store the AutoLISP code. Open Notepad on your computer by choosing Start I Programs I Accessories Notepad. This opens an empty Notepad file. Type the first line of code that we listed in this article. Choose File I Save from the Notepad menu to save the file and name it my M ne. Up. Save it in the My Documents folder.

Note: Notice that the file extension of your file is .lsp. This is the naming convention for all AutoLISP program files.

You need to make sure that the My Documents folder is defined as a search path in AutoCAD. To check this, choose Tools I Options from the AutoCAD menu to open the Options dialog box. From the Files tab, expand the Support File Search Path item by clicking the plus (+) symbol. Notice the different search paths listed for AutoCAD. Look for C: \My Documents. If it doesn't exist, add it by clicking the Add button. Then, click the Browse button and select the My Documents folder from the Browse For Folder dialog box. AutoCAD will now search that folder when you need to use a support file such as an AutoLISP file.

To use the program in AutoCAD, you need to load it into AutoCAD memory. To do so, type the following on the AutoCAD command line.

View Image -

By enclosing the text in parentheses, you're entering AutoLISP code on the command line. Load is the function. You need to enclose the name of the file in quotation marks. Omit the . lsp filename extension.

Now, enter myline on the command line to execute the program. The AutoCAD LINE command starts and prompts for the starting point of the line.

You've just created and executed your first AutoLISP program and defined a new AutoCAD command named myline.

Draw a rectangle

Next, let's create an AutoLISP program that draws a rectangle. The program will prompt the user for the starting point, the width, and height of the rectangle. The program will then calculate the four corner points of the box, based on the user's input.

The program will calculate the corner points by using the x,y-coordinates of the starting point and adding the width or height to the coordinate to create the corner points. How do we do this? We use a couple of AutoLISP functions that can obtain the x- or y-coordinate from the starting point.

Get user input

First, we need to understand how to get the starting point and in what form that point is returned in AutoLISP. We will use the function ( getpoint) to get the starting point and we'll use it in the following form:

View Image -

Let's start with the second part of this code. The get point function, as its name suggests, gets a point from a user. The text string, Pick the Box starting point : is the prompt that the user will see when the program is executed.

At the beginning of the line of code, setq ptl sets the point that the user specifies to the variable pt1. A variable stores values, so when the user picks the point on the screen, the variable pt1 stores that point for future use. As usual, each section is surrounded with parentheses as is the entire line of code.

We need to prompt the user for the width and height and store those values in variables, too. Since we want the user to input the width or height as a number that can include a decimal point, we'll use the (getreaI ) function, which gets a real number (in mathematics, any non-imaginary number, including integers and non-integers) to prompt the user for input.

View Image -

These lines of code are very similar to the previous one. Notice that both lines of code have a prompt for the width or height and both store the result in a variable named width and height.

Understand the parts of a point

Now that we have the starting point stored in the variable pt1, we need to understand the format of pt1. The pt1 variable is in the form of a list and is similar to the following.

View Image -

View Image - Figure A: We can use four points to define the rectangle.Table A: All points derived

Figure A: We can use four points to define the rectangle.Table A: All points derived

The list has three parts, the x-coordinate, the ycoordinate and the z-coordinate. Each of the parts is separated by a space and the list has a parenthesis at the beginning and ending.

We'll use two AutoLISP functions to extract the x- and y-coordinates from the list. The (car) function returns the z-coordinate and the (cadr) function returns the y-coordinate. The following is how we might use these functions to obtain the x- and y-coordinates from our variable, pt1.

View Image -

The x-coordinate is set to a variable named pt1x and the y-coordinate is set to pt1y. The (car) and (cadr) functions are used with the pt1 variable to extract the information.

Derive the other points

Once we've extracted the x- and y-coordinates, we need to add the width or height from the proper coordinate (x or y) and put the parts together to create the other points needed to draw the lines of the rectangle. We'll define the other points, as shown in Figure A.

Notice from Figure A that the other points can easily be derived by adding the width or height to the x- or y-coordinate of the starting point pt1. We derive the other points, as shown in Table A.

Point pt3 uses parts of pt2 to derive the x- and ycoordinate and point pt4 uses parts of pt1 and pt3 to derive its coordinates. Therefore, when we create our program, we need to make sure that we create the points in the order outlined in the table.

Once we have the x,and y-coordinates of a point, we need to put them together to make a usable list that AutoCAD can interpret. The (list) function takes individual items and puts them together as a list. We can use the (list) function as follows to put the x- and y-coordinates together and create a complete x,y-coordinate.

View Image -

For example, according to Table A, in order to create pt2, we need to create a list whose x-coordinate is the sum of the x-coordinate of pt1 and the width and whose y-coordinate is the y-coordinate of pt1. Here's how the code looks:

View Image -

Note that by the end of this line, we need to close three sets of parentheses!

Create the program in Notepad

Now that we've defined how to get the user input and how to derive all of the points to draw the rectangle, we need to start writing the code in Notepad. Open Notepad again, create the code in Listing A, and save the file as my box. lsp in your My Documents folder.

Note: When you type in Notepad, don't break each line of code as you see in Listing A. Here the lines of code are broken to fit within the column.

The code defines a custom command, MYBOX, includes the elements of code that we described, and uses the ( command ) function to draw the lines (using the LINE command) from the points we derived. The C in the last line uses the Close option of the LINE command to close the figure and complete the rectangle.

Try it out

To use the program in AutoCAD, enter (load "mybox") on the command line. Enter mybox on the AutoCAD command line. At the prompt, specify a point on the screen, and enter the width and height at the prompts. Notice that the program draws the box starting at the point where you picked and at the width and height you entered.

View Image - Listing A: AutoLISP code for the mybox program

Listing A: AutoLISP code for the mybox program

The door is open

We've opened the AutoLISP door for you by showing you how to create a fairly simple program. We also showed you how to save and execute the program in AutoCAD. The door is now open. Step through and create more AutoLISP programs on your own.

Copyright Element K Journals Jan 2006