Lesson
1
Sample
program, mslot
|
|
This first lesson is merely a sample
AutoLISP program with each line numbered and explained. Its purpose
is simply to provide a first introduction to the "look and feel" of an
AutoLISP program.
The program is called mslot (which
is short for "milled slot," as shown above). The program asks the
user to enter the width of the slot (the diameter of the milling cutter),
then to locate the centers at both ends of the slot. The program
then creates the 2D profile view of the milled slot using a closed polyline
with two straight segments and two arc segments, as shown below.
Since this is the first sample program,
it has been kept very simple. There are several things that would
be added if it were written as a "finished" and "fool proof" program.
(For example, it would offer a carry-over default diameter for
the slot. Also, it would have an error routine.) However, even
though it is simple, it is still a practical program which adds a new capability
to AutoCAD.
These lessons are sprinkled with
samples of programming code -- sometimes just a single line, other times
several lines. However, when a complete program is given, the beginning
and end are marked with green marker lines, as below. You should
not only study this program, but also try it out as explained in the "Introduction"
one page back (see "Fifteen Lessons").
------
marker ------ beginning of working program ------ try it out ------
;| MSLOT, short for Milled SLOT
Copyright © 1998 Ronald W. Leigh
Requests width and two center points.
Draws a polyline with two straight and two arc segments.
Variables:
a/b Centers
a1/a2 Endpoints of arc around a
b1/b2 Endpoints of arc around b
ang Angle of slot centerline
obm Old blipmode setting, 0 or 1
r Radius of arcs
w Width of slot
|;
(defun c:mslot (/ a a1 a2 ang b b1 b2 obm r w) ;line
1
(setq obm (getvar "blipmode"))
;line 2
(initget 7)
;line 3
(setq w (getreal "\nWidth of slot: "))
;line 4
(setq r (/ w 2))
;line 5
(setvar "blipmode" 1)
;line 6
(initget 1)
;line 7
(setq a (getpoint "\nLocate first center: "))
;line 8
(initget 1)
;line 9
(setq b (getpoint a "\nLocate second center: ")) ;line 10
(setvar "blipmode" 0)
;line 11
(setq ang (angle a b))
;line 12
(setq a1 (polar a (- ang (/ pi 2)) r))
;line 13
(setq a2 (polar a (+ ang (/ pi 2)) r))
;line 14
(setq b1 (polar b (- ang (/ pi 2)) r))
;line 15
(setq b2 (polar b (+ ang (/ pi 2)) r))
;line 16
(setvar "cmdecho" 0)
;line 17
(command ".pline" a1 b1 "A" b2 "L" a2 "A" "CL") ;line 18
(setvar "cmdecho" 1)
;line 19
(setvar "blipmode" obm)
;line 20
(princ)
;line 21
)
;line 22
-------- marker
-------- end of working program -------- try it out --------
Explanation
-
The comment section
-
The section of the program above the
numbered lines is the comment section. These comments state the name
and purpose of the program, the variables, etc. When you load this
file into AutoCAD, the load function does not place comments in memory.
Comments can be included in two different ways. They can be placed
between
the two two-character combinations (semicolon-fence, and fence-semicolon)
similar to the twelve lines at the beginning of this program, or they can
be placed after a single semicolon similar to the twenty-two
line numbers.
-
Program sections
-
There are five sections in the program proper.
-
Lines 1-2, Prepare:
(program name, local variables, saving system variable setting)
-
Lines 3-11, Get input:
(width of slot, location of both centers)
-
Lines 12-16, Calculate: (determine
endpoints of polyline segments)
-
Lines 17-19, Draw: (use the
PLINE command to draw the slot)
-
Lines 20-22, Reset: (restore
system variable setting)
-
Line 1
-
The program proper starts at line 1. The opening parenthesis before
defun matches the very last closing parenthesis and thus encloses the entire
program. The defun function defines the program name, which in this
case is c:mslot. This name is the symbol by which the program will
be called. The c: makes this a program to be called at AutoCAD's
command prompt, and has nothing to do with Drive C. To run the program,
only MSLOT is entered at the command prompt. The list of variables
starts with a forward slash, which make all the variables local.
-
Line 2
-
Since the program is going to turn blipmode on and off (see lines 6 and
11), the program runs the risk of changing the setting that the user had
previously selected for blipmode. To avoid this, the value of blipmode
needs to be saved so it can be restored by the setvar function in line
20. The inner expression (getvar "blipmode") is executed first. This
expression retrieves the current setting of the "blipmode" system variable,
which will be either 0 (for "off") or 1 (for "on"). Then the setq
function assigns this value to the variable obm.
-
Line 3
-
The initget function initializes the next getreal function (in line 4)
so that the user must enter a number rather than merely hitting 'Enter'.
(Hitting 'Enter' would cause the getreal function to return nil, which
would then be assigned to variable w, which would cause the program to
crash in line 5.) The initget setting of 7 also keeps the user from
entering either zero or a negative number for the width of the slot.
-
Line 4
-
The getreal function prompts the user for the width of the slot.
After the user enters a number, the setq functions assigns that number
to variable w. The "\n" in front of the prompt forces the prompt
to appear at the left margin, at the beginning of a new line.
-
Line 5
-
The value in variable w (the width) is divided by 2 and assigned to variable
r (radius). Notice that, in the expression which divides the width
by 2 (/ w 2), the divide function (indicated by the forward
slash) comes first. This is known as prefix notation. (By the
way, don't confuse this forward slash with the one found in line 1.
There it indicates local variables. Here it indicates the division
function.)
-
Line 6
-
The setvar function turns blipmode on by setting system variable "blipmode"
to 1.
-
Lines 7
-
The initget function initializes the next getpoint function (in line 8)
so that the user must indicate a point rather than merely hitting 'Enter'.
(Hitting 'Enter' would cause the getpoint function to return nil, which
would then be assigned to variable a, which would cause the program to
crash in line 10.)
-
Line 8
-
The getpoint function pauses and prompts the user to locate the first center.
This location (actually a list of the three coordinates of the point) is
then assigned to variable a by the setq function.
-
Lines 9
-
(see line 7)
-
Line 10
-
Same as line 8 except the getpoint function has as its first argument the
variable a. If the user enters the two centers on screen rather than
using the keyboard, this first argument connects a rubber-band cursor to
point a as the user moves the cursor to point b.
-
Line 11
-
The setvar function turns blipmode off so that later (in line 18) the points
submitted to the PLINE command will not place blips on the screen.
-
Line 12
-
The angle function returns the angle (in radians) between centers a and
b, which is then assigned to variable ang.
-
Line 13
-
This line calculates the location of point a1 by using the polar function.
The polar function uses a base point, an angle, and a distance. The
base point is a. The angle is 90 degrees less than ang, but the polar
function needs radians so the expression (/ pi 2) is used in
place of 90. The distance is the radius of the arc, stored in variable
r.
-
Lines 14, 15, 16
-
Similar to line 13, except in lines 15 and 16 variable b is used as the
base point, and in lines 14 and 16 the "90 degrees" (/ pi 2) is added to
ang rather than subtracted.
-
Line 17
-
The setvar function is used to turn system variable "cmdecho" (command
echo) off. This is done so that the next line, which draws a four-segment
polyline, will not echo the PLINE prompts to the screen.
-
Line 18
-
The command function submits all of its arguments to the AutoCAD command
interpreter. In effect, it runs the PLINE command and then submits
the same information you would submit if you were running the PLINE command
from the keyboard, namely, point a1, point b1, "A" for arc mode, point
b2, "L" for line mode, point a2, "A" for arc mode, and finally "CL" for
close. The dot in front of the PLINE command forces AutoCAD to use
its built-in PLINE command (just in case someone has undefined PLINE).
-
Line 19
-
Command echo is turned back on.
-
Line 20
-
Blipmode is set back to the setting it had before the program was run.
The value of variable obm was set in line 2.
-
Line 21
-
Without this line, the last function in the program would be the setvar
function which would return a number (0 or 1) which would be echoed to
the screen. The princ function echoes nothing, and thus provides
a "clean exit" for the program.
-
Line 22
-
This final parenthesis closes off the first opening parenthesis which is
found before the defun function in line 1.
HOME
Copyright ©
1988, 1998 Ronald W. Leigh