Jump to content

Find and replace text without a dialog box


y49sides

Recommended Posts

I looking for a way to automate the find and replace. I have a large amount of drawings with a specific text notation " D.F." needs to be changed to "DF". I want to automate it for my users by creating an lisp and an icon on a toolbar.

I tried writing it in the macro on an toolbar icon but I do not know how to get it past the dialog box.

Can anyone point me in the right direction or have a lisp that will do that?

Thanks in advance

Link to comment
Share on other sites

Try this but very quick and dirty

 

(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list)
 (or (vl-load-com))
 (setq	fn	       (getfiled "Select *ANY .DWG FILE* in a desired folder : "
			 ""
			 "dwg"
			 4
	       )
fold	       (vl-filename-directory fn)
full_name_list (vl-directory-files fold "*.dwg" 1)
full_name_list (mapcar (function (lambda (x)
				   (strcat fold "\\" x)
				 )
		       )
		       full_name_list
	       )
 )
 (setq	acapp (vlax-get-acad-object)
adocs (vla-get-documents acapp)
 )
 (foreach fl full_name_list
   (setq adoc (vla-open adocs fl :vlax-false))
   (setq acsp (vla-get-modelspace adoc))
   (vlax-for lt (vla-get-layouts adoc)
     (vlax-for	obj (vla-get-block lt)
(if (eq "AcDbText" (vla-get-objectname obj))
  (if (eq "D.F." (vla-get-textstring obj))
    (vla-put-textstring obj "DF")
  )
)
     )
   )
   (vla-save adoc)
   (vla-close adoc)
 )
)
(princ "\n\t***\tStart command with RT\t***")
(princ)

 

~'J'~

Link to comment
Share on other sites

Thank you very much. I got it all set up but it's not switching the text out. The "D.F." is in a string with other text. Could it be that this lisp will only look for a text string with only "D.F." in it?

Link to comment
Share on other sites

That's exactly why.

Replace this:

	  (if (eq "D.F." (vla-get-textstring obj))
    (vla-put-textstring obj "DF")
  )

with this:

	  (if (wcmatch (vla-get-textstring obj) "*D.F.*")
    (vla-put-textstring obj
             (vl-string-subst "DF" "D.F."(vla-get-textstring obj)))
  )

Link to comment
Share on other sites

Cab,

Thank you very much for all the help.

Can this code be written for just one drawing at a time.

It seems this code asks me to pick a .dwg file but it goes through all .dwg in that specific folder.

Thanks again.

Link to comment
Share on other sites

Sure, try this:

;;  Code by Fatty Mod by CAB
(defun C:RT (/ acapp acsp adoc adocs fn)
 (vl-load-com)
 (if (setq fn (getfiled "Select .DWG FILE to replace text : " "" "dwg" 4))
   (progn
     (setq acapp (vlax-get-acad-object)
           adocs (vla-get-documents acapp)
     )
     (setq adoc (vla-open adocs fn :vlax-false))
     (setq acsp (vla-get-modelspace adoc))
     (vlax-for lt (vla-get-layouts adoc)
       (vlax-for obj (vla-get-block lt)
         (if (eq "AcDbText" (vla-get-objectname obj))
           (if (wcmatch (vla-get-textstring obj) "*D.F.*")
             (vla-put-textstring obj
               (vl-string-subst "DF" "D.F." (vla-get-textstring obj))
             )
           )
         )
       )
     )
     (vla-save adoc)
     (vla-close adoc)
   )
 )
 (princ)
)

Link to comment
Share on other sites

Cab,

Thanks, I'll try that tomorrow. This code looks like it's not for the .dwg that I will currently be in. it looks like the user will still have to manually pick a .dwg. I was looking for it to change the text in the drawing i was in.

i'm sorry if I sound like a pest. I really do appreciate all of the above info.

Thanks again

Link to comment
Share on other sites

Here is edited version, sorry for belating

 

(defun C:RT (/ acapp acsp adoc adocs fn fold full_name_list st)
;; local defun  
;; by Michael Puckett
(defun Replace ( oldText newText text / i )
(if (/= oldText newText)
(while (setq i (vl-string-search oldText text))
(setq text
(vl-string-subst
newText
oldText
text
i
)
)
)
)
text
)
 (or (vl-load-com))
 (setq	fn	       (getfiled "Select *ANY .DWG FILE* in a desired folder : "
			 ""
			 "dwg"
			 4
	       )
fold	       (vl-filename-directory fn)
full_name_list (vl-directory-files fold "*.dwg" 1)
full_name_list (mapcar (function (lambda (x)
				   (strcat fold "\\" x)
				 )
		       )
		       full_name_list
	       )
 )
 (setq	acapp (vlax-get-acad-object)
adocs (vla-get-documents acapp)
 )
 (foreach fl full_name_list
   (setq adoc (vla-open adocs fl :vlax-false))
   (setq acsp (vla-get-modelSpace adoc))
   (vlax-for lt (vla-get-layouts adoc)
     (vlax-for	obj (vla-get-block lt)
(if (eq "AcDbText" (vla-get-objectname obj))
  (if (wcmatch (setq st (vla-get-textstring obj)) "*D.F.*")
    (vla-put-textstring obj (Replace "DF" "D.F." st))
  )
)
     )
   )
   (vla-save adoc)
   (vla-close adoc)
 )
)
(princ "\n\t***\tStart command with RT\t***")
(princ)

Link to comment
Share on other sites

Sure, try this:

;;  Code by Fatty Mod by CAB
(defun C:RT (/ acapp acsp adoc adocs fn)
 (vl-load-com)
 (if (setq fn (getfiled "Select .DWG FILE to replace text : " "" "dwg" 4))
   (progn
     (setq acapp (vlax-get-acad-object)
           adocs (vla-get-documents acapp)
     )
     (setq adoc (vla-open adocs fn :vlax-false))
     (setq acsp (vla-get-modelspace adoc))
     (vlax-for lt (vla-get-layouts adoc)
       (vlax-for obj (vla-get-block lt)
         (if (eq "AcDbText" (vla-get-objectname obj))
           (if (wcmatch (vla-get-textstring obj) "*D.F.*")
             (vla-put-textstring obj
               (vl-string-subst "DF" "D.F." (vla-get-textstring obj))
             )
           )
         )
       )
     )
     (vla-save adoc)
     (vla-close adoc)
   )
 )
 (princ)
)

 

Alan, thanks again

 

Regards,

 

Oleg :)

Link to comment
Share on other sites

Your original post led us to believe you wanted to process many drawings.

 

To replace Plain Text in the current drawing this is all you need:

Note that it doesn't process Mtext or Attributes. For That you need this

 

(defun C:RT (/ adoc Old$ New$)
 (vl-load-com)
 (setq Old$ "D.F."
       New$ "DF")
 (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
 (vlax-for lt (vla-get-layouts adoc)
   (vlax-for obj (vla-get-block lt)
     (if (eq "AcDbText" (vla-get-objectname obj))
       (while (vl-string-search Old$ (vla-get-textstring obj))
         (vla-put-textstring obj
           (vl-string-subst New$ Old$ (vla-get-textstring obj)))
       )
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

  • 2 weeks later...

Hi!

 

Thanks for that code, it's very useful.

 

I have same problem but I need to replace more than just one word.

Can you help me?

 

Example:

D.F. --> DF

and

MOI --> JOI

 

 

 

-Galantic

Link to comment
Share on other sites

Try this, list consist of pairs, add as many as needed

(defun C:RT (/ adoc List$)
 (vl-load-com)
 (setq $List '(("D.F." "DF") ("MOI" "JOI")))  ; (Old$ New$)
 (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
 (vlax-for lt (vla-get-layouts adoc)
   (vlax-for obj (vla-get-block lt)
     (if (eq "AcDbText" (vla-get-objectname obj))
      (mapcar '(lambda(x)
       (while (vl-string-search (car x) (vla-get-textstring obj))
         (vla-put-textstring obj
           (vl-string-subst (cadr x) (car x) (vla-get-textstring obj)))
       )
       ) $List)
     )
   )
 )
 (princ)
)

Link to comment
Share on other sites

  • 3 years later...

I am in need of something similar but I would like it to process multiple drawings at once like the original code you were presenting, but I need to to work on Mtext. I tried the one from the link you gave but it doesn't seem to want to run for me.

 

All I really want to do is scan thru all the drawings in a folder and delete "alum." from all occurrences in mtext.

 

Dznytoy

Edited by dznytoy
added
Link to comment
Share on other sites

Very nice CAB. Is it possible to modify it so that it replaces whole words only?

for instance:

 

CABLE TROUGH CPS = a text string in the drawing

 

("E " "Eaves") = find replace setting

 

results = CABLEavesTROUGH CPS

 

whereas I'd like this to be ignored.

 

cheers

P

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...