Lisp to batch rename layouts according to drawing file name

Lisp to batch rename layouts according to drawing file name

Anonymous
Not applicable
5,348 Views
4 Replies
Message 1 of 5

Lisp to batch rename layouts according to drawing file name

Anonymous
Not applicable

Hello

I am trying to create a Lisp that renames the existing layouts in a drawing according to the following logic:

- If the drawing has only one layout, rename it as the name of the drawing file.

- If there are two or more layouts, rename them as name of the file (1), name of the file (2)... in the order that they have in the current drawing (left to right), which may not be alphabetical.

 

I.E. A drawing called "MyDrawing" with one layout called "MyLayout" would have it renamed as "MyDrawing", whereas a drawing called "MyDrawing2" with three layouts named, in this order "D_Layout" "A_Layout" "B_Layout" will have them renamed as "MyDrawing2 (1)" /previously "D_Layout"/, "MyDrawing2 (2)" /previously "A_Layout"/ and "MyDrawing2 (3)" /previously "B_Layout/.

 

As a complete noob, going through the forums, I managed to find the solution for the simple case:

 (setq Q (length (layoutlist)))
( if (= 1 Q)

 (command "_-layout" "_rename" "" (vl-filename-base (getvar "dwgname")))
)

 I have seen that getting the layouts in left to right order is not such an easy task. I have tried to merge the suggestion given here

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/lisp-to-plot-layout-tabs-in-order/td...

in the else condition of the If, but I dont manage to make it work. I think it would look something like this:

 

(setq Q (length (layoutlist)))
( if (= 1 Q)

 (command "_-layout" "_rename" "" (vl-filename-base (getvar "dwgname"))) ;True condition
(
(vl-load-com)
(vlax-for layt (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
  (if (> (vla-get-TabOrder layt) 0)
    (setq layt_lst (cons (cons (vla-get-TabOrder layt) (vla-get-Name layt)) layt_lst))
  )
)
(setq layt_lst (vl-sort layt_lst '(lambda (x y) (< (car x) (car y)))))
(foreach layt layt_lst
  (setq layoutname (cdr layt))

; Do the layout name change

) ;Else condition of if
) ;if


Could anyone help me a bit? I don't know if the approach for going through the layouts is the more appropiated for my case, but all the codes in the forum seem a bit too high level for me, so I chose the one that looked the simplest.

 

Thank you very much.

Best regards

0 Likes
Accepted solutions (1)
5,349 Views
4 Replies
Replies (4)
Message 2 of 5

hmsilva
Mentor
Mentor
Accepted solution

Try

(vl-load-com)
(vlax-for layt (vla-get-layouts (vla-get-ActiveDocument (vlax-get-acad-object)))
    (if (> (vla-get-TabOrder layt) 0)
        (setq layt_lst (cons (cons (vla-get-TabOrder layt) (vla-get-Name layt)) layt_lst))
    )
)
(setq layt_lst (vl-sort layt_lst '(lambda (x y) (< (car x) (car y)))))
(setq name (vl-filename-base (getvar "dwgname"))
      i    0
)
(if (= 1 (length layt_lst))
    (command "_-layout" "_rename" "" name)
    (foreach layt layt_lst
        (command "_-layout" "_rename" (cdr layt) (strcat name " (" (itoa (setq i (1+ i))) ")"))
    )
)

Hope this helps,
Henrique

 

EESignature

Message 3 of 5

Anonymous
Not applicable

Wow. Works smoothly. Thank you so much.

All this vl-load world is fully new to me. Step by step.

Regards

0 Likes
Message 4 of 5

hmsilva
Mentor
Mentor

@Anonymous wrote:

Wow. Works smoothly. Thank you so much.

All this vl-load world is fully new to me. Step by step.

Regards


You're welcome!
Glad I could help

Henrique

EESignature

0 Likes
Message 5 of 5

sharpl
Advocate
Advocate

This as everything else form Lee Mac works like a clock, thank you, Sir!

<http://www.lee-mac.com/renumberlayouts.html>

0 Likes