4 lines of mtext converted to 4 lines text on diff layers

4 lines of mtext converted to 4 lines text on diff layers

Anonymous
Not applicable
837 Views
8 Replies
Message 1 of 9

4 lines of mtext converted to 4 lines text on diff layers

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.  The end result needs to be separate layers, the color doesn't matter, just a way to see it work.  Thanks in advance for any help.

 

thanks, LC

0 Likes
Accepted solutions (2)
838 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

Try this [in simplest terms]:

 

(defun C:MT4Lto4T (/ ss)

  (command "_.explode" (car (entsel)))

  (setq ss (ssget "_P"))

  (command

    "_.chprop" (ssname ss 0) "" "_layer" "Layer1" ""

    "_.chprop" (ssname ss 1) "" "_layer" "Layer2" ""

    "_.chprop" (ssname ss 2) "" "_layer" "Layer3" ""

    "_.chprop" (ssname ss 3) "" "_layer" "Layer4" ""

  ); command

  (princ)

); defun

 

It assumes the Layers exist in the drawing already.  It could be made to verify you picked something, and the right kind of thing, and/or to handle any number of lines in the source Mtext, and to create the Layers if they don't exist, etc.

Kent Cooper, AIA
0 Likes
Message 3 of 9

Anonymous
Not applicable
Accepted solution

fantastic Kent, thank you!  Worked great on the individual selection.  Don't suppose we can do this for a whole window of selected mtext.  (I need to do this to 462 drawings with 90,000 mtexts - piloting new database.)

LC

0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant

If the Mtext objects with 4 lines are that way only from containing 3 "hard"-return Enters, this should work:

 

(defun C:MT4Lto4T (/ mtss n ss)

  (if (setq mtss (ssget "_X" '((1 . "*\\P*\\P*\\P*")))); containing 3 hard returns

    (repeat (setq n (sslength mtss)); then

      (command "_.explode" (ssname mtss (setq n (1- n))))

      (setq ss (ssget "_P"))

      (command

        "_.chprop" (ssname ss 0) "" "_layer" "Layer1" ""

        "_.chprop" (ssname ss 1) "" "_layer" "Layer2" ""

        "_.chprop" (ssname ss 2) "" "_layer" "Layer3" ""

        "_.chprop" (ssname ss 3) "" "_layer" "Layer4" ""

      ); command

    ); repeat

  ); if

  (princ)

); defun

 

But that would do the same with any Mtext with 4 lines like that, or for that matter any with more than 3 Enters, whether or not of the characteristics you're after otherwise.  So you may need to remove the "_X" and pick them yourself somehow.  If they're restricted to only a certain Layer, or they're the only things using a certain text Style, etc., such things can be built in to help limit the selection.

 

It also doesn't account for locked Layers, but could be made to.  And it will work only on ones in the current space, so if you have them in multiple spaces, something different/additional would be needed.

Kent Cooper, AIA
0 Likes
Message 5 of 9

ВeekeeCZ
Consultant
Consultant

Another version, a bit faster.

 

(defun c:4MTextLinesToLayers ( / layers s n i d x)
  
  (setq layers '("Layer1"
		 "Layer2"
		 "Layer3"
		 "Layer4"
		 ))
  
  (if (setq n (length layers)
	    x n
	    s (ssget "_:L" '((0 . "MTEXT") (1 . "*\\P*\\P*\\P*"))))
    (progn
      (foreach l layer (if (not (tblsearch "layer" l)) (command "_.layer" "_n" l "")))
      (initcommandversion)
      (command "_.explode" s "")
      (setq s (ssget "_P"))
      (repeat (setq i (sslength s))
	(setq d (entget (ssname s (setq i (1- i)))))
	(entmod (subst (cons 8 (nth (setq x (1- (if (zerop x) n x))) layers)) (assoc 8 d) d)))))
  (princ)
  )

 

0 Likes
Message 6 of 9

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

....

  (foreach l layer (if (not (tblsearch "layer" l)) ....


I think that needs to be:

....

  (foreach l layers (if (not (tblsearch "layer" l)) ....

Kent Cooper, AIA
0 Likes
Message 7 of 9

ВeekeeCZ
Consultant
Consultant

@Kent1Cooper wrote:

@ВeekeeCZ wrote:

....

  (foreach l layer (if (not (tblsearch "layer" l)) ....


I think that needs to be:

....

  (foreach l layers (if (not (tblsearch "layer" l)) ....


 

Thanks! That typo revealed that this line is completely useless. 

0 Likes
Message 8 of 9

pbejse
Mentor
Mentor

@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, see attached visual.  The end result needs to be separate layers, the color doesn't matter, just a way to see it work.  Thanks in advance for any help.

 

thanks, LC


 

Another variation

(defun c:nMTextLinesToLayers ( / layers ss s i d x)
 (setq layers '("Layer1" "Layer2" "Layer3"  "Layer4" ))
  
  (if (and
	layers
	(setq ss (ssget "_:L" '((0 . "MTEXT") (1 . "*\\P*"))))
      )
   (repeat (setq i (sslength ss))
      (initcommandversion)
      (command "_.explode" (ssname ss (setq i (1- i))) "")
      (setq s (ssget "_P"))
     (mapcar '(lambda (e l)
          (setq d (entget e))
		(entmod (subst (cons 8 l) (assoc 8 d) d))
		)
  		(vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
       		layers
		)
	     )
	)
  (princ)
  )

 

Not limited to 4 but depends on the the number of layers you specify on your list

2 lines will get 2 layers and so on...

 

HTH

0 Likes
Message 9 of 9

pbejse
Mentor
Mentor

A wise man once said  "In the world of kung fuspeed defines the winner"

So despite the code presents additional features, one specific function ruins it. Speed should not be sacrificed when it comes to production.

So, a change in the code is in order.

 

(defun c:nMTextLinesToLayers ( / layers ss s i n d x)
 (setq layers '("Layer1" "Layer2" "Layer3"  "Layer4" ))
  
  (if (and
	layers
	(setq ss (ssget "_:L" '((0 . "MTEXT") (1 . "*\\P*"))))
      )
   (repeat (setq i (sslength ss))
      (initcommandversion)
      (command "_.explode" (ssname ss (setq i (1- i))) "")
      (setq l nil s (ssget "_P"))
      (repeat (Setq n (sslength s))
		(setq l (cons (ssname s (setq n (1- n))) l)))	
		    
     (mapcar '(lambda (e l)
          (setq d (entget e))
		(entmod (subst (cons 8 l) (assoc 8 d) d))
		)
  		l
       		layers
		)
	     )
	)
  (princ)
  )

 

HTH

 

Thanks to @ВeekeeCZ  for calling it out and ruining the fun!  [ party pooper ] 😄

 

0 Likes