Jump to content

Select Red color mtext and text


gmmdinesh

Recommended Posts

Hi Everyone..Could anyone please tell me, how to select all red colored text and mtext from model space through lisp.

 

 

Thanks in advance.

Link to comment
Share on other sites

Hi All,

Thanks for your quick reply.. Need one more help..how can change selected text and mtext to BOLD.

Link to comment
Share on other sites

Made a quick search. Found something from Grand Master Lee Mac. Hope it helps.

 

;; Args:-
;; tStyle  ~  Valid TextStyle Name
;; flags [bit-coded]
;; 0  ~  Regular
;; 1  ~  Bold
;; 2  ~  Italic

(defun PutFontStyle (tStyle flags / TypeFace Bold Italic CharSet P&Fam)
 (vl-load-com) ;; Lee Mac  ~  19.02.10

 (if (not (vl-catch-all-error-p
            (setq tStyle
              (vl-catch-all-apply
                (function vla-Item)
                  (list (vla-get-TextStyles
                          (vla-get-ActiveDocument
                            (vlax-get-acad-object))) tStyle)))))
   (progn
     (vla-getFont tStyle 'TypeFace 'Bold 'Italic 'CharSet 'P&Fam)
     (vla-SetFont tStyle  TypeFace
       (if (= 1 (logand 1 flags)) :vlax-true :vlax-false)
       (if (= 2 (logand 2 flags)) :vlax-true :vlax-false) CharSet P&Fam) t)))

 

 

Example of Usage:

 

(PutFontStyle "myStyle" 3) ;  ~  Sets the font for 'MyStyle' textstyle to Bold & Italic.

Link to comment
Share on other sites

Hi tmelancon,

Thanks for your effort..I have tried this one..but in this code we have to select the object as manually..i need select all red colored text and mtext and change to BOLD.

Can you modify the lisp.

Link to comment
Share on other sites

Do a search here for modifying mtext there is some good coding examples of finding even a single word that is red and changing its colour etc. The coding uses direct modification of the hidden code in mtext. Bold is an item that can be added.

 

HEADING

TextString = "{\\fArial|b0|i0|c0|p34;HE\\fArial|b1|i0|c0|p34;ADI\\fArial|b0|i0|c0|p34;NG}"

Link to comment
Share on other sites

Only thing I found and edited a little bit is this. I am going to keep at it. I am just trying to select all RED, MTEXT, TEXT but in VL.

 

(defun c:mtval( / ent objmtext stroldval strnewval)
 (setq ent (car (entsel)) 
 objMText (vlax-ename->vla-object ent)
 strOldval (vlax-get-property objMText "TEXTSTRING")
 strnewval (STRCAT "{\\fArial|b1|i0|c0|p34;" stroldval)
 )
 (if strnewval (vlax-put-property objmtext "TEXTSTRING" strnewval)
 )
 (vlax-release-object objmtext)
)

Link to comment
Share on other sites

Be careful when adding these hard coded values .. you could make a real mess if you don't check for existing: "{\\fArial|b1|i0|c0|p34;{\\fArial|b1|i0|c0|p34;{\\fArial|b1|i0|c0|p34;{\\fArial|b1|i0|c0|p34;asdfsafsdfsaff"

 

Maybe use something like this:

(strcat "{\\fArial|b1|i0|c0|p34;"
		  (vl-string-left-trim
		    "{\\fArial|b1|i0|c0|p34;"
		    (vlax-get-property objmtext "TEXTSTRING")
		  )
	  )

Link to comment
Share on other sites

Wow see... perfect example of me being a beginner programmer and getting ahead of myself just because the result on the "surface" seems legit.. Thank you for that input I definitely need to discipline myself more to double-triple check everything when it comes to these routines. This code is so close to doing what the op is looking for. The problem I am having is when I edit it to Selected all MTEXT with color red I get an error "Error: bad argument type: VLA-OBJECT nil". I am sure its a crossover issue.

Link to comment
Share on other sites

Try this .. it filters out text that is currently bold so you don't have to check in the loop :)

(defun c:hcb (/ _ps->ss o ss)
 ;; Use a function like this to convert a selection set to a list of items
 (defun _ps->ss (ps)
   (if	(= 'pickset (type ps))
     (vl-remove-if 'listp (mapcar 'cadr (ssnamex ps)))
   )
 )
 (if (setq ss (_ps->ss (ssget ":L" '((0 . "mtext") (1 . "~{\\fArial|b1|i0|c0|p34;*")))))
   (foreach mtext ss
     (setq o (vlax-ename->vla-object mtext))
     (vla-put-textstring o (strcat "{\\fArial|b1|i0|c0|p34;" (vla-get-textstring o)))
   )
 )
 (princ)
)

Link to comment
Share on other sites

Wow yea look at that. Hope you dont mind but I took your code and modified it slightly to give the OP exactly what he/she was looking for:

 

(defun c:hcb (/ _ps->ss o ss)
 ;; Use a function like this to convert a selection set to a list of items
 (defun _ps->ss (ps)
   (if    (= 'pickset (type ps))
     (vl-remove-if 'listp (mapcar 'cadr (ssnamex ps)))
   )
 )
 (if (setq ss (_ps->ss (ssget [color=red]"X"[/color] '((0 . "mtext") (1 . "~{\\fArial|b1|i0|c0|p34;*")[color=red](62 . 1)[/color]))))
   (foreach mtext ss
     (setq o (vlax-ename->vla-object mtext))
     (vla-put-textstring o (strcat "{\\fArial|b1|i0|c0|p34;" (vla-get-textstring o)))
   )
 )
 (princ)
)

gmmdinesh, copy and paste this code into your main file. Run it and I think you will find it is exactly what you are looking for. Plus all the extra benefits Ron added. Hope you like.

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