CADLISPMASTER
Stop wasting time on repetitive AutoCAD tasks. CAD LISP Master provides free, efficient AutoLISP codes and clear tutorials designed to automate your workflow and speed up your drafting.

AutoCAD Equal Spacing: Distribute Objects Evenly in Any Area

Distribute and space objects equally in AutoCAD using EQA.lsp. Automatically calculate 1:1:1 equal spacing and centerline alignment horizontally or ve
Distribute and Arrange Objects Equally Instantly

Spacing out multiple blocks, light fixtures, or equipment labels evenly across a defined boundary in AutoCAD usually involves a lot of manual setup. You either run the DIVIDE or MEASURE commands, draw temporary construction lines, or pull out a calculator to figure out exact center-to-center distances. If layout dimensions change later, you have to redo all those calculations from scratch.

The EQA.lsp (Equal Arrange) script completely automates equal object distribution. Simply select your items and pick two diagonal points to define your boundary box. The routine instantly calculates 1:1:1 equal spacing and centers all selected objects along the layout axis in one step.


Key Features of EQA.lsp

  • Automatic Direction Detection: If your selection box is wider than it is tall, the script arranges your objects horizontally from left to right. If it is taller, it arranges them vertically from top to bottom.

  • Bounding Center Alignment: Instead of placing items based on insertion points, the script calculates the exact geometric bounding center of each object so everything lines up neatly along the midpoint axis.

  • Visual Reference Guide: Automatically draws a subtle reference line across the boundary box, allowing you to instantly verify the target alignment field.

  • Works on Mixed Geometry: Arranges blocks, text, circles, or grouped lines seamlessly in a single operation.


Step-by-Step Instructions

  1. Load EQA.lsp into your AutoCAD session using the APPLOAD command.

  2. Type EQA in the command prompt and hit Enter.

  3. Select all the objects (blocks, text, shapes) you want to arrange, then press Enter.

  4. Click to specify the first corner of your target boundary area.

  5. Click the opposite corner to form a rectangular boundary.

  6. The script will instantly sort, align, and distribute your objects with uniform spacing.


AutoLISP Code (EQA.lsp)

Copy the code below and save it as EQA.lsp inside your AutoCAD support folder:

(defun c:EQA ( / *error* acadObj doc space ss p1_ucs p2_ucs p1 p2 minX maxX minY maxY totalW totalH cX cY cnt i ent vla-obj minpt maxpt cenX cenY items spacing targetX targetY startPt endPt lineObj oldOsnap )
  (vl-load-com)
  (setq acadObj (vlax-get-acad-object)
        doc (vla-get-activedocument acadObj)
        space (if (= (getvar "TILEMODE") 1) (vla-get-modelspace doc) (vla-get-paperspace doc))
        oldOsnap (getvar "osmode")
  )
  (defun *error* (msg)
    (if doc (vla-EndUndoMark doc))
    (if oldOsnap (setvar "osmode" oldOsnap))
    (princ "\nCAD Lisp [www.daolpro.com]")
    (princ)
  )
  (vla-StartUndoMark doc)
  (princ "\nSelect objects to arrange: ")
  (setq ss (ssget))
  (if (and ss (> (sslength ss) 0))
    (progn
      (setq p1_ucs (getpoint "\nSpecify first corner of boundary: "))
      (if p1_ucs
        (progn
          (setq p2_ucs (getcorner p1_ucs "\nSpecify opposite corner of boundary: "))
          (if p2_ucs
            (progn
              (setq p1 (trans p1_ucs 1 0)
                    p2 (trans p2_ucs 1 0)
                    minX (min (car p1) (car p2))
                    maxX (max (car p1) (car p2))
                    minY (min (cadr p1) (cadr p2))
                    maxY (max (cadr p1) (cadr p2))
                    totalW (- maxX minX)
                    totalH (- maxY minY)
                    cX (/ (+ minX maxX) 2.0)
                    cY (/ (+ minY maxY) 2.0)
              )
              (setq cnt (sslength ss) i 0 items nil)
              (repeat cnt
                (setq ent (ssname ss i)
                      vla-obj (vlax-ename->vla-object ent))
                (vla-getboundingbox vla-obj 'minpt 'maxpt)
                (setq minpt (vlax-safearray->list minpt)
                      maxpt (vlax-safearray->list maxpt)
                      cenX (/ (+ (car minpt) (car maxpt)) 2.0)
                      cenY (/ (+ (cadr minpt) (cadr maxpt)) 2.0)
                )
                (setq items (cons (list cenX cenY vla-obj) items))
                (setq i (1+ i))
              )
              (if (> totalW totalH)
                (progn
                  (setq items (vl-sort items '(lambda (a b) (< (car a) (car b)))))
                  (setq spacing (/ totalW (1+ cnt)))
                  (setq i 1)
                  (foreach item items
                    (setq vla-obj (caddr item)
                          targetX (+ minX (* i spacing))
                    )
                    (vla-move vla-obj (vlax-3d-point (list (car item) (cadr item) 0.0)) (vlax-3d-point (list targetX cY 0.0)))
                    (setq i (1+ i))
                  )
                )
                (progn
                  (setq items (vl-sort items '(lambda (a b) (> (cadr a) (cadr b)))))
                  (setq spacing (/ totalH (1+ cnt)))
                  (setq i 1)
                  (foreach item items
                    (setq vla-obj (caddr item)
                          targetY (- maxY (* i spacing))
                        )
                    (vla-move vla-obj (vlax-3d-point (list (car item) (cadr item) 0.0)) (vlax-3d-point (list cX targetY 0.0)))
                    (setq i (1+ i))
                  )
                )
              )
              (setq startPt (vlax-3d-point p1)
                    endPt (vlax-3d-point p2)
                    lineObj (vla-addline space startPt endPt)
              )
              (vla-put-color lineObj 252)
            )
          )
        )
      )
    )
  )
  (vla-EndUndoMark doc)
  (princ "\nCAD Lisp [www.daolpro.com]")
  (princ)
)

Layout Tip

If you need your boundary guide line on a specific non-printing defpoints layer, you can modify the vla-put-color section in the LISP code to assign a designated layer instead of color 252.

Post a Comment