Welcome to CADVANTAGE’s No Nonsense, Quick Start, Step by Step tutorial for AutoLISP.

This tutorial is part of the CAD-CAM @ Home scheme currently available in the city of Aurangabad in India.

The tutorial is a Goodwill Gesture for all computer users who take pains to go out of way and learn new things.

To learn more about computers in general and CAD CAM CAE in particular as detailed below, you may enroll for the
CAD++ [ CAD @ Home ] program at :

CADVANTAGE ( where AutoCAD is taught FREE )
C 2 - 6, N 7, CIDCO,
Opp. Onkar Gas, Behind Swamikripa STD,
Aurangabad 431003
India
Ph : 0240 - 480767
Email : CadGuruCool@yahoo.com
Webpage : www.geocities.com/CadGuruCool



AutoLISP Tutorial

Step 1

It is assumed that you are familiar with the basics of some version higher of AutoCAD R12.

The code that we will undertake should work in ACAD R12 through ACAD 2002.
Step 2

This tutorial assumes that you know what computer programming is and have prior experience with atleast one computer programming language.

We want to write a program in AutoLISP that will draw the milled slot as shown in figure.

The program will ask for :

1) CT = Cutter Traverse,
2) CD = Cutter Diameter, and
3) P1 = Cutter Start Point.
Step 3

We will use Notepad to write the AutoLISP program.

Click the Start button and Select Run.
Step 4

A dilaog box will appear.

Type notepad.

Click OK or press Enter on the keyboard.
Step 5

We want to be able to use the program by typing mslot at the AutoCAD command : prompt.
Step 6

Type the first line of the program as shown without understanding a byte of it.

Don't forget the last closing bracket at the end (shown with red arrow).

We will dissect the code in the next step.
Step 7

There are only two rules of syntax in AutoLISP :

1) All AutoLISP statements are enclosed in parentheses (brackets).

2) The function is followed by argument(s).

For example (+ 3 4)

Here, the function is + and the arguments are 3 and 4.

You can also type this at the Command : prompt and press Enter to see the result
Step 8

Next, we want the input from user ie. CT, CD and point P1 as discussed in Step 2. Variables will be required to store them. In AutoLISP, we need not declare the variables or their types.

Type (setq k 3.0) at the AutoCAD Command : prompt and press Enter. By doing this, you have created a variable of real type called K and stored the value of 3.0 in it, all in one stroke.

If later you want to check the value of the variable k, type !k ie. ! (exclaimation) followed by the variable name.
Step 9

In Step 8, the function setq stores in the variable immediately following it, the return value of the expression that comes next.

For eg. (setq g (+ k (- 12 2))) will store 13.0, in the variable g , where the innermost bracket (- 12 2) will be evaluated first and its answer (return value) will be added to K.

Type three more lines as shown on the right.
(defun c:mslot()
  (setq CT (getreal "Enter Cutter Traverse:"))
  (setq CD (getreal "Enter Cutter Diameter:"))
  (setq P1 (getpoint "Click Cutter Start Point:"))
)  ;; end of defun
Step 10

defun stands for Define Function.

C: indicates that the function name that comes next should be executable from the Command : prompt.

There should be no space between C: and mslot which is the function name.

( ) are part of the syntax and are related to memory management. We need not bother about memory management right now.

) at the end indicates that, like AutoLISP statements, the whole program is also enclosed within brackets.

You can put comments with ; ie. a semi-colon.
(defun c:mslot()

)  ;; end of defun
Step 11

The getreal function has one argument - the string, which is optional.

This function displays the string argument at the Command : prompt and waits for the user to enter a real number, hence the name getreal.

setq further stores it into the variable CT.
(setq CT (getreal "Enter Cutter Traverse:"))
(setq CD (getreal "Enter Cutter Diameter:"))
  (setq P1 (getpoint "Click Cutter Start Point:"))
Step 12

Similarly, the getpoint function has one argument - the string, which is optional.

This function displays the string argument at the Command : prompt and waits for the user to click using the mouse, hence the name getpoint.

setq further stores it into the variable P1.

You may also enter the coordinates for a point for getpoint.
(setq P1 (getpoint "Click Cutter Start Point:"))
Step 13

Click File > Save in notepad and save the file as mslot.lsp

Do not forget the . LSP extension.
Step 14

Switch to AutoCAD and type appload at the command: prompt.

If you are using ACAD 14 or earlier, click on the Files button at the top-right corner to get the standard file open dialog.

Select the mslot.lsp file.

Click the Open or Load button, depending on your version of AutoCAD.

A message -
mslot.lsp successfully loaded.
will appear. If you can't see the message, press F2
Step 15

Type mslot ie. the function name at the command: prompt.

You will get the Enter Cutter Traverse: prompt.
Type 6 and press Enter.

At the next prompt Enter Cutter Diameter: , type 1.5 and press Enter.

Next, the prompt Click Cutter Start Point: will appear.

At this stage, click anywhere in the drawing area and the coordinates of the point you clicked will appear at the commanad: prompt.

You can check the values by typing !CT or !CD or !P1 at the the command prompt and pressing enter as explained in Step 8.
Step 16

Using the Polar function, a new point can be created relative to a known point.
Step 17

Add four more lines for creating points P2, P3, P4 and P5.

In the first line, P2 is created relative to P1 at an angle of 90 degrees.

The polar function accepts angles in radians only, therefore, the angle 90 is written as (* 90 0.01745)
(setq P2 (polar P1 (* 90 0.01745) (/ CD 2)))
(setq P3 (polar P1 (* -90 0.01745) (/ CD 2)))

(setq P4 (polar P2 0 CT)) ;; P4 is created relative to P2
(setq P5 (polar P3 0 CT)) ;; P5 is created relative to P3
Step 18

Now we will create two lines of the slot with the command function of AutoLISP.

So, add the two lines as shown, after the polar lines but inside the last closing bracket.

Save the file in Notepad.

Load the modified mslot.lsp again using the appload command as explained in Step 14 so that AutoCAD comes to know of the additions.

Type mslot at the command: prompt and complete the steps.

You should get two lines on the screen.

There are two double quotes at the end of the command function for line meaning return.
(command "line" p2 p4 "") 
(command "line" p3 p5 "") 
__________________________________




Step 19

For drawing the left arc, observe the command sequence at the AutoCAD Command: prompt as shown below :


Note that we are specifying points in an anti-clockwise direction.

The AutoLISP equivalent of this is :
(command "arc" P2 "e" P3 "a" 180)
Again, note that there is no " " at the end of the arc command because in AutoCAD, we don't press Enter to terminate the arc command as we do in the line or erase command.

The next step is to add the above line to your AutoLISP program and try the other arc.

By the way, LISP stands for LIST Processing and we haven't even got anywhere near to it. So...... go to Step 20
Step 20

Want to dig deeper
into AutoLISP ?

Call CADVANTAGE Now !!! Ph : 0240 - 480767 in Aurangabad.