用AutoCAD進(jìn)行機(jī)械設(shè)計時,標(biāo)注尺寸公差是設(shè)計人員經(jīng)常遇到的一個問題。用Text命令手工標(biāo)注,不僅速度慢,而且容易出錯;通過對話框設(shè)定參數(shù)進(jìn)行標(biāo)注,每個不同的公差值都要設(shè)定一次。這些方法在標(biāo)注時都需要翻閱手冊,給設(shè)計者帶來很大的不便。曾有一些標(biāo)注公差的程序,使用效果并不很理想。筆者利用Autolisp語言編寫了一個尺寸公差自動標(biāo)注的程序。使用該程序進(jìn)行公差標(biāo)注不必翻閱手冊,只需選擇基本尺寸和公差帶代號即可自動標(biāo)注尺寸公差,可大大提高標(biāo)注公差的速度。 一、公差值數(shù)據(jù)存貯 在標(biāo)注公差過程中,對于一定的基本尺寸而言,能否根據(jù)基本尺寸大小、公差等級和基本偏差代號迅速查詢出上、下偏差值,是實現(xiàn)公差自動標(biāo)注的關(guān)鍵。為此,可將國標(biāo)中常用的極限偏差以Data·txt為文件名保存在數(shù)據(jù)文件中。數(shù)據(jù)文件格式如下: f6 g6 h6 h7 …… (… -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 (… -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 …… 第一行為公差帶代號,實際文件中沒有。若公差帶代號為g6,基本尺寸為45mm,則其上偏差為-0.009mm,下偏差為-0.025mm。因查詢函數(shù)是以字符位置獲取上、下偏差的,所以公差數(shù)值表應(yīng)整齊,否則獲取的公差值將出錯。 二、基本尺寸的獲取 用Entget、Substr函數(shù)獲取基本尺寸dimt、字高Txth及旋轉(zhuǎn)角Angd,便于在標(biāo)注公差時確定公差的字高、角度等參數(shù)。這里要注意,在尺寸標(biāo)注前Dimaso應(yīng)設(shè)定為off,否則取不到尺寸。對于基本尺寸前有R、r、Φ等符號時,應(yīng)作特殊處理。對用“Text”命令標(biāo)注的尺寸和用“Dimension”命令標(biāo)注的尺寸,處理方法略有不同,詳見源程序。 三、公差數(shù)值的查詢 為自動查詢出所需的公差數(shù)值,我們高設(shè)定了兩個函數(shù):首先用Record()函數(shù)檢索出數(shù)據(jù)文件中符合基本尺寸(dimt)范圍的數(shù)據(jù)行,用Read()函數(shù)將該行中所有公差值賦給相應(yīng)的變量hi,以便將相應(yīng)的公差值設(shè)置為上、下偏差。例如,當(dāng)獲取的基本尺寸為40mm、輸入的公差帶代號為h7時,Record()得到的數(shù)據(jù)行號為8,用Read()將該行上的公差數(shù)值全部賦給hi,則h29為上偏差,h30為下偏差值。相應(yīng)的程序如下: (defun read
(record fname) (setq fp (open
fname "r")) (if (= fp nil) (alert "not
open datatxt!") (progn (repeat record
(setq h (read (read-line fp)))) (setq n (length
h)) (setq b '() i 1) (while (<= i
n) (setq b (append b
(list (read (strcat "h" (itoa i)))))) (setq i (1+ i)) ) (setq i 0) (while (< i n) (setq name (nth i
b) value (nth i h) ) (set name value) (setq i (1+ i)) ) (close fp) )) ) ********************* (defun record
(dimt) (cond ((<=
dimt 3) 1) ((and (> dimt 3) (<= dimt 6)) 2) ((and (> dimt 6) (<= dimt 10)) 3) ((and (> dimt 10)
(<= dimt 14)) 4) ;dimt為獲取的基本尺寸 …… ;因篇幅所限,以下數(shù)行略。 ) ) 四、自動標(biāo)注公差 為使標(biāo)注的尺寸公差與基本尺寸協(xié)調(diào),在標(biāo)注前先根據(jù)基本尺寸的字高、旋轉(zhuǎn)角等確定出公差的字高和旋轉(zhuǎn)角,并計算出上、下偏差的標(biāo)注位置,然后用“Text”命令標(biāo)注公差。程序在設(shè)計中,考慮到了只知道上、下偏差值而不知道公差等級和公差代號的情況(此時可不輸入公差等級和代號,而直接輸入上、下偏差),同時也考慮到了某些特殊情形,如±0.01等等。 源程序(文件名為gcbz..lsp)如下: (defun c:gcbz()
(setq ss (entget (car (entsel)))) (setq gcdh (getstring "\n 輸入公差帶代號:")) (setq p
(getpoint "\n 輸入插入點:")) (setq dim (cdr
(assoc 1 ss))) (setq dim1 dim) (progn (if (= (substr
dim 1 1) "R") (setq dim1 (substr dim 2))) (if (= (substr
dim 1 1) "r") (setq dim1 (substr dim 2))) (if (= (substr
dim 1 3) "%%c") (setq dim1 (substr dim 4))) (if (= (substr
dim 1 4) "\\A1;") (setq dim1 (substr dim 5))) (if (or (=
(substr dim 1 5) "\\A1;R") (= (substr dim 1 5) "\\A1;r"))
(setq dim1 (substr dim
6))) (if (= (substr
dim 1 7) "\\A1;\\U+") (setq dim1 (substr dim 12))) );獲取各類尺寸的尺寸數(shù)值 (setq dimt (atof
dim1)) (setq k (record
dimt));調(diào)用函數(shù),獲取數(shù)據(jù)行號 (read k
"d:/data.txt");讀取數(shù)據(jù) (if (= gcdh "") (setq gc11 (getreal "\n 輸入上偏差:") gc22 (getreal "\n 輸入下偏差:")) ) (if (= gcdh
"f6") (setq gc11 h2 gc22 h3)) (if (= gcdh
"h7") (setq gc11 h1 gc22 h4)) ?。蝗〉蒙?、下偏差值 …… ; 以下數(shù)行從略 (setq gc1 (rtos gc11 2 4) gc2 (rtos gc22 2 4)) (setq txth (cdr
(assoc 40 ss))); (setq angr (cdr
(assoc 50 ss))) (setq hi (* txth
0.5)) (setq angd (* (/
angr pi) 180)) (setq p1 (polar p
(+ (/ pi 2) angr) 0.2)) (setq p2 (polar
p1 (+ (* pi 1.5) angr) (+ hi 0.8))) (setq p3 (polar
p1 angr 1.8)) (setq tole
(strcat "%%p" gc1)) (if (> gc11 0)
(setq gc1 (strcat "+" gc1))) (if (> gc22 0)
(setq gc2 (strcat "+" gc2))) (if (= (abs gc11)
(abs gc22)) (command "text" p2 txth angd tole "")) (if (/= (abs
gc11) (abs gc22)) (progn (command
"text" p1 hi angd gc1 "") (command
"text" p2 hi angd gc2 "") ) ) ) 五、程序運(yùn)行及菜單定制 將程序放在Support\子目錄下,在AutoCAD環(huán)境下用load函數(shù)將程序裝入;也可用tools/application裝入;或者直接將文件放在ACADR14.LSP文件中。這樣,在運(yùn)行AutoCAD時可直接將該程序裝入。為方便標(biāo)注,可在下拉菜單Dimension段增加尺寸公差標(biāo)注項,具體方法如下:打開菜單文件ACAD.MNU,在**DIMENSION下增加[尺寸公差標(biāo)注]^C^CGCBZ即可。 六、結(jié)束語 本程序已在AutoCAD R14、AutoCAD2000上調(diào)試通過,可函蓋全部機(jī)械設(shè)計中的公差標(biāo)注類型,可大大提高標(biāo)注尺寸公差的速度。 將常用的公差數(shù)值以數(shù)據(jù)文件形式存貯,利用Autolisp語言編程,自動檢索尺寸公差數(shù)值來實現(xiàn)尺寸公差的自動標(biāo)注,是一種很好的手段。
|