用AutoCAD进行机械设计时,标注尺寸公差是设计人员经常遇到的一个问题。用Text命令手工标注,不仅速度慢,而且容易出错;通过对话框设定参数进行标注,每个不同的公差值都要设定一次。这些方法在标注时都需要翻阅手册,给设计者带来很大的不便。曾有一些标注公差的程序,使用效果并不很理想。笔者利用Autolisp语言编写了一个尺寸公差自动标注的程序。使用该程序进行公差标注不必翻阅手册,只需选择基本尺寸和公差带代号即可自动标注尺寸公差,可大大提高标注公差的速度。 |:`f#H
一、公差值数据存贮 V#^~JJW^
在标注公差过程中,对于一定的基本尺寸而言,能否根据基本尺寸大小、公差等级和基本偏差代号迅速查询出上、下偏差值,是实现公差自动标注的关键。为此,可将国标中常用的极限偏差以Data·txt为文件名保存在数据文件中。数据文件格式如下: FD*`$.e3\
f6 g6 h6 h7 AYd7qx:~
…… g1JD8~a
(… -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 BS>|M}G)r
(… -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 JSt%L|}Y
…… #tKc!]m
第一行为公差带代号,实际文件中没有。若公差带代号为g6,基本尺寸为45mm,则其上偏差为-0.009mm,下偏差为-0.025mm。因查询函数是以字符位置获取上、下偏差的,所以公差数值表应整齐,否则获取的公差值将出错。 7qV_QZ!.
二、基本尺寸的获取 rv1kIc5Za<
用Entget、Substr函数获取基本尺寸dimt、字高Txth及旋转角Angd,便于在标注公差时确定公差的字高、角度等参数。这里要注意,在尺寸标注前Dimaso应设定为off,否则取不到尺寸。对于基本尺寸前有R、r、Φ等符号时,应作特殊处理。对用“Text”命令标注的尺寸和用“Dimension”命令标注的尺寸,处理方法略有不同,详见源程序。 |!i3Y=X
三、公差数值的查询 j./3 )
为自动查询出所需的公差数值,我们高设定了两个函数:首先用Record()函数检索出数据文件中符合基本尺寸(dimt)范围的数据行,用Read()函数将该行中所有公差值赋给相应的变量hi,以便将相应的公差值设置为上、下偏差。例如,当获取的基本尺寸为40mm、输入的公差带代号为h7时,Record()得到的数据行号为8,用Read()将该行上的公差数值全部赋给hi,则h29为上偏差,h30为下偏差值。相应的程序如下: (EIdw\
(defun read (record fname) kWc%u-_
(setq fp (open fname "r")) %+ig7a:
(if (= fp nil) F*o{dLJ)
(alert "not open datatxt!") bKYLBu:
(progn "X g@X5BG
(repeat record (setq h (read (read-line fp)))) uO>$,s
(setq n (length h)) Ku*@4#<L6h
(setq b '() i 1) "3LOL/7f
(while (<= i n) bdk"7N
(setq b (append b (list (read (strcat "h" (itoa i)))))) 9kuL1tcY
(setq i (1+ i)) U")~bU
) 7gfNe kr~W
(setq i 0) `MlQPLH
(while (< i n) 'ADt<m_$
(setq name (nth i b) 49^;T;'v
value (nth i h) BJ<hP9#
) ` QXO+'j4
(set name value) JGX E{FT
(setq i (1+ i)) 2PE|4zG
) @zB {Ig
(close fp) ~t n*y4uK
)) {3lsDU4
) 28C/^4
********************* [!,&A{.!
(defun record (dimt) >B U0B
(cond ((<= dimt 3) 1) ((and (> dimt 3) (<= dimt 6)) 2) ((and (> dimt 6) (<= dimt 10)) 3) ((and (> dimt 10) (<= dimt 14)) 4) ;dimt为获取的基本尺寸 …… ;因篇幅所限,以下数行略。
@D^y<7(
) ) nZ>qM]">u
四、自动标注公差 HsGXb\
为使标注的尺寸公差与基本尺寸协调,在标注前先根据基本尺寸的字高、旋转角等确定出公差的字高和旋转角,并计算出上、下偏差的标注位置,然后用“Text”命令标注公差。程序在设计中,考虑到了只知道上、下偏差值而不知道公差等级和公差代号的情况(此时可不输入公差等级和代号,而直接输入上、下偏差),同时也考虑到了某些特殊情形,如±0.01等等。 iUFS1SN \
源程序(文件名为gcbz..lsp)如下: %N7b
XKDP
(defun c:gcbz() (setq ss (entget (car (entsel)))) (setq gcdh (getstring " 输入公差带代号:")) (setq p (getpoint " 输入插入点:")) L&MR%5
(setq dim (cdr (assoc 1 ss))) E#v}//
(setq dim1 dim) lPSyFb"
(progn [U]U *x
(if (= (substr dim 1 1) "R") (setq dim1 (substr dim 2))) _(5SiK R
(if (= (substr dim 1 1) "r") (setq dim1 (substr dim 2))) qxf!]jm
(if (= (substr dim 1 3) "%%c") (setq dim1 (substr dim 4))) #Gx%PQ`
(if (= (substr dim 1 4) "\\A1;") (setq dim1 (substr dim 5))) 6!bVPIyYO
(if (or (= (substr dim 1 5) "\\A1;R") (= (substr dim 1 5) "\\A1;r")) (setq *BH*
dim1 (substr dim 6))) $[T^S
(if (= (substr dim 1 7) "\\A1;\\U+") (setq dim1 (substr dim 12))) >A|(mc
);获取各类尺寸的尺寸数值 IP7j)SM!
(setq dimt (atof dim1)) 2Hw&}8
(setq k (record dimt));调用函数,获取数据行号 !qS~YA
(read k "d:/data.txt");读取数据 (if (= gcdh "") (setq gc11 (getreal " 输入上偏差:") gc22 (getreal " 输入下偏差:")) z6]dF"N
) UBzX%:A
(if (= gcdh "f6") (setq gc11 h2 gc22 h3)) J:Ea|tXK^
(if (= gcdh "h7") (setq gc11 h1 gc22 h4)) ;取得上、下偏差值 …… ; 以下数行从略 (setq gc1 (rtos gc11 2 4) gc2 (rtos gc22 2 4)) 0f&B;?)!
(setq txth (cdr (assoc 40 ss))); /17Qhex
(setq angr (cdr (assoc 50 ss))) E^rKS&P
(setq hi (* txth 0.5)) Yrs7F.Y"
(setq angd (* (/ angr pi) 180)) 9Bvi2
3
(setq p1 (polar p (+ (/ pi 2) angr) 0.2)) /W1!mih
(setq p2 (polar p1 (+ (* pi 1.5) angr) (+ hi 0.8))) %OB:lAeJ
(setq p3 (polar p1 angr 1.8)) -KhNsUQk
(setq tole (strcat "%%p" gc1)) y^zII5|s
(if (> gc11 0) (setq gc1 (strcat "+" gc1))) <k!M+}a 9V
(if (> gc22 0) (setq gc2 (strcat "+" gc2))) 'i;1n
(if (= (abs gc11) (abs gc22)) (command "text" p2 txth angd tole "")) O;z:?
(if (/= (abs gc11) (abs gc22)) {^=T&aCYdS
(progn yhv(KI
(command "text" p1 hi angd gc1 "") <K;
(command "text" p2 hi angd gc2 "") g>-pC a
) 0^$L{V
) H )hO/1m
) 3u#bx1
五、程序运行及菜单定制 z/!LC;(
将程序放在Support\子目录下,在AutoCAD环境下用load函数将程序装入;也可用tools/application装入;或者直接将文件放在ACADR14.LSP文件中。这样,在运行AutoCAD时可直接将该程序装入。为方便标注,可在下拉菜单Dimension段增加尺寸公差标注项,具体方法如下:打开菜单文件ACAD.MNU,在**DIMENSION下增加[尺寸公差标注]^C^CGCBZ即可。 nNz1gV:0X
六、结束语 E-IV v
本程序已在AutoCAD R14、AutoCAD2000上调试通过,可函盖全部机械设计中的公差标注类型,可大大提高标注尺寸公差的速度。 [~*5uSG
将常用的公差数值以数据文件形式存贮,利用Autolisp语言编程,自动检索尺寸公差数值来实现尺寸公差的自动标注,是一种很好的手段。