Many years ago I wrote a function (below) to update a block (nested or not) by inserting a DWG file that contains the objects of that block.
If the file has a different name from the block the block is renamed.
I would like to write a similar function that allows me to insert a DWG that contains the block definition (and other blocks...) than updates the block with the same name.
The best would be to update the nested blocks it contains.
Scenario:
1) Main.dwg contains the block MyBlockAbc
2) MyBlockAbc contains SubBlock1 SubBlock2
3) select MyBlockAbc (can be a nested block) from Main.dwg
4) select a DWG file via dialogue, example Foo.dwg
5) update all blocks MyBlockAbc SubBlock1 SubBlock2 in Main.dwg from definitions on Foo.dwg
Any suggestion?
(defun C:ALE_BLK_REINS ( / EntDat EntNam BlkNam PatNam FilNam NewNam EntLst UpdNam NumObj)
(MODESET '("ATTREQ" "EXPERT"))
(setvar "EXPERT" 5) (setvar "ATTREQ" 0)
(if (setq EntLst (nentsel "\nSelect main or nested block to update: "))
(progn
(if (= (length EntLst) 4)
(progn
(if (and (= "DIMENSION" (DXF 0 (entget (car (last EntLst))))) (cdr (last EntLst)))
(setq EntLst (list (car EntLst) (cdr (last EntLst))))
);if
(setq
UpdNam (DXF 2 (entget (last (last EntLst))))
EntNam (car (last EntLst))
)
(if (setq EntDat (entget EntNam)) (setq BlkNam (strcase (DXF 2 EntDat))));if
);progn
(progn
(setq EntNam (car EntLst))
(if (= "ATTRIB" (DXF 0 (entget EntNam)))
(progn
(setq EntNam (ssname (ssget (cadr EntLst)) 0))
(if (setq EntDat (entget EntNam)) (setq BlkNam (strcase (DXF 2 EntDat))));if
);progn
);if
);progn
);if
(if BlkNam
(if
(and
(= (DXF 0 EntDat) "INSERT")
(setq
PatNam (ALE_UtlCfg_GetVal #CfgData "MyApp" "Ins_dir" "")
FilNam
(getfiled
"Block file"
(strcat PatNam BlkNam)
"DWG" 2
)
)
)
(progn
(if (snvalid (setq NewNam (strcase (vl-filename-base FilNam))))
(progn
(or
(= (vl-filename-directory PatNam) (vl-filename-directory FilNam))
(ALE_UtlCfg_SetVal
'#CfgData #CfgFile "MyApp" "Ins_dir" (strcat (vl-filename-directory FilNam) "\\")
)
)
(command "_.INSERT" (strcat BlkNam "=" FilNam)) (command)
(if (and UpdNam (setq UpdNam (ssget "_X" (list (cons 0 "INSERT") (cons 2 UpdNam)))))
(progn
(repeat (setq NumObj (sslength UpdNam))
(setq NumObj (1- NumObj)) (entupd (ssname UpdNam NumObj))
)
(setq UpdNam nil)
)
)
(if (setq UpdNam (ssget "_X" (list (cons 0 "INSERT") (cons 2 BlkNam))))
(progn
(repeat (setq NumObj (sslength UpdNam))
(setq NumObj (1- NumObj)) (entupd (ssname UpdNam NumObj))
)
(setq UpdNam nil)
)
)
(if (equal NewNam BlkNam)
(princ (strcat "\nBlock " BlkNam " updated. "))
(progn
(if (tblsearch "BLOCK" NewNam)
(alert (strcat
"\nBlock " BlkNam " updated"
"\nbut not renamed in " NewNam
"\n\nBlock " NewNam " already exist in the drawing."
"\n\nAttention, now the two blocks are equal but with different name!"
) )
(progn
(command "_RENAME" "_BL" BlkNam NewNam)
(princ (strcat "\nBlock " BlkNam " updated and renamed in " NewNam " "))
)
)
)
)
(and (getcname "_ATTSYNC") (= (DXF 66 EntDat) 1) (command "_.ATTSYNC" "_NAME" NewNam)) ; blocco con attributi
)
(alert
(strcat
"Block name " (chr 34) FilNam (chr 34) " not valid:"
"\n\n Inser has been ignored."
) )
);if
);progn
(princ "\nInsufficient information or non valid entity! Try again. ")
);if
(alert "MyApp message:\nA regen needed to modify this Block")
)
);progn
(princ "\nMyApp message:\nNothing selected, function cancelled. ")
);if EntLst
(MODERESET)
(princ)
); defun C:ALE_BLK_REINS