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
Specify second 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"
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
M
Enter name for new layer (becomes the current layer) <0>:
1
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
C
Enter color name or number (1-255): 1
Enter name list of layer(s) for color 1 (red) <1>: 1
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
LT
Enter loaded linetype name or [?] <Continuous>: DASHED
Enter name list of layer(s) for linetype "Continuous" <1>:
1
Enter an option [?/Make/Set/New/ON/OFF/Color/Ltype/LWeight/Plot/Freeze/Thaw/LOck/Unlock/stAte]:
enter
LISP Program Example -
(command "-LAYER" "M" "1" "C" "1" "1" "LT" "DASHED" "1" "") ;the "" ends the layer command
Control Characters in Strings
|
AutoLISP control characters
|
|||
|---|---|---|---|
|
Code
|
Description
|
Example | Returns |
|
\\
|
\ character
|
(princ "This is 1 -\\-") | This is 1 -\- |
|
\"
|
" character
|
(princ "This is a -\"-") | This is a -"- |
|
\e
|
Escape character
|
(princ "1\e2") | 1▐2 ;not sure of this usage |
|
\n
|
Newline character
|
(princ "Line 1\nLine 2") |
Line 1 Line 2 |
|
\r
|
Return character
|
(princ "12345\r") | 12345 then cursor at beginning of line |
|
\t
|
Tab character
|
(princ "1\t2\t3\nA\tB\tC") |
1 2 3 A B C |
|
\nnn
|
Character whose octal code is nnn
|
(princ "\123") | S ;not sure of this usage |
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.
|
Allowable input to the GETxxx user-input functions
|
||
|---|---|---|
|
Function name
|
Type of user input
|
User presses ENTER, Returns |
|
getint
|
An integer value on the command line
|
nil |
|
getreal
|
A real or integer value on the command line
|
nil |
|
getstring
|
A string on the command line
|
"" |
|
getpoint
|
A point value on the command line or selected from the screen
|
nil |
|
getcorner
|
A point value (the opposite corner of a box) on the command line or 2 selected point from the screen
|
nil |
|
getdist
|
A real or integer value (of distance) on the command line or determined by selecting 2 points on the screen
|
nil |
|
getangle
|
An angle value (in the current angle format) on the command line or based on 2 selected points on the screen
|
nil |
|
getorient
|
An angle value (in the current angle format) on the command line or based on 2 selected points on the screen
|
nil |
|
getkword
|
A predefined keyword or its abbreviation on the command line
|
"" |
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.