1
AutoLISP (Vanilla / Visual) / Lisp to rename block
« Last post by like_citrus on Today at 01:23:38 AM »Hi, using this code from CAD forum to rename a block.
(1) First the code is called up, then (2) you have to select the block to rename.
Is it possible to change so that it runs when you (1) select a block and then (2) code runs automatically for the block selected.
The block will be selected, then mouse-right-click and clicking on a custom menu button which runs the code.
(1) First the code is called up, then (2) you have to select the block to rename.
Is it possible to change so that it runs when you (1) select a block and then (2) code runs automatically for the block selected.
The block will be selected, then mouse-right-click and clicking on a custom menu button which runs the code.
Code: [Select]
(vl-load-com)
(defun C:RenameBlock (/ bent bobj oldn dblk match newn cmde)
(while
(not
(and
(setq bent (entsel "\nSelect Block to rename: ")) ; is selected
(setq bent (entget (setq bobj (car bent))))
(setq bobj (vlax-ename->vla-object bobj))
(= (cdr (assoc 0 bent)) "INSERT") ; is a block
(setq oldn (cdr (assoc 2 bent)))
(not (assoc 1 (tblsearch "block" oldn))) ; not an xref
(setq match (= (strcase oldn)(strcase (setq dblk (vla-get-effectivename bobj))))) ; not a dynamic block
)
)
(prompt "\nNo static Block selected!")
) ; while
(while
(not
(and
(setq newn (getstring T (strcat "\nCurrent name = " oldn (if match "" (strcat " (" dblk ")")) ". New Block name: ")))
(not (tblsearch "block" newn)) ; not exists
(or (= newn "")(snvalid newn)) ; not invalid
)
)
(prompt "\nBlock name already exists or not valid!")
)
(if (> newn "") ; entered a new name
(progn
(setq cmde (getvar 'cmdecho))
(setvar "cmdecho" 0)
(command "_.rename" "_block" oldn newn)
(prompt "\nRenamed.")
(setvar "cmdecho" cmde)
)
) ; if
(princ)
) ; defun