;;表格文字居中
(princ "\n飞诗CAD-表格文字居中 1.1，支持Text,Mtext。")
(defun c:mid_table_text
       (/ *error* box cen cmd h hobj la nss objs pt ss v vars)
  (princ "\n选择要居中的文本:")
  (setq ss (ssget '((0 . "*text"))))
  (or ss (fsxm-silenceexit))
  (setq v '("cmdecho" "fillmode" "hpname" "hpassoc"))
  (setq vars (mapcar 'getvar v))
  (defun *error* (msg)
    (foreach obj objs (vla-put-Visible obj 1))
    (mapcar 'setvar v vars)
    (vla-EndUndoMark *doc*)
    (princ msg)
  )
  (vla-StartUndoMark *doc*)
  (mapcar 'setvar v '(0 0 "SOLID" 0))
  (setq nss (ssadd))
  (setq objs (mapcar 'vlax-ename->vla-object (fsxm-ss->enlist ss)))
  (foreach obj objs (vla-put-Visible obj 0))
  (if (getcname "-hatch")
    (setq cmd ".-hatch")
    (setq cmd ".-boundary")
  )
  (Fstl_AutoLoad "TextBox")
  (foreach obj objs
    (setq box (Fsxm-TextBox (vlax-vla-object->ename obj)))
    (setq pt (fsxm-midpt (car box) (caddr box)))
    (setq la (entlast))
    (command cmd (trans pt 0 1) "")
    (while (/= 0 (getvar "cmdactive")) (command))
    (vla-put-Visible obj 1)
    (setq h (entlast))
    (if	(/= h la)			;生成了剖面线
      (progn
	(setq hobj (vlax-ename->vla-object h))
	(setq cen (apply 'fsxm-midpt (fsxm-obj-box hobj)))
	(mapcar 'entdel (fsxm-newenlist la))
	(vla-move obj (vlax-3d-point pt) (vlax-3d-point cen))
	(ssadd (vlax-vla-object->ename obj) nss)
      )
    )
  )
  (*error* (strcat "\n执行完成！共处理文字<"
		   (itoa (sslength ss))
		   ">个，成功<"
		   (itoa (sslength nss))
		   ">个..."
	   )
  )
  (sssetfirst nil nss)
  (princ)
)