tmelancon 发表于 2022-7-5 17:35:02

Replace Text, No dialog, works

Love this routine and it works great with a large script that I am currently running in conjunction with a delete block LISP to batch remove some old logos in our titleblocks since we are in a merge. I am using this code to replace the old company name with the new.
 
The problem is that not all of the previous caddies followed standards and a lot of titleblocks are not the same. (e.g. some have the company name as attribute, others dont.) This is where this program hits a snag and just doesnt change the company name. If the titleblock has the company name attributed, then this routine works like a charm, however, if it is plain text no attribute that was thrown in the titleblock, this routine skips it.
 
If someone can understand and maybe test and tweak this code to work how I am expecting it too, then mucho gracias!!
 
Single Command to run variety of text replacements needed.

(defun c:newfun()(tfindfun "Old Company" "New Company" 1)(C:DELBLK) ;; we have this running to remove any company logos)Main Code:

;-============-;;- TextFind -;;-    *~*   -;;Written by -;; Mark Mercier ;;   05-06-09   ;;-============-;; Improvements:; Text within blocks; Improved selection set.. maybe do away with the whole "list" thing and go straight VLA(defun c:tfind() (tfindfun nil nil 0) )(defun tfindfun(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. Run same as current (tfind). GOTO FINISH ; 07 Two strings have been passed. Assume automatic replace. Pass both strings to (replace) function. GOTO FINISH ; 08 FINISH. Return errors if needed. End loop and program. (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."))) (if (= caseSn 0) (setq case "N") (setq case "Y")) (while goWhile   (cond   ((= goTo 1)      (setq selSet (extTxtPt (ssget "_X" (list (cons -4 "")))))      (if selSet (setq goTo 2) (setq error "\nSelection set not found." goTo )      )   ((= 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      )    )      )   ((= goTo 3)      ; Obtain desired option from user      (while (not (mlml (list (setq searep (strcase (getstring nil "\nSelect option : "))))                (list "F" "FIND" "R" "REPLACE" "Q" "QUIT" "C" "CASE")                ))    )      (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      )    ((mlml (list searep) (list "C" "CASE"))   (while (not (mlml (list (setq case (strcase (getstring nil "\nCase sensitive? : "))))                   (list "Y" "YES" "N" "NO")                   ))       )   )    )      )   ((= 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)))       (command "zoom" "c" (trans (cadr selVar) 0 1) (* 32 (nth 3 selVar)))       (getstring "\nPress 'Enter' to continue: ")       )         )       )   )    ((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)))       (command "zoom" "c" (trans (cadr selVar) 0 1) (* 32 (nth 3 selVar)))       (getstring "\nPress 'Enter' to continue: ")       )         )       )   )    )      (if (= count 0) (setq error "\nNo matches found.") (setq error (strcat (itoa count) " matches found.")))      (setq goTo       )   ((= 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))         )       (vla-put-TextString (vlax-ename->vla-object (nth 4 selVar)) selTxt)       )   )    ((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))         )       (vla-put-TextString (vlax-ename->vla-object (nth 4 selVar)) selTxt)       )   )    )      (if (= count 0) (setq error "\nNo occurances found.") (setq error (strcat (itoa count) " occurances modified.")))      (setq goTo       )   ((= goTo       (if error (princ error))      (setq goWhile nil)      )   )   ) (princ) )(defun mlml(inSMLChar inSMLStri / returnVarMS toCheck chkWith) (setq returnVarMS nil) (if (and (= (type inSMLChar) 'LIST)      (= (type inSMLStri) 'LIST)      )   (progn   (foreach toCheck inSMLStri   (foreach chkWith inSMLChar   (if (eq toCheck chkWith) (setq returnVarMS T))   )   )   );/progn   ) 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)))      )    )      (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)))    )      )   (t      )   )   (setq subVar (1+ subVar))   )   ) uniLst ); Return list of all text-based objects (Text, MText, Attribute) in the current drawingI am sure some of you will find other ways to do this, and trust me I am all ears. We would use Lee's BFIND (which we use daily) but it has a dialog and we do not need dialog since we are running scripts. And it would be pointless to run replacement text LISP on entire databases then go back and run BFIND.. Well hopefully someone can chime in!! AS ALWAYS!! THANKS!!

tmelancon 发表于 2022-7-5 18:37:14

I guess a valid question for Lee would be, how can I use your delblocks function with your BFIND function, that way I can run a massive replace company name and delete the company logo and it be really efficient.
页: [1]
查看完整版本: Replace Text, No dialog, works