Author Topic: subfunction Rename Block  (Read 1801 times)

0 Members and 1 Guest are viewing this topic.

amc.dicsac

  • Newt
  • Posts: 109
  • Autocad 2008
subfunction Rename Block
« on: March 23, 2017, 12:44:35 PM »
Hi, I need your help.

I have this subfunction that allows me to rename blocks but for some strange reason I get the following error

; Error: Automation Error. Key not found

Code - Auto/Visual Lisp: [Select]
  1. (defun 2ap_BlockRename (bn nn / layout i)
  2.     (vlax-for i (vla-get-block layout)
  3.       (if (and
  4.             (= (vla-get-objectname i) "AcDbBlockReference")
  5.             (= (strcase (vla-get-name i)) (strcase bn))
  6.           )
  7.         (vla-put-name i nn)
  8.       )
  9.     )
  10.   )
  11. )
  12.  
  13. (defun c:RNB (/ EntObj xx)
  14. (setq EntObj (car (entsel "\nSelecciona el bloque a renombrar:")))
  15. (setq xx (getstring t "\nIngresa el nuevo nombre del bloque: "))
  16. (2ap_BlockRename (cdr (assoc 2 (entget EntObj))) xx)
  17. (princ))
<a href="http:/http://axprogramlisp.blogspot.pe" class="bbc_link" target="_blank">By Alexander Castro</a>

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: subfunction Rename Block
« Reply #1 on: March 23, 2017, 01:31:13 PM »
You are renaming block references, not the block definition, therefore a block definition corresponding to the new block name must exist otherwise you will receive the error you have reported.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: subfunction Rename Block
« Reply #2 on: March 23, 2017, 01:32:20 PM »
I would instead suggest something like:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:rnb ( / new obj old )
  2.     (while
  3.         (not
  4.             (progn (setvar 'errno 0) (setq obj (car (entsel "\nSelect block to rename <exit>: ")))
  5.                 (cond
  6.                     (   (= 7 (getvar 'errno))
  7.                         (prompt "\nMissed, try again.")
  8.                     )
  9.                     (   (null obj))
  10.                     (   (/= "AcDbBlockReference" (vla-get-objectname (setq obj (vlax-ename->vla-object obj))))
  11.                         (prompt "\nThe selected object is not a block.")
  12.                     )
  13.                     (   (progn
  14.                             (setq old (vla-get-effectivename obj))
  15.                             (while
  16.                                 (not
  17.                                     (progn (setq new (getstring t (strcat "\nSpecify new name for block \"" old "\" <exit>: ")))
  18.                                         (cond
  19.                                             (   (= "" new))
  20.                                             (   (not (snvalid new))
  21.                                                 (prompt (strcat "\n\"" new "\" is not a valid block name."))
  22.                                             )
  23.                                             (   (tblsearch "block" new)
  24.                                                 (prompt (strcat "\nA block called \"" new "\" already exists."))
  25.                                             )
  26.                                             (   t   )
  27.                                         )
  28.                                   )
  29.                                 )
  30.                             )
  31.                             (/= "" new)
  32.                         )
  33.                         (vla-put-name (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) old) new)
  34.                         (princ (strcat "\nBlock \"" old "\" renamed to \"" new "\"."))
  35.                     )
  36.                 )
  37.             )
  38.         )
  39.     )
  40.     (princ)
  41. )

amc.dicsac

  • Newt
  • Posts: 109
  • Autocad 2008
Re: subfunction Rename Block
« Reply #3 on: March 23, 2017, 02:36:26 PM »
Thanks Lee   :smitten: :smitten: :smitten:

An additional question, I would like to know if this section that I just wrote is correct ??

Code - Auto/Visual Lisp: [Select]
  1.    ( and  (tblsearch "block" new) )
  2.             (prompt (strcat "\nA block called \"" new "\" already exists."))
  3. )
<a href="http:/http://axprogramlisp.blogspot.pe" class="bbc_link" target="_blank">By Alexander Castro</a>

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: subfunction Rename Block
« Reply #4 on: March 23, 2017, 02:56:28 PM »
An additional question, I would like to know if this section that I just wrote is correct ??

Code - Auto/Visual Lisp: [Select]
  1.    ( and  (tblsearch "block" new) )
  2.             (prompt (strcat "\nA block called \"" new "\" already exists."))
  3. )

You will need a condition to terminate the loop :wink: