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

Pre-defined find & replace lisp

54 REPLIES 54
Reply
Message 1 of 55
cadee
22700 Views, 54 Replies

Pre-defined find & replace lisp

Hi

 

Is there a lisp or script program that will find and replace pre-defined text/mtext value of eg " issued for tender " for

" issued for consctruction " without having to select anything.

 

Thanks in advance

54 REPLIES 54
Message 41 of 55
Kent1Cooper
in reply to: DJPiper


@DJPiper wrote:
.... Is it possible to make the lsp accept a partial portion of the text string? ....

That's what wildcards are for [read up about the (wcmatch) function -- the same wildcards work in (ssget) filters].  This will find anything whose textual content includes the word "partial" anywhere within it, including Text, Mtext, Dimensions with override textual content:

....

  (setq tss (ssget "X" ((1 . "*partial*"))))

....

 

If you want to replace only the "partial" part, and leave whatever comes before or after it unchanged, that will take a little more code -- look at the (vl-string-subst) function.

Kent Cooper, AIA
Message 42 of 55
DJPiper
in reply to: Kent1Cooper

Thanks Ken! i'll try this.

Message 43 of 55
arukaroor
in reply to: Kent1Cooper

Hi Kent,

 

Help me please... can you modify this lisp for replacing attribute text also?. I have a bunch of drawings need to replace the attribute text. I tried BFIND lisp from Leemac, but that showing some error in my drawings. So if i get predefined find and replace for attribute text, text & mtext, i can run this by script for all drawings.

 

Thanks.

Message 44 of 55
Kent1Cooper
in reply to: arukaroor


@arukaroor wrote:

.... can you modify this lisp for replacing attribute text also?. ....


I so seldom use Blocks with Attributes that I'm not a very good resource for that kind of thing.  But I'll bet that a little Searching [here and on other sites such as Cadalyst CAD Tips, cadtutor, etc.] will find several threads with solutions for you.

Kent Cooper, AIA
Message 45 of 55
LISPlearner
in reply to: Kent1Cooper

Alright, I know this post is old but i definitely need some help with this. 

 

So basically, i want to do what people are asking above, but also have the lisp run for multiple text changes. 

For example, on our site plans, my group has to go to each text box and replace certain things that have been updated in our stnadards. a couple of examples are as follows:

 

"S.S." must now be "SS"

 

"TYP." must now be "TYP"

 

"FND." must now be "FND"

 

"OCT" OR "OCT." must now be "OCTAGONAL"

 

 

These changes end up taking all of us a ton of time to individually change, or even FIND/REPLACE multiple different things. 

Having a LISP to run through the general things we typically run into on almost every drawing would be extremely helpful and time saving. 

 

I'm not great at writing LISP routines, but i do understand i would have to repeat the same basic command multiple times with each predefined change. can anyone offer a generic LISP that is very clear on where to enter the old text and new, and also very clear on which part i should be repeating? perhaps if someone could do a generic with 2-3 instances of changes being made that I can edit. 

 

Perhaps highlight, or change the text color of exactly what i need to copy over for multiple words/ phrases.

 

Also, this is for MTEXT and TEXT entities. 

 

I would like to be able to do this with a set of predefined things and no user input needed. In such a way that if I or a coworker finds something else that is a common text mistake we are missing, I can add it in, update and everyone can just click a button, and have the text that needs fixed, be fixed. 

 

 

I sincerely appreciate any help at all in advance. 

 

***EDIT*** I should also state that I want it to be able to find text over a whole drawing, within a text box. As with my first example above, the MTEXT box would be "155'-0" S.S. TOWER" I would need it to find the "S.S." and replace it with "SS" to end with "155'-0" SS TOWER"

Message 46 of 55
BlackBox_
in reply to: LISPlearner

Hi @LISPlearner - 

 

Consider @Lee_Mac's Batch Find & Replace Text routine:

 

http://www.lee-mac.com/bfind.html

 

 

... Or his Batch Attribute Editor routine:

 

http://www.lee-mac.com/batte.html

 

 

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 47 of 55
LISPlearner
in reply to: BlackBox_

Thank you for the suggestion!

I have looked into these though, and would far prefer it to be something where i can predefine the text it looks for, rather than have a prompt every time I use it. 

Basically, we have thousands of site plans we're updating every day that each have an average or 5-10 pieces of text that need updated individually (all reoccurring throughout our catalog of drawings) and to be able to push a button, and have each instance fixed upon running the LISP would be ideal, rather than individually entering each text needing changed on each drawing. It would be quicker just to edit it for us, as we only work on one site at a time and each text discrepancy only occurs once or twice on each drawing. 

 

Message 48 of 55
daviddejesus
in reply to: greg_battin

Thanks heaps mate. Very useful the lisp created by Lee Mac

Message 49 of 55
tiberian1138
in reply to: Kent1Cooper

Please help. I am Lisp challenged. I understand the simple stuff but when it gets more complex I just cannot grasp it. Most of the time I can take Lisps that have been already written and modify them to work for my needs with little to no problem. Have done it with 80+ over the past few months.  But this one is stumping me. This routine to replace text globally is awesome and I use it for text that can be changed globally. But I also need to be able to select individual pieces of text (all mtext). Here is an example: I work for a home builder and a common change item is changing carpet to tile in select rooms. Find/Replace works but is not efficient.  I would like to be able to go through and select each room separately and it changes the text without having to type it every freaking time. What would I have to alter in this lisp to select individual rather than global? 

Message 50 of 55
Kent1Cooper
in reply to: tiberian1138


@tiberian1138 wrote:

.... This routine to replace text globally is awesome .... But I also need to be able to select individual pieces of text (all mtext). .... What would I have to alter in this lisp to select individual rather than global? 


Remove the "X" from the (ssget) function.

Kent Cooper, AIA
Message 51 of 55
Sea-Haven
in reply to: tiberian1138

As an alternative to using ssget you could do a while that just asks pick text and makes the changes, using brute force, eg "Carpet flooring" replace with a new string "Italian tiles" so its a string replace not a word replace. It would be based on the fact that your dwg's have consistent text that you use, a option would be add a custom string.

 

I use a library dcl for common picks as below if you think that is useful post again with a list of alternative materials you can have around 20 choices max. I will put it together so pick Text or Mtext. (disclaimer plain mtext not with extra parameters). You can have more detail in the replacement text just short for dcl display. Thinking more add a next dcl for tiles so pick size.

 

SeaHaven_0-1626320646155.png

 

 

Message 52 of 55
tiberian1138
in reply to: Kent1Cooper

Kent - you are a freaking life and mind saver! Thank you so much. Works absolutely perfectly! With that little lesson I think there are other routines I can work through that would help me a lot. Thank you again! ! ! 

Message 53 of 55
tiberian1138
in reply to: Sea-Haven

Thank you for the suggestion but that goes far above and beyond what I need. We do not get anywhere near that specific in our drawings. All we do is identify what rooms have tile and carpet. And quite often some rooms are switched from carpet to tile. 

Message 54 of 55
arionXMSF7
in reply to: JustoAg

Thank you, your code was really helpful! I added to it so that the script finds ALL instances of a string in side each TEXT/MTEXT object. The code you provided found only the first instance of a string per TEXT/MTEXT object but the others remain as-is. I've added some extraneous failsafe code to prevent infinite loops (which can probably be deleted) as well as some princ output so the user can also read what index of what element was found if they wish to investigate the object. Hope this is useful to you and others!

 

(defun c:rplt ()
	(setq OldTxt (getstring T "\nEnter the old text: ")
		  NewTxt (getstring T "\nEnter the new text: "))
	
	(setq ss (ssget "x" '((0 . "TEXT,MTEXT"))))
		
	(setq i (sslength ss))
	
	(while (not (minusp (setq i (1- i))))
		(setq
			oText (vlax-ename->vla-object (ssname ss i))
			Txt (vlax-get-property oText 'TextString)
			idx -1
			count 0
			maxidx 100 ; just to prevent an infinite loop. references to maxidx can be deleted. 
		)
		
		(while (and (setq idx (vl-string-search OldTxt txt (1+ idx))) (<= (setq count (1+ count)) maxidx)) ; can remove "and" as well as the second clause inside the "and" to remove infinite loop failsafe code
			(progn
				(princ (strcat "String \"" OldTxt "\" found at index " (itoa idx) " in element [" (itoa i) "]."))
				(setq newChg (vl-string-subst NewTxt OldTxt txt))
				(vlax-put-property oText 'TextString newchg)
				(vlax-invoke-method oText 'Update)
			)
		)
		; for testing, can be deleted to remove output related to infinite loop failsafe code. 
		(if (> count maxidx)
			(princ (strcat "Maximum " (itoa maxidx) " iterations exceeded at element [" (itoa i) "]."))
		)
	)
	(princ)
)

 

Message 55 of 55
Sea-Haven
in reply to: cadee

To stop looping rearrange these couple of lines

 

(setq i (sslength ss))
	
	(while (not (minusp (setq i (1- i))))
		(setq
			oText (vlax-ename->vla-object (ssname ss i))

	
	(repeat (setq i (sslength ss))
		(setq
		oText (vlax-ename->vla-object (ssname ss (setq i (1- i))))

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

Post to forums  

Autodesk Design & Make Report

”Boost