Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find/Repalce All MTEXT with LISP

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
Anonymous
1393 Views, 4 Replies

Find/Repalce All MTEXT with LISP

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

 

4 REPLIES 4
Message 2 of 5
devitg
in reply to: Anonymous

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

Message 3 of 5
Anonymous
in reply to: devitg

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. 

Message 4 of 5
ВeekeeCZ
in reply to: Anonymous

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

 

Message 5 of 5
Anonymous
in reply to: ВeekeeCZ

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

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report