Jump to content

Replace text strings in Mtext with field expression


Lem

Recommended Posts

(defun c:superstr()
   
    (setq p (ssget))  
    (if p
 (progn
            (setq osl (strlen (setq os (getstring "\nOld string: " t))))
            (setq nsl (strlen (setq ns (getstring "\nNew string: " t))))
     (setq l 0 chm 0 n (sslength p))
     (setq adj
  (cond
      ((/= osl nsl) (- nsl osl))
      (T nsl)
  )
     )
 (while (< l n)                  
     (setq d (entget (setq e (ssname p l))))
     (if (and (= (atext 0) "INSERT")(= (atext 66) 1))
  (progn
      (setq e (entnext e))
      (while e
   (setq d (entget e))
   (cond
       ((= (atext 0) "ATTRIB")
    (setq chf nil si 1)
    (setq s (cdr (setq as (assoc 1 d))))
    (while (= osl (setq sl (strlen
        (setq st (substr s si osl)))))
        (cond
     ((= st os)
         (setq s (strcat (substr s 1 (1- si)) ns
         (substr s (+ si osl))))
         (setq chf t)
         (setq si (+ si adj))
     )
        )
    (setq si (1+ si))
       )
       (if chf
    (progn       
        (setq d (subst (cons 1 s) as d))
        (entmod d)       
        (entupd e)       
        (setq chm (1+ chm))
    )
       )
       (setq e (entnext e))
       )
       ((= (atext 0) "SEQEND")
    (setq e nil))
       (T (setq e (entnext e)))
                        )
      )
  )
     )
            (if (= "MTEXT"            ; Look for MTEXT 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))
               ))
            )
         )
     (if (= "DIMENSION"            ; Look for DIMENSION 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))
               ))
            )
         )
     (if (= "TEXT"            ; 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))
 )
 )
    )
    (if (> chm 1)
       (princ (strcat "\nUpdated " (itoa chm) " text strings"))
       (princ (strcat "\nUpdated " (itoa chm) " text string"))
    )
 
    (terpri)
)
;
(defun atext (num)
   (cdr (assoc num d))
)

    I am using this lisp to replace text. it works great except when the mtext has a field expression inserted. The same thing happens when i use the "find" command , if i search the value "x" to be replaced with "y", hit find, zoom to highlighted result, and press replace, the "x" value won't be replaced if the mtext has a field expression.

   Can anyone give some help to make  this lisp work even when the mtext have a field expression.

Link to comment
Share on other sites

if using vlisp it will return the full textstring including all the hidden mtext codes, you can even add codes. Use Vl-sting-subst to change the string.

Vla-get-textstring

Vl-string-subst

Vla-put-textstring

Link to comment
Share on other sites

@Lem It appears to me that this has to do with the fact that when you are searching for strings, you looking for the literal value in the text string of the object. Fields don't actually store the literal value within the string, but only a set of codes pointing to where the field is referencing the information from. Using a simple string compare would not work in this case. That begs the question - "Why would you want to change a Field value in a Text?" If you want to change a field - you have to change what the field is referencing. Example - if the field is pointing to a Drawing property, you would have to parse the field code to determine what property it is referencing, then get the property itself and update it.

 

BTW - that also goes the same for Dimension values as well - the measured part of the dimension just reads "<>" when looking at the text string value of the dimension.

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...