Here are some more elaborate routines.
I have not tried them.
(defun c:rb (/ entiteold entitenew eoname enname jeusel1 i ent_actu
ent_data)
(setvar "cmdecho" 0)
(command "_.undo" "_begin")
(princ "automatically substitute blocks within a selection\n")
(setq entiteold
(car (entsel
"pick a block, the type which will be replaced\n"))
)
(setq entitenew (car (entsel "pick a block of the new type\n")))
(setq eoname (cdr (assoc 2 (entget entiteold))))
(princ eoname)
(princ " -> ")
(setq enname (cdr (assoc 2 (entget entitenew))))
(princ enname)
(if (/= (liste_att_ds_bloc entiteold) nil)
(alert (strcat "Warning, the replaced block contains attributes "
"\n \n This can give unexpected results "
"\n \n Use ATTREDEF instead.")
)
)
(if (/= (liste_att_ds_bloc entitenew) nil)
(alert (strcat "Warning, the replacement block contains attributes "
"\n \n This can give unexpected results"
"\n \n Use ATTREDEF instead.")
)
)
(princ (strcat"\n Select objects for which the replacement will be performed "
"[do an empty selection to perform on entire drawing] :")
)
(setq jeusel1 (ssget (list (cons 0 "INSERT") (cons 2 eoname))))
(if (= jeusel1 nil)
(setq jeusel1 (ssget "x" (list (cons 0 "INSERT") (cons 2 eoname))))
)
(setq i 0)
(repeat (sslength jeusel1)
(setq ent_actu (ssname jeusel1 i)
ent_data (entget ent_actu)
) ;setq
(setq i (1+ i))
(setq ent_data (subst (cons 2 enname) (assoc 2 ent_data) ent_data))
(entmod ent_data)
) ;repeat
(command "_.undo" "_end")
(setvar "cmdecho" 1)
(princ)
)
;Here is a little code I had that will do this. It will also swap one attribute.
;Peter Jamtgaard
;****************************RBLKS.LSP*******************************************
; Written By : Peter Jamtgaard
;.Purpose<RBLKS>: Replaces Selected BLOCKS with another picked BLOCK
;******************************************************************************
(defun rtd (a)(/ (* a 180.0) pi)) ;.radians to degrees z
(defun C:RBLKS () ;.REPLACE BLOCKS
(setvar "ATTDIA" 0)
(setq OLDATT nil)
(princ "\nSelect BLOCKS to be replaced: ")
(setq B 0 C 0)
;****************************************************************************
(setq SSET (ssget))
(if (= SSET nil)
(progn
(princ "\nNo entities were selected: ")
(setq B 1)
)
(progn
(setq NEWBLK (car (entsel "\nSelect block replacement: ")));new block name
(setq NEWLAYR (cdr (assoc 8 (entget NEWBLK)))) ;new lyr ent name
(setq NEWLT (cdr (assoc 6 (entget NEWBLK)))) ;new lt ent name
(setq NEWCLR (cdr (assoc 62 (entget NEWBLK)))) ;new clr ent name
(setq NEWTYPE (cdr (assoc 0 (entget NEWBLK)))) ;new ent type test
(if (= NEWTYPE "INSERT")
(setq B 0)
(progn
(princ "\nYour last selection was not a block: ")
(setq B 1)
)
)
)
)
;****************************************************************************
(while (= B 0)
(setq OLDENT (ssname SSET C));old BLOCK name
(if (= OLDENT nil)(setq B 1)
(progn
(setq OLDED (entget OLDENT))
(setq OLDTYPE (cdr (assoc 0 OLDED))) ;old ent type test
(if (/= OLDTYPE "INSERT")(setq SSET (ssdel OLDENT SSET))
(progn
(setq OLDBLK OLDENT)
(setq SCX (cdr (assoc 41 OLDED))) ;old blk insertion
(setq SCY (cdr (assoc 42 OLDED)))
(setq ROT (rtd (cdr (assoc 50 OLDED)))) ;old blk rotation
(setq INS (cdr (assoc 10 OLDED))) ;old blk insertion
(setq OLDTYPE (cdr (assoc 0 OLDED))) ;old ent type test
(setq REP (cdr (assoc 2 (entget NEWBLK)))) ;blk name replacement
(if (= (entnext OLDBLK) nil)(princ)
(if (= "ATTRIB" (cdr (assoc 0 (entget (entnext OLDBLK)))))
(progn
(setq OLDATT (cdr (assoc 1 (entget (entnext OLDBLK)))));Old Att. Value
(setq OLDATTANG (cdr (assoc 50 (entget (entnext OLDBLK)))));Old Att. Angle
(setq OLDATTWID (cdr (assoc 41 (entget (entnext OLDBLK)))));Old Att. Width
)
(princ "\nOld BLOCK didn't contain attributes: ")
)
)
(if (= "INSERT" OLDTYPE) ;test valid input
(progn
(command "INSERT" REP INS SCX SCY ROT);Insert NEWBLK over OLDBLK
(setq ENT (entlast))
(setq ED (entget ENT))
(setq ED (subst (cons 8 NEWLAYR) (assoc 8 ED) ED))
(if (= OLDATT nil)
(princ)
(progn
(setq NENT (entnext ENT))
(if (= NENT nil)(princ "\nLast Block has no attributes: ")
(progn
(setq NED (entget NENT))
(setq NENTTYPE (cdr (assoc 0 NED)))
(if (= NENTTYPE "ATTRIB")
(progn
(setq NED (subst (cons 1 OLDATT)(assoc 1 NED) NED))
(setq NED (subst (cons 50 OLDATTANG)(assoc 50 NED) NED))
(setq NED (subst (cons 41 OLDATTWID)(assoc 41 NED) NED))
(entmod NED)
)
(princ "\nNew Symbol doesn't contain Attributes: ")
)
)
)
)
)
(entmod ED)
(if (/= NEWLT nil)
(command "CHANGE" ENT "" "P" "LT" NEWLT "")
)
(if (/= NEWCLR nil)
(command "CHANGE" ENT "" "P" "C" NEWCLR "")
)
(setq C (+ C 1))
(princ "\nC+ ")
)
(progn
(princ "\nInvalid entity: ")
(setq B 1)
)
)
)
)
)
)
)
(command "erase" sset "")
(setvar "attdia" 1)
(prin1)
)