Author Topic: change the block name  (Read 2450 times)

0 Members and 1 Guest are viewing this topic.

ilaali

  • Mosquito
  • Posts: 6
change the block name
« on: January 25, 2021, 08:16:29 AM »
I want to change the name of the block I selected in the drawing. I wrote the code below .. failed. What do you think was missing?


(defun c:blk()
  (setq ent_sel (entsel "\select object: "))
 (setq ent_name (car ent_sel))
  (setq ent_get1 (entget ent_name))
  (setq deger1(assoc 2 ent_get1));poz

  (setq newname(getstring " new block name:"))
 (setq newname_c (cons 2 newname))
  (setq liste(subst newname_c (assoc 2 ent_get1) ent_get1))
  (entmod liste)
  )

kpblc

  • Bull Frog
  • Posts: 396
Re: change the block name
« Reply #1 on: January 25, 2021, 09:41:45 AM »
You have to rename block DEFINITION not REFERENCE.
Change
(setq deger1(assoc 2 ent_get1))
to
(setq deger1 (tblobjname "blocks" (assoc 2 ent_get1)))

P.S. I didn't check code.
Sorry for my English.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: change the block name
« Reply #2 on: January 25, 2021, 11:04:04 AM »
You can also use the built-in rename command.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ilaali

  • Mosquito
  • Posts: 6
Re: change the block name
« Reply #3 on: January 25, 2021, 01:05:25 PM »
You can also use the built-in rename command.

THANK YOU SO MUCH, I AM LEARNING THE LISP MORE. CAN YOU EXPLAIN SOME MORE ??

ilaali

  • Mosquito
  • Posts: 6
Re: change the block name
« Reply #4 on: January 25, 2021, 01:06:46 PM »
You have to rename block DEFINITION not REFERENCE.
Change
(setq deger1(assoc 2 ent_get1))
to
(setq deger1 (tblobjname "blocks" (assoc 2 ent_get1)))

P.S. I didn't check code.

I Tried, I Failed ..

ronjonp

  • Needs a day job
  • Posts: 7529
Re: change the block name
« Reply #5 on: January 25, 2021, 01:30:12 PM »
You can also use the built-in rename command.

THANK YOU SO MUCH, I AM LEARNING THE LISP MORE. CAN YOU EXPLAIN SOME MORE ??
Type 'rename' in the command line and then select blocks.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ilaali

  • Mosquito
  • Posts: 6
Re: change the block name
« Reply #6 on: January 25, 2021, 01:41:32 PM »
You can also use the built-in rename command.

THANK YOU SO MUCH, I AM LEARNING THE LISP MORE. CAN YOU EXPLAIN SOME MORE ??
Type 'rename' in the command line and then select blocks.

you are right, it is a waste of time for me ..

framednlv

  • Newt
  • Posts: 64
Re: change the block name
« Reply #7 on: January 25, 2021, 03:20:28 PM »
This is what i'm using:
Code: [Select]
(defun c:bold ()
  (vl-load-com)
  (setq bname (vla-get-EffectiveName (vlax-ename->vla-object (car(entsel "\nSelect block: ")))))
(setq bname-old (strcat bname "-old"))
(setq i 1)
(while (tblsearch "block"  bname-old)
(setq bname-old (strcat bname "-old" (itoa i)))
(setq i (1+ i))
); end if
  (command "rename" "block" bname bname-old)
)

ilaali

  • Mosquito
  • Posts: 6
Re: change the block name
« Reply #8 on: January 25, 2021, 03:33:30 PM »
This is what i'm using:
Code: [Select]
(defun c:bold ()
  (vl-load-com)
  (setq bname (vla-get-EffectiveName (vlax-ename->vla-object (car(entsel "\nSelect block: ")))))
(setq bname-old (strcat bname "-old"))
(setq i 1)
(while (tblsearch "block"  bname-old)
(setq bname-old (strcat bname "-old" (itoa i)))
(setq i (1+ i))
); end if
  (command "rename" "block" bname bname-old)
)


When I loaded the code, it didn't work. gave this message..



"Command: BOLD
Select block: renameEnter object type to rename [Block/Dimstyle/LAyer/LType/Material/multileadeRstyle/Style/Ucs/VIew/VPort]: block
Enter name for old block: G$C2336C229
Enter name for new block: G$C2336C229-oldnil"

kpblc

  • Bull Frog
  • Posts: 396
Re: change the block name
« Reply #9 on: January 25, 2021, 03:55:26 PM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:bold (/ selset ent def name adoc)
  3.   (if (setq selset (ssget "_+.:S:E" '((0 . "INSERT"))))
  4.     (progn
  5.       (setq ent  (vlax-ename->vla-object (ssname selset 0))
  6.             name (cond ((vlax-property-available-p ent 'effectivename) (vla-get-effectivename ent))
  7.                        ((vlax-property-available-p ent 'name) (vla-get-name ent))
  8.                  ) ;_ end of cond
  9.       ) ;_ end of setq
  10.       (cond
  11.         ((not name) (princ "\nEntity hasn't \"Name\" or \"EffectiveName\" property"))
  12.         ((wcmatch name "`**") (princ "\nUnnamed blocks can't be renamed"))
  13.         (t
  14.          (if (vl-catch-all-error-p
  15.                (setq
  16.                  err (vl-catch-all-apply
  17.                        (function
  18.                          (lambda ()
  19.                            (vla-put-name
  20.                              (vla-item (vla-get-blocks adoc) name)
  21.                              (strcat name
  22.                                      "_old"
  23.                                      (itoa
  24.                                        (length
  25.                                          (vl-remove-if-not (function (lambda (x) (wcmatch (strcase x) (strcase (strcat name "_old*")))))
  26.                                                            ((lambda (/ res) (vlax-for item (vla-get-blocks adoc) (setq res (cons (vla-get-name item) res))) res)
  27.                                                            )
  28.                                          ) ;_ end of vl-remove-if-not
  29.                                        ) ;_ end of length
  30.                                      ) ;_ end of itoa
  31.                              ) ;_ end of strcat
  32.                            ) ;_ end of vla-put-name
  33.                          ) ;_ end of lambda
  34.                        ) ;_ end of function
  35.                      ) ;_ end of vl-catch-all-apply
  36.                ) ;_ end of setq
  37.              ) ;_ end of vl-catch-all-error-p
  38.            (princ (strcat "\nError : " (vl-catch-all-error-message err)))
  39.          ) ;_ end of if
  40.          (vla-endundomark adoc)
  41.         )
  42.       ) ;_ end of cond
  43.     ) ;_ end of progn
  44.     (princ "\nNothing selected")
  45.   ) ;_ end of if
  46.   (princ)
  47. ) ;_ end of defun
Sorry for my English.

ilaali

  • Mosquito
  • Posts: 6
Re: change the block name
« Reply #10 on: January 26, 2021, 02:15:14 AM »
Try this:
Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun c:bold (/ selset ent def name adoc)
  3.   (if (setq selset (ssget "_+.:S:E" '((0 . "INSERT"))))
  4.     (progn
  5.       (setq ent  (vlax-ename->vla-object (ssname selset 0))
  6.             name (cond ((vlax-property-available-p ent 'effectivename) (vla-get-effectivename ent))
  7.                        ((vlax-property-available-p ent 'name) (vla-get-name ent))
  8.                  ) ;_ end of cond
  9.       ) ;_ end of setq
  10.       (cond
  11.         ((not name) (princ "\nEntity hasn't \"Name\" or \"EffectiveName\" property"))
  12.         ((wcmatch name "`**") (princ "\nUnnamed blocks can't be renamed"))
  13.         (t
  14.          (if (vl-catch-all-error-p
  15.                (setq
  16.                  err (vl-catch-all-apply
  17.                        (function
  18.                          (lambda ()
  19.                            (vla-put-name
  20.                              (vla-item (vla-get-blocks adoc) name)
  21.                              (strcat name
  22.                                      "_old"
  23.                                      (itoa
  24.                                        (length
  25.                                          (vl-remove-if-not (function (lambda (x) (wcmatch (strcase x) (strcase (strcat name "_old*")))))
  26.                                                            ((lambda (/ res) (vlax-for item (vla-get-blocks adoc) (setq res (cons (vla-get-name item) res))) res)
  27.                                                            )
  28.                                          ) ;_ end of vl-remove-if-not
  29.                                        ) ;_ end of length
  30.                                      ) ;_ end of itoa
  31.                              ) ;_ end of strcat
  32.                            ) ;_ end of vla-put-name
  33.                          ) ;_ end of lambda
  34.                        ) ;_ end of function
  35.                      ) ;_ end of vl-catch-all-apply
  36.                ) ;_ end of setq
  37.              ) ;_ end of vl-catch-all-error-p
  38.            (princ (strcat "\nError : " (vl-catch-all-error-message err)))
  39.          ) ;_ end of if
  40.          (vla-endundomark adoc)
  41.         )
  42.       ) ;_ end of cond
  43.     ) ;_ end of progn
  44.     (princ "\nNothing selected")
  45.   ) ;_ end of if
  46.   (princ)
  47. ) ;_ end of defun


thank you