Change specific word color

Change specific word color

msarqui
Collaborator Collaborator
6,670 Views
36 Replies
Message 1 of 37

Change specific word color

msarqui
Collaborator
Collaborator

Hello!

 

I found here a routine that changes a specific number within a Text, Mtext or Attribute for another number: http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/find-and-replace-text-mtext-and-attri...

 

It would be nice to have a routine that:

Step 1. Ask for a specific word

Step 2. Ask for a color

Step 3. Assign the given color in Step 2 to the given word in Step 1

It has to cover:

a. Text, Mtexts and Attributes

b. Paper and model space

c. Words inside blocs. 

d. If the word is a Single line Attibute or within a Multiline Attribute, the color of the Attribute (not only the word in the inserted block) must change. I mean, when I re-insert the block, or when I do the Attsync command, the word in the Attribute will have the the chosen color in Step 2.

 

Thanks for your help.

 

0 Likes
Accepted solutions (1)
6,671 Views
36 Replies
Replies (36)
Message 2 of 37

pbejse
Mentor
Mentor

@msarqui wrote:

Hello!

 

I found here a routine that changes a specific number within a Text, Mtext or Attribute for another number: http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/find-and-replace-text-mtext-and-attri...

 

It would be nice to have a routine that:

Step 1. Ask for a specific word

Step 2. Ask for a color

Step 3. Assign the given color in Step 2 to the given word in Step 1

It has to cover:

a. Text, Mtexts and Attributes

b. Paper and model space

c. Words inside blocs. 

d. If the word is a Single line Attibute or within a Multiline Attribute, the color of the Attribute (not only the word in the inserted block) must change. I mean, when I re-insert the block, or when I do the Attsync command, the word in the Attribute will have the the chosen color in Step 2.

 

Thanks for your help.

 


a,b,c, doable

 

d. on the other hand, say you have an attribute  tag "PRIZE" with a value of "1971 Ford Mustang" on a block called "Winnings" , the user search for "FORD" , the routine finds the word and change the color to say "BLUE". BUT there is another "Winnings" block with an attribute value of "1981 Toyota" under the same tag "PRIZE".

 

When you invoke attsync. not only the ford mustang will turn blue but also the Toyota. Why? because its under the same tag name on the block Winnings.

 

Colors of attribute definition will govern on any other color when you use Attsync. its some sort of a reset button.

0 Likes
Message 3 of 37

msarqui
Collaborator
Collaborator

Hi pejse.
Thanks for the explanation. I understand your point.
Well, I can leave whithout the "d" option. Sometimes we can not have everything. 😞

0 Likes
Message 4 of 37

pbejse
Mentor
Mentor

@msarqui wrote:

Hi pejse.
Thanks for the explanation. I understand your point.
Well, I can leave whithout the "d" option. Sometimes we can not have everything. 😞


Now that we have that settled, are you wanting a code to look a specific "word" in a phrase ? i.e. banana in "The banana cake" text string? and color the whole line or just the WORD? for, Mtext it is possible, but for Attributes/TEXT you have to split the phrase to capture the exact word "The " "banana" " cake". 3 text entity insetad of 1, Is this where the routine is leading to?

 

 

0 Likes
Message 5 of 37

Kent1Cooper
Consultant
Consultant

@pbejse wrote:
... for Attributes/TEXT you have to split the phrase to capture the exact word "The " "banana" " cake". 3 text entity insetad of 1.... 

An alternative to that, for Text, would be to convert it to Mtext first, and then apply the change to the word within the Mtext.  For an Attribute, I don't think it's possible to split it up, though in newer versions maybe you can make it into a Multiline Attribute, if those can have the same kinds of internal formatting as Mtext.

Kent Cooper, AIA
0 Likes
Message 6 of 37

msarqui
Collaborator
Collaborator

"Now that we have that settled, are you wanting a code to look a specific "word" in a phrase ? i.e. banana in "The banana cake" text string?"

 

Yes! I need to change the color of the word, not the whole phrase.

Exemple:

My original phrase is: "The banana cake"

If I chose the word banana in Step1 and color 2 in Step2, my phrase after the command will be: "The banana cake"

 

I see now your another point about TEXT versus MTEXT. I made a large review in my drawings and I noticed that, If my specific word is in a TEXT it will be a single word text, not a phrase. So, in that case a simple CHPROP maybe could be the solution.

 

What do you think?

0 Likes
Message 7 of 37

Kent1Cooper
Consultant
Consultant

@msarqui wrote:

....

Yes! I need to change the color of the word, not the whole phrase.

Exemple:

My original phrase is: "The banana cake"

If I chose the word banana in Step1 and color 2 in Step2, my phrase after the command will be: "The banana cake"

....


[Color 1, really, but let that pass....]  In that particular instance, for Mtext as a vla-object in the variable 'mt':

 

(vla-put-TextString mt (vl-string-subst "{\\C2;banana}" "banana" (vla-get-TextString mt)))

 

In the generic case, if you've saved the color number [as a string] into a variable I'll call 'color' and the word/phrase into one called 'txt':

 

(vla-put-TextString mt (vl-string-subst (strcat "{\\C" color ";" txt "}") txt (vla-get-TextString mt)))

 

HOWEVER, be aware of the possible complications of other internal formatting.  If, for example, the word banana also has a different font assigned, before you get into it for a color override, it will be starting with text content like this [in this example, the SCRIPTC font]:

 

"The {\\Fscriptc.shx|c0;banana} cake"

 

After giving the word "banana" color 2 manually, its resultant text content looks like this:

 

"The {\\Fscriptc.shx|c0;\\C2;banana} cake"

 

Note that the surrounding braces {} "wrap" both the Font and Color assignments -- there are not separate "wrappers" for each override -- so the simple string substitution above will cause trouble.  Underlining is another possibility that would be encompassed within a single {} "wrapping" along with other override(s).  And it gets even more complicated if any of those other overrides extends beyond the end of the word "banana" itself, in which case you could need an explicit change-back of the Color after the word, but without adding another } closure.  Another complication is if the word already has a different Color override.  Etc., etc. ....

Kent Cooper, AIA
Message 8 of 37

pbejse
Mentor
Mentor

Kent1Cooper wrote:

An alternative to that, for Text, would be to convert it to Mtext first,lternati and then apply the change to the word within the Mtext.  For an Attribute, I don't think it's possible to split it up, though in newer versions maybe you can make it into a Multiline Attribute, if those can have the same kinds of internal formatting as Mtext.


Good point Kent, I never notice that MTEXT restriction before.

 

@msarqui

 

 

If i may ask, whats the point of this routine? Will the color and "word" change overtime? or is it permanent?

 

 

 

0 Likes
Message 9 of 37

pbejse
Mentor
Mentor

@Kent1Cooper wrote:
 HOWEVER, be aware of the possible complications of other internal formatting.  If, for example, the word banana also has a different font assigned, before you get into it for a color override,.....

 ....


 

I already put some thought into this right from the start. the hassles of character formatting.  For this routine to be generic, one must consider that possibility. 

 

Now its wont be just a simple find and change the color routine, converting TEXT into MTEXT, maintaining/re-building character formatting, redefining blocks for TEXT/MTEXT inside a block. Makes me wonder if msarqui would settle to just underscore/overscore the target string.

 

 

0 Likes
Message 10 of 37

msarqui
Collaborator
Collaborator

Ok,


In the case of a MTEXT:
The purpose of the routine is to print the word with another color, different from the text color in which it is inserted. It will be like a flag to our reviewers.
In our block library we have hundreds of Civil Work Specifications, Electrical Specifications, or another kind of specifications type: change the word "client" to the actual name of the client, change the word "XXXXX" to the correct term and so on. All of these words now have the same color of the text in which they are inserted. So, if I have a routine that changes the color of a searched word, it would save me hundreds of hours of: find the word, enter in the Mtext editor, select the word, changes its colors, close the Mtext editor, find the next word...

 

In the case of a TEXT:
The goal is the same, but in this case I do not have a phrase, just one word.


The ATTRIBUTE are not as numerous. I could make by myself one by one.

 

In a future Autocad version, this routine could be a FIND and REPLACE command with color formatation. Like we have in the Microsoft Office products.

 

Thanks!

0 Likes
Message 11 of 37

hmsilva
Mentor
Mentor

@msarqui wrote:

... change the word "client" to the actual name of the client, change the word "XXXXX" to the correct term and so on. All of these words now have the same color of the text in which they are inserted. So, if I have a routine that changes the color of a searched word, it would save me hundreds of hours of: find the word, enter in the Mtext editor, select the word, changes its colors, close the Mtext editor, find the next word...

In the case of a TEXT:
The goal is the same, but in this case I do not have a phrase, just one word.
The ATTRIBUTE are not as numerous. I could make by myself one by one.


Hi msarqui,

 

silly question: why not just use the 'find' command, and replace all?

 

Find.PNG

 

Henrique

EESignature

0 Likes
Message 12 of 37

pbejse
Mentor
Mentor

@msarqui wrote:

Ok,


In the case of a MTEXT:
......

 

In the case of a TEXT:
The goal is the same, brd.ut in this case I do not have a phrase, just one word


Thanks!


I do hope it does not have too much character formatting as we expected it would have, that makes it easier for us

 

Again, that would make the code easier to write

 

I would like to think underscore/overscore will the same affect as flags for you reviewers.

 

Anyhoo. i'll see what i can do. 

 

0 Likes
Message 13 of 37

Kent1Cooper
Consultant
Consultant

hmsilva wrote:

.... 

silly question: why not just use the 'find' command, and replace all?

....


That will handle the wording change part, but not the color part [see the Subject line].  I assume the wording part was not part of the Subject or the original question because that can be handled using FIND.

Kent Cooper, AIA
0 Likes
Message 14 of 37

hmsilva
Mentor
Mentor

@Kent1Cooper wrote:

hmsilva wrote:

.... 

silly question: why not just use the 'find' command, and replace all?

....


That will handle the wording change part, but not the color part [see the Subject line].  I assume the wording part was not part of the Subject or the original question because that can be handled using FIND.


What I've understood is that the OP just want to change the words color to later edit the word/sentense and change to the correct word(s)...

If I'm correct, why not skip the part of change the color for later edit, and do the editing at once....

 

I may not have understood correctly the OP goal.

 

Henrique

EESignature

0 Likes
Message 15 of 37

pbejse
Mentor
Mentor

@Kent/Henrique 

 

guess it all boils down to change the "word" AND then assign color routine. I will hold off wriitng the code for now until the OP arrived at a decision as to what the routine is really all about.

 

No need to write two codes if it can be achieved by one lisp routine 🙂

 

Also if the OP is planning to use the routine on a script, native FIND is out of the window.

 

pBe

 

0 Likes
Message 16 of 37

hmsilva
Mentor
Mentor

@pbejse wrote:

 if the OP is planning to use the routine on a script, native FIND is out of the window.


Good point pBe!

 

Henrique

EESignature

0 Likes
Message 17 of 37

msarqui
Collaborator
Collaborator

Ok guys.

 

I will try to be as clear as possible. Sorry but my mother tongue is not English and sometimes I can not express all the necessary nuances to make myself understood. And in this case, I agree that we have many nuances.

 

Henrique: the command "Find and Replace" was just an example I used to you to understand my purposes. I do not want to change the word, only its color.

 

pbejse:

"I would like to think underscore / Overscore will the same Affect the flags for you Reviewers." That is a good point but if the possibility of change the color is real, it would be, in my opinion, vastly better.

"I do hope it does not have too much character formatting as we expected it would have, that makes it easier for us" Pleasesee attached an example that how we use to do in the office.

 

The routine would look like:

 

(defun c: ReplaceWordColor ()

(princ "\ n Select a Text or Mtext")
(princ "\ n Which word do you want to change color?")
(princ "\ n Wich color do you want to assing to the word?")

 

(if MTEXT
... the routine here ...

);if

 

(if TEXT

...the routine here..
);if

 

);defun

 

And please, do not start to write if you still have doubts. I understand that is is a hard work to do so, I am here to answer if you have any questions.

I have AutoCAD 2012.

 

Thanks.

0 Likes
Message 18 of 37

hmsilva
Mentor
Mentor

@msarqui wrote:

Henrique: the command "Find and Replace" was just an example I used to you to understand my purposes. I do not want to change the word, only its color.


msarqui, obviously I misunderstood your workflow...

 

Sorry

Henrique

EESignature

0 Likes
Message 19 of 37

pbejse
Mentor
Mentor

@msarqui wrote:

 

And please, do not start to write if you still have doubts. I understand that is is a hard work to do so, I am here to answer if you have any questions.

 


Thats good to know.

 

Question:

  • Will there be two or more target "WORD" present on one MTEXT string?
  • Will you be running the same routine on a MTEXT already has colors in it?
  • Like in the example, not just one color? Different item different color?

 

0 Likes
Message 20 of 37

msarqui
Collaborator
Collaborator

pbejse wrote: 

Thats good to know.

Question:

  • Will there be two or more target "WORD" present on one MTEXT string? Yes, and I think it is better to change one target "WORD" at a time. But, there is a nuance here: So, if I have two different target "WORD" on one MTEXT string, I will execute the routine two times. But, I can have two equal target "WORD" in the same MTEXT. In that case, the routine will change both.
  • Will you be running the same routine on a MTEXT already has colors in it? Yes
  • Like in the example, not just one color? Yes, the MTEXT could have more than one color Different item different color? Yes

Thanks!

0 Likes