|
|
|
AutoLISP 1.03 - Custom AutoCAD Commands and Asking Questions AutoLISP and Visual LISP programs can be used to custom AutoCAD commands.
Executing an AutoCAD Commands in LISP Syntax - (command [arguments] ...)
The command function evaluates each argument and sends it to AutoCAD in response to successive prompts. It submits command names and options as strings, coordinate points as a list of reals (2D points as 2 reals and 3D points as 3 reals). LISP Example to type at AutoCAD command prompt - (setq PT1 (list 1.0 1.0)) ;set start point (setq PT2 (list 2.0 2.0)) ;set end point (command "LINE" PT1 PT2 "") ;issue LINE command to AutoCAD, draw from PT1 to PT2, "" ends LINE command If you needed a line drawn for 3 points you would have to (setq PT3 (list 3.0 3.0)) and change LISP to (command "LINE" PT1 PT2 PT3 "")
To learn how to create LISP programs, type the command you would like to create at the command prompt, writing down each response you give.
AutoCAD command Example #1 (type at command prompt) - Command: CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: 2P
Specify first end point of circle's
diameter: pick point LISP Program Example - (command "CIRCLE" "2P" PT1 PT2) ;using previously set start and end points
AutoCAD command Example #2 (type at command prompt) - Command: CIRCLE Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: pick point Specify radius of circle or [Diameter] <3.3997> 2.0
LISP Program Example - (command "CIRCLE" PT1 "2.0") ;using previously set point and string number for radius or (command "CIRCLE" PT1 2.0) ;using previously set point and real value for radius
AutoCAD command Example #3 (type at command prompt) - Command: -LAYER (Note the "-" before Layer command to issue layer command without dialog box)
Current layer: "0" LISP Program Example - (command "-LAYER" "M" "1" "C" "1" "1" "LT" "DASHED" "1" "") ;the "" ends the layer command
Control Characters in Strings
AutoLISP "GETxxx" Functions AutoLISP has many "GETxxx" functions that pause for user input of the indicated type and returns the value of the same type entered. This is the method used to allow users to input values as needed in programs.
GETINT Syntax - (getint [message]) Example - (setq Inum (getint "\nInput Integer ? ")) ;type at command prompt Input #1 - Command: Input Integer ? 25 ;integer input returns: 25 Input #2 - Command: Input Integer ? 25.0 ;real number input Requires an integer value Input Integer ? ;allows user to re-input integer Input #3 - Command: Input Integer ? S ;non-numeric character entered Requires an integer value Input Integer ? ;allows user to re-input integer
Note - Values passed to getint can range from -32,768 to +32,767. If the user enters any character other than a number character, then getint displays the message "Requires an integer value," and allows the user to try again.
GETREAL Syntax - (getreal [message]) Example - (setq Rnum (getreal "\nInput Real Number ? ")) ;type at command prompt Input #1 - Command: Input Real Number ? 25.0 ;real number input returns: 25.0 Input #2 - Command: Input Real Number ? 25 ;Integer input returns: 25.0 Input #3 - Command: Input Real Number ? S ;non-numeric character entered Requires a numeric value Input Real Number ? ;allows user to re-input number
Note - Values passed to getreal are double-precision floating-point format. If the user enters any character other than a number character, then getreal displays the message "Requires a numeric value," and allows the user to try again. Real numbers between -1 and 1 must be contain a leading zero. Real numbers can also be expressed in scientific notation (example - 4.1e-6 equals 0.0000041).
GETSTRING Syntax - (getstring [cr] [message]) [cr] = If supplied and is not nil, this will allow the user to input spaces in their input string and must be terminated by pressing ENTER. Otherwise, the input string is terminated by pressing the SPACE or ENTER keys.
Example #1 - (setq Stir (getstring "\nInput String ? ")) ;type at command prompt Input #1 - Command: Input String ? test ;String Input returns: "test" Input #2 - Command: Input String ? 25.0 ;Numeric Input returns: "25.0" Input #3 - Command: Input String ? 25 ;Numeric Input returns: "25" Input #4 - Command: Input String ? C:\temp\ ;folder path input returns: "C:\\temp\\"
Example #2 - (setq Stag (getstring T "\nInput String ? ")) ;type at command prompt Input #1 - Command: Input String ? this is a test ;String Input returns: "this is a test"
Note - getstring will only return the first 132 characters in the input. If the input contains the backslash character "\", getstring converts it to two backslash characters "\\". This allows you to input folder paths.
GETPOINT Syntax - (getpoint [pt] [message]) [pt] = If supplied and is not nil, this will cause AutoCAD to show a rubberband from [pt] to current cursor location
Example #1 - (setq P1 (getpoint "\nInput Point ? ")) ;type at command prompt Input #1 - Command: Input Point ? pick point ;Point Input returns: coordinates of picked point in list format Input #2 - Command: Input Point ? 25.0,25.0 ;Numeric Input returns: (25.0 25.0 0.0) Input #3 - Command: Input Point ? 25,25 ;Numeric Input returns: (25.0 25.0 0.0) Input #4 - Command: Input Point ? S ;string input Invalid Point Input Point ? ;allows user to re-input number
Example #2 - (setq P0 (list 0.0 0.0)) (setq P1 (getpoint P0 "\nInput Point ? ")) ;type at command prompt Input #1 - Command: Input Point ? pick point ;Point Input showing rubberband from P0 returns: coordinates of picked point in list format
Note - getpoint will accept a single integer or real number as the pt argument, and use the AutoCAD direct distance entry mechanism to determine a point. This mechanism uses the value of the LASTPOINT system variable as the starting point, the pt input as the distance, and the current cursor location as the direction from LASTPOINT. The result is a point that is the specified number of units away from LASTPOINT in the direction of the current cursor location. This may produce unexpected results.
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
For questions or comments, please email
A'cad Solutions |