Set / update the layout names via LISP

This widget could not be displayed.

Set / update the layout names via LISP

Anonymous
Not applicable

I would like to set and update the layout names in DWG via LISP.

 

At the beginning, I don't know how many layouts I will need and in the case of revisions / changes, new layouts can be added at any time. 

 

I found a great LISP from Markus Hoffmann (many thanks to CADmaro), where after (my simple) adjustments only code sequence for the maximum number of layouts is missing (marked as "(missing code for the highest layout number)"). I can't find this definition and ask for your help.

 

The layout names should be: e.g. Page_1from8, Page_2from8, etc.

 

;Set / upgrade layout names to format: Page_XfromY

(defun c:RenameAllLayouts ()
  (vl-load-com)
  (vlax-for i (vlax-get-property
                (vlax-get-property
                  (vlax-get-acad-object)
                  'ActiveDocument
                  )
                'Layouts
                )
    (if (/= 0 (vlax-get-property i 'TabOrder))
      (if
        (vl-catch-all-error-p
          (vl-catch-all-apply
            'vlax-put-property
            (list
              i
              'Name
              (strcat
                "Page_"
                (itoa (vlax-get-property i 'TabOrder))
		"from"
		(missing code for the highest layout number)
                )

              )
            )
          )
         (princ
           (strcat
             "\nLayout "
             (vlax-get-property i 'Name)
             " can't be renamed, konnte nicht umbenannt werden, because the name has already been used."
             )
           )
         )
      )
    )
  )

;;; Quick command ALU
(defun c:ALU ()
  (c:RenameAllLayouts)
  )


(princ
  "\n\"ALU.lsp\" was loaded. Enter \"RenameAllLayouts\" or \"ALU\" to start."
  )
(princ)

 

Many thanks for your help in advance.

0 Likes
Reply
Accepted solutions (2)
2,073 Views
8 Replies
Replies (8)

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

I'm not very savvy with the "vl-" commands. Here's my take. It uses a function I created a while back.

 

(defun c:RenameAllLayouts ( / LayoutListOrdered cnt len name lays)
  ;Helper Function(s)
  (defun LayoutListOrdered ( / dLays lays oLays cnt)
    (setq dLays (cdr (assoc -1 (dictsearch (namedobjdict) "ACAD_LAYOUT"))))
    (setq lays (mapcar '(lambda (l) (cons (cdr (assoc 71 (dictsearch dLays l))) l)) (layoutlist)))
    (repeat (1- (setq cnt (1+ (length lays))))
      (setq oLays (cons (cdr (assoc (setq cnt (1- cnt)) lays)) oLays))
    );repeat
  );defun
  ;Prep
  (setq cnt 0)
  (setq lays (LayoutListOrdered))
  (setq len (itoa (length lays)))
  (setvar 'CMDECHO 0)
  ;Rename Layouts
  (foreach lay lays
    (setq name (strcat "Page_" (itoa (setq cnt (1+ cnt))) "from" len))
    (if (member name lays)
      (command "-LAYOUT" "_r" name (strcat "tmp_" (itoa cnt)))
    );if
    (command "-LAYOUT" "_r" lay name)
  );foreach
  ;finish up
  (setvar 'CMDECHO 1)
  (prompt "\nLayout Naming Complete.")
  (princ)
);defun

 

Best,

~DD


Need AutoLisp help? Try my custom GPT 'AutoLISP Ace':
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace

Anonymous
Not applicable

Hi DD,

 

many thanks for your help.

 

Your code works perfectly and is also easier than my example - great school for me, a beginner.

 

Best regards from Germany

 

PS: which method / tools do you use for LISP?

0 Likes

cadffm
Consultant
Consultant
Accepted solution

>>

(missing code for the highest layout number)
(itoa(length(layoutlist)))

Or you saves the number one time before you walk thru your layouts:
(Setq nLayouts (itoa(length(layoutlist))))

Then you need just 
place this:
nLayouts

 

Sebastian

CodeDing
Advisor
Advisor

@Anonymous ,

 


PS: which method / tools do you use for LISP?


I use:

- AutoCAD

- Notepad

- Developer Help Tools

- Lots of practice

 

Once you become familiar with the language, your ability to apply it to AutoCAD just comes naturally.

Best,

~DD


Need AutoLisp help? Try my custom GPT 'AutoLISP Ace':
https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace

cadffm
Consultant
Consultant

>PS: which method / tools do you use for LISP?

 

Autocads VLIDE

Das AutoLISP Kochbuch (oder autolisp.info)

F1 Help (Lisp and DXF Reference)

 

In German:

Nehme dir kein Ziel vor, sondern arbeite die nächsten Wochen das Tutorial durch.

 

Dieses Tutorial vermittelt grundlegende Dinge, neben ein paar AutoLISP-Funktionen zugleich Wissen wie man programmiert.

Das wichtigste ist aber die Info wie der Syntax ist, was es bedeutet und wie man sich weiterhelfen kann.

 

Gehe erst auf die nächste Seite wenn das wirklich sitzt, die so vermittelten Grundlagen sind Gold wert.

<Geschichten zum Kopf schütteln lasse ich an dieser Stelle mal aus>

 

Sebastian

Sea-Haven
Mentor
Mentor

Hi if you have a look at this it will return layout number directly so no need for a defun tabs is the layouts via vl

 

 

 

(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq tabs (vla-get-Layouts doc))
(vlax-for lay tabs
(setq x (vla-get-taborder lay))
rename layout using x
)

 

 

 

valentina_hadla_work_ecu
Community Visitor
Community Visitor

HELLO CD, 

thank you for your help, it worked perfectly 

can you please help me to change the start number of counting, I've tried 

I managed to change to change text  by replacing ("page_") with the name I needed

and delete the part ("from")

0 Likes

cadffm
Consultant
Consultant

Hi,

 

==>

(setq cnt 0)
 
 

 

 

Sebastian