Find and replace text

Find and replace text

zasanil
Advocate Advocate
11,769 Views
22 Replies
Message 1 of 23

Find and replace text

zasanil
Advocate
Advocate

I've searched around and found a couple lisps that would find text and replace them (without a dialog). One exmple is from this forum post here: http://forums.augi.com/showthread.php?43053-Take-existing-drawings-and-find-all-quot-quot-and-replac... I have copied the code it below. I wanted to see if anyone could modify it to also find text inside of tables also which it doesn't seem to do.

Thanks!

 

;-------------------------------------------------------------------------------
; FindReplaceAll - Changes Text, Mtext, Dimensions and Attribute Block entities
; that have a Find$ string with a Replace$ string.
; Arguments: 2
;   Find$ = Phrase string to find
;   Replace$ = Phrase to replace it with
; Syntax: (FindReplaceAll "old string" "new string")
; Returns: Updates Text, Mtext, Dimension and Attribute Block entities
;-------------------------------------------------------------------------------
(defun c:FindReplaceAll (Find$ Replace$ / BlkEntList@ BlkEntName^ BlkEntType$ Cnt#
  DimEntList@ DimEntName^ DimEntType$ EntList@ EntName^ EntType$ FindReplace:
  Mid$ Mid2$ NewText$ Num# Replace$ SS& Text$)
  ;-----------------------------------------------------------------------------
  ; FindReplace: - Returns Str$ with Find$ changed to Replace$
  ; Arguments: 3
  ;   Str$ = Text string
  ;   Find$ = Phrase string to find
  ;   Replace$ = Phrase to replace Find$ with
  ; Returns: Returns Str$ with Find$ changed to Replace$
  ;-----------------------------------------------------------------------------
  (defun FindReplace: (Str$ Find$ Replace$ / Cnt# FindLen# Loop Mid$ NewStr$ ReplaceLen#)
    (setq Loop t Cnt# 1 NewStr$ Str$ FindLen# (strlen Find$) ReplaceLen# (strlen Replace$))
    (while Loop
      (setq Mid$ (substr NewStr$ Cnt# FindLen#))
      (if (= Mid$ Find$)
        (setq NewStr$ (strcat (substr NewStr$ 1 (1- Cnt#)) Replace$ (substr NewStr$ (+ Cnt# FindLen#)))
              Cnt# (+ Cnt# ReplaceLen#)
        );setq
        (setq Cnt# (1+ Cnt#))
      );if
      (if (= Mid$ "") (setq Loop nil))
    );while
    NewStr$
  );defun FindReplace:
  ;-----------------------------------------------------------------------------
  ; Start of Main function
  ;-----------------------------------------------------------------------------
  (if (and (= (type Find$) 'STR)(= (type Replace$) 'STR)(/= Find$ ""))
    (progn
      (if (setq SS& (ssget "x" (list '(-4 . "<AND")'(-4 . "<OR")'(0 . "TEXT")'(0 . "MTEXT")'(0 . "DIMENSION")'(0 . "INSERT")'(-4 . "OR>")(cons 410 (getvar "CTAB"))'(-4 . "AND>"))))
        (progn
          (command "UNDO" "BEGIN")
          (setq Cnt# 0)
          (repeat (sslength SS&)
            (setq EntName^ (ssname SS& Cnt#)
                  EntList@ (entget EntName^)
                  EntType$ (cdr (assoc 0 EntList@))
                  Text$ (cdr (assoc 1 EntList@))
            );setq
            (if (= EntType$ "INSERT")
              (if (assoc 66 EntList@)
                (progn
                  (while (/= (cdr (assoc 0 EntList@)) "SEQEND")
                    (setq EntList@ (entget EntName^))
                    (if (= (cdr (assoc 0 EntList@)) "ATTRIB")
                      (progn
                        (setq Text$ (cdr (assoc 1 EntList@)))
                        (if (wcmatch Text$ (strcat "*" Find$ "*"))
                          (progn
                            (setq ReplaceWith$ (FindReplace: Text$ Find$ Replace$))
                            (entmod (subst (cons 1 ReplaceWith$) (assoc 1 EntList@) EntList@))
                            (entupd EntName^)
                          );progn
                        );if
                      );progn
                    );if
                    (setq EntName^ (entnext EntName^))
                  );while
                );progn
              );if
              (if (wcmatch Text$ (strcat "*" Find$ "*"))
                (progn
                  (setq ReplaceWith$ (FindReplace: Text$ Find$ Replace$))
                  (entmod (subst (cons 1 ReplaceWith$) (assoc 1 EntList@) EntList@))
                  (entupd EntName^)
                );progn
              );if
            );if
            (setq Cnt# (1+ Cnt#))
          );repeat
          (command "UNDO" "END")
        );progn
      );if
    );progn
  );if
  (princ)
);defun FindReplaceAll
Dan Nicholson C.I.D.
PCB Design Engineer
0 Likes
Accepted solutions (2)
11,770 Views
22 Replies
Replies (22)
Message 21 of 23

Designer-Drafter_Joe
Participant
Participant
Yes, that's right! Thank you for that reminder. [?]
0 Likes
Message 22 of 23

Designer-Drafter_Joe
Participant
Participant

Hello again, ec-cad.  I've tried to use the "test.lsp" that you attached previously, but I get a very long error message when trying to just load it in "VLIDE" in AutoCAD. Here is what it says: 

(LAMBDA (MSG /) "acet-error" (SETQ ACET:ERROR-ALIVE -1) (IF (AND (/= MSG "quit / exit abort") (/= MSG "Function cancelled") (/= MSG "console break") (= (QUOTE STR) (TYPE MSG))) (PRINC (STRCAT "\n" MSG)) (PROGN (IF (AND (/= (QUOTE STR) (TYPE MSG)) (/= (QUOTE SYM) (TYPE MSG))) (PRINC "\nError function called improperly!")))) (ACET-CMD-EXIT) (IF ACET:UNDO-CTL (PROGN (SETVAR "CMDECHO" 0) (ACET-UNDO-END) (IF (= #BNS_ERROR_UNDO_FLAG 1) (COMMAND "_.UNDO" "1")) (ACET-CMD-EXIT))) (ACET-SYSVAR-RESTORE) (IF (AND acet-ui-progress (acet-ui-progress)) (acet-ui-progress)) (ACET-UNDO-RESTORE ACET:UNDO-CTL) (SETQ ACET:UNDO-CTL nil #BNS_ERROR_UNDO_FLAG nil) (SETQ *ERROR* ACET:OLD-ERROR) (IF ACET-UI-PROGRESS-DONE (ACET-UI-PROGRESS-DONE)) (SETQ ACET:ERROR-ALIVE 0) (PRINC))

Any ideas on this?

Thank you!

Drafter-Joe

0 Likes
Message 23 of 23

ec-cad
Collaborator
Collaborator

The code 'test.lsp' was a copy of the MSG 6 by CADaSchtroumpf to zasanil.

The only change I put in there were a few (setq FLAG x) lines, to find where it stops.

There is no Error trapping routine. And notinig named "ACET'  in the test.lsp I posted.

You must have loaded some other acet-xxx type lisp or arx code.

No clues here.

🙂

ECCAD

0 Likes