Organic Posted April 17, 2009 Posted April 17, 2009 Does anyone know a lisp which resizes an mtext entity to the width of the text it contains? Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 Easy fix would be to just set it to zero (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)) Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 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 Quote
Organic Posted April 17, 2009 Author Posted April 17, 2009 Easy fix would be to just set it to zero (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. Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 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)) Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 (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)) Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 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)) Quote
Organic Posted April 17, 2009 Author Posted April 17, 2009 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? Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 The codes I provided will set the MTEXT width to 0, as I thought this may provide another option for you. This code, http://www.cadtutor.net/forum/showpost.php?p=230634&postcount=6 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. Quote
Lee Mac Posted April 17, 2009 Posted April 17, 2009 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... Quote
Lee Mac Posted April 18, 2009 Posted April 18, 2009 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)) Quote
Organic Posted April 18, 2009 Author Posted April 18, 2009 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. Quote
Organic Posted April 18, 2009 Author Posted April 18, 2009 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. Quote
Lee Mac Posted April 18, 2009 Posted April 18, 2009 It does equal the longest line + 1 character. Quote
Recommended Posts
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.