Replace all texts strings

Replace all texts strings

msarqui
Collaborator Collaborator
3,623 Views
15 Replies
Message 1 of 16

Replace all texts strings

msarqui
Collaborator
Collaborator

Hello guys,

It would be possible to have a routine to replace the strings of multiple texts/mtexts by another string?

If possible with a SSGET to have multiple selection.
The routine would have to consider that the selected texts can contain different strings.
The routine will automatically change every string of selected texts by, for exemple "XXX"

 

Exemple:

The text "SITE PLAN" will became "XXX"

The text "ELEVATIONS" will became "XXX"

and so on...

 

Thanks for your help!

Marcelo

0 Likes
Accepted solutions (1)
3,624 Views
15 Replies
Replies (15)
Message 2 of 16

hmsilva
Mentor
Mentor

Hi Marcelo,

perhaps something like this:

 

(defun c:demo (/ replace_ newpath path paths ss)

   (defun replace_ (sset pattern newpattern / ent i str)
      (repeat (setq i (sslength sset))
         (setq ent (entget (ssname sset (setq i (1- i))))
               str (cdr (assoc 1 ent))
         )
         (if (wcmatch str (strcat "*" pattern "*"))
            (progn
               (while (wcmatch str (strcat "*" pattern "*"))
                  (setq str (vl-string-subst newpattern pattern str))
               )
               (entmod (subst (cons 1 str) (assoc 1 ent) ent))
            )
         )
      )
   )

   (if (setq paths   '("SITE PLAN" "ELEVATIONS")
             newpath "XXX"
             ss      (ssget "_:L" '((0 . "TEXT,MTEXT")))
       )
      (foreach path paths
         (replace_ ss path newpath)
      )
   )
   (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 3 of 16

paullimapa
Mentor
Mentor

Is there a reason for not using the built-in AutoCAD "FIND" command?

 

 

 

Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator
Layer Apps | List on Steroids | VP Zoom Scales | Exchange App Store


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 4 of 16

msarqui
Collaborator
Collaborator

Hello Henrique


Sorry my poor English. I do not explained myself well.
What I mean is, all texts, no matter what kind of string it contains, will be replaced by "XXXX".
The "SITE PLAN" and "ELEVATIONS" were examples only, to show that the text can contain one word, two words, and so on...

So, the text can contain any type of strings.

And that is the reason to not use FIND command.

 

Thanks,

Marcelo

0 Likes
Message 5 of 16

paullimapa
Mentor
Mentor

That's why I mentioned AutoCAD's built-in FIND command.

You just have to click on the down arrow (see attached image) and check the option "Use wildcards".

Then under "Find what" enter asterisk *

Under "Replace with" enter something like ALL

And it'll replace all text strings with ALL

 

 

Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator
Layer Apps | List on Steroids | VP Zoom Scales | Exchange App Store


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 16

msarqui
Collaborator
Collaborator
Hi pli.
If I could, I would edit my last reply to add this: "It must be in a selection set.
Thansk
0 Likes
Message 7 of 16

paullimapa
Mentor
Mentor

Yep, that feature is available to with AutoCAD's built-in FIND command under "Find where" (see attached screen capture)

 

 

Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator
Layer Apps | List on Steroids | VP Zoom Scales | Exchange App Store


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 8 of 16

hmsilva
Mentor
Mentor

@msarqui wrote:

Hello Henrique


Sorry my poor English. I do not explained myself well.
What I mean is, all texts, no matter what kind of string it contains, will be replaced by "XXXX".
The "SITE PLAN" and "ELEVATIONS" were examples only, to show that the text can contain one word, two words, and so on...

So, the text can contain any type of strings.

And that is the reason to not use FIND command.

 

Thanks,

Marcelo


Hi Marcelo,

quickly revised code...

 

(defun c:demo (/ replace_ newpath path ss)

    (defun replace_ (sset pattern newpattern / ent i str)
        (repeat (setq i (sslength sset))
            (setq ent (entget (ssname sset (setq i (1- i))))
                  str (cdr (assoc 1 ent))
            )
            (if (wcmatch str (strcat "*" pattern "*"))
                (progn
                    (while (wcmatch str (strcat "*" pattern "*"))
                        (setq str (vl-string-subst newpattern pattern str))
                    )
                    (entmod (subst (cons 1 str) (assoc 1 ent) ent))
                )
            )
        )
    )

    (if (and (setq path (getstring T "\nEnter text to replace: "))
             (/= path "")
             (setq newpath (getstring T "\nEnter text to replace with: "))
             (/= newpath "")
             (setq ss (ssget "_:L" '((0 . "TEXT,MTEXT"))))
        )
        (replace_ ss path newpath)
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 9 of 16

Kent1Cooper
Consultant
Consultant
Accepted solution

For all-Text selection, you can just do it in the Properties box, putting your XXX or whatever you want in the Content slot in the Text category.  But you can't do multiple Mtext that easily [if you try, it wants you to edit the content of each object separately].  For an either-or-both-types selection, here's one way:

 

(defun C:TXXX (/ ss n); = Text content to "XXX"

  (vl-load-com); if needed

  (prompt "\nTo change content of ALL selected Text/Mtext to \"XXX\",")

  (if (setq ss (ssget "_:L" '((0 . "*TEXT"))))

    (repeat (setq n (sslength ss))

      (vla-put-TextString (vlax-ename->vla-object (ssname ss (setq n (1- n)))) "XXX")

    ); repeat

  ); if

); defun

 

It could, of course, be made to ask the User for the content they want all such objects to have, if XXX isn't always what you want.

Kent Cooper, AIA
0 Likes
Message 10 of 16

msarqui
Collaborator
Collaborator

That is perfect Kent. Thanks a lot!


Henrique, thanks for try, but Kent got exactly what I would like to have.
Pli, I really needed this on lisp. Kudos for you because I did not know about the wild cards in the find command.


Thanks all.
Marcelo

0 Likes
Message 11 of 16

hmsilva
Mentor
Mentor

@msarqui wrote:

That is perfect Kent. Thanks a lot!


Henrique, thanks for try, but Kent got exactly what I would like to have.
...

 


Marcelo,

your goal was just select text/mtext objects and put a text string "XXX" in selected text objects???? 🙂

 

I'm glad you found a solution!

Henrique

 

EESignature

0 Likes
Message 12 of 16

msarqui
Collaborator
Collaborator

@hmsilva wrote:

 Marcelo,

your goal was just select text/mtext objects and put a text string "XXX" in selected text objects???? 🙂

 


Yes Henrique, just that.
As you know, english is not my strongest ability... 🙂

0 Likes
Message 13 of 16

hmsilva
Mentor
Mentor

@msarqui wrote:

@hmsilva wrote:

 Marcelo,

your goal was just select text/mtext objects and put a text string "XXX" in selected text objects???? 🙂

 


Yes Henrique, just that.
As you know, english is not my strongest ability... 🙂


🙂

Cheers

Henrique

EESignature

0 Likes
Message 14 of 16

Anonymous
Not applicable

How would you modify this so that you can preload in some options? Like say you input "x" and you get 'xxxx', or you input 'email' and the lisp returns 'toohottohoot@blahinc.com'?

0 Likes
Message 15 of 16

Sea-Haven
Mentor
Mentor

You would need a list of options, and the text needs a check does search text exist if yes replace should it be just total replace or a word ?

 

(("x" "XXXX")("mail" "alanh.com.au"))

 

Do a google pretty sure this exists letting you do multiple replace. Maybe Lee-mac.com

0 Likes
Message 16 of 16

pbejse
Mentor
Mentor

@Anonymous wrote:

How would you modify this so that you can preload in some options? Like say you input "x" and you get 'xxxx', or you input 'email' and the lisp returns 'toohottohoot@blahinc.com'?


I you wanted to be prompted for what is and what will at every call to the command, might as well stick with Find and Replace.

Otherwise, use an external text file as the source. If the values are constant then go with hard-coding the string values.

 

 

0 Likes