Lesson 9
Lesson 9
SAW.LSP
Preparallon
,.Clean up
(redra-.v)
(setvar "cmdecho" CE-SAV)
(pnnc)
)
In a previous module we learned the function (getvar). This funct1on "go' a system vanable stored in AutoCAD.Frequently
when writing an AutollSP routine, you'll want the user to provide information:the name of a layer, the endpoint of a line, the
angle of rotation, the number of rows, etc. Autol!SP provides a means of requesting user input through the (getxxx) functions
(getint [prompt])
This function pauses for integer input and returns an integer value. The [prompt] here and throughout is optional. though
recommended, information for the user. The prompt must be in string form.
(getreal [prompt])
(getstring [ T ] [prompt])
This function pauses for string input and returns a string value.
The optionalT disables the spacebar for use as the <enter> key, allowing use of spaces in a string.The T, which always has a
value of Tor non-nil, is most commonly used as the flag, but anything that has value can be used, such as a number.
Use the T option with (getstring) when the user will type in multiple words or numbers.
Do not use the T option when the user expects the spacebar to act as <enter>,or when spaces are mvalid (as when
prompting for a layer name in previous versions of AutoCAD).
This function pauses for point input and returns a point list of {x y z). The optional point-list is used as a basepoint for the ' drag"
line.
will show from the first point you picked.> {2.4 5.7 0.0) actual returned value may val}'
This function pauses for realinput and returns a real value.The user can enter a decimalnumber,or a number based on the
current linear unit measurement setting, or can pick two points and the distance between those points will be returned.The
optional[point-list] is used as basepoint for 'drag" line requiring user to only enter the second point.
All inputs for points are available:pick, type in absolute or relative coordinates, use an object snap, point filter, or tracking.
(getdist "Type in a d1stance or pick two points: ") Type in a distance or pick two points: <pick point> Specify second point:
<pick point>
9.125 actual returned value may vary
Angles in AutoLISP are measured in radians. As long as AutoLISP is using the information this is no problem. But when
the angle needs to be presented to the user the radians must be reinterpreted in degrees. Later we will show you two
programs (DegreesToRadians) and (RadiansToDegrees) that will do the translating for you.
If you have changed the angle base or direction from the default (0 is the positive X-axis and counter-clockwise) then
you might need to use the (getorient) function.
Note that the choice of which (getxxx) function to use depends on the data type of the desired response.Only the
appropriate data type will be accepted as input. If you enter the wrong type of data, AutoLISP will automatically ask you to
try again.
Command: (getint "Type a number: ;
Type a number:1.25
Requires an integer value.
Type a number:3
3
Always nest one of these (getxxx) functions inside a (setq) so that you can recall the information later.
Writing Prompts
Write prompts so that the user will know exactly what you expect.
(getinl "How many rows: ')
Always capitalize the first character of the prompt and use a colon and a space to finish up the string.This makes it look
like other AutoCAD prompts.Don't capitalize all of it because 11looks like you are YELLING.
PRACTICE
5. At the Command: prompt, type the following and pick points or give numbers as prompted:
Point:"))
Radius: "l l
You will want to use an AutoLISP routine over and over again and not have to type it in at the Command line every time. An
AutoLISP routine can be created in any text editor or word processor, and then saved as a file in ASCII text format
You will want to use an AutoliSP routine over and over again and not have to type it in at the Command line every time.An
AutoLISP routine can be created in any text editor or word processor, and then saved as a file in ASCII text format
AutoCAD itself includes a powerful text editor and debugger called Visual LlSP.We will use VisualLISP as our text editor and
later learn some of its debugging features.
To open the Visual LISP editor,choose Tools>AutoLISP>VisualLISP Editor from the pulldown, or type VLIDE at the
Command line.
All AutoUSP files must have the .LSP file extension.Visual liSP saves to this extension by default
There is no word-wrapping in Visual LISP.
You can have multiple AutoUSP files loaded at once.
Visual LISP has an automatic backup feature on by default that saves a file to filename._sp. It is controlled under
Tools>Environment Options> General Options.
Color Coding in the Visual LISP Editor
One thing you will notice as soon as you load a file into the Visual LISP editor is the color coding. This helps to identify each
type of data in a routine.
4. Start a new file in the VisualLISP editor and save it to the name BOX1.LSP. Using the (setq),(getpoint),and (command)
functions, write up a routine that will request user input of four points and construct a line through each point to create a
polygon.
A complete solution to this exercise is on your class disk as BOX1-A.LSP and on the next page.
Solution
Self Check:
Setting up an AutoLISP Routine
2.What function will allow the user to input a real value by picking two points?