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
22553 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 2 of 55
Kent1Cooper
in reply to: cadee


@Anonymous wrote:

....

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.

....


FIND has a "Replace with" option, and a "Replace All" option if you know you want to do that [even if there's only one], so you don't need to pick anything.

 

If you're thinking of a generic replacement routine, in which the User would invoke it and provide the content to be replaced and what to replace it with, I don't see any advantage over FIND, since you might just as well type it into the FIND dialog box as in answer to a routine's prompts, or as arguments to a function, or something.

 

But if you want specific content built directly into a single-purpose routine, questions arise.

 

Would those text strings be the entire content of the entities?  [And would they always have those spaces at the ends?]  FIND can replace even partial content, but if you need that in a routine, while it could be done, it would be trickier than if you're replacing content in its entirety.

 

Is the upper- vs. lower-case condition always the same?  FIND has a Match Case option, which you can turn off if you want to replace any case configuration of the same letters.  But again, while a routine could be made to do that, it would be trickier than if you always have the same case situation.

Kent Cooper, AIA
Message 3 of 55
cadee
in reply to: cadee

Intially what happened was that the a number of drawings were intended to be tender issue, so the revsion note was " issued for tender " then an engineer decided they should have been " issued for construction ". What I wanted to do, was to create a menu button so all I had to do was pick it and the program would do the find and replace. I wanted to be able to modify the lisp / script so that if the exact wording and case weren't exact in the program that I could edit it to suit the wording/case I wanted.

 

I am using Autocad 2010 but my colleague uses Autocad Lt 2010.

 

I know about Autocad's Find & Replace command but sometimes when time is of the essence, you want something quicker.

 

Thanks

Message 4 of 55
JustoAg
in reply to: cadee

See this as a starting point:

(defun f2 ()
	(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)))
		(setq Txt (vlax-get-property oText 'TextString))
		
		(if (vl-string-search OldTxt txt)
			(progn
				(setq newChg (vl-string-subst NewTxt OldTxt txt))
				(vlax-put-property oText 'TextString newchg)
				(vlax-invoke-method oText 'Update)
			)
		)
	)
	(princ)
)

Maybe you can improve it.

 

Hope this helps.

 

---

Justo Aguiar

Message 5 of 55
Kent1Cooper
in reply to: cadee


@Anonymous wrote:

...a number of drawings were intended to be tender issue, so the revsion note was " issued for tender " then an engineer decided they should have been " issued for construction ". What I wanted to do, was to create a menu button so all I had to do was pick it and the program would do the find and replace. I wanted to be able to modify the lisp / script so that if the exact wording and case weren't exact in the program that I could edit it to suit the wording/case I wanted.

 

I am using Autocad 2010 but my colleague uses Autocad Lt 2010.

....


If that revision note is [as I asked before] a Text/Mtext entity in which "issued for tender" [and I'm going to assume that it doesn't really have spaces at the ends] is the complete content, it's pretty easy.  You don't even need to look for Text/Mtext entities by entity types, but can look for that content alone, assuming [as seems pretty likely] that nothing else is going to have a 1-code entry with the same content.  And then you don't have to check anything about the content of each entity before replacing it, because you know its content is what you want to replace.

 

(defun C:TRC ; = Text Replace for Complete text/mtext strings
  (/ told tnew tss tdata)
  (setq
    told (getstring "\Old text content to be replaced: ")
    tnew (getstring T "\nNew text content to replace it with: ")
    tss (ssget "X" (list (cons 1 told)))
  ); end setq
  (repeat (sslength tss)
    (setq
      tdata (entget (ssname tss 0))
      tdata (subst (cons 1 tnew) (assoc 1 tdata) tdata)
    ); end setq
    (entmod tdata)
    (ssdel (ssname tss 0) tss)
  ); end repeat
); end defun

Or, if you want the content preset so the User doesn't have to answer prompts, and you'll edit it for each instance:

 

(defun C:TRC ; = Text Replace for Complete text/mtext strings, pre-defined content
  (/ tss tdata)
  (setq tss (ssget "X" (list (cons 1 "issued for tender"))))
  (repeat (sslength tss)
    (setq
      tdata (entget (ssname tss 0))
      tdata (subst (cons 1 "issued for construction") (assoc 1 tdata) tdata)
    ); end setq
    (entmod tdata)
    (ssdel (ssname tss 0) tss)
  ); end repeat
); end defun

 But AutoCAD LT can't use Lisp, as I understand.  I'm not sure how you'd do it there, if it can be done at all.

 

 

Kent Cooper, AIA
Message 6 of 55
cadee
in reply to: cadee

The TRC lisp program that didn't wait for prompts worked a treat.

 

Thank You

Message 7 of 55
pbejse
in reply to: cadee

Just in case you ran into Annotative MText  use this

 

;;; 		Modifed	code of Kent Cooper		;;;
;;;							;;;
                  
(defun c:TRC ; = Text Replace for Complete text/mtext strings, pre-defined content
  (/ tss)(vl-load-com)
  (if (setq tss (ssget "X" (list (cons 1 "issued for tender"))))
		(mapcar	'(lambda (j)
                  	(vla-put-textstring (vlax-ename->vla-object j) "issued for construction" ))
                  	(mapcar 'cadr (ssnamex tss)))
    )
	(princ)
  )   

 

 

You'll know it when you see it  Smiley Happy

Message 8 of 55
kameron1967
in reply to: pbejse

Pbejse - just curious - would this work with attribute values also?

Message 9 of 55
greg_battin
in reply to: cadee

Here's one from senor Lee-Mac that works for the current drawing or it can do "batches" of other drawings.

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

~Greg

Message 10 of 55
kameron1967
in reply to: greg_battin

Thanks, Greg.  Yes, that routine that LeeMac created was very helpful.  This one that I want to do is actually not as sophisticated.  It does not require a user intervention.  I just want to run it in a script fashion - that is, load it and run it using scripro.  I did, however, found something that would work for me.

 

Thank you.

Message 11 of 55
pbejse
in reply to: kameron1967

why of course it can, you need to provide us the block name if you want this to run without promptng for selection

 

 

 

Message 12 of 55
kameron1967
in reply to: pbejse

The block name is BLOCKA and Tag name is DESCRIPTION.  But the change I want to update is what's written on the description.  So if it says "ISSUED FOR ABCDE", I'd like it to say "ISSUED FOR 12345".  Let me know if you need more information.

 

Thanks, pbejse.

Message 13 of 55
blackseabrew
in reply to: Kent1Cooper

Have you ever used the AEFINDTERMTEXT command in ACAD Electrical?  I've tried but it's not working.

Message 14 of 55
greg_battin
in reply to: blackseabrew

If the command isn't working because it says that the command is "Unknown" you may be able to use the command REDEFINE and bring the command "back to life"

Message 15 of 55
kameron1967
in reply to: pbejse

Pbejse - I was wondering if you have a modified version that includes block name (BlockA) and description attribute (DESC) so that if Block A exists, we then check if DESC has "Issued for ABC".  If it does, we then update it to say "Issued for XYZ".  Thanks in advance.

Message 16 of 55
pbejse
in reply to: kameron1967


@kameron1967 wrote:

Pbejse - I was wondering if you have a modified version that includes block name (BlockA) and description attribute (DESC) so that if Block A exists, we then check if DESC has "Issued for ABC".  If it does, we then update it to say "Issued for XYZ".  Thanks in advance.


 

  Start with this <----

 

Holler if you hit a snag.

 

 

 

 

Message 17 of 55
kameron1967
in reply to: pbejse

Thanks, Pbejse!  I think I have what I need.  But will let you know if I do hit a snag! 🙂

Message 18 of 55
rwydesk500
in reply to: Kent1Cooper

How would the second instance of code be modified to account for spaces at the beginning and end of a text string? Thank you!

Regards,

Rick Yoerger

AutoCAD Electrical 2016
Message 19 of 55
Kent1Cooper
in reply to: rwydesk500


@rwydesk500 wrote:

How would the second instance of code be modified to account for spaces at the beginning and end of a text string? Thank you!


If I understand correctly, just include them inside the double-quotes wrapping the string, e.g. " issued for construction ".  If you might not always have the same number of spaces at the beginning or end of existing strings, you would probably need to use something like this:

 

(setq tss (ssget "X" '((1 . "*issued for tender*"))))

 

to find them [though that would also find strings with the same content even if what's outside it is not just spaces], and apply (vl-string-trim) if you want them removed.

Kent Cooper, AIA
Message 20 of 55
rwydesk500
in reply to: Kent1Cooper

I thought the spaces at either end of the word I am replacing was what was preventing it from working. However, it is not. After I load TRC.lsp and type the command, it returns the following error...

 

"Command: TRC ; error: bad argument type: lselsetp nil"

 

...not sure why. I cut and pasted the code directly from your post. Thanks

Regards,

Rick Yoerger

AutoCAD Electrical 2016

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

Post to forums  

Autodesk Design & Make Report

”Boost