Jump to content

Find and replace text without a dialog box


Recommended Posts

Posted

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

Posted

Did you look at the similar threads in this forum, as shown at the bottom of this page? This topic has come up a few times.

Posted

yes, that is why I decided to post this question. I was looking for something a bit more specific.

Thanks

Posted

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'~

Posted

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?

Posted

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)))
  )

Posted

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.

Posted

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)
)

Posted

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

Posted

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)

Posted
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 :)

Posted

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)
)

Posted

CAB,

Thanks. The last one work perfectly. I appreciate al the help.

  • 2 weeks later...
Posted

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

Posted

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)
)

  • 3 years later...
Posted (edited)

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
Posted

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

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...