Batch rename block in multi drawing ?

Batch rename block in multi drawing ?

Anonymous
Not applicable
3,236 Views
11 Replies
Message 1 of 12

Batch rename block in multi drawing ?

Anonymous
Not applicable

Hi all,

 

I have code rename block, now i want to rename in multi drawing. Can some one tell me the way do it.

 

Thank in advance.

 

(defun c:rmb (/ old_name new_name)
(if (setq old_name (cdr (assoc 2 (entget (car (entsel "\n Select block: "))))))
(progn
(if (setq new_name (getstring "\nNew name block: "))
(progn
(vla-put-Name
(vla-item
(vla-get-blocks
(vla-get-activedocument
(vlax-get-acad-object)
)
)
old_name
)
new_name
)
)
);if
);progn
);if
(princ)
);defun

0 Likes
Accepted solutions (1)
3,237 Views
11 Replies
Replies (11)
Message 2 of 12

Luís Augusto
Advocate
Advocate
0 Likes
Message 3 of 12

Anonymous
Not applicable

Thanks Luis,

 

I writed my code: but how can add (if (tblsearch "BLOCK" old_name) then progn rename_block) ???????? Can some one tell me, pls.

 

(defun C:rename_multi_block_in_multi_drawing (/ :bb old_name new_name DirPath DwgFile DwgList Ofile Scrfile)
(vl-load-com)
(setq :bb (car (entsel "\nSelect block to RENAME:")))
(setq old_name (vla-get-effectivename (vlax-ename->vla-object :bb)))
(setq new_name (getstring "\nChon ten moi cho block: "))
(if (setq DirPath (getfiled "Select the drawing directory:" "" "dwg" 0))
(setq DirPath (vl-filename-directory DirPath))
)
(if DirPath
(setq DwgList (vl-directory-files DirPath "*.dwg" 1))
)
(setq Scrfile (strcat DirPath "\\" "test.scr"))
(setq Ofile (open Scrfile "w"))
(foreach Dwg DwgList
(setq DwgFile (strcat DirPath "\\" Dwg))
(write-line
(strcat "_.open\r" (chr 34) DwgFile (chr 34) "\r") Ofile
)
(write-line "-RENAME" Ofile)
(write-line "block" Ofile)
(write-line old_name Ofile)
(write-line new_name Ofile)
(write-line "_.qsave" Ofile)
(write-line "_.close" Ofile)
);foreach
(close Ofile)
(command "_.script" Scrfile)
(vla-sendcommand
(vla-get-activedocument (vlax-get-acad-object))
"(vl-file-delete Scrfile)\r"
)
(princ)
);defun
(defun c:Reb ()(C:rename_multi_block_in_multi_drawing))

0 Likes
Message 4 of 12

Luís Augusto
Advocate
Advocate

No error handling.

 

(defun c:batchRename (/ fileSCR dir run)

  (setq	dir	(strcat	(LM:browseforfolder
			  "Select a folder"
			  (strcat (getenv "UserProfile") "\\Desktop")
			  0
			)
			"\\"
		)
	fileSCR	(open (strcat dir "batchFile.scr") "w")
  )

  (foreach dwg (vl-directory-files dir "*.dwg" 1)
    (write-line "_.open" fileSCR)
    (write-line (strcat "\"" dir dwg "\"") fileSCR)

    (write-line	;load your lisp here
      "(load \"C:/Users/Augusto/Desktop/rename.lsp\")"
      fileSCR
    )
    (write-line "_.qsave" fileSCR)
    (write-line "_.close" fileSCR)
  )
  (close fileSCR)

  (if (findfile (setq fileSCR (strcat dir "batchFile.scr")))
    (command "_script" fileSCR)
  )	
)

;; Browse for Folder  -  Lee Mac
;; Displays a dialog prompting the user to select a folder.
;; msg - [str] message to display at top of dialog
;; dir - [str] [optional] root directory (or nil)
;; bit - [int] bit-coded flag specifying dialog display settings
;; Returns: [str] Selected folder filepath, else nil.
 
(defun LM:browseforfolder ( msg dir bit / err fld pth shl slf )
    (setq err
        (vl-catch-all-apply
            (function
                (lambda ( / app hwd )
                    (if (setq app (vlax-get-acad-object)
                              shl (vla-getinterfaceobject app "shell.application")
                              hwd (vl-catch-all-apply 'vla-get-hwnd (list app))
                              fld (vlax-invoke-method shl 'browseforfolder (if (vl-catch-all-error-p hwd) 0 hwd) msg bit dir)
                        )
                        (setq slf (vlax-get-property fld 'self)
                              pth (vlax-get-property slf 'path)
                              pth (vl-string-right-trim "\\" (vl-string-translate "/" "\\" pth))
                        )
                    )
                )
            )
        )
    )
    (if slf (vlax-release-object slf))
    (if fld (vlax-release-object fld))
    (if shl (vlax-release-object shl))
    (if (vl-catch-all-error-p err)
        (prompt (vl-catch-all-error-message err))
        pth
    )
)
(princ)

 

Lisp file to rename blocks attached.

 

  (setq	btable '(
		 ("OLDBLOCK1" . "NEWBLOCK1")
		 ("OLDBLOCK2" . "NEWBLOCK2")
		 ("OLDBLOCK3" . "NEWBLOCK3")
		)
  )

  (foreach b btable
		  (if
			(and 
				(not (tblsearch "BLOCK" (cdr b)))
				(tblsearch "BLOCK" (car b))
		    )
		    (command "_.RENAME" "_Block" (car b) (cdr b))
		   )
   )

 

Best regards, Luís Augusto.

0 Likes
Message 5 of 12

Anonymous
Not applicable

Luis,

 

i mean select block name and new_name in current drawing opened then replace this block in multidrawing. So how can assign old_name and new_name to another drawing ??

0 Likes
Message 6 of 12

Luís Augusto
Advocate
Advocate
Hello Pony,

For each dwg you have to make the selection again?

Rename the blocks in the current dwg?
0 Likes
Message 7 of 12

dennis
Advisor
Advisor
Accepted solution

A trick I use often is to write out a simple txt file to the user's temp folder.  Then have code check for that file.  If it doesn't find the file, then do the prompting sequence for old block name and new block name.  Write that data to the txt file.  If the code 'sees' that text file, then it doesn't prompt, it pulls the info out of the text file and runs.

Then delete the text file if you are in the last drawing collected to process.

Message 8 of 12

Anonymous
Not applicable
Hi dennis,

I understood. Write old and new_name to temp txt file.

Thanks for your solution.
0 Likes
Message 9 of 12

Anonymous
Not applicable
Luis,

I don't want selection for each dwg. Only select in current dwg, and for each dwg run script with old_name and new_name in current dwg.

Thank you.
0 Likes
Message 10 of 12

Anonymous
Not applicable

Uhm,

 

I modify my lisp and run no error but drawing load lisp then don't rename block. Why ?????

 

Can someone tell me, pls ????

0 Likes
Message 11 of 12

Anonymous
Not applicable

here my lisp

 

(setq tmp (strcat "C:/Users/Public/temp.txt"))
(setq lst nil)
(if (findfile tmp)
(progn
(setq fo (open tmp "r"))
(while (setq aline (read-line fo))
(setq lst (cons aline lst))
) ;while
(princ)
(close fo)
;return lst is (old_name new_name)
(setq lst (reverse lst))
;;; (print lst)
(print (car lst) (cadr lst))
) ;progn
) ;if
(if (and (not (tblsearch "BLOCK" (cadr lst)))
(tblsearch "BLOCK" (car lst))
) ;and
(command "_.RENAME" "_Block" (car lst) (cadr lst))

0 Likes
Message 12 of 12

Anonymous
Not applicable

Ah delete line: (print (car lst) (cadr lst))

 

Okey.

 

Finish this topic. 

 

Thank you.

0 Likes