【正文】
ation you use in AutoCAD is to simply copy the text out of the text history window which you access using the F2 key. Then simply use the COPY function to copy the history text into your Notepad programming session.ErrorsWhen you inadvertently fet parenthesis, quote marks or some other key piece of code in your LSP file you will experience a variety of problems. To see these problems demonstrated try loading in the following programming examples and see what happens.(defun c:fz ()(setq old_radius (getvar “filletrad))(setvar “filletrad” 0)(mand “.fillet” pause pause)(setvar “filletrad” old_radius))The error here is a missing ” after the word FILLETRAD in the second line. You should see the Autolisp interpreter asking for a ” mark.(defun c:fz ()(setq old_radius (getvar “filletrad”))(setvar “filletrad” 0)(mand “.fillet” pause pause)(setvar “filletrad” old_radius)The error here is a missing ) at the end of the program to close the DEFUN statement. You should see the Autolisp interpreter asking for a closing parenthesis.(defun c:fz ()(setq old_radius (getvar “filletrad”))(setvar “filletrad” 0)(mand “.filllet” pause pause)(setvar “filletrad” old_radius))The error here is an extra L in the word FILLLET. You’ll notice that the function will actually run but will halt when the fillet mand should run.Input and Selection SetsIn the first two installments of our programming series I covered how to store variables, manipulate system variables and use the COMMAND statement to pass instructions to the AutoCAD editor. While these skills are key to writing basic programs you’ll find that to write truly flexible programs you’ll need to be able to:?Prompt your users for input values (numbers, text, points) ?Prompt your users for sets of data (using selection set identification) ?Filter the AutoCAD database for certain entity types ?Work with entity points (line endpoints, circle centers, etc) The mands we’ll be utilizing to achieve these goals are as follows:?GETREAL (to GET a user defined REAL number) ?GETINT (to GET a user defined INTeger number) ?GETSTRING (to GET a user defined STRING of text) ?GETPOINT (to GET a user defined POINT) ?SSGET (to GET a user defined Selection Set) ?SSGET with the “X” option (to filter the database for objects) ?CAR, CADR and CADDR (to break points into X, Y and Z ponents) As in past installments I’ll use short pieces of programming code to illustrate the concepts and ramp up the level of plexity gradually until you can understand even plex examples.Getting Values from the UserGetting user supplied values is straightforward in Autolisp as can be seen with these statements:(setq var1 (getreal “\nPlease input a REAL number: “))(setq var2 (getint “\nPlease input an INTEGER number: “))(setq var3 (getstring “\nPlease input a piece of text: “))Try pasting these examples into your AutoCAD mand line to run the statements interactively and you’ll see immediately what’s happening. The interesting thing to note is that when using the GETINT statement that you can’t input a real number or a piece of text. You’ll also notice that a prompt is generated by the mand so the user knows what information to input.Note: The \n character is simply a line feed that assures you that the prompt to the user will be placed on a new mand prompt line in the AutoCAD session!The result of these statements will be a stored variable VAR1, VAR2 or VAR3 that can be used in other programs. Here’s a simple example of a program you can run interactively that prompts the user then makes use of the stored variable:(setq dimtxt_height (getreal “\nDimension text height: “))(setvar “dimtxt” dimtxt_height)While this is a very simple example it illustrates the concept perfectly. Just acquire a value from the user then use the variable name in other mands instead of fixed values. (See examples from the previous newsletter issues if you have any questions on the SETVAR statement.)Getting and Working with PointsWhile getting a point from the AutoCAD user is very simple using this form:(setq point1 (getreal “\nPlease select a POINT location: “))Working with the points isn’t easy because the point is actually three pieces of data that must be picked apart. Point data is really a list of X, Y and Z coordinates lumped together which can be deposed using CAR, CADR and CADDR functions as follows:(setq point1_x (car point1))(setq point1_y (cadr point1)) (setq point1_z (caddr point1))Note that an intermediate variable called POINT1 stored the point while variables POINT1_X, POINT1_Y and POINT1_Z contact the actual values for the x, y and z coordinates, respectively.This CAR, CADR, CADDR approach requires equal ponents of memorization and getting used to, but you will find that you’ll grow accustomed to its usage faster than you think.Working with SetsYet another ponent of AutoCAD programming is that AutoCAD tends to operate on sets (AutoCAD calls them selection sets) of objects.