Mtext Formatting In A Field

Mtext Formatting In A Field

Anonymous
Not applicable
2,361 Views
16 Replies
Message 1 of 17

Mtext Formatting In A Field

Anonymous
Not applicable

Hello!

 

I am hoping someone here can help with this problem.  I am trying to have some text that is stored in a variable output in a field that should be a different color from rest of the text.  I have looked high and low in multilple forums but have not found the exact the solution I need.  I have stubled upon the Mtext formatting codes, I have seen a few examples, but when I try to replicate it does not work for me.  I don't understand how they are to be used in a LISP routine.  I know how to change the color of the text the usual way, I would think there is a way to code it.  Below is a screen capture of what I am hoping to accomplish, currently all the text is blue:

 

Capture.PNG

 

This is the line of code that produces the top two lines:

 

(setq dwg (strcat "DWG. PER CUST E-MAIL:" (chr 10) (vl-filename-base (getvar "dwgname")) ".dxf" (chr 10) "DWG#: " dwg2))

 

I hope I have explained this well enough to be understood.

 

Thank you in advance for your time and attention!

 

Joe

0 Likes
Accepted solutions (1)
2,362 Views
16 Replies
Replies (16)
Message 2 of 17

rkmcswain
Mentor
Mentor

Are you asking how to set the colors of the partial strings inside the mtext block?

 

If so....

 

(vl-cmdf ".mtext" (getpoint "\nPick Point") "_W" 0.0
	 (strcat "\\C6;DWG. PER CUST E-MAIL:\\P80721354_-.DXF\\P\\C5;REV: NONE\\P1 1/2"
		 (CHR 34)
		 " 50 HSLA\\P19"
		 (CHR 34)
		 " B.EA\\P12.028# EA.\\PJK\\P9/25/15")
	 "")
R.K. McSwain     | CADpanacea | on twitter
0 Likes
Message 3 of 17

hmsilva
Mentor
Mentor

@Anonymous wrote:

Hello!

 

I am hoping someone here can help with this problem.  I am trying to have some text that is stored in a variable output in a field that should be a different color from rest of the text.  I have looked high and low in multilple forums but have not found the exact the solution I need.  I have stubled upon the Mtext formatting codes, I have seen a few examples, but when I try to replicate it does not work for me.  I don't understand how they are to be used in a LISP routine.  I know how to change the color of the text the usual way, I would think there is a way to code it.  Below is a screen capture of what I am hoping to accomplish, currently all the text is blue:

 

Capture.PNG

 

This is the line of code that produces the top two lines:

 

(setq dwg (strcat "DWG. PER CUST E-MAIL:" (chr 10) (vl-filename-base (getvar "dwgname")) ".dxf" (chr 10) "DWG#: " dwg2))

 

I hope I have explained this well enough to be understood.

 

Thank you in advance for your time and attention!

 

Joe


Hi Joe,

in addition to RK advices, and if the 'blue' is bylayer, you'll only have to override the color for color 6...

ex.

(setq dwg (strcat "{\\C6;DWG. PER CUST E-MAIL:" "\\P" (vl-filename-base (getvar "dwgname")) ".dxf}" (chr 10) dwg2 ))

sorry but I did remove the "DWG#: "....

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 4 of 17

Anonymous
Not applicable
Thank you for responding rk! No, I am just needing the top two lines changed. I should've included the whole routine so that you could see what is generating the text.

Thanks again!
0 Likes
Message 5 of 17

Anonymous
Not applicable
Thank you hm! Yes, the blue is setup as bylayer.

I will give these ideas a try and let you and rk know if it worked or not.

Thank you both, again!
0 Likes
Message 6 of 17

Anonymous
Not applicable

hmsilva,

 

Sorry to say the code didn't work.  Below is a screenshot of the output:

 

Capture.PNG

 

Here is the code:

(setq dwg (strcat "{\\C6;DWG. PER CUST E-MAIL:" "\\P" (vl-filename-base (getvar "dwgname")) ".dxf}" "\\P" "DWG#: " dwg2))

 

It seems the mtext formatting code is being ignored.  Thank you for trying.  Do you have any ideas as to why the mtext formatting is being ignored?

 

Joe  

0 Likes
Message 7 of 17

Anonymous
Not applicable

rkmcswain,

 

Your code worked when I tried it as it's own program.  When I tried to incorporate it into my code, it worked the same as Henrique's.  I will attach the entire routine I am using.  There are only 4 other lines that would need to have the mtext formatting.  I am certain as you read it, there will be some wincing.  It doesn't look very good, but it does give me the result's I need.

 

Thank you again!

 

Joe

0 Likes
Message 8 of 17

hmsilva
Mentor
Mentor

@Anonymous wrote:

hmsilva,

 

Sorry to say the code didn't work.  Below is a screenshot of the output:

...

It seems the mtext formatting code is being ignored.  Thank you for trying.  Do you have any ideas as to why the mtext formatting is being ignored?


Hi Joe,

unfortunately, it seems that 'fields' evaluates the text string as a 'text' string and ignores the 'mtext' formatting codes...

Internal MText editor, also ignores the 'mtext' formatting codes, if we enter formatting codes the editor evalutes as text characters, not formatting codes, if we define an external text editor, ie. notepad, the the formatting codes are evaluated as formatting codes in MText, but not in fields...

 

Sorry, but I can´t find a way to format text inside a field... 😞

 

Henrique

EESignature

0 Likes
Message 9 of 17

Anonymous
Not applicable
Thank you Henrique! I appreciate your assistance so far.

I was thinking along the same lines at the end of the day yesterday. As I was doing some searching yesterday, I found a few ideas that have to do with converting the fields to text. It seems to me after that it would be possible to apply the mtext formatting...Would you have any ideas is this direction?
0 Likes
Message 10 of 17

hmsilva
Mentor
Mentor

@Anonymous wrote:
Thank you Henrique! I appreciate your assistance so far.

I was thinking along the same lines at the end of the day yesterday. As I was doing some searching yesterday, I found a few ideas that have to do with converting the fields to text. It seems to me after that it would be possible to apply the mtext formatting...Would you have any ideas is this direction?

Hi Joe,

this thread may be useful...

 

Hope this helps,
Henrique

 

EESignature

0 Likes
Message 11 of 17

Anonymous
Not applicable
Hi Henrique,

Thank you for the link! I think I am seeing a connection between what I am trying to do and the code in the thread. I believe it will be useful, as soon as I can try something out. I'll let you what I come up with.

Thanks again!

Joe
0 Likes
Message 12 of 17

Anonymous
Not applicable
Henrique,

I tried the code from the link and it does work, but, it converts all the fields. I only need it to convert 1 field. I don't know if it makes a difference, the fields I have are all custom defined fields.

Thank you again!

Joe
0 Likes
Message 13 of 17

hmsilva
Mentor
Mentor

@Anonymous wrote:
Henrique,

I tried the code from the link and it does work, but, it converts all the fields. I only need it to convert 1 field. I don't know if it makes a difference, the fields I have are all custom defined fields.

Thank you again!

Joe

Joe,

to convert only the field we need to convert, we have fo provide to the 'ssfield' function, a test sting not present in other fields, the more complete, better.

If the field you need to convert it's the only field using the dwg variable,

(ssfield "Lisp.dwg>%")

should do the trick...

 

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 14 of 17

Anonymous
Not applicable

Henrique,

 

I tried it as you had it and several other ways and none of the fields were converted.  Maybe it would help if you could see what the other fieldcodes actually are.  I am attaching a drawing to see if that might help so that you can see in context the other fields.

 

Thanks again, alot!

 

Joe 

0 Likes
Message 15 of 17

hmsilva
Mentor
Mentor
Accepted solution

Joe,

the following quick and dirty 'demo' will search for an MText entity, with a field, having "CustomDP.DwgNum" in the 'FIELDCODE', will remove the fields, search for "REV:", and format the Mtext string with color 6 all characters until "REV:"...

 

(defun c:demo (/ ssfield hnd i obj pre pos suf ss str)
   ;; http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/convert-field-to-text/td-p/1722015/page/2
   ;; by Ian_Bryant
   (defun del-field (ent / edic elist etype obj val)
      (if
         (and
            (setq edic (cdr (assoc 360 (setq elist (entget ent)))))
            (dictsearch edic "ACAD_FIELD")
         )
           (progn
              (setq obj   (vlax-ename->vla-object ent)
                    etype (cdr (assoc 0 elist))
              )
              (cond
                 ((= etype "MTEXT")
                  (setq val (vla-get-textstring obj))
                  (dictremove edic "ACAD_FIELD")
                  (vla-put-textstring obj val)
                 )
                 (T (dictremove edic "ACAD_FIELD"))
              )
           )
      )
   )
   ;; by me
   (defun ssfield (str / hnd i obj ss ss1)
      (if (setq ss (ssget "_X" '((0 . "MTEXT"))))
         (progn
            (setq ss1 (ssadd))
            (repeat (setq i (sslength ss))
               (setq hnd (ssname ss (setq i (1- i)))
                     obj (vlax-ename->vla-object hnd)
               )
               (if (and (vlax-method-applicable-p obj 'FIELDCODE)
                        (wcmatch (vlax-invoke-method obj 'FIELDCODE) (strcat "*" str "*"))
                   )
                  (ssadd hnd ss1)
               )
            )
            ss1
         )
      )
   )
   (if (setq ss (ssfield "CustomDP.DwgNum"))
      (repeat (setq i (sslength ss))
         (setq hnd (ssname ss (setq i (1- i)))
               obj (vlax-ename->vla-object hnd)
         )
         (if (vlax-write-enabled-p obj)
            (progn
               (del-field hnd)
               (setq str (vla-get-textstring obj))
               (if (and (setq pos (vl-string-search "REV:" str))
                        (setq pre (substr str 1 pos))
                        (setq suf (substr str (1+ pos)))
                   )
                  (vla-put-textstring obj (strcat "{\\C6;" pre "}" suf))
               )
            )
         )
      )
   )
   (vla-regen (vla-get-activedocument (vlax-get-acad-object)) acallviewports)
   (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 16 of 17

Anonymous
Not applicable
Henrique,

This now works as expected!

Thank you so much!

Joe
0 Likes
Message 17 of 17

hmsilva
Mentor
Mentor

@Anonymous wrote:
Henrique,

This now works as expected!

Thank you so much!

Joe

You're welcome, Joe
Glad I could help

Henrique

EESignature

0 Likes