Jump to content

Recommended Posts

Posted

Does anyone know a lisp which resizes an mtext entity to the width of the text it contains?

Posted

Easy fix would be to just set it to zero :P

 

(defun c:mwid (/ ent)
 (if (and (setq ent (car (entsel "\nSelect MTEXT: ")))
          (eq "MTEXT" (cdadr (entget ent))))
     (vla-put-width (vlax-ename->vla-object ent) 0))
 (princ))

Posted

Or, if you wanted it exactly (or almost exact):

 

(defun c:mwid (/ ent Obj tBox)
 (while (and (setq ent (car (entsel "\nSelect MTEXT: ")))
             (eq "MTEXT" (cdadr (entget ent))))
   (setq Obj (vlax-ename->vla-object ent)
         tBox (textbox (list (cons 1 (strcat (chr 46) (vla-get-TextString Obj))))))
   (vla-put-width Obj (- (caadr tBox) (caar tBox))))
 (princ))

 

^^ Only for single lined MTEXT

Posted
Easy fix would be to just set it to zero :P

 

(defun c:mwid (/ ent)
 (if (and (setq ent (car (entsel "\nSelect MTEXT: ")))
          (eq "MTEXT" (cdadr (entget ent))))
     (vla-put-width (vlax-ename->vla-object ent) 0))
 (princ))

 

Gives the following "error: no function definition: VLAX-ENAME->VLA-OBJECT".

 

I need it to be able to handle multiple line MTEXT as well.

Posted
Gives the following "error: no function definition: VLAX-ENAME->VLA-OBJECT".

 

I need it to be able to handle multiple line MTEXT as well.

 

Sorry, didn't add (vl-load-com), in the LISP to load the Visual LISP functions.

 

I dont realise that I've forgotten it as I include it in my ACADDOC.lsp.

 

Apologies:

 


(defun c:mwid (/ ent)
 (vl-load-com)
 (if (and (setq ent (car (entsel "\nSelect MTEXT: ")))
          (eq "MTEXT" (cdadr (entget ent))))
     (vla-put-width (vlax-ename->vla-object ent) 0))
 (princ))

Posted
(defun c:mwid (/ ent Obj tBox)
 (vl-load-com)
 (while (and (setq ent (car (entsel "\nSelect MTEXT: ")))
             (eq "MTEXT" (cdadr (entget ent))))
   (setq Obj (vlax-ename->vla-object ent)
         tBox (textbox (list (cons 1 (strcat (chr 46) (vla-get-TextString Obj))))))
   (vla-put-width Obj (- (caadr tBox) (caar tBox))))
 (princ))

Posted

Or, as an alternative selection method:

 

(defun c:mwid (/ ss)
 (vl-load-com)
 (if (setq ss (ssget '((0 . "MTEXT"))))
   (foreach x (mapcar 'vlax-ename->vla-object
                      (vl-remove-if 'listp
                        (mapcar 'cadr (ssnamex ss))))
     (vla-put-width x 0)))
 (princ))

 

Or in VL:

 

(defun c:mwid  (/ ss)
 (vl-load-com)
 (if (setq ss (ssget '((0 . "MTEXT"))))
   (progn
     (vlax-for Obj
       (setq ss (vla-get-ActiveSelectionSet
                  (vla-get-ActiveDocument
                    (vlax-get-Acad-Object))))
       (vla-put-width Obj 0))
     (vla-delete ss)))
 (princ))

Posted

Still not quite working although thanks. VL code sets it to 0 it seems. First code block alters it, although not to match the contents width. Would it also be possible to adjust the vertical width to match that of the number of lines/height of the content inside?

Posted
Would it also be possible to adjust the vertical width to match that of the number of lines/height of the content inside?

 

I thought that the vertical height of MTEXT automatically adjusted itself to suit its contents... :wink:

 

Maybe theres a few extra characters in there that you don't know about...

Posted

Give this a shot for multiple text lines....

 

 

(defun c:mwid (/ ent Obj tStr pos tLst)
 (vl-load-com)
 (while (and (setq ent (car (entsel "\nSelect MTEXT Entity: ")))
             (eq "MTEXT" (cdadr (entget ent))))
   (setq Obj (vlax-ename->vla-object ent)
         tStr (vla-get-TextString Obj))
   (while (setq pos (vl-string-search "\\P" tStr))
     (setq tLst (cons (substr tStr 1 pos) tLst)
           tStr (substr tStr (+ pos 3))))
   (vla-put-Width Obj
     (apply 'max
       (mapcar '(lambda (y) (- (caadr y) (caar y)))
         (mapcar '(lambda (x) (textbox (list (cons 1 (strcat (chr 46) x)))))
           (cons tStr tLst))))))
 (princ))

Posted

 

Will set the width to the length of a single line of MTEXT, but it takes more coding to set it to the exact width for multiple lines.

 

Setting it to the length of the longest line would be fine (double lisp have a function to count characters?).

 

Thanks guys.

 

Edit: will have a look at the above now.

Posted

Not quite what I want as it is changing the position of the contents and moving them (i.e. is making adding text to lien 1 to make it longer etc if you know what i mean), I want the text to stay on their respective lines that they are currently on, while the width is adjusted to equal tha =t (or slightly more) of the longest line of text in the mtext.

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