@skchui6159 ,
Check this fix, it is full process (exclude the WScript this on you 😀)
it will work if you will be accurate in naming the "new title block" as well as the existing block name.
; define 3 constants
(setq NEW_TITLE_BLOCK "new title block-a4")
(setq BLOCK_CONTAINER "new_title_block_container")
(setq OLD_TITLE_BLOCK (strcase "old title block-a4,title block,title block1,1a32,title block 3,title block 4,a1titleblock,-title_a1_detail"))
BLOCK_CONTAINER is actually "new_tiltle_block_container.dwg" (which you have to create) and inside you insert the NEW_TITLE_BLOCK "new title block-A4" and save it in a folder that is defined in support file search path.
study the above very carefully.
Program Process:
Search for existing NEW_TITLE_BLOCK and delete it if exist? this to make way for the real new title block to insert.
temporary insert BLOCK_CONTAINER in model space releasing NEW_TITLE_BLOCK to be available for insert in all layouts.
scan drawing for all existing title blocks (exclude model space) set to ss1
iterate each block in ss1 selection, get bounding box of existing title blocks
insert NEW_TITLE_BLOCK in layout, insertion point is lower left corner of the existing (you can change it to middle point if you like)
read existing block attributes (build dotted pair list)
apply the attribute value to NEW_TITLE_BLOCK
erase existing title block.
erase new title block from model space was release from BLOCK_CONTAINER
clean up and exit 😀
enjoy
Moshe
;; Unique - Lee Mac
;; Returns a list with duplicate elements removed.
(defun LM:Unique ( l )
(if l (cons (car l) (LM:Unique (vl-remove (car l) (cdr l)))))
)
(defun c:attr2attr (/ _getAttrData remove_new_title_block insert_new_title_block_container insert_new_title_block ; local functions
savAttdia savAttreq savCTab NEW_TITLE_BLOCK BLOCK_CONTAINER OLD_TITLE_BLOCK ss1 ename1 ; local variables
modelEnt AcDbBlkRef1 AcDbBlkRef2 p0 p1 attributes1 attributes2 tag2 AcDbAttrib2) ; local variables
(defun _getAttrData (AcDbBlkRef)
(mapcar
(function
(lambda (AcDbAttrib)
(cons (strcase (vla-get-tagString AcDbAttrib)) (vla-get-textString AcDbAttrib))
)
); function
(vlax-invoke AcDbBlkRef 'GetAttributes)
); mapcar
); _getAttrData
(defun remove_new_title_block (/ ss ename)
(if (setq ss (ssget "_x" (list '(0 . "insert") (cons '2 NEW_TITLE_BLOCK))))
(foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
(entdel ename)
); foreach
); if
); remove_new_title_block
(defun insert_new_title_block_container (/ fname)
(if (setq fname (findfile (strcat BLOCK_CONTAINER ".dwg")))
(progn
(command "._insert" (strcat "*" fname) "0,0,0" 1 0)
(entlast)
); progn
(prompt (strcat "\nContainer block " BLOCK_CONTAINER " is not exist."))
); if
); insert_new_title_block_container
(defun insert_new_title_block (base / fname)
(if (tblsearch "block" NEW_TITLE_BLOCK)
(progn
(command "._insert" NEW_TITLE_BLOCK base 1 1 0)
T
); progn
(prompt (strcat "\nBlock " NEW_TITLE_BLOCK " is not exist."))
); if
); insert_new_title_block
; here start c:attr2attr
(setvar "cmdecho" 0)
(command "._undo" "_begin")
(setq savAttdia (getvar "attdia"))
(setq savAttreq (getvar "attreq"))
(setvar "attdia" 0)
(setvar "attreq" 0)
(setq savCTab (getvar "ctab"))
(setq NEW_TITLE_BLOCK "new title block-a4")
(setq BLOCK_CONTAINER "new_title_block_container")
(setq OLD_TITLE_BLOCK (strcase "old title block-a4,title block,title block1,1a32,title block 3,title block 4,a1titleblock,-title_a1_detail"))
; delete new title block if exist?
(remove_new_title_block)
(setvar "ctab" "Model")
; select existing title blocks, reject from model space
(if (and
(setq modelEnt (insert_new_title_block_container)) ; insert new title block container
(setq ss1 (ssget "_x" (list '(0 . "insert") (cons '2 (strcat OLD_TITLE_BLOCK ",`*U*")) (cons '410 "~Model"))))
)
(progn
(foreach ename1 (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss1)))
(setq AcDbBlkRef1 (vlax-ename->vla-object ename1))
(if (wcmatch (strcase (vla-get-effectiveName AcDbBlkRef1)) OLD_TITLE_BLOCK)
(progn
(vla-getBoundingBox AcDbBlkRef1 'minpt 'maxpt)
(setq p0 (vlax-safearray->list minpt))
(setq p1 (vlax-safearray->list maxpt))
(setvar "ctab" (cdr (assoc '410 (entget ename1))))
(command "._pspace")
(if (insert_new_title_block p0) ; insert new title block
(progn
(setq AcDbBlkRef2 (vlax-ename->vla-object (entlast)))
(setq attributes1 (LM:Unique (_getAttrData AcDbBlkRef1)))
(foreach AcDbAttrib2 (setq attributes2 (vlax-invoke AcDbBlkRef2 'GetAttributes))
(setq tag2 (strcase (vla-get-tagString AcDbAttrib2)))
(if (setq dat (assoc tag2 attributes1))
(vla-put-textString AcDbAttrib2 (cdr dat))
); if
); foreach
(foreach o attributes2
(vlax-release-object o)
)
(vlax-release-object AcDbBlkRef2)
); progn
); if
); progn
); if
(vla-delete AcDbBlkRef1) ; delete old title block
(vlax-release-object AcDbBlkRef1)
); foreach
); progn
); if
; delete origin block
(if modelEnt
(entdel modelEnt)
)
(setvar "ctab" savCTab)
(setvar "attreq" savAttreq)
(setvar "attdia" savAttdia)
(command "._undo" "_end")
(setvar "cmdecho" 1)
(princ "\nDone.")
(princ)
); c:attr2attr