(defun C:tt (/ EN ENT LST LST1 OLDAUN OLDOSM 
T SS i ptlst ss1 STR TXT ZG)
(setq oldaun (getvar "aunits")
oldosm (getvar "osmode"))
(setvar "aunits" 3) ;设为弧度
(setvar "osmode" 0 ) ;设为无捕捉方式
;选择封闭方框
(if (setq ss (ssget '((0 . "LWPOLYLINE") (70 . 1) (90 . 4)))) (progn
(setq i -1)
(repeat (sslength ss)
(setq ent (entget(ssname ss (setq i (1+ i)))))
(setq ptlst (list))
(foreach x ent (if (= (car x) 10) (setq ptlst (cons (cdr x) ptlst))))
(if (setq ss1 (ssget "WP" ptlst '((0 . "TEXT")))) (progn
(while (> (sslength ss1) 0)
(setq ent (entget(setq en (ssname ss1 0))));取出第一个数据
(setq pt (cdr(assoc 10 ent))
txt (cdr(assoc 1 ent))
zg (cdr(assoc 40 ent))
lst (cons (list pt txt zg) lst);;获取表
ss (ssdel en ss1))
(entdel en);删除图元
)
(setq lst1 (vl-sort lst '(lambda (e1 e2) (< (cadar e1) (cadar e2)))));根据y坐标排序
(setq str "")
(foreach e lst1 (setq str (strcat (cadr e) "\n" str)))
(setq pt (car (last lst1));插入点
zg (last (last lst1)));字高
(setq pt (polar pt (* pi 0.5) zg))
(vl-cmdf "MTEXT" pt "H" zg "W" 0 (substr str 1 (1- (strlen str))) "")
))
)
))
(setvar "aunits" oldaun) ;设为弧度
(setvar "osmode" oldosm )
(princ)
)