Script (or other automation) to apply page setup to layout

Script (or other automation) to apply page setup to layout

lhaydenWCHJH
Explorer Explorer
1,123 Views
12 Replies
Message 1 of 13

Script (or other automation) to apply page setup to layout

lhaydenWCHJH
Explorer
Explorer

Howdy All,

 

I'm pretty new to automating CAD tasks, so I've been scouring forums and other corners of the internet trying to create a script to select and apply a different page setup in my paperspace layout.  

 

Backstory: Many of my drawings have PDF or None as the current page setup. I need to apply the already-loaded page setup "UEU" to be the current page setup for my active layout.

 

The following keystrokes work when I type them into my command line. 

pagesetup

u; selecting the page setup I want, based on the first letter of the setup name

alt+s; set current

alt+c; close

 

When I attempt to run this as a script, the popup window opens but does not highlight the appropriate page setup and thusly hangs up the rest of the script.  --After fixing the page setup selection, I'll need to figure out how to write alt+ into a script... but that's next step.--  🙂  

 

I've tried recording these steps as a macro but replay also hangs up after opening the page setup manager window.

 

I've tried inserting and redefining the page setup from another file, but it didn't apply the new setup as current.  

 

I've tried various lisps and scripts from forums/chatgpt, but these have only returned errors.

 

Anyone have a suggestion of what to try next? 

 

0 Likes
Accepted solutions (1)
1,124 Views
12 Replies
Replies (12)
Message 2 of 13

paullimapa
Mentor
Mentor
0 Likes
Message 3 of 13

lhaydenWCHJH
Explorer
Explorer

Thanks for the suggestion. Unfortunately, this solution gets me stuck in a page setup selection window which I cannot edit or trigger via command line. So, similar to what I've run into with other attempts.  

0 Likes
Message 4 of 13

paullimapa
Mentor
Mentor

Did you try the other one called Test that's on the same thread:

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/apply-page-setup-to-multiple-layouts...

What you enter is case sensitive & I did make one minor modification to allow for pagesetup names entries with a space and I'm calling this one Ps2L.lsp

; Ps2L.lsp type pagesetup name to assign to layouts (case sensitive)
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/apply-page-setup-to-multiple-layouts/m-p/4651451/highlight/true#M317264
(defun C:Ps2L (/ page_setup config l)
  (vl-load-com)
  (or acDoc (setq acDoc (vla-get-activedocument (vlax-get-acad-object))))
  (princ "\n************************************\n")
  (if
    (and
      (> (vla-get-Count (setq page_setup (vla-get-plotconfigurations acDoc))) 0)
      (vlax-for a page_setup (princ (strcat "\n" (car (setq l (cons (vla-get-name a) l))))))
      (not (textscr))
      (member (setq config (getstring T "\nSelect page setup: ")) l)
      )
     (vlax-for layout (vla-get-layouts acDoc)
       (if (not (eq (strcase (vla-get-name layout)) "MODEL"))
         (vla-copyfrom layout (vla-item page_setup config))
         )
       )
    )
;;;  (vla-regen acDoc acAllViewPorts)
  (graphscr)
  (princ)
  )

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 5 of 13

lhaydenWCHJH
Explorer
Explorer

Same error.

0 Likes
Message 6 of 13

paullimapa
Mentor
Mentor

Please elaborate what is the error message you're getting.

I tested the code on the attached PSM  Demo Dwg.dwg file.

Before running the function my current layouts with their page setups were:

paullimapa_0-1732049877847.png

Then at the command line I enter the following to load the lisp function:

(load"Ps2L")

At the command line I then enter:

Command: Ps2L

The Text screen window appears listing the Pagesetups available for me to type in:

paullimapa_1-1732049984806.png

After entering: PDF-ANSI C

I enter command PAGESETUP and I see all the layouts have been changed to that setup:

paullimapa_2-1732050061889.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 7 of 13

lhaydenWCHJH
Explorer
Explorer

I run the PS2L lisp and the following window pops up:

lhaydenWCHJH_0-1732050677094.png

The only way to select UEU is with mouse clicks.  Typing UEU or down arrow both do nothing to change which page setup is selected.    (I need no mouse input so I can batch apply these across multiple drawings)  Perhaps I'm missing some step here, or I need a different function. 

0 Likes
Message 8 of 13

paullimapa
Mentor
Mentor

Looks like you have another lisp file that has the exact same name but runs a completely different function.

To avoid this problem I've created a different function called TestMe.lsp

Save the attached & to load drag & drop it into your drawing area or execute the following at the command prompt:

(load"TestMe")

Then to execute this lisp function enter at the command prompt:

TestMe

You should not be getting any popup windows for you to select from.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 9 of 13

lhaydenWCHJH
Explorer
Explorer

Company policy won't allow me to download this .lsp.   Can you paste the code to this conversation?

0 Likes
Message 10 of 13

paullimapa
Mentor
Mentor
Accepted solution

sure...similar as before but just changed the function name to TestMe:

; TestMe.lsp type pagesetup name to assign to layouts (case sensitive)
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/apply-page-setup-to-multiple-layouts/m-p/4651451/highlight/true#M317264
(defun C:TestMe (/ page_setup config l)
  (vl-load-com)
  (or acDoc (setq acDoc (vla-get-activedocument (vlax-get-acad-object))))
  (princ "\n************************************\n")
  (if
    (and
      (> (vla-get-Count (setq page_setup (vla-get-plotconfigurations acDoc))) 0)
      (vlax-for a page_setup (princ (strcat "\n" (car (setq l (cons (vla-get-name a) l))))))
      (not (textscr))
      (member (setq config (getstring T "\nSelect page setup: ")) l)
      )
     (vlax-for layout (vla-get-layouts acDoc)
       (if (not (eq (strcase (vla-get-name layout)) "MODEL"))
         (vla-copyfrom layout (vla-item page_setup config))
         )
       )
    )
;;;  (vla-regen acDoc acAllViewPorts)
  (graphscr)
  (princ)
  )

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 11 of 13

lhaydenWCHJH
Explorer
Explorer

This test file worked so I closed my autocad to reset all lisps. After that, your PS2L lisp worked like a charm.   

 

Thanks ever so much for your help!

0 Likes
Message 12 of 13

paullimapa
Mentor
Mentor

glad to have helped...cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 13 of 13

phanaem
Collaborator
Collaborator

@paullimapaThanks for editing my lisp. Anyway, selecting the page setup by typing is not such a good idea, so I updated the lisp.

@lhaydenWCHJHIf the lisp works after restarting, it might be in conflict with other lisp. I guess is the acDoc variable. I've seen it used as a function in some other lisp programs.

Attached is the updated version. It allows to apply the same settings to all the layouts, but now you have the option to select a Layout as the source or a predefined page setup. Page setup, even with it's advantages, might not be so commonly used.

When the lisp displays the available sources, select one by picking it from the list or type the number associated with it (the number displayed as prefix).

phanaem_0-1732108938698.png

 

If needed, I think it could be modified to select the destination layouts. For now, the setup is applied to ALL layouts.

 

; Apply plot configuration to all layouts 
; Stefan M.
; 26.11.2013 https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/apply-page-setup-to-multiple-layouts/m-p/4651451/highlight/true#M317264
; updated 20.11.2024

(defun C:SetPlot (/ *error* acDoc srtm dyn msg plot_config config config_list layout_source)
  (vl-load-com)
  (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))

  (if (= 8 (logand 8 (getvar 'undoctl))) (vla-endundomark acDoc))
  (vla-startundomark acDoc)
  
  (setq srtm (getvar 'shortcutmenu))
  (setq dyn (getvar 'dynmode))

  (setvar 'shortcutmenu 8)
  (setvar 'dynmode 3)

  (defun *error* (msg)
    (if msg
      (progn
        (if
          (not (wcmatch (strcase msg) "*EXIT*,*CANCEL*,*ABORT*"))
          (princ (strcat "\nERROR: " msg))
        )
      )
    )
    (setvar 'shortcutmenu srtm)
    (setvar 'dynmode dyn)
    (if (= 8 (logand 8 (getvar 'undoctl))) (vla-endundomark acDoc))
    (princ)
  )

  (or *source* (setq *source* "Layout"))

  (initget "Layout Pagesetup")

  (setq *source* (cond
                   ((getkword (strcat "\nSpecify plot setup source [Layout/Pagesetup] < " *source* ">: ")))
                   (*source*)
                 )
  )

  (cond
    ((eq *source* "Pagesetup")
     (if
       (and
         (setq msg "\nNo plotting configurations found.")
         (> (vla-get-Count (setq plot_config (vla-get-plotconfigurations acDoc))) 0)
         (progn
           (vlax-for a plot_config
             (if
               (eq (vla-get-modeltype a) :vlax-false)
               (setq config_list (cons (vla-get-name a) config_list))
             )
           )
           config_list
         )
         (setq msg "\nPlot configuration not selected.")
         (setq config (_select_from_list "Select page setup: " config_list))
         (setq config (vla-item page_setup config))
       )
       (progn
         (vlax-for layout (vla-get-layouts acDoc)
           (if
             (not (eq (strcase (vla-get-name layout)) "MODEL"))
             (vla-copyfrom layout config)
           )
         )
         (vla-regen acdoc acactiveviewport)
       )
       (if msg (princ msg))
     )
    )
    ((eq *source* "Layout")
     (if
       (and
         (setq msg "\nSource layout not selected.")
         (setq layout_source (_select_from_list "Select page setup: " (layoutlist)))
         (setq config (vla-item (vla-get-layouts acDoc) layout_source))
       )
       (progn
         (foreach layout (layoutlist)
           (if
             (not (eq layout layout_source))
             (vla-copyfrom (vla-item (vla-get-layouts acDoc) layout) config)
           )
         )
         (vla-regen acdoc acactiveviewport)
       )
       (if msg (princ msg))
     )
    )
  )

  (*error* nil)
  (princ)
)

;Select an item from a popup menu
(defun _select_from_list (msg lst / strcat_list k i ini_list opt_list)

  (defun strcat_list (lst sep)
    (apply 'strcat
      (mapcar 'strcat
        (cons "" (mapcar '(lambda (x) sep) (cdr lst)))
        lst
      )
    )
  )
  
  (repeat (setq i (length lst))
    (setq ini_list (cons (itoa (setq i (1- i))) ini_list))
  )

  (setq opt_list (mapcar
                   '(lambda (a b)
                      (strcat a " " b)
                    )
                   ini_list
                   lst
                 )
  )

  (initget (strcat_list ini_list " "))
  (setq k (getkword (strcat "\n" msg " [" (strcat_list opt_list "/") "]: ")))

  (if k
    (nth (vl-position k ini_list) lst)
  )
)

 

 

0 Likes