Find/Repalce All MTEXT with LISP

Find/Repalce All MTEXT with LISP

Anonymous
Not applicable
1,629 Views
4 Replies
Message 1 of 5

Find/Repalce All MTEXT with LISP

Anonymous
Not applicable

I am trying to find/replace all step numbers in a drawing file, based on an excel sheet with new/old step numbers. My automation starts with excel vba to get two arrays of Old Step and New Step numbers to submit into the find replace. The excel vba then sends commands to autocad to run a lisp I wrote to cycle through and find replace each of the items in the array. For some reason, it will change the first instance (encircled) but not the second instance (part of a description). Is there something in my LISP that is keeping it from replacing all instances? 

mdaiber_0-1606139735237.png

mdaiber_1-1606139779581.png

; 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 "x" '((0 . "MTEXT"))))
(ssget "x" '((0 . "MTEXT")))
(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 (= "MTEXT"
(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)
)

 

0 Likes
Accepted solutions (1)
1,630 Views
4 Replies
Replies (4)
Message 2 of 5

devitg
Advisor
Advisor

Hi @Anonymous  For better understanding, and maybe get further help, please upload such sample.dwg

0 Likes
Message 3 of 5

Anonymous
Not applicable

See attached sample. When running the CHGTEXT, both *63s are able to change, but only the encircled *64 is able to be changed. Is there a character limiter within the code? or something keeping it from being able to process longer mtext objects? Again, I am very unfamiliar with LISP; I have just noticed that when decreasing the number of characters in the mtext object, it eventually works. 

0 Likes
Message 4 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

It's because your MTEXT is too long - the contents split between more pairs. Your code search just 1, not 3.

 

((-1 . ) (0 . "MTEXT") (330 . ) (5 . "1675") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbMText") (10 67.0296 148.345 0.0) (40 . 0.0924496) (41 . 5.08234) (46 . 0.0) (71 . 1) (72 . 1) (3 . "{\\Fengineering|c0;\\W0.84506;\\P*45) NONOUTAGE: LINE-XXXX (01/01/0001 - 01/01/0001);\\P\t TEST OUTAGE DESCRIPTION LINE ONE; SW#### to STRUCTURE ABC. \\P\t TEST OUTAGE DESCRIPTION LINE TWO:\\P\t FROM NEW STRUCTURE X TO NEW STRUCTURE Y FOR CIRCUIT 123 \\P\t FROM") (1 . " NEW TOWER A TO NEW STRUCTURE C FOR BOTH CIRCUITS. }") (7 . "None") (210 0.0 0.0 1.0) (11 1.0 2.44547e-13 0.0) (42 . 4.92673) (43 . 0.790005) (50 . 2.44547e-13) (73 . 2) (44 . 0.86543))

 

 

 

 

Here's the fixed code.

 

; 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 "x" '((0 . "MTEXT"))))
  (ssget "x" '((0 . "MTEXT")))
  (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 (= "MTEXT"
		   (cdr (assoc 0 (setq e (entget (setq d (ssname p l)))))))
	      (progn
		(setq chf nil si 1)
		(setq s (setq as (getpropertyvalue d "Contents")))
		(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))
			  (setpropertyvalue d "Contents" s)
			  (setq chm (1+ chm))
			  ))
		)
	      )
	    (setq l (1+ l))
	    )
	  ))
  (princ "Changed ")
  (princ chm)
  (princ " text lines.")
  (terpri)
  (setq *error* olderr)
  (princ)
  )

 

0 Likes
Message 5 of 5

Anonymous
Not applicable

@ВeekeeCZ That works! Thank you so much for your help. It is much much appreciated.

0 Likes