Author Topic: Rename a block with a common prefix - wildcard? and/or Replace Block  (Read 14168 times)

0 Members and 1 Guest are viewing this topic.

tedg

  • Swamp Rat
  • Posts: 811
I know you can use wildcards in the "rename" dialog, but that doesn't work on the command line.
I want to rename a block in a bunch of drawings, but the block names vary but have the same prefix.
Specifically these are exported Revit title blocks to AutoCAD and I need to replace them with a proper AutoCAD title block.

When Revit exports blocks, it gives them specific names based on the sheet name, so in my case, the title block (block) names vary like:
(examples only): "TitleBlock - Titleblock Structural Notes"  and "TitleBlock - Titleblock Foundation Plan" etc
I want to rename and/or replace these blocks with a common AutoCAD created one.
The attributes aren't an issue, they are simply text on top of the title block and will remain, so it's just replacing one block with another.

I am trying to rename them something common and predictable so I can replace them with "insert oldblock=newblock" type of programming.


I looked into some other threads like THIS ONE and have looked at Lee Mac's "copy/rename" block routine, but this asks you to select the block.


Anyone have any ideas on how I can achieve this?


Thanks

Windows 10 Pro 64bit, AutoCAD 2023, REVIT 2023

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #1 on: October 28, 2015, 10:05:31 AM »
I recall   -insert Block name= block name  should work for you....
Be your Best


Michael Farrell
http://primeservicesglobal.com/

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #2 on: October 28, 2015, 10:18:05 AM »
Maybe something like this:

Code - Auto/Visual Lisp: [Select]
  1. ;; Assuming no dynamic blocks
  2. (defun _rename (newblock pattern / name)
  3.   (if (findfile newblock)
  4.       (if (wcmatch (strcase (setq name (vla-get-name b))) (strcase pattern))
  5.         (progn (command ".-INSERT" (strcat name "=" newblock) '(0 0 0) nil)
  6.                (print (strcat name " redefined..."))
  7.         )
  8.       )
  9.     )
  10.   )
  11.   (princ)
  12. )
  13. (_rename "C:\\Path\\newblock.dwg" "*titleblock*")
*edit to fix variable missed
« Last Edit: October 28, 2015, 11:08:10 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

tedg

  • Swamp Rat
  • Posts: 811
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #3 on: October 28, 2015, 11:16:09 AM »
I recall   -insert Block name= block name  should work for you....
The problem is that these blocks names vary but with a common prefix, not wanting to find the exact block name but use a wild card.
This "-insert" command will not allow wildcards, and the rename command will not allow wild cards.


I tried to do something like:
"-insert TitleBlock*=c:\path\newTitleBlock" ... but "TitleBlock*" is an invalid block name.

Windows 10 Pro 64bit, AutoCAD 2023, REVIT 2023

tedg

  • Swamp Rat
  • Posts: 811
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #4 on: October 28, 2015, 11:22:41 AM »
Maybe something like this:

Code - Auto/Visual Lisp: [Select]
  1. ;; Assuming no dynamic blocks
  2. (defun _rename (newblock pattern / name)
  3.   (if (findfile newblock)
  4.       (if (wcmatch (strcase (setq name (vla-get-name b))) (strcase pattern))
  5.    (progn (command ".-INSERT" (strcat name "=" newblock) '(0 0 0) nil)
  6.           (print (strcat name " redefined..."))
  7.    )
  8.       )
  9.     )
  10.   )
  11.   (princ)
  12. )
  13. (_rename "C:\\Path\\newblock.dwg" "*titleblock*")
*edit to fix variable missed
Thanks Ronjonp, I'll poke around with this to see if I can use this, looks promising.
Thanks again.
Windows 10 Pro 64bit, AutoCAD 2023, REVIT 2023

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #5 on: October 28, 2015, 11:31:12 AM »
Maybe something like this:

Code - Auto/Visual Lisp: [Select]
  1. ;; Assuming no dynamic blocks
  2. (defun _rename (newblock pattern / name)
  3.   (if (findfile newblock)
  4.       (if (wcmatch (strcase (setq name (vla-get-name b))) (strcase pattern))
  5.    (progn (command ".-INSERT" (strcat name "=" newblock) '(0 0 0) nil)
  6.           (print (strcat name " redefined..."))
  7.    )
  8.       )
  9.     )
  10.   )
  11.   (princ)
  12. )
  13. (_rename "C:\\Path\\newblock.dwg" "*titleblock*")
*edit to fix variable missed
Thanks Ronjonp, I'll poke around with this to see if I can use this, looks promising.
Thanks again.


Sounds good .. let me know how it goes.  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

tedg

  • Swamp Rat
  • Posts: 811
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #6 on: October 28, 2015, 02:51:34 PM »
Maybe something like this:


Thanks Ronjonp, I'll poke around with this to see if I can use this, looks promising.
Thanks again.


Sounds good .. let me know how it goes.  :)


Well it seems this is a bit over my head, I couldn't make it work, and I wasn't sure how to edit it to fit my file(s) and path names.
I'm thinking this wants to redefine a block and then rename it based on an outside file name? I'm just not clever enough to figure it out.


I got some other functions to work I was working on, including replacing a "known" block name with an outside file.


All I really need now is a way to "-rename" a block within a drawing that has a variable name but a common prefix, and then I would be good to go.


Something that would rename "TitleBlock - Blah Blah Blah"  to "TitleBlock" (where "blah blah blah" changes per drawing).

Like if you could use wildcards on the command line like you can in the rename dialog it would look something like:

"-rename" "block" "TitleBlock*" new block name: "TitleBlock"
Windows 10 Pro 64bit, AutoCAD 2023, REVIT 2023

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #7 on: October 28, 2015, 04:16:37 PM »
Oh I gotcha .. give the code below a try. This assumes that your *titleblock* blocks have the same geometry & are just named differently.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b e el nb)
  2.   (setq nb "titleblock")
  3.   ;; This selection set will fail on modified dynamic blocks ( so be aware :) )
  4.   (if (setq b (ssget "_x" (list '(0 . "insert") (cons 2 (strcat "*" nb "*")))))
  5.     (progn
  6.       ;; Convert selection set to a list
  7.       (setq b (mapcar 'cadr (ssnamex b)))
  8.       ;; If the 'titleblock' definition does not exist in drawing
  9.       (if (null (tblobjname "block" nb))
  10.         ;; Rename the first entry's block definition to 'titleblock'
  11.         (progn
  12.           (setq e (entget (tblobjname "block" (cdr (assoc 2 (entget (car b)))))))
  13.           (entmod
  14.             (subst (cons 2 nb) (assoc 2 (entget (cdr (assoc 330 e)))) (entget (cdr (assoc 330 e))))
  15.           )
  16.         )
  17.       )
  18.       ;; Now cycle through blocks making their name 'titleblock'
  19.       (foreach e b (setq el (entget e)) (entmod (subst (cons 2 nb) (assoc 2 el) el)))
  20.     )
  21.   )
  22.   (princ)
  23. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

tedg

  • Swamp Rat
  • Posts: 811
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #8 on: October 29, 2015, 11:16:45 AM »
Oh I gotcha .. give the code below a try. This assumes that your *titleblock* blocks have the same geometry & are just named differently.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ b e el nb)
  2.   (setq nb "titleblock")
  3.   ;; This selection set will fail on modified dynamic blocks ( so be aware )
  4.   (if (setq b (ssget "_x" (list '(0 . "insert") (cons 2 (strcat "*" nb "*")))))
  5.     (progn
  6.       ;; Convert selection set to a list
  7.       (setq b (mapcar 'cadr (ssnamex b)))
  8.       ;; If the 'titleblock' definition does not exist in drawing
  9.       (if (null (tblobjname "block" nb))
  10.    ;; Rename the first entry's block definition to 'titleblock'
  11.    (progn
  12.      (setq e (entget (tblobjname "block" (cdr (assoc 2 (entget (car b)))))))
  13.      (entmod
  14.        (subst (cons 2 nb) (assoc 2 (entget (cdr (assoc 330 e)))) (entget (cdr (assoc 330 e))))
  15.      )
  16.    )
  17.       )
  18.       ;; Now cycle through blocks making their name 'titleblock'
  19.       (foreach e b (setq el (entget e)) (entmod (subst (cons 2 nb) (assoc 2 el) el)))
  20.     )
  21.   )
  22.   (princ)
  23. )


Thank you sir... that is perfect!
Cant thank you enough, it does exactly what I needed it to do but couldn't wrap my head around the code.
[cheers]
Windows 10 Pro 64bit, AutoCAD 2023, REVIT 2023

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #9 on: October 29, 2015, 03:26:49 PM »
Glad you could make use of it  :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

colidolid

  • Mosquito
  • Posts: 1
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #10 on: July 31, 2021, 03:10:17 AM »
Sorry, I go around and find this topic

Could you anyone can help I wish this lisp can do as below:

Rename multi Blocks reference by selection old name to new name(MText or Text)

 Ex:

Input : Select1 is Multi Blocks,                  Seclect2 is Multi MText or Text

Block old name (A$C1CC872FC)                   ABCD
Block old name (A$C4ADF4549)                   IJKH
Block old name (A$C5BE828AB)                   JKLM

Result :
Block old name (A$C1CC872FC) ----> Block New Name ( ABCD)
Block old name (A$C4ADF4549) ----> Block New Name ( IJKH)
Block old name (A$C5BE828AB) ----> Block New Name ( JKLM)
.................

Thank you

sani

  • Mosquito
  • Posts: 2
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #11 on: October 07, 2023, 06:47:39 AM »
how these codes gonna work?
can someone explain?
I need to rename all blocks, need to add prefix in all blocks. pls help.

sani

  • Mosquito
  • Posts: 2
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #12 on: October 07, 2023, 06:48:56 AM »
ronjonp, can you make it a lisp file?

ronjonp

  • Needs a day job
  • Posts: 7524
Re: Rename a block with a common prefix - wildcard? and/or Replace Block
« Reply #13 on: October 09, 2023, 07:24:11 PM »
Do you want something like this? It will place a prefix in front of all blocknames.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ key)
  2.   (setq key "PREFIX-")
  3.     (vl-catch-all-apply
  4.       'vla-put-name
  5.       (list a (strcat key (vl-string-left-trim key (vla-get-name a))))
  6.     )
  7.   )
  8.   (princ)
  9. )

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC