Executing AutoCAD Commands through AutoLISP
Rather than have an entirely new set of drawing and editing commands, AutoLISP permits the user to tap all of AutoCAD’s existing commands from within AutoLISP using the (command) function.
Explanation: the (command) function
(command [command_name] [argument…])
The first argument to the (command) function is always the command name. It is followed by the command’s options input in the same order they would be typed at the AutoCAD “Command:” prompt.
- AutoCAD commands and options are typed in as strings.
- DO NOT use command aliases.
- You can include any AutoLISP symbol or expression that evaluates to an appropriate value.
(setq RADIUS 1.25)
(command “circle” “4,4” RADIUS)
- Use the predefined symbol PAUSE to wait for user input.
(command “circle” PAUSE RADIUS)
- The null string “” is the equivalent of hitting the spacebar or <enter> key in AutoCAD.
(command “LINE” PT1 PT2 “”)
The (command) function with no arguments is equivalent to canceling the active command.
(command)
nil
The (command) function always returns nil.
Examples
This AutoLISP routine:
(command “line” “0,0” “1,5” “”)
looks like this at the Command Line:
Command: line
Specify first point: 0,0
Specify next point or [Undo]: 1,5
Specify next point or [Undo]:
Command: nil
- This AutoLISP routine:
(command “circle” PAUSE “2”)
looks like this at the Command Line:
Command: circle
Specify center point for circle or [3P/2P/Ttr (tan tan radius)]: <pick a point>
Specify radius of circle or [Diameter]: 2
Command: nil
- This AutoLISP routine:
(setq STARTING_WIDTH 0.1
ENDING_WIDTH 0.1
POINT1 “2,2” POINT2 “4,4”
)
(command “pline” POINT1 “w” STARTING_WIDTH ENDING_WIDTH POINT2 “”)
looks like this at the Command Line:
Command: pline
Specify start point: 2,2
Current line-width is 0.0000
Specify next point or
[Arc/Close/Halfwidth/Length/Undo/Width]: w
Specify starting width <0.0000>: 0.100000000000000 Specify ending width <0.1000>: 0.100000000000000
Specify next point or
[Arc/Close/Halfwidth/Length/Undo/Width]: 4,4
Specify next point or
[Arc/Close/Halfwidth/Length/Undo/Width]:
Command: nil
PRACTICE
Try the following statements at the Command line, first using the (setq) command to specify several points, and then executing AutoCAD commands through AutoLISP. Estimated time for completion: 5 minutes.
(setq PT1 “0,0”)
(setq PT2 “6,3”)
(setq PT3 “2,5”)
!PT1
!PT2
(command “line” PT1 PT2 PT3 “”)
(command “circle” PT2 2.0 )
(command “line” PT1 PT3 “”)