AutoCAD 2015 and AutoCAD LT 2015 Bible

The most comprehensive AutoCAD book around!

Another way to align a rectangle with a line on the fly–using AutoLISP

This is a guest post by Sanjay Kulkarni, an AutoCAD programmer. You can read more about him at the end of this post.

In an earlier post, Ellen showed how to align a rectangle with a line on the fly. You can easily create the same effect using AutoLISP. This AutoLISP routine is simple, and you can use the code as a starting point for your own routines.

First, Ellen has created a video to show you how this works.

Here are the steps to use this AutoLISP routine:

  1. Download the AutoLISP file. You need to do this only once, of course.
  2. Start the APPLOAD command. In AutoCAD 2012, you can go to Manage tab>Applications panel>Load Application.
  3. In the Load/Unload Application dialog box, make sure that the Files of Type drop-down list displays the .lsp extension. In the Look In box, navigate to the AutoLISP file and select it.
  4. Click the Load button. You should see a message at the bottom of the dialog box saying that the file was successfully loaded.
  5. Click Close to close the dialog box. For a quicker method of loading a program, see my tip, “Loading AutoLISP programs quickly“.
  6. Draw a diagonal line in your drawing.
  7. On the command line, type ra and press Enter.
  8. Follow the prompts. At the Select the line to align rectangle with: prompt, select the line to align the rectangle with. The program repeats the prompt until you select a line or press the Escape key to abort the program.
  9. At the Specify first corner point or [Chamfer/Elevation/Fillet/Thickness/Width]: prompt, specify the first corner of the rectangle. You can now see that the rectangle is aligned with the line.
  10. At the Specify other corner point or [Area/Dimensions/Rotation]: prompt, specify the other corner of the rectangle to finish the routine and create a rectangle aligned with the selected line.

Tip: You can repeat the command in the drawing as long as the drawing is open. But if you open another drawing you will need to reload the program. To avoid having to do this, you can add it to your Startup Suite. In the Load/Unload Application dialog box, drag the AutoLISP file from the list of files at the top to the Startup Suite in the lower-right section of the dialog box.

I won’t show you the actual code in all my posts, but this one is simple enough that it will be useful to look at its structure. As I said, you can use this same structure for many situations. It’s similar to a script file; you are using the COMMAND function to execute an AutoCAD command. The code also defines an alias, RA. The PAUSE function waits for your input. Of course, the code also defines some variables, does some error checking, sets up the prompts, and a little more.

(defun RectAlin ()
(setq oOSMODE (getvar “osmode”))
(setvar “osmode” 0)
;;; selecting the line
(setq theLine nil)
(while (= theLine nil)
(setq theLine
“\nSelect the line to align rectangle with [Escape to cancel]:”
(setq eLstLine (entget theLine))
(if (/= (cdr (assoc 0 eLstLine)) “LINE”)
(setq theLine nil)
(setq sPt (cdr (assoc 10 eLstLine)))
(setq ePt (cdr (assoc 11 eLstLine)))
;;; Create aligned rectangle
(command “rectang” PAUSE “R” “P” sPt ePt PAUSE)
(setvar “osmode” oOSMODE)
(defun c:RA ()

Do you use AutoLISP in this way? Let us know how you use simple routines like this in your work–leave a comment!

Sanjay Kulkarni is an experienced CAD (AutoCAD, Inventor, SolidEdge, CATIA, NX) programmer and a member of the Autodesk Developer Network. He is fluent in AutoLISP, VBA, and VB.NET. He has written for AugiWORLD and Inside AutoCAD (a monthly magazine that has since gone out of publication). He can be contacted at

Ellen Finkelstein

3 comments to Another way to align a rectangle with a line on the fly–using AutoLISP

  • James Donaldson

    How do you draw the rectangle to a specific size using the RA lsp? I tried both the @ and # and got totally different results and not the required size of rectangle.
    The lsp redefine’s the rectangle command to draw at the selected angle. Is there any way to keep RT (rectangle) to keep the drawing snapang and the RA the selected angle? The ability to do both would really speed things along.

  • Sanjay Kulkarni


    Thank you for your response.

    If I understand correctly, you want to draw a rectangle of specific size (length & width) aligned to a line.

    If so, you can use the Dimension option to the prompt
    Specify other corner point or [Area/Dimensions/Rotation]:
    that appears after you click the first point.

    This is the normal working mode of the Rectang command. This is logical since after specifying the two end points of the selected line for rotation, the RA.lsp passes control to the normal Rectang command.

    I hope this helps. If not let me know.

    Excuse me for the delay in responding.

  • Eddy Vandeghinste


    Thanks, great routine .i loaded this morning and use it already.

    Can you help me :when i rotate the ucs around the Z-axes the color is changing or something going wrong because i can’t see no more the crosshairs.


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=""> <s> <strike> <strong>