Jump to content

AutoCAD Script to replace text


nycadguy

Recommended Posts

Hi, I am looking to see if I can create a script that will prompt to replace specific text areas in paperspace without opening the layout tab. I am new to AutoCAD scripts but have seen them in action and see a possible opportunity to use one here to speed things up. Any info will be greatly appreciated, Thanks!

Link to comment
Share on other sites

This might help.

 

;-============-;
;- Text  Repl -;
;-    *~*     -;
;  Written by -;
; Mark Mercier ;
;   05-06-09   ;
;-============-;
(defun treplace(inputF inputR caseSn / goTo goWhile count case strinF strinR selVar selTxt seaLoc selSet error)
 (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)
      (if (setq selSet (extTxtPt (ssget "X"))) (setq goTo 2) (setq error "\nSelection set not found." goTo )
      )
     ((= goTo 2)
      ; Check input, pass to whatever.
      (cond
    ((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 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)
 ); Function to replace text, given input, output and case sensitivity

(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 drawing

Call this function using a search string and replace string, as well as a case sensitivity flag (1 for case sensitive, 0 for not). The user interface can be constructed with a simple function, just grab some variables using (getstring). You can also try altering the selection set within this function to determine exactly what text needs to be altered. I'll let you figure that part out on your own~

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...