Find and Replace Specific Text

Find and Replace Specific Text

Migumby
Advocate Advocate
1,201 Views
4 Replies
Message 1 of 5

Find and Replace Specific Text

Migumby
Advocate
Advocate

Hello,

This is a lsp that seams a little faster to me than the find command.

It selects windowed text strings.

Does anyone know how to change it to select only “__” with-in the text string.

I use the double underscore in my titles for grouping that need to be renamed.

Thanks

 

 

; CHGTEXT.LSP
; CHANGES TEXT GLOBALLY
; This program will replace every occurrence of an "old string" with a
; "new string".
(defun chgterr (s)
(if (/= s "Function cancelled")
(princ (strcat "\nError: " s))
)
(setq p nil)
(setq *error* olderr)
(princ)
)

(defun C:CHGTEXT (/ p l n e os as ns st s nsl osl sl si chf chm olderr)
(setq olderr *error*
*error* chgterr
chm 0)
(setq p (ssget))
(if p (progn
(while (= 0 (setq osl (strlen (setq os (getstring t "\nOld string: ")))))
(princ "Null input invalid")
)
(setq nsl (strlen (setq ns (getstring t "\nNew string: "))))
(setq l 0 n (sslength p))
(while (< l n)
(if (= "TEXT"
(cdr (assoc 0 (setq e (entget (ssname p l))))))
(progn
(setq chf nil si 1)
(setq s (cdr (setq as (assoc 1 e))))
(while (= osl (setq sl (strlen
(setq st (substr s si osl)))))
(if (= st os)
(progn
(setq s (strcat (substr s 1 (1- si)) ns
(substr s (+ si osl))))
(setq chf t)
(setq si (+ si nsl))
)
(setq si (1+ si))
)
)
(if chf (progn
(setq e (subst (cons 1 s) as e))
(entmod e)
(setq chm (1+ chm))
))
)
)
(setq l (1+ l))
)
))
(princ "Changed ")
(princ chm)
(princ " text lines.")
(terpri)
(setq *error* olderr)
(princ)
)

Accepted solutions (1)
1,202 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant

Find and replace (ssget) function with... for

 

... user filtred selection

(ssget  '((0 . "TEXT") (1 . "*__*")))

... select all TEXTs contains "*__*"

(ssget "_X" '((0 . "TEXT") (1 . "*__*")))

 

 

EDIT:

I probably misunderstood. But you can do suggested above anyway.

 

If you want to replace just "__" with some new text, then

 

replace this line

(while (= 0 (setq osl (strlen (setq os (getstring t "\nOld string: ")))))

 

with this line

(while (= 0 (setq osl (strlen (setq os "__"))))

 

0 Likes
Message 3 of 5

Migumby
Advocate
Advocate

I'm sorry,

I still don't understand lsp enough to know where exactly to place this code.

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

@Migumby wrote:

I'm sorry,

I still don't understand lsp enough to know where exactly to place this code.


No worries, the lines a was talked above are red.

 

Spoiler
(defun chgterr (s)
  (if (/= s "Function cancelled")
    (princ (strcat "\nError: " s))
    )
  (setq p nil)
  (setq *error* olderr)
  (princ)
  )
(defun C:Replace__ (/ p l n e os as ns st s nsl osl sl si chf chm olderr)
  (setq olderr *error*
	*error* chgterr
	chm 0)
  (setq p (ssget "_X" '((0 . "TEXT") (1 . "*__*"))))
  (if p (progn
	  (while (= 0 (setq osl (strlen (setq os "__"))))
	    (princ "Null input invalid")
	    )
	  (setq nsl (strlen (setq ns (getstring t "\nNew string: "))))
	  (setq l 0 n (sslength p))
	  (while (< l n)
	    (if (= "TEXT"
		   (cdr (assoc 0 (setq e (entget (ssname p l))))))
	      (progn
		(setq chf nil si 1)
		(setq s (cdr (setq as (assoc 1 e))))
		(while (= osl (setq sl (strlen
					 (setq st (substr s si osl)))))
		  (if (= st os)
		    (progn
		      (setq s (strcat (substr s 1 (1- si)) ns
				      (substr s (+ si osl))))
		      (setq chf t)
		      (setq si (+ si nsl))
		      )
		    (setq si (1+ si))
		    )
		  )
		(if chf (progn
			  (setq e (subst (cons 1 s) as e))
			  (entmod e)
			  (setq chm (1+ chm))
			  ))
		)
	      )
	    (setq l (1+ l))
	    )
	  ))
  (princ "Changed ")
  (princ chm)
  (princ " text lines.")
  (terpri)
  (setq *error* olderr)
  (princ)
  )

Right now it selects all text contains *__* and "__" would replaced with new string entered by user.

If you still want select just some of texts, then erase blue "_X" from the code.

Better now?

0 Likes
Message 5 of 5

Migumby
Advocate
Advocate

thank you greatly.

0 Likes