Jump to content

Change MText Width (after width has been changed))


Recommended Posts

Posted

I found some code by kent1cooper here

 

(defun C:MTW ; = MText Width
  (/ ss wf n mt)
  (if
    (and
      (setq ss (ssget "_:L" '((0 . "MTEXT"))))
      (setq wf (getreal "\nWidth Factor to apply: "))
    ); and
    (repeat (setq n (sslength ss)); then
      (setq mt (ssname ss (setq n (1- n))))
      (setpropertyvalue mt "Contents"
        (strcat "{\\W" (rtos wf 2) ";" (getpropertyvalue mt "Contents") "}")
      ); setpropertyvalue
    ); repeat
  ); if
  (princ)
); defun

 

This works great.... BUT once the width has been changed it no longer works.

image.thumb.png.a2829b80960ee1710230e9419d7702e9.png

Is it a simple fix to change the width after it has been changed? Ideally select multiple and not have to pick the dropdown to change.

image.png.5176b39386db29c19b65c78ba9bd27d5.png

 

If you have a style that has a defined width, the routine works until you change it to something else.

If you run STRIPMTEXT on a string, it resets something so the change text code works again.

 

 

 

 

Posted (edited)

The code is adding a width string to the text. These characters are hidden but will show up if you click on the text and look at it in the properties menu, or via the LISP line:

(assoc 1 (entget(car(entsel))))

 

In the code it adds {\\W'wf'; to the string where wf is the width factor

 

Something like

{\\W0.8;Here is a text string}

If you run the code again, it will add another width factor to the text string... but won't remove the first, so you get something like this:

{\\W0.5;{\\W0.8;Here is a text string}} - noting that the new text width is applied and then the first one is applied.. result is you only see what was applied first. Need to remove this formatting to the text string before applying a new one.

 

You might also have issues if other text formatting is used, such as underlined, italics, bold...

 

For width you might be able to amend Lee Macs Unformat LISP to just unformat the width portion:

https://lee-mac.com/unformatstring.html

In these text strings replace with just 'W'

ACcFfHLlOopQTW

 

and here 

ACcFfHLlOoPpQSTW

change to SW

 

... and then apply the new width formatting from your LISP

 

.. though CAD is off so not tested - all part of the fun for you!

 

 

 

...if Lee Mac is passing through, feel free to correct me, though many years later I am just about understanding some of your codes.

Edited by Steven P
Posted

This should achieve the desired result without overriding nested width formatting -

(defun c:mtw ( / enx idx sel str wid )
    (initget 6)
    (cond
        (   (not (setq wid (getreal "\nSpecify new width factor: "))))
        (   (setq sel (ssget "_:L" '((0 . "MTEXT"))))
            (repeat (setq idx (sslength sel))
                (setq idx (1- idx)
                      enx (entget (ssname sel idx))
                      str (assoc 1 enx)
                )
                (entmod (subst (cons 1 (addupdatewidth (cdr str) wid)) str enx))
            )
        )
    )
    (princ)
)

(defun addupdatewidth ( str wid / ps1 ps2 ps3 )
    (cond
        (   (= "" str) str)
        (   (and (setq ps1 (vl-string-search "{\\W" str))
                 (setq ps2 (vl-string-search ";" str ps1))
                 (setq ps3 (vl-string-search "}" str ps2))
            )
            (strcat
                (addupdatewidth (substr str 1 ps1) wid)
                (substr str (1+ ps1) 3)
                (rtos wid 2)
                (substr str (1+ ps2) (- ps3 ps2 -1))
                (addupdatewidth (substr str (+ ps3 2)) wid)
            )
        )
        (   (strcat "{\\W" (rtos wid 2) ";" str "}"))
    )
)

(princ)

 

Posted

Your code modified

(defun C:MTW ; = MText Width
(/ ss wf n mt val_text start end)
  (if
    (and
      (setq ss (ssget "_:L" '((0 . "MTEXT"))))
      (setq wf (getreal "\nWidth Factor to apply: "))
    ); and
    (repeat (setq n (sslength ss)); then
      (setq mt (ssname ss (setq n (1- n))))
      (setq val_text (getpropertyvalue mt "Contents"))
      (cond
        ((vl-string-search "{" val_text)
          (cond
            ((vl-string-search "\\W" val_text)
              (setq
                start (vl-string-search "\\W" val_text)
                end (vl-string-search ";" val_text 1)
              )
              (repeat (1+ (- end start))
                (setq val_text (vl-string-subst "" (chr (vl-string-elt val_text start)) val_text start))
              )
              (setpropertyvalue mt "Contents" val_text)
            )
            (T
              (setpropertyvalue
                mt
                "Contents"
                (strcat
                  "{\\W" (rtos wf 2)
                  ";"
                  (vl-string-left-trim "{" val_text)
                )
              )
            )
          )
        )
        (T
          (setpropertyvalue mt "Contents"
            (strcat "{\\W" (rtos wf 2) ";" (getpropertyvalue mt "Contents") "}")
          ); setpropertyvalue
        )
      )
    ); repeat
  ); if
  (princ)
)

 

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