CAD tip # 11780:
Question

Just set the two parameters - block- and attribute-name of your title block in the LISP file and load it with APPLOAD (drag to the briefcase icon) to every AutoCAD session. You can also optionally comment out the CommandEnded reactor and so check the attribute only on a layout switch, or you can run the renaming process only on open/save (the 3rd reactor). Use the LOAon and LOAoff commands to switch reactor(s) ON and OFF.
See example of the setting:
(setq _LOAblknameList "TitleBlock*") ; preset your title block name (setq _LOAattnameList "LayoutName,LName") ; preset your att. tag for the attribute ; carrying the requested layout name
The layout is then automatically renamed to the current value of the specified attribute in the title block (if any) inserted in the respective layout.
See sample video:
Updated version on Download
Source code:
;RenameLOA - change layout name dynamically by attribute value ;9/2024, V.Michl - www.arkance.world - www.cadforum.cz (setq _LOAblknameList "TitleBlock,AltBlock1,AllOther*") ; preset your title block name(s) - a list or wildcard ; mod 9/24 (setq _LOAattnameList "LayoutName,MyLayout") ; preset your att. tag for the attribute carrying the requested layout name - a list or wildcard ; mod 9/24 (vl-load-com) (setq _LOAdoc (vla-get-activedocument (vlax-get-acad-object))) (defun _LOAgetAttVal (lname / ss blk property props i) ; get attr value in layout(defun vl-getattributevalue ( blk tag ) (setq tag (strcase tag)) (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes) ) ) ;---- (defun getAttValues ( blk ) (setq blk (vlax-ename->vla-object blk)) (mapcar '(lambda ( att ) (cons (vla-get-tagstring att) (vla-get-textstring att))) (vlax-invoke blk 'getattributes)) ) ;---- (defun ssdynblk (effname / ssx c en blk) ;--------- *U modified dynblk (setq ssx (ssget "_X" (list (cons 0 "INSERT")(cons 2 "`*U*")(cons 410 lname)))) (setq c 0) (if ssx (repeat (sslength ssx) (setq en (vlax-ename->vla-object (ssname ssx c)) c (1+ c)) ; (if (= (strcase (vla-get-effectivename en)) (strcase effname)) (if (wcmatch (strcase (vla-get-effectivename en)) (strcase effname)) ; mod 9/24 (setq blk en) ) ) ) blk ) ;---- (setq ss (ssget "_X" (list (cons 0 "INSERT")(cons 2 _LOAblknameList)(cons 410 lname)))) ; all titles in this layout (if (not ss)(progn (setq blk (ssdynblk _LOAblknameList))(if blk (ssadd blk ss)))) ; dynblock? ;(if ss ;(setq blk (vlax-ename->vla-object (ssname ss 0))) ;(setq blk (ssdynblk _LOAblknameList)) ; dynblocks, slow... ;) (if ss (repeat (setq i (sslength ss)) (setq blk (ssname ss (setq i (1- i)))) (setq props (getAttValues blk)) (foreach a props (if (wcmatch (strcase (car a)) (strcase _LOAattnameList))(setq property (cdr a))) ) )) ; (if blk (setq property (vl-getattributevalue blk _LOAattname))) ; (if blk (setq property (strcat (vl-getattributevalue blk _LOAattname) "-" (vl-getattributevalue blk "Revision") "/" (vl-getattributevalue blk "Drawing")))) ; triple atts, eg. NAME-REVISION/DRAWING property ) (defun _LOAchangedLayout (reactor layout / actl lname aname) ; single layout (setq lname (getvar "CTAB")); (car layout) ; act.layout (setq actl (vla-get-activelayout _LOAdoc)) ; vla-get-name (if (/= lname "Model")(progn (setq aname (_LOAgetAttVal lname)) ; get attribute (if (and aname (/= lname aname)) (progn (vla-put-name actl aname)(princ " * LOA renamed * "))) ; rename )) ) (defun _LOAprocess (reactor layout / ol actl lname aname) ; all layouts (setq ol (getvar "CTAB")); (car layout) ; act.layout (foreach lay (layoutlist) (setvar "CTAB" lay) (setq actl (vla-get-activelayout _LOAdoc)) (setq aname (_LOAgetAttVal lay)) ; get attribute (if (and aname (/= lay aname)) (progn (vla-put-name actl aname)(if (= ol lay)(setq ol aname))(princ " * LOA renamed * "))) ; rename (setvar "CTAB" ol) ) ) (defun C:LOAoff ( ) (if #LayoutSwitcher# (progn (vlr-remove #LayoutSwitcher#)(setq #LayoutSwitcher# nil))) (if #CommandEnded# (progn (vlr-remove #CommandEnded#)(setq #CommandEnded# nil))) (princ "\nNow OFF") (princ) ) (defun C:LOAon ( ) (if(not #LayoutSwitcher#) (setq #LayoutSwitcher# (VLR-Miscellaneous-Reactor nil '((:VLR-layoutSwitched . _LOAchangedLayout))))) ; on layout change (if(not #CommandEnded#) (setq #CommandEnded# (VLR-Command-Reactor nil '((:VLR-commandEnded . _LOAchangedLayout))))) ; and on any command (UNCOMMENT) ;(if(not #CommandEnded#) (setq #CommandEnded# (vlr-editor-reactor nil '((:vlr-endDwgOpen . _LOAprocess)(:vlr-beginSave . _LOAprocess)(:vlr-beginDxfOut . _LOAprocess))))) ; and on open/save (UNCOMMENT) (_LOAchangedLayout nil nil) (princ "\nNow ON") (princ) ) (princ "\nARKANCE LOA reactor loaded.") (C:LOAon) (princ)
Since June 2018, the reactor also supports dynamic blocks. Since September 2024, you can specify the block name and attribute name by a list or wildcard.
Download the latest version from Download which includes also an optional onOpen/onSave reactor (instead of onAnyCommand).

