UNDERLINE MULTIPLE MTEXT ENTITIES

UNDERLINE MULTIPLE MTEXT ENTITIES

cleftwich
Enthusiast Enthusiast
13,655 Views
23 Replies
Message 1 of 24

UNDERLINE MULTIPLE MTEXT ENTITIES

cleftwich
Enthusiast
Enthusiast

Does anyone know how to underline multiple mtext entities at one time through a lisp or other means. Currently I use an archaic and time consuming way of completing this task, any help would be appreciated greatly,

 

Thanks,

Charles

0 Likes
Accepted solutions (2)
13,656 Views
23 Replies
Replies (23)
Message 2 of 24

pendean
Community Legend
Community Legend
Accepted solution
LISP customization forum is over here (as long as you are not running the LT version of AutoCAD) https://forums.autodesk.com/t5/autocad-customization/ct-p/AutoCADTopic1

0 Likes
Message 3 of 24

Kent1Cooper
Consultant
Consultant
Accepted solution

I assume you mean underlining the entire content.  Try this quickie:

(defun C:UMT (/ ss n edata); = Underline MText(s)
  (if (setq ss (ssget "_:L" '((0 . "MTEXT"))))
    (repeat (setq n (sslength ss)); then
      (setq edata (entget (ssname ss (setq n (1- n)))))
      (entmod
        (subst
          (cons 1 (strcat "{\\L" (cdr (assoc 1 edata))))
          (assoc 1 edata)
          edata
        ); subst
      ); entmod
    ); repeat
  ); if
  (princ)
); defun

It's possible that if a given Mtext object has other internal formatting at the beginning, something could be thrown off.  It could be made to check whether there's already underlining code at the beginning before applying it, but in a quick test, having two of them doesn't seem to bother it.  Nor does not  having the  }  at the end that manual underlining supplies, but that can be added:

  (cons 1 (strcat "{\\L" (cdr (assoc 1 edata)) "}"))

Kent Cooper, AIA
Message 4 of 24

cleftwich
Enthusiast
Enthusiast

Exactly what I was looking for thanks so much.

0 Likes
Message 5 of 24

CAD7875
Participant
Participant

life saver!!!! thanks😁

0 Likes
Message 6 of 24

cleftwich
Enthusiast
Enthusiast

Thank you works perfectly

0 Likes
Message 7 of 24

carl.lowrey
Explorer
Explorer

Thanks Kent. Great to find these little gems in the archives when you need them quickly.

0 Likes
Message 8 of 24

chillme1
Advocate
Advocate

Placing the lisp file within my SFSP, I then tried loading UMT (load "UMT") and also (load "UMT.lsp") with no success:

 

Examples:
Command: (load "UMT")
; error: LOAD failed: "UMT"
Command: (load "UMT.lsp")
; error: LOAD failed: "UMT.lsp"

 

It is probably something basic but not sure exactly what at this point.

 

<red-faced from embarrassment but willing to ask and learn>

Thanks for your comments,

Clint Hill
0 Likes
Message 9 of 24

chillme1
Advocate
Advocate

No sooner than I post do I (think) I see the anomaly.

Thanks for your comments,

Clint Hill
0 Likes
Message 10 of 24

chillme1
Advocate
Advocate

The error from my copy/paste attempt is: error: extra right paren on input

Thanks for your comments,

Clint Hill
0 Likes
Message 11 of 24

Kent1Cooper
Consultant
Consultant

It works for me.  In case something went wacky in your copy/pasting, try the attached.  [And use the file name -- (load "UnderlineMText") -- not just the command name; change the file name if you prefer.]

Kent Cooper, AIA
Message 12 of 24

chillme1
Advocate
Advocate

Kent, You have my sincere thanks in taking time to contribute. I look forward to using your code as a learning tool as well.

Thanks for your comments,

Clint Hill
0 Likes
Message 13 of 24

leandro_miya
Explorer
Explorer

Hi guys,

 

Is it possible to change the code for simple Text? If so, what changes should I make?

 

Sorry to bother, but I don't know lisp yet.

 

Thanks!

Message 14 of 24

Kent1Cooper
Consultant
Consultant

@leandro_miya wrote:

.... Is it possible to change the code for simple Text? If so, what changes should I make? ....


Try this modification -- the red parts are all that I changed [untested]:

(defun C:UT (/ ss n edata); = Underline Text(s)
  (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
    (repeat (setq n (sslength ss)); then
      (setq edata (entget (ssname ss (setq n (1- n)))))
      (entmod
        (subst
          (cons 1 (strcat "%%U" (cdr (assoc 1 edata))))
          (assoc 1 edata)
          edata
        ); subst
      ); entmod
    ); repeat
  ); if
  (princ)
); defun

If any selected Text object has its entire contents already underlined by the same %%U entry at the beginning, the above will un-underline it.  It could be made to check for that, if desired, and not add another %%U.

Kent Cooper, AIA
0 Likes
Message 15 of 24

sam_underdown
Observer
Observer

@Kent1Cooper Is it possible to create a similar lisp routine where it removes the underline, rather than applying it? I'm still learning about lisp routines, apologies if you have answered this query elsewhere 

0 Likes
Message 16 of 24

Kent1Cooper
Consultant
Consultant

@sam_underdown wrote:

.... Is it possible to create a similar lisp routine where it removes the underline, rather than applying it? .... 


Quickly, with minimal testing:

(defun C:UUT (/ ss n edata); = Un-Underline Text(s)
  ;; to remove all underlining from Text objects
  (if (setq ss (ssget "_:L" '((0 . "TEXT"))))
    (repeat (setq n (sslength ss)); then
      (setq
        edata (entget (ssname ss (setq n (1- n))))
        txt (cdr (assoc 1 edata))
      ); setq
      (while (wcmatch txt "*%%U*")
        (setq txt (vl-string-subst "" "%%U" txt))
      ); while
      (entmod (subst (cons 1 txt) (assoc 1 edata) edata))
    ); repeat
  ); if
  (princ)
); defun

(defun C:UUMT (/ ss n edata); = Un-Underline MText(s)
  ;; to remove underlining from Mtext whose ENTIRE content is underlined
  (if (setq ss (ssget "_:L" '((0 . "MTEXT") (1 . "{\\L*"))))
    (repeat (setq n (sslength ss)); then
      (setq
        edata (entget (ssname ss (setq n (1- n))))
        txt (cdr (assoc 1 edata))
      ); setq
      (entmod
        (subst
          (cons 1 (substr txt 4 (- (strlen txt) 4)))
          (assoc 1 edata)
          edata
        ); subst
      ); entmod
    ); repeat
  ); if
  (princ)
); defun

But NOTE the description for UUMT, that it's for Mtext whose entire content is underlined [just as the earlier routines were built to apply underlining only to entire content].  It depends on its content starting with "{\\L"  and ending with "}".  If it starts with underlined content but the underlining doesn't go under all of it, it will lose the last "real" character in place of losing the "}", because it just takes off the last character, whatever that is.

 

Part of "minimal testing" is that I looked at combinations of underline and color formatting, and found that if both are applied from the beginning of the content, the underlining format code always seems to come before the color code, no matter in which order they were applied, so it seems to appropriately take off the underlining code but leave the color code.  But if the underlining doesn't go all the way, odd stuff is left in the text string at the point where it ended, though the visible object looks right.  I did not try other kinds of formatting [e.g. tracking, width factor, obliquing] for their formatting-code relationships to underlining.

 

In the case of plain Text, UUT doesn't care whether the entire content is underlined -- it removes all underline-code content, starting and stopping, wherever and however many there are.

Kent Cooper, AIA
Message 17 of 24

sam_underdown
Observer
Observer

@Kent1Cooper thank you for your quick response. I have created the lisp routine using the coding provided; the command loads as normal but when i highlight/click the group of mtext that needs to be amended, it states nothing has been selected, any idea why? i have attached the lisp routine file below if you want to check it 

 

Thanks for your assistance 

0 Likes
Message 18 of 24

Kent1Cooper
Consultant
Consultant

@sam_underdown wrote:

... when i highlight/click the group of mtext that needs to be amended, it states nothing has been selected.... 


Locked Layer(s)?  Mtext that doesn't have the underlining code right at the start, including something else like obliquing from the start if its code element gets in ahead of the underlining code?  You used the UUT command name but picked Mtext objects?  [I wouldn't name the file the way you did, since it also includes one for plain Text.]

Kent Cooper, AIA
0 Likes
Message 19 of 24

sam_underdown
Observer
Observer

All layers are unlocked, the level is displayed as +100.00

 

I have removed the + sign and the command works, the + sign was causing me the problem! 

 

Thank you 

0 Likes
Message 20 of 24

Kent1Cooper
Consultant
Consultant

@sam_underdown wrote:

.... the level is displayed as +100.00

I have removed the + sign and the command works, the + sign was causing me the problem! 

....


There must be something else going on.  When I make Mtext just like that, UUMT works to remove the underlining.  Do this at the Command line:

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

and select an Mtext object that UUMT doesn't work on [i.e. that is not recognized in UUMT's selection].  What does it return?  The one I made returns this:

"{\\L+100.0}"

and I suspect yours will have some other complication making it not fit the selection filter criteria.  [Like maybe, does the underlining actually start after the + sign?]

Kent Cooper, AIA
0 Likes