- UID
- 675606
- 积分
- 3400
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2013-5-6
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2020-6-11 15:35:11
|
显示全部楼层
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;文本对齐于框格工具
- ;;;Align Object对齐对象,简称AO
- ;;;主要处理Text,兼顾attdef和Mtext
- ;;;1 选择集中取一个对象,如果判断在表格内,所有文字按表格内处理。否则,直接...
- ;;;2 表格内文字起点离边框,半个字高。如果有多行,误差在一个字高的,认为处于同
- ;;; 一行;第一行文字中心到上边线为一个字高
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;[功能] 先选择,后执行通用函数
- ;;(sslength (_StartSSget '((0 . "DIMENSION"))))
- (defun _StartSSget (fil / SS)
- (cond ((cadr (ssgetfirst)) (setq ss (apply 'ssget (list "_p" fil)))))
- (cond (SS (sssetfirst nil nil))
- (T (setq ss (apply 'ssget (list ":S" fil))))
- )
- ss
- )
- ;;[功能] 分离选择集(setq fil '((0 . "DIMENSION")))
- (defun _PartSSget (ss fil)
- (if (>= (atoi (getvar "AcadVer")) 20) ;2015
- (command-s "_.select" ss "")
- (vl-cmdf "_.select" ss "")
- )
- (apply 'ssget (list "_p" fil))
- )
- (defun C:TAO ()
- (C:AO)
- )
- (defun C:AO (/ *error* optimizeCode do2 do1 do3 CODE E FILTERLST FLAG KEY MIDPT MYSS P PTS SS SSET SSETCIRCLE SSETINSERT SSETMTEXT SSETTEXT VALIST0 VARLIST VARTXTLST X)
- ;;0 错误处理
- (defun *error* (msg)
- (HH:Once=>Init VARLIST valist0)
- )
- ;;1 优化代码的函数
- ;; 示例(setq vartxtlst (list "ss1" "ss2" "ss3"))(setq filterlst (list "circle" "*line" "*text"))
- ;; (optimizeCode ss varlst fillst)
- (defun optimizeCode (ss varlst fillst)
- (mapcar (function (lambda (x y) (set x (_PartSSget ss (list (cons 0 y))))))
- (mapcar 'read varlst)
- fillst
- )
- )
- ;;2 非表格内:多行多列
- (defun do2 (ss code / H H1 I LST LST1 P P0 PT W0 X Y0)
- ;;第1步 行间距,取第一个对象来确定行间距,对于文字是合理的
- ;;如字高3.5,文字间距7,符合机械制图标准
- (setq h (apply 'mapcar (cons '- (Entity:Box (ssname ss 0)))))
- (setq h (* (abs (cadr h)) 2)) ;默认间距
- (initget 46)
- (setq h1 (getreal (strcat "\n >>输入行间距<" (VL-PRINC-TO-STRING h) ">:")))
- (cond ((not h1) (setq h1 h)))
- ;;第2步 排序、基点、最大宽度w0
- (setq lst (HH:ssPts:Sort ss "Yx" (* h1 0.75 0.5))) ;Y坐标大,排在前;Y相同时,x坐标大排在前
- (setq y0 (cadr (cdr (assoc code (entget (car lst)))))) ;左上的Y坐标
- ;;最大宽度
- (setq
- w0 (mapcar '(lambda (x) (abs (car (apply 'mapcar (cons '- (Entity:Box x)))))) lst)
- )
- (setq w0 (apply 'max w0))
- ;;第3步 处理同列
- ;;以p0为基点,小于w0均视为同列
- (while lst
- (setq i -1)
- (setq lst1 lst)
- (setq p (cdr (assoc code (entget (car lst)))))
- (setq p0 (list (car p) y0)) ;每行最上对象的坐标
- (while (setq x (car lst1))
- (setq lst1 (cdr lst1))
- (setq p (cdr (assoc code (entget x)))) ;p为坐标
- (if (equal (car p) (car p0) w0) ;如果x坐标小于最大宽度,认为是同一列
- (progn
- (setq pt (mapcar '- p0 (list 0 (* (setq i (1+ i)) h1))))
- (vl-cmdf "_.MOVE" x "" p pt)
- (setq lst (vl-remove x lst))
- )
- )
- )
- )
- )
- ;;3 表格内文字 SSET
- (defun do1 (ss code MYSS key / EN EN0 FLAG H HEIGHT I LST LST1 LST11 LST1N MIDPT N OBJ P P0 PE PTS SCALEFACTOR SS1 STR STR0 W WIDTH X)
- ;;第1步 行间距,取第一个对象来确定行间距,对于文字是合理的
- ;;如字高3.5,文字间距7,符合机械制图标准(setq ss (ssget))(setq sSet ss)
- (setq h (apply 'mapcar (cons '- (Entity:Box (ssname ss 0)))))
- (setq h (abs (cadr h)))
- ;;第2步 同行合并
- (setq lst (HH:ssPts:Sort ss "Yx" (* h 0.5))) ;Y坐标大,排在前;Y相同时,x坐标大排在前
- (while (setq x (car lst))
- (setq lst (cdr lst))
- (setq Pe (Entity:Box x))
- (setq pts (HH_PtInCurve (apply 'MJ:MIDPOINT Pe) MYSS)) ;实体所在外盒包围点
- ;;ssget "C"可能选择到表格;ssget "W"又选择不到大于表格的,ChooseP1P2也许是不错的选择
- (setq ss1 (ChooseP1P2 (car pts) (cadr pts) '((0 . "*TEXT,ATTDEF"))))
- (ssadd x ss1)
- (VL-CATCH-ALL-APPLY
- '(lambda () (command "_.JUSTIFYTEXT" ss1 "" key))
- nil
- )
- (repeat (setq i (sslength ss1))
- (setq x (SsName ss1 (setq i (1- i))))
- (setq lst (vl-remove x lst))
- (ssadd x sSet) ;(command "._Select" sSet "_Add" ss1 "")
- )
- ;;第3步 lst1中有多少行?
- (setq lst1N (HH:ssPts:Sort ss1 "xY" (* h 0.5)))
- (setq lst1 lst1N)
- (while (setq x (car lst1))
- (setq lst1 (cdr lst1))
- (setq en0 (entget x))
- (setq p0 (cdr (assoc code en0))) ;p0为坐标
- (setq Flag (equal "ATTDEF" (cdr (assoc 0 en0))))
- (if Flag
- (setq str0 (cdr (assoc 2 en0)))
- (setq str0 (cdr (assoc 1 en0)))
- )
- (setq lst11 lst1)
- (while (setq x (car lst11))
- (setq lst11 (cdr lst11))
- (setq en (entget x))
- (setq p (cdr (assoc code en))) ;p为坐标
- (if (equal (cadr p) (cadr p0) (* h 0.5)) ;如果y坐标小于h,认为是同一行
- (progn
- (if (equal "ATTDEF" (cdr (assoc 0 en)))
- (setq str (cdr (assoc 2 en)))
- (setq str (cdr (assoc 1 en)))
- )
- (setq lst1 (vl-remove x lst1))
- (setq lst1N (vl-remove x lst1N))
- (setq str0 (strcat str0 str))
- (entdel x)
- )
- )
- )
- (if Flag
- (entmod (subst (cons 2 str0) (assoc 2 en0) en0))
- (entmod (subst (cons 1 str0) (assoc 1 en0) en0))
- )
- )
- ;;第4步 移动 如果text attdef长度大于表格,处理比例因子
- (setq w (abs (car (apply 'mapcar (cons '- pts))))) ;包围盒长度
- (setq h (abs (cadr (apply 'mapcar (cons '- pts))))) ;包围盒高
- (setq Height (abs (cadr (apply 'mapcar (cons '- Pe))))) ;字高
- (setq n (length lst1N)) ;行
- (setq h (/ h n)) ;字间距
- (cond ((equal key "ML")
- (setq p0 (list (+ (caar pts) (* Height 0.5)) (cadadr pts)));右移半个字
- )
- ((equal key "MC")
- (setq p0 (list (+ (caar pts) (* w 0.5)) (cadadr pts)))
- )
- (T (setq p0 (list (- (caadr pts) (* Height 0.5)) (cadadr pts))));左移半个字
- )
- (setq p0 (mapcar '+ p0 (list 0 (* h 0.5)))) ;便于下面增加
- (foreach x lst1N
- (setq p0 (mapcar '- p0 (list 0 h))) ;步长
- (setq obj (vlax-ename->vla-object x))
- (setq Pe (Entity:Box x))
- (cond
- ((equal key "ML")
- (setq MidPt (list (caar pe) (* (+ (cadar pe) (cadadr pe)) 0.5)))
- ) ;实体左中点
- ((equal key "MC") (setq MidPt (apply 'MJ:MIDPOINT Pe))) ;实体中点
- (T (setq MidPt (list (caadr pe) (* (+ (cadar pe) (cadadr pe)) 0.5))));实体右中点
- )
- (vl-cmdf "_.MOVE" x "" MidPt p0)
- ;;如果text attdef长度大于表格,处理比例因子
- (if
- (member (vlax-get obj 'ObjectName) (list "AcDbText" "AcDbAttributeDefinition"))
- (progn
- (setq ScaleFactor (vlax-get obj 'ScaleFactor))
- (setq width (abs (car (apply 'mapcar (cons '- Pe))))) ;字长
- (setq w (- w Height)) ;减去一个字高
- (if (> width w)
- (progn
- (setq ScaleFactor (* (/ w width) ScaleFactor))
- (vlax-put obj 'ScaleFactor ScaleFactor)
- )
- )
- )
- )
- )
- )
- )
- ;;4 表格内非文字
- (defun do3 (ss code MYSS sSet / FIL H H1 HEIGHT I LST LST1 LST1N P P0 PE PT PTS SS1 W W0 X Y0)
- ;;第1步 行间距,取第一个对象来确定行间距(setq ss(ssget))
- (setq x (ssname ss 0))
- (setq h (apply 'mapcar (cons '- (Entity:Box x))))
- (setq h (abs (cadr h)))
- (setq fil (list (assoc 0 (entget x))))
- ;;第2步 多行多列
- (setq lst (HH:ssPts:Sort ss "Yx" (* h 0.5))) ;Y坐标大,排在前;Y相同时,x坐标大排在前
- ;;(setq lst (HH:ssPts:Sort ss "xY" (* h 0.5))) ;x坐标小,排在前;x坐标相同,Y坐标大排在前
- (while (setq x (car lst))
- (setq lst (cdr lst))
- (setq Pe (Entity:Box x))
- (entdel x)
- (ssdel x MYSS)
- (setq pts (HH_PtInCurve (apply 'MJ:MIDPOINT Pe) MYSS)) ;实体所在外盒包围点
- (entdel x)
- ;;ssget "C"可能选择到表格;ssget "W"又选择不到大于表格的,ChooseP1P2也许是不错的选择
- (setq ss1 (ChooseP1P2 (car pts) (cadr pts) fil))
- (ssadd x ss1)
- (repeat (setq i (sslength ss1))
- (setq x (SsName ss1 (setq i (1- i))))
- (setq lst (vl-remove x lst))
- (ssadd x sSet) ;(command "._Select" sSet "_Add" ss1 "")
- )
-
- ;;第3步 移动
- ;;以p0为基点,小于w0均视为同列
- (setq lst1N (HH:ssPts:Sort ss1 "Yx" (* h 0.5)));Y坐标大,排在前;Y相同时,x坐标大排在前
- (setq w (abs (car (apply 'mapcar (cons '- pts))))) ;包围盒长度
- (setq h (abs (cadr (apply 'mapcar (cons '- pts))))) ;包围盒高
- (setq Height (abs (cadr (apply 'mapcar (cons '- Pe)))))
- (setq w0 (abs (car (apply 'mapcar (cons '- Pe)))));实体宽度
- (setq y0 (cadr (cdr (assoc code (entget (car lst1N)))))) ;左上的Y坐标
- (setq h1 (* w0 1.5));间距
- (while lst1N
- (setq i -1)
- (setq lst1 lst1N)
- (setq p (cdr (assoc code (entget (car lst1)))))
- (setq p0 (list (car p) y0)) ;每行最上对象的坐标
- (while (setq x (car lst1))
- (setq lst1 (cdr lst1))
- (setq p (cdr (assoc code (entget x)))) ;p为坐标
- (if (equal (car p) (car p0) w0) ;如果x坐标小于最大宽度,认为是同一列
- (progn
- (setq pt (mapcar '- p0 (list 0 (* (setq i (1+ i)) h1))))
- (vl-cmdf "_.MOVE" x "" p pt)
- (setq lst1N (vl-remove x lst1N))
- )
- )
- )
- )
- )
- )
- ;;主程序
- (if (findfile "actscale.arx")
- (ARXLOAD "actscale.arx")
- )
- (if (findfile "acTscale.crx")
- (ARXLOAD "acTscale.crx")
- )
- (princ "\n 单行文字、多行文字、圆、块,择其一类对齐")
- (setq sSet (_StartSSget '((0 . "*TEXT,ATTDEF,ARC,ELLIPSE,INSERT,CIRCLE"))))
- (if sSet
- (progn
- (vl-load-com)
- (command "._ucs" "_W")
- (or *ACAD* (setq *ACAD* (vlax-get-acad-object)))
- (or *DOC* (setq *DOC* (vla-get-ActiveDocument *ACAD*)))
- (_StartUndo *DOC*)
- (setq VARLIST (list "cmdecho" "osmode" "shortcutmenu"))
- (setq valist0 (HH:Once=>get VARLIST))
- (HH:Once=>Init VARLIST (list 0 0 11))
- (setq vartxtlst (list "sSetText" "sSetMText" "sSetCIRCLE" "sSetINSERT"))
- (setq filterlst (list "TEXT,ATTDEF" "MTEXT" "CIRCLE,ARC,ELLIPSE,*LINE" "INSERT"))
- (mapcar '(lambda (x) (set (read x) nil)) vartxtlst) ;加载失败后改进方案,增加此句
- (apply 'optimizeCode (list sSet vartxtlst filterlst))
- (setq Flag (or sSetText sSetMText)) ;T表示文字
- ;;只取其中一类
- (setq ss nil) ;加载失败后改进方案,增加此句
- (setq ss (cond (sSetText)
- (sSetMText)
- (sSetCIRCLE)
- (sSetINSERT)
- )
- )
- )
- )
- ;;如果是文字,预先处理一下
- (cond
- ((and Flag ss)
- (initget "mC mL mR") ;区分大小写
- (setq key (getkword "\n 文本对齐于 [左中(L)/正中(C)/右中(R)]:<L>"))
- (cond ((not key) (setq key "ML")))
- (setq key (strcase key))
- (setq P
- (VL-CATCH-ALL-APPLY
- '(lambda () (command "_.JUSTIFYTEXT" ss "" key))
- nil
- )
- )
- (if (VL-CATCH-ALL-ERROR-P P)
- (progn
- (if (findfile "actscale.arx")
- (ARXLOAD "actscale.arx")
- )
- (if (findfile "acTscale.crx")
- (ARXLOAD "acTscale.crx")
- )
- (command "_.JUSTIFYTEXT" ss "" key)
- )
- )
- )
- )
- ;;如果是单行文字或属性文字,处理组码11
- (cond (sSetText (setq code 11))
- (T (setq code 10))
- )
- (if ss
- (progn
- ;;取ss内一个对象,判断是否在表格内
- (setq e (ssname ss 0))
- (setq pts (Entity:Box e)) ;实体包围盒2角点
- (setq MidPt (apply 'MJ:MIDPOINT pts)) ;实体中点
- (setq MYSS (HH:viewpnts)) ;屏幕2角点
- (setq MYSS
- (ssget "C"
- (car MYSS)
- (cadr MYSS)
- '((0 . "*TEXT,ATTDEF,ELLIPSE,ARC,CIRCLE,*LINE,INSERT"))
- )
- )
- ;;下面几句是本程序关键
- (SS_SSsub1 MYss ss)
- ;;(ssdel e MYss)
- (if (not sSetMText)
- (entdel e)
- ) ;此句对于Leader文字对象,是有影响的,加IF判断
- (setq pts (HH_PtInCurve MidPt MYSS)) ;第一个实体所在外盒包围点
- (if (not sSetMText)
- (entdel e)
- )
- ;;表格外:按多行多列处理;表格内:文字按多行处理,其余按多行多列处理
- (cond (pts
- (if Flag
- (do1 ss code MYSS key) ;文字
- (do3 ss code MYSS sSet)
- )
- ) ;在表格内
- (T (do2 ss code)) ;非表格对象
- )
- ;;如果sSet为T,则ss为T,故下面几名可以写在这里
- (HH:Once=>Init VARLIST valist0)
- (_EndUndo *DOC*)
- (command "_.select" sSet "") ;因为ss内对象有些已经删除
- (HH::EntSSHighLight (ssget "_p"))
- )
- )
- (GC)
- (princ "\n 黄明儒温馨提示:对象对齐 AO") ;原ayEntSSHighLight
- (princ)
- )
- ;;[功能] 选择集相减(command "._Select" ss1 "_Remove" ss2 "")
- (defun SS_SSsub1 (SS1 SS2 / I)
- (repeat (setq i (sslength SS2))
- (SsDel (SsName SS2 (setq i (1- i))) SS1)
- )
- )
|
评分
-
查看全部评分
|