Change specific word color

Change specific word color

msarqui
Collaborator Collaborator
6,698 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,699 Views
36 Replies
Replies (36)
Message 21 of 37

pbejse
Mentor
Mentor

@msarqui wrote:

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!


OK, i think i get the general idea now. I'll start writing the code....

 

0 Likes
Message 22 of 37

pbejse
Mentor
Mentor

Try this 

Color Me Bad

 

(defun c:CMB (/ str ss rgx obj objtype)
;;;	Color Me Bad  pBe Feb 2015     	;;;
;;;	Special Thanks to Lee Mac 	;;;
;;;	for introducing me to RegEx	;;;

  (if (and
	(setq
	  Str (getstring T "\nWhich word do you want to change color? ")
	)
	(snvalid str)
	(setq col (acad_colordlg
		    (if	col
		      col
		      7
		    )
		    nil
		  )
	)
	(setq ss (ssget "_X" '((0 . "MTEXT,TEXT"))))
      )

    (progn
      (setq rgx (vlax-get-or-create-object "VBScript.RegExp"))
      (vlax-put-property rgx 'global actrue)
      (vlax-put-property rgx 'ignorecase actrue)
      (vlax-put-property rgx 'multiline actrue)

      (repeat (setq i (sslength ss))
	(setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
	(cond
	  ((null (vl-string-search
		   (strcase str)
		   (strcase (setq s (vla-get-textstring obj)))
		 )
	   )
	  )
	  ((equal (setq objtype (vla-get-Objectname obj)) "AcDbMText")
	   (vlax-put-property
	     rgx
	     'pattern
	     (Strcat
	       "\\\\C([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]);"
	       str
	     )
	   )
	   (vla-put-textstring
	     obj
	     (if (not (eq s
			  (setq	s (vlax-invoke
				    rgx
				    'replace
				    s
				    (Strcat "\\C" (itoa col) ";" str)
				  )
			  )
		      )
		 )
	       s
	       (progn
		 (vlax-put-property rgx 'pattern str)
		 (vlax-invoke
		   rgx
		   'replace
		   s
		   (Strcat "{\\C" (itoa col) ";" str "}")
		 )
	       )
	     )
	   )
	  )
	  ((eq objtype "AcDbText") (vla-put-color obj col))
	  ((eq objtype "AcDbBlockReference")
	   (princ "\n<<< Discontinued >>>")
	  )
	)
      )
      (vlax-release-object rgx)
    )
  )
  (princ)
)

 

Command: CMB
Which word do you want to change color? CSA S37-13

 

HTH

 

 

Message 23 of 37

hmsilva
Mentor
Mentor

Using the 'VBScript RegExp Object', nice approach, pBe!!! Smiley Happy

 

Cheers

Henrique

EESignature

0 Likes
Message 24 of 37

pbejse
Mentor
Mentor

@hmsilva wrote:

Using the 'VBScript RegExp Object', nice approach, pBe!!! Smiley Happy

 

Cheers

Henrique


Thank you Henrique, dealing vl-string functions is too troublesome . too many things to consider.

 

Hope the code is enough for msarqui. I saw a bug on the code but I will modify it tomorrow to maintain the  letter case of the target "word"

 

Cheers my friend 🙂

 

0 Likes
Message 25 of 37

msarqui
Collaborator
Collaborator

Thanks pbejse!

 

I will do some tests right now. 🙂

0 Likes
Message 26 of 37

msarqui
Collaborator
Collaborator

@pbejse wrote:
Hope the code is enough for msarqui. I saw a bug on the code but I will modify it tomorrow to maintain the  letter case of the target "word"

Yes! Is enough and I am very thankful to you. The routine is really fast, even in a large drawing (like the one I have). Also, it keeps the existing formatation.


I did not understand if your comment about the letter case of the target "word" is related to what I will tell you now. If the answer is yes, I'm sorry, but I noticed one small thing and, I want to say before all, it doesn't bothers me because I do not have this "small words" as a target. I think maybe this could be something to work for a future and more specific routine. So, here is the thing: if I type "and" for the target word, 1 for the color, and I have a word like "standard", this last will change to "standard". But, like I said, in my drawings I have only "larger words" and the routine changed all very consistently.
Many many thanks!.

 

0 Likes
Message 27 of 37

pbejse
Mentor
Mentor
Accepted solution

@msarqui wrote:

Yes! Is enough and I am very thankful to you. The routine is really fast, even in a large drawing (like the one I have). Also, it keeps the existing formatation.


....Many many thanks!.

 


Excellent and you are welcome msarqui

 


msarqui wrote:


I did not understand if your comment about the letter case of the target "word" is related to what I will tell you now. If the answer is yes, I'm sorry, but I noticed one small thing and, I want to say before all, it doesn't bothers me because I do not have this "small words" as a target. I think maybe this could be something to work for a future and more specific routine. So, here is the thing: if I type "and" for the target word, 1 for the color, and I have a word like "standard", this last will change to "standard". But, like I said, in my drawings I have only "larger words" and the routine changed all very consistently.
 


I meant, when you typed in BANANA  and the target "word" is "Banana" the original letter case will remain as Banana

 


......
((null (and (setq p (vl-string-search (strcase str) (strcase (setq s (vla-get-textstring obj))))) (setq str (substr s (1+ p) (strlen str))) ) )
)
......

 

 

As for "and" in standard, its as simple as removing (snvlaid str) from the code and replacing it with (/= str ""), then you can type " and " <--with whitespace

 

Also, i found out that Multi-line attributes can be assigned with colors  < Copy or Swap Text.>  Maybe one of these days i'll modify the code to do just that.

 

pBe

0 Likes
Message 28 of 37

msarqui
Collaborator
Collaborator

Its is perfect now.

Thanks a lot. I am having a fun time using it. 🙂

0 Likes
Message 29 of 37

Kent1Cooper
Consultant
Consultant

pbejse wrote:

...

As for "and" in standard, its as simple as removing (snvlaid str) from the code and replacing it with (/= str ""), then you can type " and " <--with whitespace

....


I can imagine a further complication....  The following situation may not be likely to occur with "and" as a potentially-nested word, but there are countless other possible nested words.  Imagine that the word you want is "wood" and some text might include the word "plywood," or "woodwork," in which you don't want to change the partial "wood" inside.  Typing in " wood " with surrounding spaces as the target word could miss it in certain instances, since it may not always have spaces on both sides of it, for example where it occurs followed immediately by a comma or period or right parenthesis, or preceded by a left parenthesis.  Maybe it's nothing for the OP to be concerned about, if they would usually be using the routine on longer and less-likely-to-be-nested words, but it remains a possibility.

Kent Cooper, AIA
0 Likes
Message 30 of 37

pbejse
Mentor
Mentor

@Kent1Cooper wrote:

I can imagine a further complication....  

                            ....., but it remains a possibility.


I guess the user had to be extra careful with the target words then. But you are right, there will be complications.

 

Very observant of you Kent, thank you for the info.

 

pBe

0 Likes
Message 31 of 37

Anonymous
Not applicable
Hi pbejse, thanks for the code.Is there any chance we could use your code with ARCALIGNEDTEXT objects though?
0 Likes
Message 32 of 37

Anonymous
Not applicable

Hello, I just found this program and was wondering why it might not work if the word you enter contains a Parentheses? such as: (BP-101).

thanks!

 

Paul

0 Likes
Message 33 of 37

justin.lockman
Observer
Observer

@pbejse 

 

Hello,

 

I realize that this is a 4-year old thread, but is the lisp routine here is the closest thing I've found to what I'm looking for.  Works great, but there one thing I need modified on it, and have no experience with LISP.

 

I'd like to be able to pick a true color.  I found the lisp variable "acad_truecolordlg" which I used to replace "acad_colordlg" which gives me the full color options, but when I select an RGB color, for instance 180,0,65 it throws an error, "; error: bad argument type: fixnump: ((62 . 242) (420 . 11796545))" that I have no idea what it means, nor how to resolve.

 

Message 34 of 37

billb
Observer
Observer

I know it's an old thread but this LISP program would be perfect if it included True Color.  I haven't been able to find one like it anywhere out there.

0 Likes
Message 35 of 37

Sea-Haven
Mentor
Mentor

I know its a old post but a solution to And & Standard is the simple (strlen str) dont need to worry about space or full stop and comma.

0 Likes
Message 36 of 37

Sea-Haven
Mentor
Mentor

When you look at this mtext with one word "HEADING" I have set to a rgb color 

 \c3590462;\fArial|b0|i0|c0|p34;HEADING

if it was say red color 1 

 \c1;\fArial|b0|i0|c0|p34;HEADING

 

So you need to look at this line in the code. 

(Strcat "\\C" (itoa col) ";" str)

To get the number for the rgb you will need the color routines by Lee-mac to convert the rgb to ID number. You may need rtos to make the number a string rather than itoa.

0 Likes
Message 37 of 37

X1JIANGWOO
Participant
Participant

If anyone is still around, how can I modify this lisp to search multileader text ? 

 

Thanks in advance. 

0 Likes