Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Batch Rename Blocks

26 REPLIES 26
Reply
Message 1 of 27
mad_monk42
5735 Views, 26 Replies

Batch Rename Blocks

Hi,
Please excuse if this question has already been posted.
(I couldn't find one exactly)

I would like to batch rename all blocks in a drawing to NEWJOB01, NEWJOB02 ,etc.
I can't use wildcards with RENAME command because the old names don't always have a common name.
I prefer to use a lisp routine.

Thanks,

Murtz
26 REPLIES 26
Message 2 of 27
YankeeSteve
in reply to: mad_monk42

(defun C:BN- ( / blist bl b n)
(setq blist '() bl nil b 0 n "NEWJOB")
(while (setq bl (tblnext "block" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_b" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
(princ)
)
Message 3 of 27
mad_monk42
in reply to: mad_monk42

Thanks a lot.

Murtz
Message 4 of 27
abrusil33
in reply to: YankeeSteve

I would like to rename just a single revision Letter and or option Numbers in the name of multiple blocks

(text string "ZZZ-123-rev.A-object name-option.1-date").


For example, I have 15 blocks in the drawing with names similar to:

  • "blabla-REV.A-blabla-OPTION.1-blabla"

and I need to revise to

  • REV.B and / or (optional) OPTION.2 ("blabla-REV.B-blabla-OPTION.2-blabla"), so I have to 

"C:ren", and change one by one, coz wild cards not working if blocks have different characters among *REV.* and *OPTION.*

 

Would be awesome if it will work like: - > (lisp...) - > type: *REV.A* rename to *REV.B*, and *OPTION.1* to *OPTION.2* for all blocks in the set.

 

P.S. all blocks has same naming structure: "A-B-rev.A-<name>-option.1-<date>" 

<name> - Table 1 , or Table 34, ...(stays unchanged)

<date> - 2011, or 2013 ... (stays unchanged)

Message 5 of 27
Kent1Cooper
in reply to: abrusil33

A few questions:

 

Would you always want to change both the REV.[letter] and the OPTION.[number] in every Block name?  Or might you at least sometimes want to change only one or the other?

 

Would something that automatically just increases the letter and the number by one, regardless of their starting values, without User input, be appropriate?  Or would you want the User to specify which letter/number values to change and what to change them to [for instance, if you might sometimes want to change from, say, A to C instead of to B]?

Kent Cooper, AIA
Message 6 of 27
pbejse
in reply to: abrusil33

Same question as Kent 

 

Message 7 of 27
abrusil33
in reply to: Kent1Cooper

The program "All File Renamer" http://www.altarsoft.com/all_file_renamer.shtml

will rename the portion of any name in all files in this format:

 

Find: REV.1

Replace with: REV.2

 

But this program is for windows explorer only.

If there is a LISP that will run Find and Replace in the Block names, that would be awesome.

 

Any Ideas?

Thanx

Message 8 of 27
pbejse
in reply to: abrusil33


@abrusil33 wrote:

 

Any Ideas?

Thanx


Ideas Yes, Need more info though, answer the question at Kent Coopers post, We do not  blindly write a program without knowing all the details. (sometimes we do 🙂 )

 

 

Message 9 of 27
pbejse
in reply to: pbejse

Find and replace approach

 

(defun C:BNM- (/ blist bl b find replace FindList Replacelist  NewName)
(defun _match (b l)
       (vl-every '(lambda (k)
          (wcmatch (strcase b) (strcat "*" k "*")))  l
                                        )
  )
  (setq blist '()
        bl nil
  FindList nil
  Replacelist nil)
  (while  (and
		  (setq find (Strcase (getstring "\nString to Find/Press Enter for none: ")))
		  (not (eq "" find))
		  (setq FindList (cons find FindList))
		  (setq replace (Strcase (getstring (strcat "\nReplace " find " with: "))))
		  (not (eq "" replace))
		  (setq replaceList (cons replace replaceList))
		  	)
		  )
  (and
  FindList
  (while (setq bl (tblnext "block" (not bl)))
    (and (zerop (logand 53 (cdr (assoc 70 bl))))
	 (_match (setq bl (cdr (assoc 2 bl))) FindList)
         (or (member bl blist) (setq blist (cons bl blist)))
    )
  )
  blist
  (foreach itm blist
    (setq Newname itm)
    (mapcar '(lambda (x y / p)
	(setq p (vl-string-search x (strcase itm))
    	      Newname	(strcat (strcat (substr Newname 1 p) y 
    			(substr Newname (+ p (strlen x) 1))))))
	   findlist Replacelist)
    (command "_rename" "_b" itm NewName)
  )
  )
  (print (length blist))(princ " Blocks Renamed")
  (princ)
)

 

This demo will look for REV.A and OPTION.1

 

"A-B-REV.A-Table 2-OPTION.1-2011" 

"A-B-REV.A-Table 3-OPTION.1-2012" 

"A-B-REV.B-Table 4-OPTION.3-2012" 

 

command:BNM-

String to Find/Press Enter for none: Rev.A

Replace REV.C with: Rev.B

String to Find/Press Enter for none: option.1

Replace OPTION.1 with: Option.2

 

now if and only if these two items are found on the block name

("OPTION.1" "REV.A")

Only then it will renamed the block with this new values

("OPTION.2" "REV.B") 

 

2  Blocks Renamed

 

"A-B-REV.B-Table 2-OPTION.2-2011" 

"A-B-REV.B-Table 3-OPTION.2-2012"

"A-B-REV.B-Table 4-OPTION.3-2012" 

 

Now if you only need to find 1 "string match" on the block name, This will look for REV.B regardless of option number

 

command:BNM-

String to Find/Press Enter for none: Rev.B

Replace REV.C with: Rev.C

String to Find/Press Enter for none:

 

3  Blocks Renamed

 

"A-B-REV.C-Table 2-OPTION.2-2011" 

"A-B-REV.C-Table 3-OPTION.2-2012"

"A-B-REV.C-Table 4-OPTION.3-2012" 

 

Another one would be a "Bump" to next value. without prompting for "replace with" 

 

HTH

Message 10 of 27
abrusil33
in reply to: pbejse

THAT IS BRILLIANT. 100% PURE WHAT I NEEDED.


Thank you "pbejse" you are a quiet Expert Elite ++

Message 11 of 27
abrusil33
in reply to: pbejse

I can modify code to run: BNM- once if I need to, but normally I have to make sure if both revisions are current, so extra reminder won't hurt.

You guys are genious e s! 

Message 12 of 27
pbejse
in reply to: abrusil33


@abrusil33 wrote:

I can modify code to run: BNM- once if I need to, but normally I have to make sure if both revisions are current, so extra reminder won't hurt.

 


Good for you, Holler if you need more help.

 

Smiley Happy

Message 13 of 27
abrusil33
in reply to: pbejse

Guys, 
Just figured out that this routine would not work if only 3D blocks in the drawing.
I have to make at least one 2D block within the drawing, and then it will do the rename, as long as I remove 2D block and purge, it is not renaming.  I know it easy for you to fix, but not for me at this time...Thanx

Message 14 of 27
abrusil33
in reply to: pbejse

I am attaching the file as an example that routine would not work as long as associative dimension is in there.

Message 15 of 27
pbejse
in reply to: abrusil33


@abrusil33 wrote:

I am attaching the file as an example that routine would not work as long as associative dimension is in there.


My bad abrusil33, error in blist evaluation, nothing to do with associative dimension.

  

(defun C:BNM- (/ blist bl b find replace FindList Replacelist  NewName)
(defun _match (b l)
       (vl-every '(lambda (k)
          (wcmatch (strcase b) (strcat "*" k "*")))  l
                                        )
  )
  (setq blist '()
        bl nil
  FindList nil
  Replacelist nil)
  (while  (and
		  (setq find (Strcase (getstring "\nString to Find/Press Enter for none: ")))
		  (not (eq "" find))
		  (setq FindList (cons find FindList))
		  (setq replace (Strcase (getstring (strcat "\nReplace " find " with: "))))
		  (not (eq "" replace))
		  (setq replaceList (cons replace replaceList))
		  	)
		  )
  (if (and FindList
      (progn
	  (while (setq bl (tblnext "block" (not bl)))
	    (and (zerop (logand 53 (cdr (assoc 70 bl))))
		 (_match (setq bl (cdr (assoc 2 bl))) FindList)
	         (or (member bl blist) (setq blist (cons bl blist)))
	    )
	  ) blist))
	  (foreach itm blist
	    (setq Newname itm)
	    (mapcar '(lambda (x y / p)
		(setq p (vl-string-search x (strcase itm))
	    	      Newname	(strcat (strcat (substr Newname 1 p) y 
	    			(substr Newname (+ p (strlen x) 1))))))
		   findlist Replacelist)
	    (command "_rename" "_b" itm NewName)
	  )
  )
  (print (length blist))(princ " Blocks Renamed")
  (princ)
)

 

HTH

 

Message 16 of 27
abrusil33
in reply to: pbejse

Thanks, works like a charm... !!!! 

I appresiate your work, Patrik.

Message 17 of 27
pbejse
in reply to: abrusil33


@abrusil33 wrote:

Thanks, works like a charm... !!!! 

I appresiate your work, Patrik.


Glad it works for you abrusil33

 

Cheers

 

Message 18 of 27
abrusil33
in reply to: pbejse

Hi there,

 

I created OpenDCL form with text input for old rev and new rev

 

I changed in your routine:

 

(setq find (Strcase (strcat "rev" "." revN)))  ;;(revN - is a text input from the form - old revision)

(setq replace (Strcase (strcat "rev" "." revNto)))  ;;(revNto - is a text input for new rev. number)

 

I think option (while (and .... will not work here, I mean it is not working and I am stuck.. I am trying to make the form that will Rev up all the blocks in the drawing by taking two new inputs (1 - for number revision 2- for letter revision)

 

 

Thanx  (I spent all my weekend time to figure it out and am afraid I won't ble to fix it on my own)

 

++++++++

(defun C:BNM- (/ blist bl b find replace FindList Replacelist NewName)
(defun _match (b l)
(vl-every '(lambda (k)
(wcmatch (strcase b) (strcat "*" k "*"))) l
)
)
(setq blist '()
bl nil
FindList nil
Replacelist nil)
(while (and
(setq find (Strcase (getstring "\nString to Find/Press Enter for none: ")))
(not (eq "" find))
(setq FindList (cons find FindList))
(setq replace (Strcase (getstring (strcat "\nReplace " find " with: "))))
(not (eq "" replace))
(setq replaceList (cons replace replaceList))
)
)
(and
FindList
(while (setq bl (tblnext "block" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(_match (setq bl (cdr (assoc 2 bl))) FindList)
(or (member bl blist) (setq blist (cons bl blist)))
)
)
blist
(foreach itm blist
(setq Newname itm)
(mapcar '(lambda (x y / p)
(setq p (vl-string-search x (strcase itm))
Newname (strcat (strcat (substr Newname 1 p) y
(substr Newname (+ p (strlen x) 1))))))
findlist Replacelist)
(command "_rename" "_b" itm NewName)
)
)
(print (length blist))(princ " Blocks Renamed")
(princ)
)

 

 

Message 19 of 27
Netelaana
in reply to: YankeeSteve


@YankeeSteve wrote:
(defun C:BN- ( / blist bl b n)
(setq blist '() bl nil b 0 n "NEWJOB")
(while (setq bl (tblnext "block" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_b" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
(princ)
)

this worked wonders for me - is there a way to modify it to do the same for layer names?

Message 20 of 27
Netelaana
in reply to: Netelaana

....never mind, ran stuck, but then saw my mistake and got it to work. Thanks for starting me out in the right direction!

 

(defun C:LN- ( / blist bl b n)
(setq blist '() bl nil b 0 n "LAYER")
(while (setq bl (tblnext "layer" (not bl)))
(and (zerop (logand 53 (cdr (assoc 70 bl))))
(not (wcmatch "`**" (setq bl (cdr (assoc 2 bl)))))
(or (member bl blist) (setq blist (cons bl blist)))
)
)
(while (and (car blist) (setq b (1+ b)))
(command "_.-rename" "_la" (car blist)
(strcat n (if (< b 10) "0" "") (itoa b)))
(setq blist (cdr blist))
)
(princ)
) 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost