Тема: LISP. Поиск и замена текста

Может кому пригодится:

    ;-============-;
    ;- Text  Find -;
    ;-    *~*     -;
    ;  Written by -;
    ; Mark Mercier ;
    ;   05-06-09   ;
    ;-============-;

(defun c:tfind2 ()
  (tfind2fun nil nil 0)
) ;_ end of defun

(defun tfind2fun (inputF inputR caseSn / goto goWhile strinF strinR selSet selTxt searep case count error)
    ; 01 Create selection set. GOTO 02 if success, or GOTO 08 if fail
    ; 02 Check passed input. If both nil, GOTO 03. If first string and second nil, GOTO 06. If both strings, GOTO 07. Otherwise, return error and GOTO 08
    ; 03 Display menus and obtain data from user. If Search, GOTO 04. If Replace, GOTO 05
    ; 04 Search option selected. Prompt user for single search term. GOTO 06
    ; 05 Replace option selected. Prompt user for search term and replace term. GOTO 07
    ; 06 One string has been passed. Assume automatic search. GOTO FINISH
    ; 07 Two strings have been passed. Assume automatic replace. GOTO FINISH
    ; 08 FINISH. Return errors or messages if needed.
  (vl-load-com)
  (setq goTo 1)
  (setq goWhile 1)
  (setq count 0)
  (if (not (mlml (list caseSn) (list 0 1)))
    (progn (setq goWhile nil) (princ "\nCase selection not recognized."))
  ) ;_ end of if
  (if (= caseSn 0)
    (setq case "N")
    (setq case "Y")
  ) ;_ end of if
  (while goWhile
    (cond
      ((= goTo 1)
       (if (setq selSet (extTxtPt (ssget "X")))
         (setq goTo 2)
         (setq error "\nSelection set not found."
               goTo  8
         ) ;_ end of setq
       ) ;_ end of if
      )
      ((= goTo 2)
    ; Check input, pass to whatever.
       (cond
         ((and (= inputF nil) (= inputR nil))
          (setq goTo 3)
         )
         ((and (= (type inputF) 'STR) (= inputR nil))
          (setq strinF inputF)
          (setq goTo 6)
         )
         ((and (= (type inputF) 'STR) (= (type inputR) 'STR))
          (setq strinF inputF)
          (setq strinR inputR)
          (setq goTo 7)
         )
         (t
          (setq error "\nPassed arguments are not accepted.")
          (setq goTo 8)
         )
       ) ;_ end of cond
      )
      ((= goTo 3)
    ; Obtain desired option from user
       (while (not
                (mlml (list (setq searep (strcase (getstring nil "\nSelect option [Find/Replace/Quit/Case]: "))))
                      (list "F" "FIND" "R" "REPLACE" "Q" "QUIT" "C" "CASE")
                ) ;_ end of mlml
              ) ;_ end of not
       ) ;_ end of while
       (cond
         ((mlml (list searep) (list "F" "FIND"))
          (setq goTo 4)
         )
         ((mlml (list searep) (list "R" "REPLACE"))
          (setq goTo 5)
         )
         ((mlml (list searep) (list "Q" "QUIT"))
          (setq goTo 8)
         )
         ((mlml (list searep) (list "C" "CASE"))
          (while (not (mlml (list (setq case (strcase (getstring nil "\nCase sensitive? [Yes/No]: "))))
                            (list "Y" "YES" "N" "NO")
                      ) ;_ end of mlml
                 ) ;_ end of not
          ) ;_ end of while
         )
       ) ;_ end of cond
      )
      ((= goTo 4)
    ; Obtain search string from user, set to strinF
       (while (eq "" (setq strinF (getstring t "\nEnter search term: "))))
       (setq goTo 6)
      )
      ((= goTo 5)
    ; Obtain search string and replace string from user, set to strinF and strinR respectively
       (while (eq "" (setq strinF (getstring t "\nEnter find term: "))))
       (while (eq "" (setq strinR (getstring t "\nEnter replace term: "))))
       (setq goTo 7)
      )
      ((= goTo 6)
    ; Search drawing for strinF
       (cond
         ((mlml (list case) (list "Y" "YES"))
    ; Compare using (vl-string-search strinF input), view selection
    ; use "while" to get all search occurances
          (foreach selVar selSet
            (if (vl-string-search strinF (nth 0 selVar))
              (progn
                (setq count (1+ count))
                (if (/= (getvar "ctab") (caddr selVar))
                  (command "ctab" (caddr selVar))
                ) ;_ end of if
                (command "zoom" "c" (trans (cadr selVar) 0 1) (* 32 (nth 3 selVar)))
                (getstring "\nPress 'Enter' to continue: ")
              ) ;_ end of progn
            ) ;_ end of if
          ) ;_ end of foreach
         )
         ((mlml (list case) (list "N" "NO"))
    ; Compare using (vl-string-search (strcase strinF) (strcase input)), view selection
    ; use "while" to get all search occurances
          (foreach selVar selSet
            (if (vl-string-search (strcase strinF) (strcase (nth 0 selVar)))
              (progn
                (setq count (1+ count))
                (if (/= (getvar "ctab") (caddr selVar))
                  (command "ctab" (caddr selVar))
                ) ;_ end of if
                (command "zoom" "c" (trans (cadr selVar) 0 1) (* 32 (nth 3 selVar)))
                (getstring "\nPress 'Enter' to continue: ")
              ) ;_ end of progn
            ) ;_ end of if
          ) ;_ end of foreach
         )
       ) ;_ end of cond
       (if (= count 0)
         (setq error "\nNo matches found.")
         (setq error (strcat (itoa count) " matches found."))
       ) ;_ end of if
       (setq goTo 8)
      )
      ((= goTo 7)
    ; Replace strinF with strinR
       (cond
         ((mlml (list case) (list "Y" "YES"))
    ; Compare using (vl-search-string strinF input), modify using (vl-string-subst) within a while loop
          (foreach selVar selSet
            (setq selTxt (nth 0 selVar))
            (setq seaLoc 0)
            (while (setq seaLoc (vl-string-search strinF selTxt seaLoc))
              (setq selTxt (vl-string-subst strinR strinF selTxt seaLoc))
              (setq seaLoc (+ seaLoc (strlen strinR)))
              (setq count (1+ count))
            ) ;_ end of while
            (vla-put-textstring (vlax-ename->vla-object (nth 4 selVar)) selTxt)
          ) ;_ end of foreach
         )
         ((mlml (list case) (list "N" "NO"))
    ; Compare using (vl-string-search (strcase strinF) (strcase input)), modify using (vl-string-subst) within a while loop
          (foreach selVar selSet
            (setq selTxt (nth 0 selVar))
            (setq seaLoc 0)
            (while (setq seaLoc (vl-string-search (strcase strinF) (strcase selTxt) seaLoc))
              (setq selTxt (strcat (substr selTxt 1 seaLoc) strinR (substr selTxt (+ 1 seaLoc (strlen strinF)))))
              (setq seaLoc (+ seaLoc (strlen strinR)))
              (setq count (1+ count))
            ) ;_ end of while
            (vla-put-textstring (vlax-ename->vla-object (nth 4 selVar)) selTxt)
          ) ;_ end of foreach
         )
       ) ;_ end of cond
       (if (= count 0)
         (setq error "\nNo occurances found.")
         (setq error (strcat (itoa count) " occurances modified."))
       ) ;_ end of if
       (setq goTo 8)
      )
      ((= goTo 8)
       (if error
         (princ error)
       ) ;_ end of if
       (setq goWhile nil)
      )
    ) ;_ end of cond
  ) ;_ end of while
  (princ)
) ;_ end of defun

(defun mlml (inSMLChar inSMLStri / returnVarMS toCheck chkWith)
  (setq returnVarMS nil)
  (if (and (= (type inSMLChar) 'list)
           (= (type inSMLStri) 'list)
      ) ;_ end of and
    (progn
      (foreach toCheck inSMLStri
        (foreach chkWith inSMLChar
          (if (eq toCheck chkWith)
            (setq returnVarMS t)
          ) ;_ end of if
        ) ;_ end of foreach
      ) ;_ end of foreach
    ) ;/progn
  ) ;_ end of if
  returnVarMS
)   ; Checks a list to see if a member of that list is the same as a member of another list. Returns T or nil

(defun extTxtPt (ssList / subVar getEnt entTyp entTxt entPnt entLay entHgt grp66 entAtt getEntAtt entAttTyp uniLst)
  (setq uniLst nil)
  (setq subVar 0)
  (if ssList
    (repeat (sslength ssList)
      (setq getEnt (entget (cadr (car (ssnamex ssList subVar)))))
      (setq entTyp (cdr (assoc 0 getEnt)))
      (cond
        ((or (= entTyp "TEXT") (= entTyp "MTEXT"))
         (setq entTxt (cdr (assoc 1 getEnt)))
         (setq entPnt (cdr (assoc 10 getEnt)))
         (setq entHgt (cdr (assoc 40 getEnt)))
         (setq entLay (cdr (assoc 410 getEnt)))
         (setq entNam (cdr (assoc -1 getEnt)))

         (setq uniLst (append uniLst (list (list entTxt entPnt entLay entHgt entNam))))
        )
        ((= entTyp "INSERT")
         (setq grp66 (assoc 66 getEnt))
         (if grp66
           (progn
             (setq entAtt (entnext (cdr (assoc -1 getEnt))))
             (setq getEntAtt (entget entAtt))
             (setq entAttTyp (cdr (assoc 0 getEntAtt)))
           ) ;_ end of progn
         ) ;_ end of if
         (while (= entAttTyp "ATTRIB")
           (setq entTxt (cdr (assoc 1 getEntAtt)))
           (setq entPnt (cdr (assoc 10 getEntAtt)))
           (setq entHgt (cdr (assoc 40 getEntAtt)))
           (setq entLay (cdr (assoc 410 getEntAtt)))
           (setq entNam (cdr (assoc -1 getEntAtt)))

           (setq uniLst (append uniLst (list (list entTxt entPnt entLay entHgt entNam))))

    ; Get next entity.
           (setq entAtt (entnext (cdr (assoc -1 getEntAtt))))

    ; Get ent and ent type
           (setq getEntAtt (entget entAtt))
           (setq entAttTyp (cdr (assoc 0 getEntAtt)))
         ) ;_ end of while
        )
        (t
        )
      ) ;_ end of cond
      (setq subVar (1+ subVar))
    ) ;_ end of repeat
  ) ;_ end of if
  uniLst
)   ; Return list of all text-based objects (Text, MText, Attribute) in the current drawing

Источник: http://www.cadtutor.net/forum/showthread.php?t=35933

Re: LISP. Поиск и замена текста

Nike,
Слегка модифицировать программу есть желание?