用AutoCAD进行机械设计时,标注尺寸公差是设计人员经常遇到的一个问题。用Text命令手工标注,不仅速度慢,而且容易出错;通过对话框设定参数进行标注,每个不同的公差值都要设定一次。这些方法在标注时都需要翻阅手册,给设计者带来很大的不便。曾有一些标注公差的程序,使用效果并不很理想。笔者利用Autolisp语言编写了一个尺寸公差自动标注的程序。使用该程序进行公差标注不必翻阅手册,只需选择基本尺寸和公差带代号即可自动标注尺寸公差,可大大提高标注公差的速度。 wP.b2X_V
一、公差值数据存贮 6('2.^8
在标注公差过程中,对于一定的基本尺寸而言,能否根据基本尺寸大小、公差等级和基本偏差代号迅速查询出上、下偏差值,是实现公差自动标注的关键。为此,可将国标中常用的极限偏差以Data·txt为文件名保存在数据文件中。数据文件格式如下: d 6 t#4!
f6 g6 h6 h7 25%[nkO4
…… .6Tan2[%
(… -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 $8gj}0}eH
(… -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 Coz\fL
…… :/
,h)h)|
第一行为公差带代号,实际文件中没有。若公差带代号为g6,基本尺寸为45mm,则其上偏差为-0.009mm,下偏差为-0.025mm。因查询函数是以字符位置获取上、下偏差的,所以公差数值表应整齐,否则获取的公差值将出错。 lB9 9J"A
二、基本尺寸的获取 "w.gP8`
用Entget、Substr函数获取基本尺寸dimt、字高Txth及旋转角Angd,便于在标注公差时确定公差的字高、角度等参数。这里要注意,在尺寸标注前Dimaso应设定为off,否则取不到尺寸。对于基本尺寸前有R、r、Φ等符号时,应作特殊处理。对用“Text”命令标注的尺寸和用“Dimension”命令标注的尺寸,处理方法略有不同,详见源程序。 e ?FjN 9
三、公差数值的查询 4Cs
|F7R
为自动查询出所需的公差数值,我们高设定了两个函数:首先用Record()函数检索出数据文件中符合基本尺寸(dimt)范围的数据行,用Read()函数将该行中所有公差值赋给相应的变量hi,以便将相应的公差值设置为上、下偏差。例如,当获取的基本尺寸为40mm、输入的公差带代号为h7时,Record()得到的数据行号为8,用Read()将该行上的公差数值全部赋给hi,则h29为上偏差,h30为下偏差值。相应的程序如下: F)_zR
(defun read (record fname) mm(Ff >O
(setq fp (open fname "r")) z97RNT|Y7U
(if (= fp nil) ?5(Cwy ?
(alert "not open datatxt!") K
TJm[44
(progn 9?$Qk0jc
(repeat record (setq h (read (read-line fp)))) 7 lc -
(setq n (length h)) JGl0
(i*|
(setq b '() i 1) ;oW#>!HrY
(while (<= i n) d>[=]
(setq b (append b (list (read (strcat "h" (itoa i)))))) EM9K^l`
(setq i (1+ i)) qKu/~0a/
) ]E/^(T-O
(setq i 0) zvjp]yTx"
(while (< i n) Kx,#Wg{H
(setq name (nth i b) lezX-5Z
value (nth i h) 5Fa.X|R~
) 6v(?Lr`D
(set name value) Gf` `0F)
(setq i (1+ i)) vz'/]E
) 4eOS+&
(close fp) 9yla &XTD
))
i+r h&,
) L-.
+yNX)
********************* f@Oi$9CZn
(defun record (dimt) !R@v\Eu
(cond ((<= dimt 3) 1) ((and (> dimt 3) (<= dimt 6)) 2) ((and (> dimt 6) (<= dimt 10)) 3) ((and (> dimt 10) (<= dimt 14)) 4) ;dimt为获取的基本尺寸 …… ;因篇幅所限,以下数行略。 <k59Ni9
) ) wD+4#=/j
四、自动标注公差 #Bu W
为使标注的尺寸公差与基本尺寸协调,在标注前先根据基本尺寸的字高、旋转角等确定出公差的字高和旋转角,并计算出上、下偏差的标注位置,然后用“Text”命令标注公差。程序在设计中,考虑到了只知道上、下偏差值而不知道公差等级和公差代号的情况(此时可不输入公差等级和代号,而直接输入上、下偏差),同时也考虑到了某些特殊情形,如±0.01等等。 ;ED` 7
源程序(文件名为gcbz..lsp)如下: _{$eOwB
(defun c:gcbz() (setq ss (entget (car (entsel)))) (setq gcdh (getstring " 输入公差带代号:")) (setq p (getpoint " 输入插入点:")) b ]A9$-
(setq dim (cdr (assoc 1 ss))) QX`Qnk|Y
(setq dim1 dim) (%p@G5GU
(progn 9BW"^$
(if (= (substr dim 1 1) "R") (setq dim1 (substr dim 2))) )wT@`p"4
(if (= (substr dim 1 1) "r") (setq dim1 (substr dim 2))) FFC"rG
(if (= (substr dim 1 3) "%%c") (setq dim1 (substr dim 4))) JK.<(=y\
(if (= (substr dim 1 4) "\\A1;") (setq dim1 (substr dim 5))) :Y4m3|
(if (or (= (substr dim 1 5) "\\A1;R") (= (substr dim 1 5) "\\A1;r")) (setq |.]sL0;4Z
dim1 (substr dim 6))) #Y|t,x;
(if (= (substr dim 1 7) "\\A1;\\U+") (setq dim1 (substr dim 12))) oUSv)G.zb
);获取各类尺寸的尺寸数值 M/<>'%sj
(setq dimt (atof dim1)) H5MO3DJ
(setq k (record dimt));调用函数,获取数据行号 nulLK28q
(read k "d:/data.txt");读取数据 (if (= gcdh "") (setq gc11 (getreal " 输入上偏差:") gc22 (getreal " 输入下偏差:")) y}aKL(AaU
) pAdx 6
(if (= gcdh "f6") (setq gc11 h2 gc22 h3)) $W_sIS0\z
(if (= gcdh "h7") (setq gc11 h1 gc22 h4)) ;取得上、下偏差值 …… ; 以下数行从略 (setq gc1 (rtos gc11 2 4) gc2 (rtos gc22 2 4)) U+[ p>iP
(setq txth (cdr (assoc 40 ss))); dMw7UJ
(setq angr (cdr (assoc 50 ss))) T( LlNq
(setq hi (* txth 0.5)) <PMQ$s>KK
(setq angd (* (/ angr pi) 180)) #/"?.Z;SSH
(setq p1 (polar p (+ (/ pi 2) angr) 0.2)) PvHX#wJ
(setq p2 (polar p1 (+ (* pi 1.5) angr) (+ hi 0.8))) $X\deJ1Hi
(setq p3 (polar p1 angr 1.8)) |f+`FOliP
(setq tole (strcat "%%p" gc1)) _|^cudRv
(if (> gc11 0) (setq gc1 (strcat "+" gc1))) n,Gvgf
(if (> gc22 0) (setq gc2 (strcat "+" gc2))) OV>JmYe1{/
(if (= (abs gc11) (abs gc22)) (command "text" p2 txth angd tole "")) X}fu $2
(if (/= (abs gc11) (abs gc22)) gPJZpaS
(progn 8?l/x
(command "text" p1 hi angd gc1 "") j'IZ etT
(command "text" p2 hi angd gc2 "") !_i;6UVG
) PS:"mP7n
) eVRPjVzQ'Q
) [\BLb8
五、程序运行及菜单定制 vRI0fDu
将程序放在Support\子目录下,在AutoCAD环境下用load函数将程序装入;也可用tools/application装入;或者直接将文件放在ACADR14.LSP文件中。这样,在运行AutoCAD时可直接将该程序装入。为方便标注,可在下拉菜单Dimension段增加尺寸公差标注项,具体方法如下:打开菜单文件ACAD.MNU,在**DIMENSION下增加[尺寸公差标注]^C^CGCBZ即可。 ]K0G!T R<
六、结束语 v?O6|0#x
本程序已在AutoCAD R14、AutoCAD2000上调试通过,可函盖全部机械设计中的公差标注类型,可大大提高标注尺寸公差的速度。 59k[A~)~
将常用的公差数值以数据文件形式存贮,利用Autolisp语言编程,自动检索尺寸公差数值来实现尺寸公差的自动标注,是一种很好的手段。