Fifteen Lessons

Be sure to read the Introduction below this table before proceeding with these lessons.

  1 - Sample program  mslot  (draw milled slot)
  2 - Syntax, arithmetic, & nesting  area3  (calculate area of triangle, 3 sides known)
  3 - Variables, setq, & formulas  ba  (calculate bend allowance)
  4 - Editors, file format, & defun  presston  (calculate press tonnage)
  5 - User input & system variables  table  (draw blank table)
  6 - Geometry & command functions  squangle  (draw square at any angle) 
 boltcirc  (draws a circular pattern of holes) 
 balloon  (draw numbered leader balloon)
  7 - Trig functions  rightss  (solve right triangle, side-side known) 
 rightsa  (solve right triangle, side-angle known) 
 bctable  (create table of bolt circle dimensions)
  8 - Conditionals & getkword  solids  (calculate volume & surface area) 
 rf  (display reference file)
  9 - Iteration  ccircles  (draw concentric circles) 
 curvtext  (draw text on a circular baseline) 
 monthly  (calculate monthly mortgage payments)
10 - Strings & printing  sysvars  (display values in key system variables) 
 strfacts  (string facts, analyze a  sentence) 
 stext  (spaced text, user sets vertical spacing)
11 - List manipulation  ol (on-line, locate point on an imaginary line) 
 trimlist (identifies points near a central point)
12 - Files  write  (writes any number of lines to a file) 
 read  (reads file, displays or adds to drawing) 
 balloon2  (cf. balloon, saves balloon dia. in file)
13 - Entity access  c-scale  (scales several circles in one operation) 
 cove  (adds a cove, or reverse fillet, to two lines) 
 polycnc  (polyline's CNC information sent to file)
14 - Table access  balloon3  (cf. balloon2, saves more info. in file) 
 tstyles  (lists text styles by variable/fixed height)
15 - Miscellaneous  ba2  (cf. ba, adds default prompts & error routine)


Viewing the lessons

It is best to view these lessons with your browser window maximized.  This is especially true when you are looking at the illustrative code and the sample programs that are contained in each lesson.

There is a rather standard way of presenting AutoLISP programs that involves indenting nested functions.  This format helps the programmer and anyone else viewing the program to instantly "see" the structure of each section of the program, which is very helpful in understanding how the program works.  If your browser window is too narrow, you may not see the program the way it was intended to appear.

Elements in the lessons

These lessons are progressive; they build from the easiest topics to the more difficult ones.  The first lesson is merely a sample program with lots of documentation to explain how it works.  Each subsequent lesson includes the following three elements in various proportions:

The theory includes the terminology, concepts, and explanations.  You should read the theory carefully, looking up terms you don't understand in the glossary.
This includes suggested exercises and practical problems to solve.  Concepts often remain theoretical and impractical until you actually work with them, so don't skip this part of the lesson.
Sample programs
These sample programs serve to illustrate the topic at hand, so you should study them carefully.  The programs are also fully functional, so you should try them out as well.  (See the instructions below on the two methods of trying a program, and on loading a program into AutoCAD.)
The first lesson is merely a sample program with lots of documentation, it does not follow the above pattern.  In Lessons 2 and Lesson 3 the practice sessions take place at the AutoCAD command prompt.  Then, beginning in Lesson 4, the practice sessions involve writing programs in a text file and loading them into AutoCAD.  However, in all the lessons, the sample programs are meant to be tried out, that is, placed in a file, then loaded into AutoCAD, then run several times.  The following sections will help you accomplish that.

Trying a program -- preferred method

1.  Type the program
Experience indicates that the best way to try a sample programs is to type it yourself.  This forces you to take note of every detail of the program -- increasing your understanding of the program and making it easier to revise the program later.  As you type, remember that accuracy is very important.  Most of the program should be in lower case.  Every parenthesis, quotation mark, and even every space must be in place.  You should include the documentation that comes before the program proper, especially if you think you might want to revise the program.  You do not need to include the line numbers.
2.  Save it to file
Save the program to file under the appropriate name (same as the program being defined, without the "c:").  The file should have extension .LSP.  For example, if the name of the program (the part after "defun") is c:mslot, save the program in a file called MSLOT.LSP.  You will probably want to place this file in a new folder where you can keep all your sample programs.
Trying a program -- lazy method, if you must

If you don't want to type the programs, you can use a typical Windows capture method to save them, following these steps:

1.  Highlight the program
Highlight the sample program on screen by dragging the cursor through the entire program.  Be sure to highlight everything between the green "marker lines" that indicate the beginning and end of the program.  Do not highlight the marker lines.
2.  Place on clipboard
Use Ctrl-C to place the highlighted section on the clipboard.
3.  Paste into a text editor
Open Notepad (or any other text editor that can save files in plain ASCII format), and use Ctrl-V to paste the program.
4.  Save it to file

(See step 2 in the previous section.)
Loading a program into AutoCAD

After you have made a copy of the program using one of the above methods, and placed it in an .LSP file, you must load the program into AutoCAD.  In AutoCAD, enter

    (load "\\folder\\programname")

Include the opening and closing parentheses and the quotation marks.  (Regarding the double back-slashes: you can, if you prefer, replace each pair of double back-slashes with a single forward-slash.)  In place of folder use the name of the folder on which you saved the program.  In place of programname use the name of the file in which you saved the program.  (You don't need to add the .LSP extension because the load function looks for .LSP files by default).

It is also possible to place the file in a folder that is in the AutoCAD library path.  (Here is one easy way to find out what AutoCAD's library path is:  In AutoCAD, use the INSERT command and, when prompted for a block name, enter a name you know does not exist, either in the drawing or on file.  The error message that appears on screen will list all the folders in the library path.)  When you place the file in a folder in the library path, you can use the load function without including a path, as follows: (load "programname")

If the program loads successfully, the command line should report the name of the program that has been loaded from file.  If you receive an error message, check to make sure that the file exists where you thought it did, and that there are no mistakes in the content of the file.

Then run the program by entering its name.  (The program name follows the "defun", but does not include the "c:")

Be sure to experiment.  Don't just run the program once.  Run it several times with different input each time.  Then revise the program, save it, reload it, and run it several more times.  The more you work with AutoLISP programs, the more you will understand them.


Copyright © 1988, 1998, 1999 Ronald W. Leigh