Jump to content

Batch Find & Replace Text


Lee Mac

Recommended Posts

Hi Lee,

 

I use this lisp generally just using the single line text setting.

Is it easy to change the lisp so that a forward slash is not seen as a new word. For instance:

 

IR --> I/R

but

R --> Ridge --> this ends up as I/Ridge as the forward slash seems to be seen as a new word.

 

I guess it would be good if the 'search and replace' strings could be run in a predetermined order, but I'm almost certain your not looking to rewrite this lisp any time soon..

 

On other thing, is there an easy way to restrict the single line text setting to only apply to a certain layer? Or is it easier to put a wrapper around this lisp to lock all but the one layer and then unlock them after?

It would be amazing if any replaced text strings could then be moved to a pre-set layer, just rambling, the '/' issue is my main issue.

 

Thanks for your time and great lisps.

 

P

Link to comment
Share on other sites

I guess it would be good if the 'search and replace' strings could be run in a predetermined order, but I'm almost certain your not looking to rewrite this lisp any time soon..

 

I haven't looked back over the code for this application in detail, but I believe the program will perform the replacements in the order in which they appear in the list, therefore, if you were to enter the replacements as:

 

Find: R

Replace: Ridge

Find: IR

Replace: I/R

 

Then you should hopefully obtain the desired result.

 

On other thing, is there an easy way to restrict the single line text setting to only apply to a certain layer? Or is it easier to put a wrapper around this lisp to lock all but the one layer and then unlock them after?

It would be amazing if any replaced text strings could then be moved to a pre-set layer, just rambling, the '/' issue is my main issue.

 

As a quick workaround for this - you could change lines 1318-1322 from:

(not
 (and (= 2 (logand 2 bit))
   (vl-position (strcase (vla-get-layer obj)) lkl)
 )
)

to:

(= "YOURLAYERHERE" (strcase (vla-get-layer obj)))

Where YOURLAYERHERE is the layer you wish to modify (this must be uppercase!)

 

I hope this helps!

Link to comment
Share on other sites

Lee,

I didn't knew what to expect, after seeing these dialogs in your main post,

So I just checked your code - just to see whats like writing a COMPLETE program and It looks like your creating your own AutoCAD! (literally)

Also I've found some nice subfunctions inside. :D

 

Good job, mate! :beer:

 

P.S. I knew about this thread quite a while, but just now I could appreciate the effort you did put in this project.

Link to comment
Share on other sites

Brilliant I will give it a go. Thanks

 

You're welcome Least - I hope it works as expected.

 

Lee,

I didn't knew what to expect, after seeing these dialogs in your main post,

So I just checked your code - just to see whats like writing a COMPLETE program and It looks like your creating your own AutoCAD! (literally)

Also I've found some nice subfunctions inside. :D

 

Good job, mate! :beer:

 

P.S. I knew about this thread quite a while, but just now I could appreciate the effort you did put in this project.

 

Thank you for your kind words and appreciation Grrr - the development of this particular application did indeed involve a great deal of work, mostly to accommodate the majority of MText formatting which could be present within the content of annotation objects which support such formatting, whilst also performing the find & replace operations around it...

 

That said, the code for this program is now 7 years old and is in dire need of a complete overhaul. If you were looking for a more representative example, you might wish to look at the code for my Incremental Numbering Suite or Batch Attribute Editor which are more recently & extensively developed applications.

Link to comment
Share on other sites

I haven't looked back over the code for this application in detail, but I believe the program will perform the replacements in the order in which they appear in the list, therefore, if you were to enter the replacements as:

 

Find: R

Replace: Ridge

Find: IR

Replace: I/R

 

Then you should hopefully obtain the desired result.

 

 

Looks like the routine sorts the list into alphabetical order on import.

I have had a look, is it this function _sortbyfirst?

 

Thanks

sort.jpg

Link to comment
Share on other sites

Looks like the routine sorts the list into alphabetical order on import.

I have had a look, is it this function _sortbyfirst?

 

Sorry, I thought you were entering the items manually - change line 748 from:

(vl-sort lst (function (lambda ( a b ) (< (car a) (car b)))))

to:

lst

Link to comment
Share on other sites

  • 5 months later...

I am looking for something very similar to this. using this program to create the list of things i need to change would be extremely useful, as altering a code to add things manually leaves for human error (I am very new to coding)

So basically, what I want is exactly what you've got, but I want to be able to put the predefined find and replace within another larger command (basically it just cleans up our drawings with 6 or so commands at once).

In short, I want to be able to type in a command and have this done automatically, with no user intervention. making it human error proof for my large group of drafters. I need it to only evaluate the current drawing, and look only in text/mtext/multileaders.

I want to embed this concept within another command so its done automatically with no user intervention. is this possible?

 

Just to clarify, I am REALLY new to autoLISP and things of the sort, so I really appreciate very cut nd dry codes or advice. I've been trying to write this code by using a lot of different things I've found, but no idea how to make it do what i want.

 

in fact, I will place a copy of the code that we already have that is so close to what i want, but not quite there. It searches attributes only, and i need it to search text/mtext, but simply don't have the knowledge to bend the code to what i need. any advice, or fixes would be GREATLY appreciated.

 

(defun attreplace (old new / aval)
 (vlax-for n
    (vla-get-blocks
      (vla-get-activedocument
	(vlax-get-acad-object)))
   (vlax-for m n
   (if (and (= "AcDbBlockReference" (vla-get-objectname m))
     (= :vlax-true (vla-get-hasattributes m)))
(foreach a
	 (vlax-invoke m  'getattributes)
  (setq aval (vla-get-textstring a))
  (while (vl-string-search old aval) ;;<- look for string
   (setq aval (vl-string-subst new old aval )) ;;<-replace here
    )
  (vla-put-textstring a aval)
  )
     ))
     ))
	(attreplace "TECHNOLOGIES" "TECH")
	(attreplace "COMMUNICATION COMPONENTS INC." "COMM COMPONENTS")
	(attreplace "DUAL BAND" "DB")
	(attreplace "Remote Radio Head" "Rem Radio Hd")
	(attreplace "Other - With RF" "Other-W/ RF")
	(attreplace "Other - Without RF" "Other-W/O RF")
	(attreplace "COMMUNICATIONS" "COMM")
	(attreplace "COMMUNICATION" "COMM")
	(attreplace "TECHNOLOGY" "TECH")
	(attreplace "FULL BAND" "FB")
	(attreplace "MASTHEAD" "MSTHD")
	(attreplace "DUAL DUPLEX" "DD")

	(princ)
  )

 

As you can see, this is very cut and dry with where/how to add new things for it to search for. I love the setup of this code. Can anyone direct me to how i should alter this for text/mtext?

Link to comment
Share on other sites

My god you are my savior. This works beautifully!! I can't thank you enough, I've been searching for this for weeks!!!

 

All of Lee's lisps are fantastic, but this one is truly mind blowing, I love whenever I get to use it!

 

Thanks Lee! :beer:

Link to comment
Share on other sites

  • 8 months later...

Hello

One more here that has to say "Thank you" for the great amount of time saved by this Lisp in particular.

I am having a strange result that I would like to ask:

When I have a MTEXT (plain text, no heavy format) as a block attribute, and I replace, it duplicates the replacement i.e.:

I want to replace "Hello" by "Hi". For a "Hello" I get "HiHi". If I then replace "HiHi" for "Hi" I end up getting what I wanted.

Any idea on why this happens and how to solved if possible?

Thank you so much again

Regards

Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...

First of all, 

 

I want to thank you Lee Mac for this excellent LISP. I found the interface very helpful and it is amazing that you are able to import a batch of strings from a separate txt file as your search material.

 

I have done a lot of searches with this lisp but a couple of days ago I got some weird results. For some reason the report is contradictory. Basically if my find-string was "A" and replace string "B" the report says that it has made a replacement C --> D. So the report says that it has made a replacement of a text-string that was not included in the search anyhow.

 

I'm quite confident that there is a logical explanation behind this but it is weird that it has performed well earlier and now I have met these problems. 

 

Do you guys have a clue what might be the cause of this?

Link to comment
Share on other sites

  • 11 months later...

the sub function _ReplaceText change string to list the add char code in the list will break the vba regex replace function with non-ASCII character like Chinese or Japenese

I change it to below code will make it work, basically, it will lose the format manipulate, but it will has no error anyway.

(defun _ReplaceText ( newstr oldstr str case whole / )
		(_RegExReplace newstr oldstr str)
	)

Would you please update you code to support no-ASCII character?

Here is a dwg with a Chinese single-line text enity.

Drawing2.dwg

Edited by dirtyacc
Link to comment
Share on other sites

  • 1 year later...

Thank you so much for this solution. 

Can I replace a one line text by a multiline text ? If yes, I would be grateful if you can help me to do it

Link to comment
Share on other sites

On 7/9/2021 at 11:28 AM, fatima-ezzahramohtich said:

Thank you so much for this solution. 

Can I replace a one line text by a multiline text ? If yes, I would be grateful if you can help me to do it

if Its one txt
image.png.f61deaa5bdf9c46197d448bf5b2d6312.png

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