AutoCAD LT Automatic Layouts & Viewports

AutoCAD LT Automatic Layouts & Viewports

josemiguel5TJSU
Contributor Contributor
498 Views
2 Replies
Message 1 of 3

AutoCAD LT Automatic Layouts & Viewports

josemiguel5TJSU
Contributor
Contributor

Hi! I managed to make a LISP for automatic layout and viewport creation that works in AutoCAD LT 2026.1 (Won't run on full AutoCAD).

 

You can change the Matrix here:

 

 (setq pairs
    '(
      (1 0) (2 0)
      (0 1) (1 1) (2 1)
      (0 2) (1 2) (2 2)
  (1 3)
    )
  )

 

And Delta X / Delta Y here;
 
(setq dx (* 42.0 (car p)))
(setq dy (* 42.0 (cadr p)))
 
Usage:
 
1) Must run from a master layout, which will be copied and have it's viewport shifted by dx and dy, according to the Matrix scheme.
2) Requires AutoCAD LT version 2026.1 (August 2025 Update).
 
Notes:
 
3) Will not run on full AutoCAD, because the DXF internal codes of the viewport-type entity differ in LT (Viewport witdh and lenghts are stored in DXF codes 40 and 41 in AutoCAD LT, while in full AutoCAD they are stored in DXF codes 41 and 42). For full AutoCAD there allready exist other lisps that will simply change the model coordinates for each Viewport.
4) I had to do a double stretch in each direction followed by a move in opposite directions, because in LT the Viewport model coordinates are locked (you cannot change them or pan inside the viewport window, programatically).
5) I used ChatGPT to help code, since my LISP knowledge is very poor.
6) I attach a simple dwg example, for testing purposes.
 
Full Code:
 
;; ========================================================
;; Helper: stretch and counter-move viewport
;; ========================================================
(defun VM-BATCH-STRETCH (layout dx dy / ss en data width height hw hh cX cY origX origY newX newY deltaX deltaY p1 p2)
  (command "_LAYOUT" "_S" layout)
  (command "_REGEN")
  (setq ss (ssget "X" (list (cons 0 "VIEWPORT") (cons 410 layout))))
  (if (and ss (> (sslength ss) 0))
    (progn
      (setq en (ssname ss 0))
      (setq data (entget en))
      (setq origX (car (cdr (assoc 10 data))))
      (setq origY (cadr (cdr (assoc 10 data))))
      (setq width  (cdr (assoc 40 data)))
      (setq height (cdr (assoc 41 data)))
      (setq hw (/ width 2.0))
      (setq hh (/ height 2.0))
      (setq cX origX cY origY)

      ;; Horizontal stretch
      (if (/= dx 0)
        (progn
          (setq p1 (list (+ cX hw) (- cY hh)))
          (setq p2 (list (+ cX hw) (+ cY hh)))
          (vl-cmdf "_STRETCH" "_C" p1 p2 "" p1 (list (+ (car p1) dx) (cadr p1) 0))
          (command "_REGEN")
          (setq data (entget en))
          (setq width  (cdr (assoc 40 data)))
          (setq cX (car (cdr (assoc 10 data))))
          (setq hw (/ width 2.0))
          (setq p1 (list (- cX hw) (- cY hh)))
          (setq p2 (list (- cX hw) (+ cY hh)))
          (vl-cmdf "_STRETCH" "_C" p1 p2 "" p1 (list (+ (car p1) dx) (cadr p1) 0))
          (command "_REGEN")
        )
      )

      ;; Vertical stretch
      (if (/= dy 0)
        (progn
          (setq data (entget en))
          (setq height (cdr (assoc 41 data)))
          (setq cY (cadr (cdr (assoc 10 data))))
          (setq hh (/ height 2.0))
          (setq p1 (list (- cX hw) (+ cY hh)))
          (setq p2 (list (+ cX hw) (+ cY hh)))
          (vl-cmdf "_STRETCH" "_C" p1 p2 "" p1 (list (car p1) (+ (cadr p1) dy) 0))
          (command "_REGEN")
          (setq p1 (list (- cX hw) (- cY hh)))
          (setq p2 (list (+ cX hw) (- cY hh)))
          (vl-cmdf "_STRETCH" "_C" p1 p2 "" p1 (list (car p1) (+ (cadr p1) dy) 0))
          (command "_REGEN")
        )
      )

      ;; Counter-move
      (setq data (entget en))
      (setq newX (car (cdr (assoc 10 data))))
      (setq newY (cadr (cdr (assoc 10 data))))
      (setq deltaX (- origX newX))
      (setq deltaY (- origY newY))
      (vl-cmdf "_MOVE" en "" '(0 0 0) (list deltaX deltaY 0))
      (command "_REGEN")
    )
  )
)

;; ========================================================
;; Main command: clone layouts and apply stretches
;; ========================================================
(defun c:VM-LAYOUTA01 (/ layoutName layouts dxdy newLayout layoutCounter dx dy i)
  (vl-load-com)
  (setq layoutName "VM01")
  (command "_LAYOUT" "_S" layoutName)

  (setq pairs
    '(
      (1 0) (2 0)
      (0 1) (1 1) (2 1)
      (0 2) (1 2) (2 2)
	  (1 3)
    )
  )

  (setq layouts (list layoutName))
  (setq dxdy (list (list 0 0)))
  (setq layoutCounter 2)

  (foreach p pairs
    (setq newLayout (strcat "VM" (if (< layoutCounter 10) "0" "") (itoa layoutCounter)))
    (command "-LAYOUT" "COPY" layoutName newLayout "")
    (setq dx (* 42.0 (car p)))
    (setq dy (* 42.0 (cadr p)))
    (setq layouts (append layouts (list newLayout)))
    (setq dxdy (append dxdy (list (list dx dy))))
    (setq layoutCounter (1+ layoutCounter))
  )

  (setq i 0)
  (while (< i (length layouts))
    (setq newLayout (nth i layouts))
    (setq dx (car (nth i dxdy)))
    (setq dy (cadr (nth i dxdy)))
    (VM-BATCH-STRETCH newLayout dx dy)
    (setq i (1+ i))
  )

  (princ "\nAll layouts cloned and viewport stretches applied.\n")
  (princ)
)
 
0 Likes
499 Views
2 Replies
Replies (2)
Message 2 of 3

-didier-
Advisor
Advisor

Bonjour @josemiguel5TJSU 

 

I didn’t understand what your program was supposed to do, it seems to be working.

On the other hand, I do not agree with your statement, the DXF codes are identical between Acad LT and Acad full.
Whether it is for the viewports or other entities.

 

Amicalement

Éternel débutant.. my site for learning : Programmer dans AutoCAD

DA

EESignature

0 Likes
Message 3 of 3

Sea-Haven
Mentor
Mentor

I have a couple of make layouts you can walk along a pline making layouts that match, or pick a point, in the 1st autorotate the layout to match, in both you can move the rectangs made rotate them or delete, also run again to make more, the actual make the layouts is done as a step 2.

 

Looking at your code very briefly compared to mine I select a sheet size and so select a title block to be used in a layout, part 2 of that is that the viewprt size is known for that title block, so internal scale of the viewport is set, all titles in a layout are at true size i1 1:1 scale.

 

Not free but cheap, think cup of coffee as I normally have to set it up to match a end users dwg's. Have a look at images.

 

Not tested in LT but can provide some test code to check that some VL functions are supported.

 

SeaHaven_0-1761695992599.png

SeaHaven_1-1761696025492.png

 

0 Likes