Automatically Change Color Of Specific Word In MTEXT

Automatically Change Color Of Specific Word In MTEXT

X1JIANGWOO
Participant Participant
560 Views
10 Replies
Message 1 of 11

Automatically Change Color Of Specific Word In MTEXT

X1JIANGWOO
Participant
Participant

Hello All !

 

I May be overthinking this but is there an easy way to simply have specific words change color when I open a drawing ? There are only 8 words/phrases that I want to change to a specific color. 

 

Everything that I have found requires user input or selection. 

Any help is greatly appreciated ! 

 

Shawn

 

 

 

0 Likes
561 Views
10 Replies
Replies (10)
Message 2 of 11

JTBWorld
Advisor
Advisor

If you can't find someone willing to do it for free you can contact us and we can help. 


Jimmy Bergmark
JTB World - Software development and consulting for CAD and license usage reports
https://jtbworld.com

0 Likes
Message 3 of 11

Kent1Cooper
Consultant
Consultant

To get you started:
If 'mtdata' is the entity data list of an Mtext object, and you want to change the string "TEST" to be red, this does that [minimally tested]:

 

(entmod
  (subst
    (cons 1 (vl-string-subst "{\\C1;TEST}" "TEST" (cdr (assoc 1 mtdata))))
    (assoc 1 mtdata)
    mtdata
  )
)

 

Case-sensitive.  If the word "TEST" occurs more than once in the Mtext, it will change the color of only the first instance.

 

It would surely be made more complicated if any other kind of Mtext-internal formatting covers that part of the content.

Kent Cooper, AIA
0 Likes
Message 4 of 11

X1JIANGWOO
Participant
Participant

Thank you Kent.  

 

Apologies

Its been a while since I've used AutoCAD. How would I test this ? 

 

Shawn

0 Likes
Message 5 of 11

Kent1Cooper
Consultant
Consultant

@X1JIANGWOO wrote:

.... How would I test this ? ....


Make a piece of Mtext containing "TEST" in it somewhere.

 

Put this in at the command line:

(setq mtdata (entget (car (entsel))))

and pick that Mtext object.

 

Then paste in the code in my previous Reply.  Change the 1 that immediately follows a C to whatever color number you prefer.

 

Or replace TEST in two places with WHATEVER STRING YOU WANT TO CHANGE THE COLOR OF.

Kent Cooper, AIA
0 Likes
Message 6 of 11

john.uhden
Mentor
Mentor

@Kent1Cooper 

That's great if the text is < (or maybe <=) 256 characters.

T'would be better to grab the 'Textstring property of the vla-object.

John F. Uhden

0 Likes
Message 7 of 11

Kent1Cooper
Consultant
Consultant

@john.uhden wrote:

That's great if the text is < (or maybe <=) 256 characters.

....


I doubt very much that any of their "only 8 words/phrases that I want to change to a specific color" would be of excessive length.  If done that way, it would be best to put the string into a variable and (strcat) the color-coding part around it, rather than include it all twice as with the word "TEST" in my suggestion.

Kent Cooper, AIA
0 Likes
Message 8 of 11

john.uhden
Mentor
Mentor

@Kent1Cooper 

Just curious... if the word already has color coding in front of it, and you add another color coding (different color of course), which one will prevail?  What I'm getting at is does the previous color coding have to be removed?

John F. Uhden

0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

I forewarned about that -- see the end of Message 3.  This kind of nesting colors, in an Mtext object whose overall color is white [ByLayer]:

Kent1Cooper_0-1658243689208.png

comes to this:

(1 . "WHITE WITH {\\C1;RED INSIDE AND \\C3;GREEN INSIDE THAT\\C1; BACK TO RED} BACK TO WHITE.")

That's without both color changes starting at the same place.  It doesn't merely end the green to go back to red, but instead overtly switches to red, but it merely ends the red to go back to white.

 

If a word is made red and then made green starting from the same place:

Kent1Cooper_1-1658244044576.png

[that is, I picked everything that's not white and turned it all red, then picked the part that I made green inside that], it does this:

(1 . "WHITE WITH {\\C3;GREEN-INSIDE-RED\\C1; BACK TO RED} BACK TO WHITE")

in which the green overrules the original switch to red [there aren't both red and green color codes together], but then it knows to switch to it to red at the end of the green.

Kent Cooper, AIA
0 Likes
Message 10 of 11

X1JIANGWOO
Participant
Participant

I gave it shot. the color did not change.  I'm sure it's user error on my end. 

I tried "TEST" in MTEXT. 

 

X1JIANGWOO_0-1658266715607.png

 

0 Likes
Message 11 of 11

Kent1Cooper
Consultant
Consultant

That has a color-5 override for that text content.  Is that before or after applying the original code?  It looks like before, since it's just the result of setting the entity data into the mtdata variable, which should then be followed by the color-change code.  If after, was it already color 5?  Does it change if you REGENerate?

Kent Cooper, AIA
0 Likes