Mtext string formatting width

Mtext string formatting width

msarqui
Collaborator Collaborator
3,472 Views
14 Replies
Message 1 of 15

Mtext string formatting width

msarqui
Collaborator
Collaborator

Hi guys,

 

I have been searching for this for a long time but it seams to have no conclusive answers in the CAD forums.

So, could someone please tel me if exist, by any chances, a way to change the width of a Mtext, by LISP?

I am not talking about the Box Width, but the width of the string inside a Mtext Box. Like in the picture bellow, when we edit a Mtext.

Capture.JPG

p.s. I do not want to remove formatting, I want to modify the value.

Many thanks!

Marcelo

0 Likes
Accepted solutions (2)
3,473 Views
14 Replies
Replies (14)
Message 2 of 15

Kent1Cooper
Consultant
Consultant

If you want to apply it to the entire string, and you have a fixed width factor you want, try this:

 

(vla-put-TextString
(setq obj
(vlax-ename->vla-object
(car (entsel "\nSelect Mtext to set to width factor of 1.23456: "))
)
)
(strcat "{\\W1.2346;" (vla-get-TextString obj) "}"); use your desired width factor
)

 

 

It could be made to ask the User for the width factor.

 

I haven't tried what happens if you use it on something that already has some override width factor [either on the entire string or part of it] applied.

Kent Cooper, AIA
0 Likes
Message 3 of 15

msarqui
Collaborator
Collaborator

@Kent1Cooper wrote:

 

 

It could be made to ask the User for the width factor.

 


Hi Kent,

Thanks for the answer.

So, to ask the User for the width factor, I need to put the value into a variable?

How to do this, please?

 

0 Likes
Message 4 of 15

msarqui
Collaborator
Collaborator
Hi Kent,
I used your code and it did not work. The width factor of the Mtext did not change.
0 Likes
Message 5 of 15

msarqui
Collaborator
Collaborator

@Kent1Cooper wrote:

 

 

I haven't tried what happens if you use it on something that already has some override width factor [either on the entire string or part of it] applied.


Sorry Kent, it worked. But as you said, only in Mtext that does not have override width factor.

So, can be made to work in one that already has?

And, can be made to ask for the width?

 

Many thanks!

0 Likes
Message 6 of 15

Kent1Cooper
Consultant
Consultant
Accepted solution

@msarqui wrote:
.... it worked. But as you said, only in Mtext that does not have override width factor.

So, can be made to work in one that already has?

And, can be made to ask for the width?

 

....

Search for a routine called StripMtext to remove any already-existing width override(s).

 

To have it ask the User for the width [once you've removed any other width override(s)], something like this [lightly tested]:

(setq wid (getdist "\nWidth factor to apply to Mtext: "))
(vla-put-TextString
(setq obj
(vlax-ename->vla-object
(car (entsel "\nSelect Mtext to which to apply width factor: "))
)
)
(strcat "{\\W" (rtos wid 2 8) ";" (vla-get-TextString obj) "}")
)
Kent Cooper, AIA
0 Likes
Message 7 of 15

msarqui
Collaborator
Collaborator
Perfect solution Kent!
Many thanks!
0 Likes
Message 8 of 15

ВeekeeCZ
Consultant
Consultant
Accepted solution

Hi @msarqui,

since I've done a color setting in similar thread, here is a small adjustment for Mtext Width.

It set a width no matter if was some before.

 

Spoiler
(vl-load-com)

(defun c:MTextWidth ( / wid ss obj txt x y)
  
  (if (and (setq wid (getreal "\nWidth: "))
           (setq ss (ssget '((0 . "MTEXT"))))
           )
    (repeat (setq i (sslength ss))
      (setq obj (vlax-ename->vla-object (ssname ss (setq i (1- i))))
            txt (vla-get-TextString obj)
            txt (vl-string-trim "{}" txt))
      (while (and (setq x (vl-string-search "\\W" txt))
                  (setq y (vl-string-search ";" txt x)))
        (setq txt (strcat (substr txt 1 x)
                          (substr txt (+ 2 y)))))
      (vla-put-TextString obj (strcat "{\\W" (rtos wid 2 8) ";" txt "}"))))
  (princ)
)
0 Likes
Message 9 of 15

msarqui
Collaborator
Collaborator
Nice one Bee!
Thanks!
0 Likes
Message 10 of 15

john.uhden
Mentor
Mentor

I have the UNFORMAT code that Steve Doman and Joe Burke originally used in STRIPMTEXT.  I could pull out the code to change just the width factor, if needed.

John F. Uhden

0 Likes
Message 11 of 15

brody.tears
Enthusiast
Enthusiast

i know this post was made in 2016 so im hoping to get a response.
however this is exactly what i am looking for.  i would try to disect the STRIPMTEXT code my self but i just dont know enough about lisp to attempt it.

 

what i need is to automatically select all mtext in a drawing and remove any width factor modifications while still retaining all other modifications.

here is the kicker i need the raw code section no defun so i can add it in to my existing lisp im working on and then run it through accoreconsole (cant appload or defun in accoreconsole)

0 Likes
Message 12 of 15

john.uhden
Mentor
Mentor

@brody.tears 

This might help.

It's the code I gave to Steve Doman and Joe Burke for their initial version of StripMtext back in 2003.   They later switched to the use of RegularExpressions, which is beyond me.

Focus on the next to last condition where you see "\\ACFHQSTW."

Maybe all you need for the width is "\\W."

I know I made a newer version of the function to accept the formatting codes to remove as an argument, but since I found this much I decided to search no further.  But you can let me know if you might want me to find it.

  (defun @UnFormat (Mtext KeepLF / Old New Tmp Str)
    (cond
      ((= (type Mtext) 'VLA-Object))
      ((= (type Mtext) 'Ename)
        (setq Mtext (vlax-ename->vla-object Mtext))
      )
      (1 (setq Mtext nil))
    )
    (if (= KeepLF 0)(setq KeepLF nil))
    (and
      Mtext
      (= (vlax-get Mtext 'ObjectName) "AcDbMText")
      (setq Old (vlax-get Mtext 'TextString))
      (setq Tmp Old)
      (setq New "")
      (while (/= Tmp "")
        (cond
          ((wcmatch (strcase (setq Str (substr Tmp 1 2))) "\\[\\{}`~]")
            (setq Tmp (substr Tmp 3)
                  New   (strcat New Str)
            )
          )
          ((wcmatch (substr Tmp 1 1) "[{}]")
            (setq Tmp (substr Tmp 2))
          )
          ((and KeepLF (wcmatch (strcase (substr Tmp 1 2)) "\\P"))
            (setq New (strcat New (substr Tmp 1 2))
                  Tmp (substr Tmp 3)
            )
          )
          ;; added "\n" (03-03-08)
          ((and KeepLF (wcmatch (strcase (substr Tmp 1 1)) "\N"))
            (setq New (strcat New (substr Tmp 1 1))
                  Tmp (substr Tmp 2)
            )
          )
          ((wcmatch (strcase (substr Tmp 1 2)) "\\[LOP]")
            (setq Tmp (substr Tmp 3))
          )
          ((wcmatch (strcase (substr Tmp 1 2)) "\\[ACFHQSTW]")
            (setq Tmp (substr Tmp (+ 2 (vl-string-search ";" Tmp))))
          )
          (1
            (setq New (strcat New (substr Tmp 1 1))
                  Tmp (substr Tmp 2)
            )
          )
        )
      )
      (/= Old New)
      (not
        (vl-catch-all-error-p
          (vl-catch-all-apply
            'vlax-put (list Mtext 'TextString New)
          )
        )
      )
    )
  )

 

John F. Uhden

0 Likes
Message 13 of 15

brody.tears
Enthusiast
Enthusiast
First of all thank you for replying to this old thread.
and second I'm sorry but i am super new to lisp and I'm basically just bashing together found code segments to achieve what i need it to do. so i am not super good a just reading raw code and understanding what it dose.

But if i understand what ur saying i can simply change the line of code
((wcmatch (strcase (substr Tmp 1 2)) "\\[ACFHQSTW]")
to
((wcmatch (strcase (substr Tmp 1 2)) "\\[W]")
and it will select all mtext in a dwg and remove just the Width formting?
0 Likes
Message 14 of 15

john.uhden
Mentor
Mentor
@brody.tears,
Um, no. What I gave you is just the unformatting function.
How about if I provide you another function that selects all the mtext
(only in ModelSpace I presume) and then applies the unformat function to
all the mtext entities?
I will not be including dealing with locked layers, so that's up to you,
okay?

John F. Uhden

0 Likes
Message 15 of 15

brody.tears
Enthusiast
Enthusiast
that would be perfect thank you.
Correct only in the model space and Yes it will need to be applied to all MTEXT
no worries i can handle unlocking and then relocking the layers just fine
there is also no need to define it as a command as it will always be ran in the lisp i am building so i just need the segment that selects all mtext and removes the width formating.
0 Likes