;;表格文字居中
(princ "\n飞诗CAD-表格文字居中 1.0，支持Text,Mtext。")
(defun c:mid_table_text
       (/ *error* cen cmd h hobj la nss objs pt ss v vars w)
  (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")
  )
  (foreach obj objs
    (if	(= (vla-get-ObjectName obj) "AcDbMText")
      (progn
	(setq w (vla-get-Width obj))
	(vla-put-Width obj 0)
	(setq pt (apply 'fsxm-midpt (fsxm-obj-box obj)))
	(vla-put-Width obj w)
      )
      (setq pt (apply 'fsxm-midpt (fsxm-obj-box obj)))
    )
    (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))
	(vlax-invoke obj 'move pt cen)
	(ssadd (vlax-vla-object->ename obj) nss)
      )
    )
  )
  (*error* (strcat "\n执行完成！共处理文字<"
		   (itoa (sslength ss))
		   ">个，成功<"
		   (itoa (sslength nss))
		   ">个..."
	   )
  )
  (sssetfirst nil nss)
  (princ)
)