rename block

rename block

Anonymous
Not applicable
1,328 Views
5 Replies
Message 1 of 6

rename block

Anonymous
Not applicable
hello, I have a lsp rename block's name.  it's only one select.
I want to select multi blocks. thank you for reading and need help
(defun c:rb (/ os ent elist ins)
    (setvar "cmdecho" 0)
    (setq os (getvar "osmode"))
    (if (setq ent (car (entsel "\nselect block:")))
      (progn (setq elist (entget ent))
        (setq obn (cdr (assoc 2 elist)))
        (setq ins (cdr (assoc 10 elist)))
        (setq rname nil)
        (while (= rname nil)
          (setq rname (getstring t "\nNew block name :"))
          (setq byn (assoc 2 (tblsearch "block" rname)))
          (setq byn2 (cdr byn))
          (if (= rname byn2)
            (progn (setq rname nil)
               (prompt "already exsisted")
            ) ;progn
          );if
        ) ;while
        (if (/= rname "")
          (progn (command "explode" ent)
            (setvar "osmode" 0)
            (command "_.block" rname ins "P" "")
            (command "_.insert" rname ins "" "" "")
            (setvar "osmode" os)
          );progn
        );if
      );progn
     );if
(princ)
);defun
 
 
0 Likes
Accepted solutions (1)
1,329 Views
5 Replies
Replies (5)
Message 2 of 6

Sea-Haven
Mentor
Mentor

Change this to  (while (setq ent (car (entsel "\nselect block:"))) but note you must remove a (progn and corresponding ) ; progn further in code. 

0 Likes
Message 3 of 6

_gile
Consultant
Consultant

Hi,

 

Try this:

(defun c:renameBlock (/ ent elst oldName newName)
    (while
        (and
            (setq ent (car (entsel "\nSelect block: ")))
            (= (cdr (assoc 0 (setq elst (entget ent)))) "INSERT")
        )
           (setq oldName (cdr (assoc 2 elst))
                 newName (getstring (strcat "\nCurrent name: '" oldName "'. New bloc name: "))
           )
           (while (tblsearch "BLOCK" newName)
               (setq newName (getstring (strcat "\n'" newName "' already exists. New bloc name: ")))
           )
           (entmod (subst (cons 2 newName)
                          (cons 2 oldName)
                          (entget (cdr (assoc 330 (entget (tblobjname "BLOCK" oldName)))))
                   )
           )
    )
    (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 6

dbhunia
Advisor
Advisor

Try some thing like.......

 

(defun rb (ent rname / os ent elist ins)
    (setq os (getvar "osmode") cmd (getvar "cmdecho"))
    (setq elist (entget ent))
    (setq ins (cdr (assoc 10 elist)))
	(setq byn (assoc 2 (tblsearch "block" rname)))
	(setq byn2 (cdr byn))
	(if (= rname byn2)
		(progn (setq rname nil)
			(prompt "already exsisted")
		) ;progn
		(progn 
			(setvar "osmode" 0)(setvar "cmdecho" 0)
			(command "explode" (ssget ":L"))
			(command "_.block" rname ins "P" "")
			(command "_.insert" rname ins "" "" "")
			(setvar "osmode" os)(setvar "cmdecho" cmd)
		);progn
	);if
(princ)
);defun
(defun c:RSB ( / Blk Sel_blk Blk_Name New_Name)
(princ "\nSelect Blocks to Rename...")
(setq Blk (ssget ":L" '((0 . "INSERT"))))
	(foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex Blk)))
		(setq Sel_blk (ssadd))
		(sssetfirst nil (ssadd e Sel_blk))
		(setq Blk_Name (vla-get-name (vlax-ename->vla-object e)))
		(setq New_Name (getstring t (strcat "\nEnter New Block Name for the Block <" Blk_Name "> : " )))
		(if (/= New_Name "")(rb e New_Name))
		(sssetfirst nil)
	)
(princ)
)

 

You need to workout on it ........ Hopefully you can manage that...

 

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 5 of 6

_gile
Consultant
Consultant
Accepted solution

My previous purpose works like the RENAME command, i.e. change the name of the block definition.

If the goal is to create a new block definition with the same entities as in the block definition of the selected blocks and change the definition of the selected blocks as does the code in the OP for a single selection, you can try the following one.

 

(defun c:renameBlock (/ ss i lst oldName newName oldDef ent)
    (if (setq ss (ssget '((0 . "INSERT"))))
        (progn
            (repeat (setq i (sslength ss))
                (setq lst (cons (entget (ssname ss (setq i (1- i)))) lst))
            )
            (setq oldName (cdr (assoc 2 (car lst))))
            ;; check if all selected blocks have the same name
            (if (vl-every '(lambda (x) (= x oldName))
                          (mapcar '(lambda (l) (cdr (assoc 2 l))) lst)
                )
                (progn
                    (setq newName (getstring (strcat "\nCurrent name: '" oldName "'. New bloc name: ")))
                    (while (tblsearch "BLOCK" newName)
                        (setq newName (getstring (strcat "\n'" newName "' already exists. New bloc name: ")))
                    )
                    (setq oldDef (entget (tblobjname "BLOCK" oldName))
                          ent    (cdr (assoc -2 oldDef))
                    )
                    ;; create a new block definition
                    (entmake (list (assoc 0 oldDef)
                                   (cons 2 newName)
                                   (assoc 70 oldDef)
                                   (assoc 10 oldDef)
                             )
                    )
                    ;; copy the source block entities
                    (while ent
                        (entmake (entget ent))
                        (setq ent (entnext ent))
                    )
                    (entmake '((0 . "ENDBLK")))
                    ;; change the definition name of the selected blocks
                    (foreach l lst
                        (entmod (subst (cons 2 newName) (cons 2 oldName) l))
                    )
                )
                (prompt "\nSelected blocks have different names.")
            )
        )
    )
    (princ)
)

PS: IMO, having multiple block definitions which are identical is not a good practice.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

Anonymous
Not applicable

thank you so much

0 Likes