Jump to content

The Best Text Find And Replace LISP Ever...


Freerefill

Recommended Posts

  • 2 weeks later...
  • Replies 46
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    17

  • The Buzzard

    6

  • Freerefill

    6

  • Voaraghamanthar

    6

Top Posters In This Topic

Posted Images

Hey guys I'm new to this site. I've got a question related to the above find and replace lisp routine. I'm trying to find a specific peace of text as I cycle through my tabs in a drawing yet every time I get to the selection part it prompts me to select the peace of text manually. Is there a way to get the lisp to select text without me clicking on it? I have 115 tabs in a drawing and I am using a routine to go to each tab enter model space through the viewport and set the ucs to the rotation of the text then the viewport to that same ucs, then return to paper space and go to the next tab. (setq ss(ssget)) this sequence requires me to manually select

this one below will select automatically but wants me to type my selection... any suggestions to make this automatic???

(initget 1 "1")

(setq str (getkword "\nSelect String for filter [1]: "))

(setq ss (ssget "_X"

(list (cons 1

(if (eq str "ALL")

"*1*"

(strcat "*" str "*")

)

)

)

)

)

(sssetfirst nil ss)

)

Link to comment
Share on other sites

OK, so I've figured out my find problem with the lisp. I can get it to go through all 115 tabs and rotate to a specific text in each viewport. Now for some reason I'm loosing 1.11 degrees in my rotation after the first tab witch comes in perfect. Any thoughts as to why?

Link to comment
Share on other sites

  • 1 year later...

Great lisp, especially because it finds values that come from fields, is there a way to add a Select feature to the options to select objects with the text string?

 

i.e. [select/Find/Replace/Case/Quit]

Link to comment
Share on other sites

  • 2 years later...
On 3/17/2017 at 3:52 PM, jstroebel said:

Great lisp, especially because it finds values that come from fields, is there a way to add a Select feature to the options to select objects with the text string?

 

i.e. [select/Find/Replace/Case/Quit]

That's something I've been working on for a bit now. I have a string that occurs multiple times per DWG but I just need to replace a few instances of it where it appears with another string. I'm trying to set it up so that I can run it within a batch scripting routine across multiple DWGs. I've hunted for solutions and haven't found anything online. Here is the latest version I've tried, but it still replaces all instances and not the selected instances.

 

My current best guess is that I can't use vl-string-subst to in combination with a selection set the way I've been trying. 

(defun c:test (/ OldText NewText SS Count EntList EntText)
  (if (and (setq OldText (getstring "\nEnter text to replace: "))
	   (/= OldText "")
      )
    (if	(and (setq NewText (getstring "\Enter new text: "))
	     (/= NewText "")
	)
      (if (setq	SS
		 (ssget "X" (list (cons 0 "*TEXT") (cons 1 "BOLTS,*CL*, ")))
	  )
	(progn
	  (setq Count 0)
	  (while (< Count (sslength SS))
	    (setq EntList (entget (ssname SS Count)))
	    (setq EntText (cdr (assoc 1 EntList)))
	    (while
	      (vl-string-search (strcase OldText) (strcase EntText))
	       (setq EntText (vl-string-subst
			       NewText
			       (strcase OldText)
			       (strcase EntText)
			     )
	       )
	    )
	    (setq EntList
		   (subst (cons 1 EntText) (assoc 1 EntList) EntList)
	    )
	    (setq EntList (entmod EntList))
	    (entupd (cdr (assoc -1 EntList)))
	    (setq Count (1+ Count))
	  )
	)
      )
    )
  )
  (princ)
)

 

Link to comment
Share on other sites

After working on this off and on for a few months I've got it doing what I'm needing. The Resulting code currently looks like this:

(defun c:test (/ OldText NewText SS Count EntList EntText)
  (if (and (setq OldText (getstring "\nEnter text to replace: "))
	   (/= OldText ""))
    (if	(and (setq NewText (getstring "\Enter new text: "))
	     (/= NewText ""))
      (if (setq SS (ssget "X" (list (cons 0 "*TEXT") (cons 1 "BOLTS`,*CL*`, *"))))  ;The wildcard string in the cons 1 can be changed as needed
	(progn
	  (setq Count 0)
	  (while (< Count (sslength SS))
	    (setq EntList (entget (ssname SS Count)))
	    (setq EntText (cdr (assoc 1 EntList)))
	    (while
	      (vl-string-search (strcase OldText) (strcase EntText))
	       (setq EntText (vl-string-subst
			       NewText
			       (strcase OldText)
			       (strcase EntText))))
	    (setq EntList
		   (subst (cons 1 EntText) (assoc 1 EntList) EntList))
	    (setq EntList (entmod EntList))
	    (entupd (cdr (assoc -1 EntList)))
	    (setq Count (1+ Count)))))))
  (princ))

I'm going to continue to modify this so that the cons 1 search string is requested from the command line and not hard-coded in.

In 4 years of looking I haven't found a routine that does a select-find-replace online.

Link to comment
Share on other sites

'The Best Text Find And Replace LISP Ever...' , wow , if posted by Master Lee I might have believed it but I think even he would never make such a statement. More something like , this is my best version , so far 😁

 

4 hours ago, crazitalk said:

That's something I've been working on for a bit now. I have a string that occurs multiple times per DWG but I just need to replace a few instances of it where it appears with another string. I'm trying to set it up so that I can run it within a batch scripting routine across multiple DWGs. I've hunted for solutions and haven't found anything online. Here is the latest version I've tried, but it still replaces all instances and not the selected instances.

 

My current best guess is that I can't use vl-string-subst to in combination with a selection set the way I've been trying. 

 

 

How do you imagine letting a script do the selecting for you???

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