用AutoCAD进行机械设计时,标注尺寸公差是设计人员经常遇到的一个问题。用Text命令手工标注,不仅速度慢,而且容易出错;通过对话框设定参数进行标注,每个不同的公差值都要设定一次。这些方法在标注时都需要翻阅手册,给设计者带来很大的不便。曾有一些标注公差的程序,使用效果并不很理想。笔者利用Autolisp语言编写了一个尺寸公差自动标注的程序。使用该程序进行公差标注不必翻阅手册,只需选择基本尺寸和公差带代号即可自动标注尺寸公差,可大大提高标注公差的速度。 Xj|j\2$ 0
一、公差值数据存贮 y{"8VT)
在标注公差过程中,对于一定的基本尺寸而言,能否根据基本尺寸大小、公差等级和基本偏差代号迅速查询出上、下偏差值,是实现公差自动标注的关键。为此,可将国标中常用的极限偏差以Data·txt为文件名保存在数据文件中。数据文件格式如下: MUVp8!*@
f6 g6 h6 h7 DQ~+\
…… MPNBA1s
(… -0.025 -0.041 -0.009 -0.025 0 -0.016 0 -0.025 …);dimt40~50mm (… -0.030 -0.049 -0.010 -0.029 0 -0.019 0 -0.030 …);dimt50~65mm se7_:0+w
(… -0.030 -0.049 -0.010 -0.029 0 -0.019 0 -0.030 …);dimt65~80mm (… -0.036 -0.058 -0.012 -0.034 0 -0.022 0 -0.035 …);dimt80~100mm dHd{9ftyF
…… bzB9u&
第一行为公差带代号,实际文件中没有。若公差带代号为g6,基本尺寸为45mm,则其上偏差为-0.009mm,下偏差为-0.025mm。因查询函数是以字符位置获取上、下偏差的,所以公差数值表应整齐,否则获取的公差值将出错。 =p^*y-z
二、基本尺寸的获取 X8?|5$Ey
用Entget、Substr函数获取基本尺寸dimt、字高Txth及旋转角Angd,便于在标注公差时确定公差的字高、角度等参数。这里要注意,在尺寸标注前Dimaso应设定为off,否则取不到尺寸。对于基本尺寸前有R、r、Φ等符号时,应作特殊处理。对用“Text”命令标注的尺寸和用“Dimension”命令标注的尺寸,处理方法略有不同,详见源程序。 ]$KH78MTW
三、公差数值的查询 U4^dDj
为自动查询出所需的公差数值,我们高设定了两个函数:首先用Record()函数检索出数据文件中符合基本尺寸(dimt)范围的数据行,用Read()函数将该行中所有公差值赋给相应的变量hi,以便将相应的公差值设置为上、下偏差。例如,当获取的基本尺寸为40mm、输入的公差带代号为h7时,Record()得到的数据行号为8,用Read()将该行上的公差数值全部赋给hi,则h29为上偏差,h30为下偏差值。相应的程序如下: uXtfP?3Vy
(defun read (record fname) [ub,&j^
(setq fp (open fname "r")) NeeymyW
(if (= fp nil) GRlA9Q
(alert "not open datatxt!") tAYu|\]
(progn }7Pd\t G]
(repeat record (setq h (read (read-line fp)))) %qN8uQx
(setq n (length h)) 9u"im+=:
(setq b '() i 1) X @r5^A[9
(while (<= i n) l
^$$d8
(setq b (append b (list (read (strcat "h" (itoa i)))))) F~<$E*&h@
(setq i (1+ i)) ,!bOzth2>K
) ?Tb'J`MO
(setq i 0) B/pNM81(
(while (< i n) Q7`zrCh
(setq name (nth i b) `^|mNh
value (nth i h) zg|yW6l)9
) \/{qE hP
(set name value) 0^{zq|%Q!
(setq i (1+ i)) !tX14O~B-
) PP$Ig2Q
(close fp) sHh2>f@x$
)) AE^&hH0^
) WMl_$Fd6
********************* y\)bxmC
(defun record (dimt) 7.akp
(cond ((<= dimt 3) 1) ((and (> dimt 3) (<= dimt 6)) 2) ((and (> dimt 6) (<= dimt 10)) 3) ((and (> dimt 10) (<= dimt 14)) 4) ;dimt为获取的基本尺寸 …… ;因篇幅所限,以下数行略。 %Sxy!gGz%%
) ) j+Wgjf
四、自动标注公差 aLhTaB-va
为使标注的尺寸公差与基本尺寸协调,在标注前先根据基本尺寸的字高、旋转角等确定出公差的字高和旋转角,并计算出上、下偏差的标注位置,然后用“Text”命令标注公差。程序在设计中,考虑到了只知道上、下偏差值而不知道公差等级和公差代号的情况(此时可不输入公差等级和代号,而直接输入上、下偏差),同时也考虑到了某些特殊情形,如±0.01等等。 vT3LhN+1
源程序(文件名为gcbz..lsp)如下: ~5]AXi'e~
(defun c:gcbz() (setq ss (entget (car (entsel)))) (setq gcdh (getstring " 输入公差带代号:")) (setq p (getpoint " 输入插入点:")) b@5&<V;r2
(setq dim (cdr (assoc 1 ss))) uodO^5"-
(setq dim1 dim) xI_WkoI
(progn zB 7wGl9
(if (= (substr dim 1 1) "R") (setq dim1 (substr dim 2))) E2%7 v
(if (= (substr dim 1 1) "r") (setq dim1 (substr dim 2))) Fv e,&~
(if (= (substr dim 1 3) "%%c") (setq dim1 (substr dim 4))) ~+^,o_hT
(if (= (substr dim 1 4) "\\A1;") (setq dim1 (substr dim 5))) G{ F>=z"(l
(if (or (= (substr dim 1 5) "\\A1;R") (= (substr dim 1 5) "\\A1;r")) (setq !YAkHrF`[0
dim1 (substr dim 6))) OM2|c}]ZQ
(if (= (substr dim 1 7) "\\A1;\\U+") (setq dim1 (substr dim 12))) c3oI\lU
);获取各类尺寸的尺寸数值 EL~s90C
(setq dimt (atof dim1)) z,/dYvT<
(setq k (record dimt));调用函数,获取数据行号 $W`
&7
(read k "d:/data.txt");读取数据 (if (= gcdh "") (setq gc11 (getreal " 输入上偏差:") gc22 (getreal " 输入下偏差:")) N9{ivq|fO
) v-OGY[|97
(if (= gcdh "f6") (setq gc11 h2 gc22 h3)) nLT]'B]$+
(if (= gcdh "h7") (setq gc11 h1 gc22 h4)) ;取得上、下偏差值 …… ; 以下数行从略 (setq gc1 (rtos gc11 2 4) gc2 (rtos gc22 2 4)) 2NE/ZqREg
(setq txth (cdr (assoc 40 ss))); _H:SoJ'
(setq angr (cdr (assoc 50 ss))) 5nf|CQH6?
(setq hi (* txth 0.5)) F)v+.5T1
(setq angd (* (/ angr pi) 180)) }R}tIC-:
(setq p1 (polar p (+ (/ pi 2) angr) 0.2)) lWnV{/q\X
(setq p2 (polar p1 (+ (* pi 1.5) angr) (+ hi 0.8))) &
}k=V4L
(setq p3 (polar p1 angr 1.8)) QF-.")Z
(setq tole (strcat "%%p" gc1)) V<ODt%
(if (> gc11 0) (setq gc1 (strcat "+" gc1))) )?Jj#HtW
(if (> gc22 0) (setq gc2 (strcat "+" gc2))) @0B<b7Jv
(if (= (abs gc11) (abs gc22)) (command "text" p2 txth angd tole "")) t"RgEH@
(if (/= (abs gc11) (abs gc22)) zA( 2+e 7
(progn V@cRJ3ZF
(command "text" p1 hi angd gc1 "") S,Tm=} wj
(command "text" p2 hi angd gc2 "") a$;+-Y
) f
( UcJx
) u%ih7v!r\
) EEaKT`/d
五、程序运行及菜单定制 Uc7X)
将程序放在Support\子目录下,在AutoCAD环境下用load函数将程序装入;也可用tools/application装入;或者直接将文件放在ACADR14.LSP文件中。这样,在运行AutoCAD时可直接将该程序装入。为方便标注,可在下拉菜单Dimension段增加尺寸公差标注项,具体方法如下:打开菜单文件ACAD.MNU,在**DIMENSION下增加[尺寸公差标注]^C^CGCBZ即可。 oHeo]<Fbv
六、结束语 jY ^ndr0;
本程序已在AutoCAD R14、AutoCAD2000上调试通过,可函盖全部机械设计中的公差标注类型,可大大提高标注尺寸公差的速度。 (m.]0v*&c
将常用的公差数值以数据文件形式存贮,利用Autolisp语言编程,自动检索尺寸公差数值来实现尺寸公差的自动标注,是一种很好的手段。