Jump to content

Lisp routine to stretch Mtext so all is on one line


Recommended Posts

Posted

I ran a batch program that searched and replaced some text on a titleblock for our current client. The program successfully found and replaced the text, but we have one bit of text that is Mtext and it is now on two lines. I need to have it on one line. I could manually select and change it, but we have 200 files that need the same change. Does anyone have a Lisp routine to stretch Mtext so all is on one line? Thanks.

Posted

The following code will set the MText width to zero, forcing the text on one line:

 

(defun c:OneLineMText ( / i s )
   (if (setq s (ssget "_X" '((0 . "MTEXT"))))
       (repeat (setq i (sslength s))
           (vla-put-width (vlax-ename->vla-object (ssname s (setq i (1- i)))) 0.0)
       )
   )
   (princ)
)
(vl-load-com) (princ)

 

It is currently set to alter ALL MText in a drawing - to alter specific MText objects you will need to add extra filters to the ssget filter list. You didn't specify any unique characteristics of your MText, so I couldn't filter for anything.

Posted

Thanks for the reply. The line reads "WEST JEFFERSON, OH". That's all I want changed. Is there any way to isolate that text? Thanks.

Posted

On the assumption that your MText contains no formatting applied from within the editor, :

 

(defun c:OneLineMText ( / i s )
   (if (setq s (ssget "_X" '((0 . "MTEXT") (1 . "WEST JEFFERSON, OH"))))
       (repeat (setq i (sslength s))
           (vla-put-width (vlax-ename->vla-object (ssname s (setq i (1- i)))) 0.0)
       )
   )
   (princ)
)
(vl-load-com) (princ)

Posted

I tried the first routine and it worked great. However, the second does not seem to do anything. I'm afraid to run the 1st on all of the files in case there is more than one instance of Mtext. Is there any more info you need from me to get the second one working? Thanks.

Posted

Not doing anything means that no MText matching the filter has been found; does the MText have formatting applied through the MText editor? Does the string in the filter exactly match the MText content?

 

Or, you can filter for the MText using a different property, such as its Layer, for example.

Posted

Thanks. As you can see from the formatting codes in the Contents property, formatting has been applied to the MText throught the MText editor.

 

From here you have two options:

 

1. Change the filter to:

 

(defun c:OneLineMText ( / i s )
   (if (setq s (ssget "_X" '((0 . "MTEXT") (1 . "*WEST JEFFERSON, OH*"))))
       (repeat (setq i (sslength s))
           (vla-put-width (vlax-ename->vla-object (ssname s (setq i (1- i)))) 0.0)
       )
   )
   (princ)
)
(vl-load-com) (princ)

This will change all MText containing the words "WEST JEFFERSON, OH"

 

2. Include the formatting codes in the filter:

 

(defun c:OneLineMText ( / i s )
   (if (setq s (ssget "_X" '((0 . "MTEXT") (1 . "\\fArial|b0|i0|c0|p34|;WEST JEFFERSON, OH"))))
       (repeat (setq i (sslength s))
           (vla-put-width (vlax-ename->vla-object (ssname s (setq i (1- i)))) 0.0)
       )
   )
   (princ)
)
(vl-load-com) (princ)

This assumes the same formatting has been applied to the other instances of this text.

Posted

Thanks, that worked great! Appreciate the help.

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