;;打断文字并换行
(defun c:fs_textaddln (/ enp)
  (cond	((setq enp (fsxm-entsel "\n选择要从中换行的文字:" '((0 . "text"))))
	 (sssetfirst nil (fs_textadd&exp enp "\\P"))
	 (c:fs_textaddln)
	)
  )
  (princ)
)
;;打断文字
(defun c:fs_brktext (/ enp)
  (cond	((setq enp (fsxm-entsel "\n选择要被打断的文字:" '((0 . "text"))))
	 (sssetfirst nil (fs_textadd&exp enp "\\H1X"))
	 (c:fs_brktext)
	)
  )
  (princ)
)
;;text->mtext->加入文字->炸开
(defun fs_textadd&exp (enp add / la mtext text txt)
  (setq text (car enp))
  (setq txt (fsxm-ptsplittext enp))
  (setq txt (strcat (car txt) add (cadr txt)))
  (setq mtext (fsxm-text->mtext text))
  (entdel text)
  (fsxm-setendxf mtext 1 txt)
  (setq la (entlast))
  (command ".explode" mtext "")
  (fsxm-newSelection la)
)
;;加入文字
(defun c:fs_addtext (/ add enp pt txt)
  (setq add (getstring "\n请输入要添加的文字："))
  (while (setq enp (entsel "\n请点选文字加入位置："))
    (setq pt (cadr enp))
    (setq txt (fsxm-ptsplittext enp))
    (setq txt (strcat (car txt) add (cadr txt)))
    (fsxm-setendxf (car enp) 1 txt)
  )
)
;;text->Mtext
(defun c:fs_text2mtext (/ n ss)
  (or (setq ss (ssget '((0 . "text")))) (fsxm-silenceexit))
  (setq n (itoa (sslength ss)))
  (setvar "cmdecho" 0)
  (command ".undo" "be")
  (foreach en (fsxm-ss->enlist ss)
    (fsxm-text->mtext en)
    (entdel en)
  )
  (command ".undo" "e")
  (princ (strcat "\n本次共将(" n ")个TEXT转化成了MTEXT。"))
  (princ)
)
;;用text做Mtext
(defun fsxm-text->mtext	(e / box box2 data nda obj)
  (setq	nda (vl-remove-if
	      '(lambda (a)
		 (member (car a) '(-1 0 330 5 100 51 41 11))
	       )
	      (setq data (entget e))
	    )
  )
  (setq box (fsxm-obj-box (vlax-ename->vla-object e)))
  (entmake (append
	     '((0 . "MTEXT")
	       (100 . "AcDbEntity")
	       (100 . "AcDbMText")
	       (41 . 0)
	       (60 . 1)
	      )
	     nda
	   )
  )
  (setq obj (vlax-ename->vla-object (entlast)))
  (vla-copy obj)
  (setvar "cmdecho" 0)
  (sssetfirst)
  (command ".explode" (entlast) "")
  (setq box2 (fsxm-obj-box (vlax-ename->vla-object (entlast))))
  (entdel (entlast))
  (vlax-invoke obj 'move (car box2) (car box))
  (vla-put-Visible obj :vlax-true)
  (vlax-vla-object->ename obj)
)
;;用点分开文字
(defun fsxm-ptsplittext	(ep / box ck d en i len old pt txt txt2)
  (setq pt (trans (cadr ep) 1 0))	;
  (setq en (car ep))
  (setq d (entget en))
  (setq	len (- (car pt)
	       (cadr (assoc 10 d))
	       (caar (textbox d))
	    )
	len (/ len (cos (cdr (assoc 50 d))))
  )
  (setq txt (cdr (assoc 1 d)))
  (setq txt2 "")
  (while (and (/= "" txt)
	      (progn
		(if (> (ascii txt) 128)
		  (setq i 2)
		  (setq i 1)
		)
		(setq txt2 (strcat txt2 (substr txt 1 i)))
		(setq txt (substr txt (1+ i)))
		(setq d (subst (cons 1 txt2) (assoc 1 d) d))
		(setq box (textbox d))
		(setq ck (- (caadr box) (caar box)))
		(setq ck (- ck len))
		(< ck 0)
	      )
	 )
    (setq old (list ck txt2 txt))
  )
  (if (and old (< (abs (car old)) ck))
    (cdr old)
    (list txt2 txt)
  )
)
;;分开文字
(defun fs_split_str (txt / i lst)
  (while (/= "" txt)
    (if	(> (ascii txt) 128)
      (setq i 2)
      (setq i 1)
    )
    (setq lst (cons (substr txt 1 i) lst))
    (setq txt (substr txt (1+ i)))
  )
  (reverse lst)
)
(defun c:fs_textexh (/ mtxt str txt)
  (setq txt (car (entsel)))
  (or txt (fsxm-silenceexit))
  (setq mtxt (fsxm-text->mtext txt))
  (setq str (fsxm-getendxf txt 1))
  (setq str (fsxm-join (fs_split_str str) "\\H1X"))
  (entdel txt)
  (fsxm-setendxf mtxt 1 str)
  (sssetfirst nil)
  (command ".explode" mtxt "")
  (princ)
)