AutoCAD中尺寸公差的自動標注
AutoCAD中尺寸公差標注很麻煩,本文給出一短小AutoLISP程序,實現了公差值的自動標準,通過鼠標兩次選擇即可完成其操作,使用起來非常方便、快捷。
?
AutoCAD在用于機械設計時,公差標注有兩種方法;其一是通過DIM參數設定完成,但參數設定繁瑣,速度也慢,每一個不同公差值都要設定一次,在R12 0中名義尺寸與公差值的小數點位數相同使名義尺寸顯得累贅(R13 0對此作了改進);其二是用TEXT指令直接寫入,但速度也慢,字的位置也不易寫準,常需用MOVE指令移動一次。上述兩種方法都需在作圖時備公差數值表,先查表后標注,因此在尺寸公差標注上所花時間很多。
筆者用AutoLisp編寫了一尺寸公差自動標注程序,使用效果良好,速度也快,調用時僅兩次操作即可完成:首次操作選擇公差等級;再次操作選擇被標注尺寸即可完成尺寸公差的自動標注。程序由四部分組成:輸入公差等級自動查表;選取被標注尺寸并進行相應處理;尺寸大小分類并查取上下偏差值;公差值寫入。若將該指令加入菜單后操作起來將更方便,即將公差等級符號(如H7、b6、r6、JS7等)做成幻燈片在菜單上調用。
一、輸入公差等級和查表
在菜單上選擇了公差標準條目后、屏幕上出現相應幻燈片(如圖示)當選取相應公差等級的項目后,完成了兩個指令輸入:首先給出了公差等級(實際上是給出了在公差值表中該等級的相應行號n值);其次是啟動了公差標準Lisp程序。Lisp程序啟動后,打開公差值表(Tole.TXT)使用repeat函數在公差值表中連續讀取一行數據,至直與所標公差等級相應的第n行為止。
若所標公差為H8,則菜單上相應行內容為:
[DAN(LH8)]^C^P(setg n 3)tolerance
若所標公差為JS6,則菜單上相應行內容為:
[DAN(JS6)]^C^P(setg n 10)tolerance
公差標注幻燈片
(注DAN為幻燈片庫名 DAN.SLB,LH8、JS6為幻燈片名LH8.SLD和JS6.SLP,tolerance為公差標準Lisp程序指令名)
若不做幻燈片則在啟動Lisp前先鍵入Lisp變量n的值。為使標注更方便,操作當前層自動換至尺寸標注層(DIM層)。
二、選取被標注尺寸并進行處理
根據Lisp提示在圖形中選取被標注尺寸,通過相應處理,得到了該尺寸的數值、字體高度、位置、角度等留作備用,使用的函數是entget和assoc。在獲取被標注尺寸時,使用了Substr函數將圓標注尺寸前面的R、r、Φ隱去便于后續數值大小判別。同時還判別了該尺寸是否帶有小數點,可使公差值寫入時位置更準確,因為小數點所占不到一個字寬。
三、尺寸大小分類并獲取相應上下限偏差值
在第一步查表所得的數據行含有該公差等級中的全部上下限數值(GB1801-79表中的一列結合第二步獲取的被標注尺寸數值,本步即可查出被標注尺寸的上下限偏差值,選用Cond函數判別偏差值所處位置,再用Substr函數將其讀出,如若被標注尺寸為40,則上偏差值為第75字符起的6個字符,下偏差值為第82字符起的6個字符,要求Tole.TXT數據表應豎列整齊。
四、公差值寫入
用TEXT指令將公差值寫入,其寫入位置依據名義尺寸的位數及是否有小數點算出,角度字高也隨名義尺寸變化。對JS級公差作特殊處理(n=10、11、12*)。
為了使Lisp具有通用性,對絕對值公差(如+0.01 0、+0.02 0、0 +0.01、*0.01 等)也作了考慮,因為這些公差常用的不多,直接寫入了Lisp程序。程序中n從40起,數據表中并沒有第40行以及后續行。
該Lisp程序不大,但函蓋了全部機械設計中的公差標注類型如相對公差(國標)、絕對公差、一般尺寸、半徑尺寸。為了使標注美觀還考慮了小數點,使用了While函數可對同公差等級的尺寸連續標注。
五、幾點說明
1 由于需獲取尺寸標注的名義尺寸,故在尺寸標注前DIMASO應設定為DFF,否則取不到尺寸數值。
2 程序是以字符位置取上、下限偏差,故公差數值表(tole.TXT)中應整齊,只能用純文本編輯軟件編寫(如EDIT)。
3 幻燈片編排可根據使用頻度來安排,本人是用AutoCAD進行模具設計,故幻燈片第一頁20個做了上述排序,讀者可根據實際情況作調整。
4本程序在AutoCAD R11.0 R12.0、R13.0上通過,源程序和公差值表如下:
(defun c:tolerance ()
(setq txt (open "tole.txt" "r"))
?(repeat n (setq tols$ (read-line txt)))
?(close txt)
(setvar "cmdecho" 0) (command "layer" "s" "dim" "")
(while T
?(setq obj (entget (car (entsel))))
?(setq posi (assoc 10 obj))
?(setq txth (cdr (assoc 40 obj)))
?(setq toleh (* 0.6 txth))
?(setq angr (cdr (assoc 50 obj)))
?(setq angd (* (/ angr pi) 180))
?(setq dim$ (cdr (assoc 1 obj)))
?(setq ln (strlen dim$))
?(if (= (substr dim$ 1 1) "R") (setq dim$ (substr dim$ 2 (- ln 1))))
?(if (= (substr dim$ 1 1) "r") (setq dim$ (substr dim$ 2 (- ln 1))))
?if (= (substr dim$ 1 1) "%") (setq dim$ (substr dim$ 4 (- ln 3)) ln (- ln 2)))
?(setq dimt (atof dim$))
?(setq lupr (getvar "luprec"))
?(setvar "luprec" 0)
?(if (= (strlen dim$) (strlen (rtos (atoi dim$)))) (setq ln (+ ln 0.7)))
?(setvar "luprec" lupr)
?(cond ((and (<= dimt 3) (> dimt 0)) (setq st1 5 st2 12))
?((and (<=? dimt 6) (> dimt 3)) (setq st1 19 st2 26))
?((and (<=? dimt 10) (> dimt 6)) (setq st1 33 st2 40))
?((and (<=? dimt 18) (> dimt 10)) (setq st1 47 st2 54))
?((and (<=? dimt 30) (> dimt 18)) (setq st1 61 st2 68))
?((and (<=? dimt 50) (> dimt 30)) (setq st1 75 st2 82))
?((and (<=? dimt 80) (> dimt 50)) (setq st1 89 st2 96))
?((and (<=? dimt 120) (> dimt 80)) (setq st1 103 st2 110 ))
?((and (<= dimt 180) (> dimt 120)) (setq st1 117 st2 124))
?((and (<= dimt 250) (> dimt 180)) (setq st1 131 st2 138))
?((and (<= dimt 315) (> dimt 250)) (setq st1 145 st2 152))
? ((and (<= dimt 400) (> dimt 315)) (setq st1 159 st2 166))
?((and (<= dimt 500) (> dimt 400)) (setq st1 173 st2 180))
?((and (<= dimt 630) (> dimt 500)) (setq st1 187 st2 194))
?)
?(setq tole1$ (substr tols$ st1 6) tole2$ (substr tols$ st2 6))
?(setq x1 (+ (cadr posi) (* (cos angr) (* (- ln 1.2) txth))))
?(setq y1 (+ (caddr posi) (* (sin angr) (* (- ln 1.2) txth))))
?(setq x2 (+ x1 (* (cos (+ angr 1.5708)) (* 0.85 txth))))
?(setq y2 (+ y1 (* (sin (+ angr 1.5708)) (* 0.85 txth))))
?(setq xy1 (list x1 y1))
?(setq xy2 (list x2 y2))
?(if (or (= n 10) (= n 11) (= n 12) (= n 13) (= n 27) (= n 28))
?(progn (setq tole$ (strcat "%%p" tole1$))(command "text" xy1 txth angd tole$))
?(progn (if (= n 40) (setq tole1$ "+0.01" tole2$ " 0"))
?(if (= n 41) (setq tole1$ "+0.02" tole2$ " 0"))
?(if (= n 42) (setq tole1$ "+0.05" tole2$ " 0"))
(if (= n 43) (setq tole1$ "+0.10" tole2$ " 0"))
?(if (= n 48) (setq tole1$ " 0" tole2$ "-0.10"))
?(if (= n 47) (setq tole1$ " 0" tole2$ "-0.05"))
?(if (= n 46) (setq tole1$ " 0" tole2$ "-0.02"))
? (if (= n 45) (setq tole1$ " 0" tole2$ "-0.01"))
?(command "text" xy2 toleh angd tole1$ "text" xy1 toleh angd tole2$))
? )
?)
)(princ)
H6 |+0.006 0 +0.008 0 +0.009 0 +0.011 0 +0.013 0 +0.016 0 …
H7 |+0.010 0 +0.012 0 +0.015 0+0.018 0 +0.021 0 +0.025 0 …
H8 |+0.014 0 +0.018 0 +0.022 0+0.027 0 +0.033 0 +0.039 0 …
H9 |+0.025 0 +0.030 0 +0.036 0+0.043 0 +0.052 0 +0.062 0 …
G7 |+0.012 +0.002 +0.016 +0.004 +0.020 +0.005 +0.024+0.006 +0.028 +0.007 +0.034 +0.009 …
K7 | 0 -0.010 +0.003 -0.009 +0.005 -0.010 +0.006 -0.012 +0.006 -0.015 +0.007 -0.018 …
N7 |-0.004 -0.014 -0.004 -0.016 -0.004 -0.019 -0.005-0.023 -0.007 -0.028 -0.008 -0.033 …
S7 |-0.014 -0.024 -0.015 -0.027 -0.017 -0.032 -0.021 -0.039 -0.027 -0.048 -0.034 -0.059 …
U7 |-0.018 -0.028 -0.019 -0.031 -0.022 -0.037 -0.026-0.044 -0.033 -0.054 -0.051 -0.076 …
JS6 |0.003 0.004 0.005 0.006 0.007 0.008 …
JS7 |0.005 0.006 0.007 0.009 0.010 0.012 …
JS8 |0.007 0.009 0.011 0.013 0.016 0.019 …
JS9 |0.012 0.015 0.018 0.021 0.026 0.031 …
h6 | 0 -0.006 0 -0.008 0 -0.009 0 -0.011 0 -0.013 0 -0.016 …
h7 | 0 -0.010 0 -0.012 0 -0.015 0 -0.018 0 -0.021 0 -0.025 …
h8 | 0 -0.014 0 -0.018 0 -0.022 0 -0.027 0 -0.033 0 -0.039 …
h9 | 0 -0.025 0 -0.030 0 -0.036 0 -0.043 0 -0.052 0 -0.062 …
m6 |+0.008 +0.002 +0.012 +0.004 +0.015 +0.006 +0.018 +0.007 +0.021 +0.008 +0.025 +0.009 …
m7 |+0.012 +0.002 +0.016 +0.004 +0.021 +0.006 +0.025 +0.007 +0.029 +0.008 +0.034 +0.009 …
g6 |-0.002 -0.008 -0.004 -0.012 -0.005 -0.014 -0.006 -0.017 -0.007 -0.020 -0.009 -0.025 …
k6 |+0.006 0 +0.009 +0.001 +0.010 +0.001 +0.012 +0.001 +0.015 +0.002 +0.018 +0.002 …
n6 |+0.010 +0.004 +0.016 +0.008 +0.019 +0.010 +0.023 +0.012 +0.028 +0.015 +0.033 +0.017 …
s6 |+0.020 +0.014 +0.027 +0.019 +0.032 +0.023 +0.039 +0.028 +0.048 +0.035 +0.059 +0.043 …
u6 |+0.024 +0.018 +0.031 +0.023 +0.037 +0.028 +0.044 +0.033 +0.054 +0.041 +0.076 +0.060 …
f7 |-0.006 -0.016 -0.010 -0.022 -0.013 -0.028 -0.016 -0.034 -0.020 -0.041 -0.025 -0.050 …
f8 |-0.006 -0.020 -0.010 -0.028 -0.013 -0.035 -0.016 -0.043 -0.020 -0.053 -0.025 -0.064 …
公差值表tole.txt
評論