By Dennis Collin
When working with complex AutoCAD drawing files, there is often a need to present this information on several different drawing layouts. These drawing layouts usually manifest as physical paper drawing copies or more commonly now as digital plots in either a PDF or DWF format.
It is good practice to ensure that these layout tabs are properly named, usually in accordance with the unique drawing number on the titlesheet. Although these layout tabs can be renamed manually, in a similar way to MS Excel worksheets, it can be a bit awkward if they need to be renamed in a sequential manner. I was recently asked is there a way to batch rename a series of layout tabs, with a suitable prefix, numbering system and suffix etc.? The answer is no with out-of-the-box commands, but as I have posted previously both AutoCAD and the latest version of AutoCAD LT have access to a programming environment.
I located a useful Lisp routine on the Autodesk forums which makes the bulk layout renaming task much easier. Simply copy and paste the text below into a text file application like Notepad and save it to the following name, Relay.LSP
(defun c:relays ( / idx lst lyc obj pre srt suf num )
(vlax-for lay (setq lyc (vla-get-layouts (vla-get-activedocument (vlax-get-acad-object))))
(if (= :vlax-false (vla-get-modeltype lay))
(setq lst (cons (vla-get-name lay) lst)
srt (cons (vla-get-taborder lay) srt)
obj (cons lay obj)
)
)
)
(if (setq
pre (getstring t "\nSpecify prefix <none>: ")
suf (getstring T "\nSpecify suffix <None>: ")
num (1- (cond ((getint "\nSpecify starting number <1>: "))(1)))
srt (vl-sort-i srt '<)
obj (mapcar '(lambda ( n ) (nth n obj)) srt)
idx (LM:listbox "Select Layouts to Rename" (mapcar '(lambda ( n ) (nth n lst)) srt) 3)
)
(progn
;; Temporary rename to free up keys held by other layouts in the selection
(foreach n idx (vla-put-name (nth n obj) (vla-get-handle (nth n obj))))
(foreach n idx (vla-put-name (nth n obj) (getname lyc pre suf num)))
)
)
(princ)
)
(defun getname ( lyc pre suf int / int rtn )
(while
(not
(vl-catch-all-error-p
(vl-catch-all-apply 'vla-item
(list lyc
(setq int (1+ int)
rtn (strcat pre (if (< 9 int) (itoa int) (strcat "0" (itoa int)))suf)
)
)
)
)
)
)
rtn
)
;; List Box - Lee Mac
;; Displays a DCL list box allowing the user to make a selection from the supplied data.
;; msg - [str] Dialog label
;; lst - [lst] List of strings to display
;; bit - [int] 1=allow multiple; 2=return indexes
;; Returns: [lst] List of selected items/indexes, else nil
(defun LM:listbox ( msg lst bit / dch des tmp rtn )
(cond
( (not
(and
(setq tmp (vl-filename-mktemp nil nil ".dcl"))
(setq des (open tmp "w"))
(write-line
(strcat "listbox:dialog{label=\"" msg "\";spacer;:list_box{key=\"list\";multiple_select="
(if (= 1 (logand 1 bit)) "true" "false") ";width=50;height=15;}spacer;ok_cancel;}"
)
des
)
(not (close des))
(< 0 (setq dch (load_dialog tmp)))
(new_dialog "listbox" dch)
)
)
(prompt "\nError Loading List Box Dialog.")
)
( t
(start_list "list")
(foreach itm lst (add_list itm))
(end_list)
(setq rtn (set_tile "list" "0"))
(action_tile "list" "(setq rtn $value)")
(setq rtn
(if (= 1 (start_dialog))
(if (= 2 (logand 2 bit))
(read (strcat "(" rtn ")"))
(mapcar '(lambda ( x ) (nth x lst)) (read (strcat "(" rtn ")")))
)
)
)
)
)
(if (< 0 dch)
(unload_dialog dch)
)
(if (and tmp (setq tmp (findfile tmp)))
(vl-file-delete tmp)
)
rtn
)
(vl-load-com) (princ)
Once saved, add the Lisp routine to a suitable resource folder and load into your AutoCAD start-up suite using the Appload Function.
This routine can then be run by typing in ‘Relays’ resulting in the following prompt.
Specify a suitable prefix, suffix, number and start reference as required. Then select the layouts that require renaming or modifying and the Lisp routine will do the rest!
Once complete the selected layouts will be renamed to the new standard.
For more information on Lisp click here:
The original code came from Lee Mac’s site, which is a useful resource for all things relating to AutoLisp!
Whatever your discipline, it is beneficial to have a good understanding of all AutoCAD’s commands. These are covered on many of Cadline’s AutoCAD training courses, we provide training for all levels from Essentials to Advanced, including update training and bespoke workshops, in small groups to 1-2-1 sessions. Information on our training services can be found at https://training.cadline.co.uk/ where one of the training team will be happy to help.
Comments (0 comments)