Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find and replace text strings with no dialogs?

Status
Not open for further replies.

CadDraftee

Mechanical
Sep 16, 2003
48
How to find and replace text strings with no dialogs? I wish to run a scipt if possible? Or does anyone know of a lsp that I can add my own find and replace values to?
 
Replies continue below

Recommended for you

I forgot to mention that I need it to cover attributes.
 
I did not write this and claim no stars for it's posting here, but it works!

;
"CT" Change text. Correct spelling errors
;
(DEFUN chgterr (s)
(if (/= s "Function cancelled") ; If an error (such as CTRL-C) occurs
(princ (strcat "\nError: " s)) ; while this command is active...
)
(setq p nil) ; Free selection set
(setq *error* olderr) ; Restore old *error* handler
(princ)
)

;
(DEFUN C:CT (/ p l n e os as ns st s nsl osl sl si chf chm olderr) ; Correct spelling errors
(setq olderr *error* ; Initialize variables
*error* chgterr
chm 0)
(setq p (ssget)) ; Select objects
(if p (progn ; If any objects selected
(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) ; For each selected object...
(if (= &quot;TEXT&quot; ; Look for TEXT entity type (group 0)
(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) ; Found old string
(setq si (+ si nsl))
)
(setq si (1+ si))
)
)
(if chf (progn ; Substitute new string for old
(setq e (subst (cons 1 s) as e))
(entmod e) ; Modify the TEXT entity
(setq chm (1+ chm))
))
)
)
(setq l (1+ l))
)
))
(princ &quot;Changed &quot;) ; Print total lines changed
(princ chm)
(princ &quot; text lines.&quot;)
(terpri)
(setq *error* olderr) ; Restore old *error* handler
(princ)
)
 
Sorry about that.
&quot;CT&quot; runs it.
Of course, load it first !!!
In the lisp, look for the DEFUN C: and what comes after is the command that AutoCAD would recognize.
 
CadDraftee, you have that replace attributes or only text?.
 
I need to do both text and or attribute. I know how to do it in one drawing using the &quot;Find and Replace&quot; command which is fantastic - but i want to write it into a script cause I dont want to have to do 50+ drawings individully and I have about 30 items to change in each drawing. Which makes a bit of typing. ie every &quot;a&quot; is to now be &quot;1&quot; and every &quot;b&quot; is to now be &quot;2&quot; etc etc.

Does help anyone with my issue - just seems I can't explanin myself for what i really want.
 

CadDraftee,

What are your criteria for changing the
text/attributes. Are the changes the same
for a batch of drawings. Do you have a sample
of the drawing showing the old/new text and/or
attributes. You don't want dialogs, so how do
you intend to make the changes known to the
routine...command line inputs?? It seems that
the info you have provided are inadequate to
get a decent reply from this forum.
 
Ok - in my drawing I have continuation arrows with reference to the continuaing drawing. All of these are now changing. So I know that every occurance of F-123 is now going to be F0123 and all occuances of F-506 will be F0106. I can do this using search and replace in to pull down menus but I cant use it in a scipt cause you can't override the cmddia command for this particular command. So I was hoping that someone knows of a way to either override the dialog in the script routine or that someone smart enough has been able to write a program of the similar that I can use in a scipt.
 
Caddraftee,

&quot;So I know that every occurance of F-123 is now going to
F0123 and all occuances of F-506 will be F0106.&quot;

Here's your routine:

;;;No error checking
;;;11-17-2003 CopyRight (C) 2005 L Estember
;;;Not for Resale or Mass Distribution,
;;;CopyRight (C) Material

(defun C:CHTXT ()

(setq ss (ssget &quot;X&quot;
(list '(0 . &quot;TEXT,MTEXT&quot;)
'(-4 . &quot;<AND&quot;)
'(-4 . &quot;<OR&quot;)
(cons 1 &quot;F-123&quot;)
(cons 1 &quot;F-506&quot;)
'(-4 . &quot;OR>&quot;)
'(-4 . &quot;AND>&quot;)
)
)
)

(if (> (sslength ss) 1)
(progn
(setq i -1)
(while (and (setq e (ssname ss (setq i (1+ i))))
(setq ed (entget e))
(setq str (cdr (assoc 1 ed)))
(setq newstr (cond ((= str &quot;F-123&quot;) &quot;F0123&quot;)
((= str &quot;F-506&quot;) &quot;F0106&quot;)
)
)
)
(entmod (setq ed (subst (cons 1 newstr) (assoc 1 ed) ed)))
(entupd e)
);while
);progn
);if

(princ)
);defun


:::Happy Mtexting or Texting....
 

The routine I posted is good for 2 or more occurrences.
If there's only 1, it won't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor