Categories

AutoCAD 2013 & AutoCAD LT 2013 Bible

The most comprehensive AutoCAD book around!

AutoCAD 2012 Bible

Tutorial: Create a custom command for AutoCAD using AutoLISP

AutoLISP offers powerful programming capabilities to AutoCAD, but you don’t need to be a programmer to use it. You can fairly easily create a simple custom command that can make your work go more quickly. In this tutorial, you’ll create a custom command that draws a red circle in any location and with any radius. You could use it to encircle areas where corrections need to be made.

  1. Open Notepad.
  2. Type the following:

(defun c:redcircle (/ center pt-on-circumference)
(terpri)
(setq center (getpoint “Specify center of red circle:”))
(terpri)
(setq pt-on-circumference (getpoint center “Pick a point on the circumference:”))
(command “_circle” center pt-on-circumference)
(command “_chprop” “_last” “” “_color” “red” “”)
)

  1. autocad-tips-custom-command-autolisp-1Save the file in a folder that is in your support file search path. Name it redcircle.lsp (not .txt, the default for Notepad).
  2. In AutoCAD,  choose Manage tab> Applications panel> Load Application (the APPLOAD command) to open the Load/Unload Applications dialog box.
  3. Choose redcircle.lsp and click Load. You should see the message: redcirle.lsp successfully loaded at the bottom of the dialog box.
  4. Click Close.
  5. On the command line, type redcircle and press Enter.
  6. At the Specify center of red circle: prompt, specify the circle’s center.
  7. At the Pick a point on the circumference: prompt, pick a point on the desired circumference to specify the radius.

AutoCAD draws the circle and immediately makes it red.

autocad-tips-custom-command-autolisp-2

How does it work?

Here’s a line-by-line explanation:

(defun c:redcircle (/ center pt-on-circumference): Everything in AutoLISP is enclosed in parentheses, so you start with an open parenthesis. (You’ll close it at the very end of the routine.) defun followed by c: creates a custom command. Then add the name of the command. The parentheses after the command name are for variables that you’ll define. In this case, they are center and pt-on-circumference. The forward slash and space just have to be there.

(terpri): This function makes the upcoming prompt go on a new line so it’s easy to read.

(setq center (getpoint “Specify center of red circle:”)): The setq function means you’re defining a variable. Its name is center. Then you need another open parenthesis for getpoint which lets the user specify a point. After that is the prompt in quotation marks. You can put anything you want; it should be self-explanatory to the user. After that, you need to close both sets of parentheses.

(setq pt-on-circumference (getpoint center “Pick a point on the circumference:”)): Here again, you’re defining a variable, pt-on-circumference, and getting the point from the user. The only part that isn’t obvious is putting the center variable after getpoint. But you need to do this to create the rubberband line from the center; this is how AutoCAD measures the radius.

 (command “_circle” center pt-on-circumference): Using the command function accesses AutoCAD’s regular commands. The underscore before circle allows for translation; it isn’t necessary. You put the command name in quotation marks. Then you give AutoCAD the information it needs to create the circle, namely the center and radius (specified by a point on the circumference).  The AutoLISP routine applies the values you gave the variables when you picked the 2 points.

(command “_chprop” “_last” “” “_color” “red” “”): We now use the CHPROP command to make the circle red. This command is just perfect for AutoLISP routines where you want everything to be on the command line, rather than on the ribbon or in a dialog box. Then, you specify the last object created; this is applied to the Select objects: prompt. The 2 empty quotation marks end selection. Then you use the Color option and specify read as the color. All the options go in quotation marks. The final empty quotation marks end the command.

): This closes the first open parenthesis and ends the routine.

How do you use the routine?

Save the file with a filename extension of .lsp in a folder that’s in AutoCAD’s support file search path. If you don’t know how to load an AutoLISP file, see “How to load an AutoLISP program” and “How to load an AutoLISP program quickly.” Then just type the name of the command you created (in this example it’s REDCIRCLE) on the command line.

What else can you create?

Try creating an AutoLISP routine that draws a blue line.

Leave a comment and let us know what custom commands you created!

13 comments to Tutorial: Create a custom command for AutoCAD using AutoLISP

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>