The Basics in a Nutshell - Page II.
We now need to be able to "store" our AutoLisp routines in a
file.
AutoLisp files are simple ASCII text files with the extension
"lsp".
Open up Notepad or any other simple text editor and type in the following
:
(defun testline ()
;define the function
(setq a (getpoint "\nEnter First Point : "))
;get the first point
(setq b (getpoint "\nEnter Second Point : "))
;get the second point
(command "Line" a b "")
;draw the line
) ;end defun
Now, save this file as "testline.lsp" remembering, to save it
as a ASCII Text file and ensuring that it is saved in a directory in your
AutoCAD's search path. Now open AutoCAD and type this :
(load "testline")
This will load the function into memory. (Did you notice that you do
not have to stipulate the "lsp" extension?) Now type this :
(testline)
Your function should now run.
Let's edit this routine so that it acts like a standard AutoCAD command
:
(defun c:testline ()
;define the function
(setq a (getpoint "\nEnter First Point : "))
;get the first point
(setq b (getpoint "\nEnter Second Point : "))
;get the second point
(command "Line" a b "")
;draw the line
) ;end defun
By preceding the function name with c: we do not have to enclose the
name with brackets when running. Re-load the function and run it again.
(load "testline")
testline
Much better, Hey.
We do have one problem though. Did you notice that when we loaded the
routine, an annoying "nil" kept on popping up. We also got the
same "nil" returned to us when we ran the routine. To suppress
this "nil" when loading or
running, we can use the (princ) function. Here's what your routine would
look like :
(defun c:testline ()
;define the function
(setq a (getpoint "\nEnter First Point : "))
;get the first point
(setq b (getpoint "\nEnter Second Point : "))
;get the second point
(command "Line" a b "")
;draw the line
(princ)
;clean running
) ;end defun
(princ)
;clean loading
For more details on the (defun) function, refer to The AfraLisp
Tutorial :
Define Function (defun).
And for a more detailed expanation of loading AutoLisp routines, refer
to the AfraLisp tutorial :
Loading AutoLisp Files.
Now it's time to make AutoLisp do some calculations for us.
Let's say we wanted AutoLisp to draw a beam, in elevation, for us.
First of all we would start by getting input from the user regarding
certain parameters that we would need to draw the beam.
Here's what we are trying to draw, along with the values that the user
needs to input.
The values that we need to retrieve from the user are as follows :
Insertion Point ip
Length of Beam lb
Height of Beam hb
Flange Thickness wt
End Plate Thickness ep
Length of Notch nl
Depth of Notch nd
Let's write a routine to retrieve these values first.
(defun c:testbeam ()
;define the function
;********************************************************
;Get User Inputs
(setq lb (getdist "\nLength of Beam : "))
;get the length of the beam
(setq hb (getdist "\nHeight of Beam : "))
;get the height of the beam
(setq wt (getdist "\nFlange Thickness : "))
;get the thickness of the flange
(setq ep (getdist "\nEnd Plate Thickness : "))
;get the thickness of the end plate
(setq nl (getdist "\nLength of Notch : "))
;get the length of notch
(setq nd (getdist "\nDepth of Notch : "))
;get the depth of the notch
;End of User Inputs
;*********************************************************
;Get Insertion Point
(setq ip (getpoint "\nInsertion Point : "))
;get the insertion point
;********************************************************
(princ)
;finish cleanly
) ;end of defun
;**********************************************************
(princ) ;load cleanly
;**********************************************************
Load and run the routine. You will be prompted for all the values
listed above. Enter some numbers and then check the value of all the
variables by preceeding the value names with "!" (e.g. !ip).
O.K. we've got all the values we need from the user.
But first we need to do some calculations to determine the other points
required before we can draw the beam.
See you on the next page.......
|