Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
cadee
Posts: 180
Registered: ‎08-10-2004
Message 1 of 34 (4,994 Views)

Pre-defined find & replace lisp

4994 Views, 33 Replies
02-10-2011 06:06 AM

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

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 2 of 34 (4,980 Views)

Re: Pre-defined find & replace lisp

02-10-2011 07:21 AM in reply to: cadee

cadee 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
Distinguished Contributor
cadee
Posts: 180
Registered: ‎08-10-2004
Message 3 of 34 (4,972 Views)

Re: Pre-defined find & replace lisp

02-10-2011 07:52 AM 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

Valued Contributor
JustoAg
Posts: 80
Registered: ‎09-23-2008
Message 4 of 34 (4,967 Views)

Re: Pre-defined find & replace lisp

02-10-2011 09:31 AM 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

*Expert Elite*
Kent1Cooper
Posts: 5,242
Registered: ‎09-13-2004
Message 5 of 34 (4,958 Views)

Re: Pre-defined find & replace lisp

02-10-2011 12:54 PM in reply to: cadee

cadee 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
Distinguished Contributor
cadee
Posts: 180
Registered: ‎08-10-2004
Message 6 of 34 (4,933 Views)

Re: Pre-defined find & replace lisp

02-11-2011 12:32 AM in reply to: cadee

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

 

Thank You

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 7 of 34 (4,924 Views)

Re: Pre-defined find & replace lisp

02-11-2011 02:53 AM 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  :smileyhappy:

Distinguished Contributor
kameron1967
Posts: 156
Registered: ‎09-05-2008
Message 8 of 34 (4,647 Views)

Re: Pre-defined find & replace lisp

06-02-2011 10:48 AM in reply to: pbejse

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

Distinguished Contributor
greg.battin
Posts: 147
Registered: ‎12-10-2010
Message 9 of 34 (4,648 Views)

Re: Pre-defined find & replace lisp

06-02-2011 10:55 AM 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

Distinguished Contributor
kameron1967
Posts: 156
Registered: ‎09-05-2008
Message 10 of 34 (4,635 Views)

Re: Pre-defined find & replace lisp

06-02-2011 05:37 PM 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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community