4 lines of mtext converted to text on 4 different layers lisp routine

4 lines of mtext converted to text on 4 different layers lisp routine

Anonymous
Not applicable
727 Views
5 Replies
Message 1 of 6

4 lines of mtext converted to text on 4 different layers lisp routine

Anonymous
Not applicable
 

Hello, looking for a lisp routine that would change an mtext with 4 lines (1 layer) to 4 separate lines of text (or mtext) and on 4 different layers, see attached visual.

 

thanks, LC

0 Likes
Accepted solutions (1)
728 Views
5 Replies
Replies (5)
Message 2 of 6

steven-g
Mentor
Mentor

This "https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/bd-p/130" would be a much better place for this question, full of people who are really good with Lisp.

0 Likes
Message 3 of 6

Anonymous
Not applicable
thank you, will do
0 Likes
Message 4 of 6

hak_vz
Advisor
Advisor

Try this

(defun c:split_mtext ( / layernames ss e _last ent i *error*)
	(defun *error* ( msg )
		(if (not (member msg '("Function cancelled" "quit / exit abort")))
			(princ (strcat "\nError: " msg))
		)
		(princ)
	)
	(setq layernames '("ONE" "TWO" "THREE" "FOUR")) ;change names in desired order
	(while (setq ss (ssget "_+.:E:S" '((0 . "MTEXT"))))
		(princ "\nSelect mtext >")
		(setq e (ssname ss 0) _last (entlast) i 0)
		(command "_.explode" e)
		(while (setq _last (entnext _last))
			(setq ent (entget _last))
			(setq ent (subst  (cons 8 (nth i layernames)) (assoc 8 ent) ent))
			(entmod ent)
			(setq i (1+ i))
		)
	)
	(princ)
)

 

What you should do is to change names of test layers in my code

(setq layernames '("ONE" "TWO" "THREE" "FOUR"))

and have those layers created in your drawing.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:
 

Hello, looking for a lisp routine that would change an mtext with 4 lines (1 layer) to 4 separate lines of text (or mtext) and on 4 different layers....


Here's a version that takes a different approach to some operations, and also lets you choose as many Mtext objects as you want all at once.  It prevents selection of Mtext on locked Layers.

Since no System Variables are changed that should be assured of being set back, it seems to me the only purpose of an *error* handler is to ensure conclusion of an UNDO begin/end wrapper, so that the whole batch will be undone with one U after running it.

It will work with 4-line Mtexts, but to be more generic, works with those of more [or less] than that.  Put more Layer names into the 'laynames' list than the number of lines you expect the Mtext objects to contain, and it will change the Layers of the resulting Text objects as far as they go.

(defun C:MT2L ; = MText {to} Layers
  (/ *error* doc laynames ss1 n1 n2 ss2)
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break"))
      (princ (strcat "\nError: " errmsg))
    ); if
    (vla-endundomark doc)
    (princ)
  ); defun - *error*
  (vla-startundomark (setq doc (vla-get-activedocument (vlax-get-acad-object))))
  (setq laynames '("1" "2" "3" "4" "5" "6" "7")) ;; <---EDIT
  (prompt "\nTo split Mtext object(s) onto different Layers,")
  (if (setq ss1 (ssget "_:L" '((0 . "MTEXT"))))
    (repeat (setq n1 (sslength ss1)); then
      (setq n2 0); set for first, reset for subsequent
      (command "_.explode" (ssname ss1 (setq n1 (1- n1))))
      (repeat (sslength (setq ss2 (ssget "_P")))
        (command "_.chprop" (ssname ss2 n2) "" "_layer" (nth n2 laynames) "")
        (setq n2 (1+ n2))
      ); repeat [Texts]
    ); repeat [Mtexts]
    (prompt "\nNo Mtext(s) on unlocked Layer(s) selected.")
  ); if
  (vla-endundomark doc)
  (princ)
); defun

 

Kent Cooper, AIA
0 Likes
Message 6 of 6

Anonymous
Not applicable

Kent, this is the one!  great routine, thank you very much!  Very much appreciated.

0 Likes