(defun C:GEAR ()
(setvar "CMDECHO" 0)
(setq osmode (getvar "OSMODE"))
(setvar "OSMODE" 0)
(prompt
"\n-------------------------------------------------"
)
(setq N (getint "\n输入齿数:")
DP (getreal "\n输入节圆直径:")
SD (getreal "\n输入安装轴直径:")
F 0
)
;================================================================
(setq PA 14.5 ;PRESSURE ANGLE
RD (/ 180 3.1416) ;CONVERSION TO RADIANS
P (/ (float N) DP) ;DIAMETRAL PITCH
A (/ 1.0 P) ;ADDENDUM
AC (+ (/ 1.0 P) DP) ;ADDENDUM CIRCLE
B (/ 1.157 P) ;DEDENTUM
DC (/ (+ N 2) P) ;DEDENTUM CIRCLE
BC (* DP (COS (/ PA RD))) ;BASE CIRCLE
AB (/ 360.0 N) ;ANGLE BETWEEN CENTER OF TEETH
TC (* DP (SIN (/ 90.0 RD N))) ;CHORDAL THICKNESS
DR (- DP (* 2 B)) ;ROOT CIRCLE
WC (- AC (* 2 A)) ;WORKING DEPTH CIRCLE
DO (+ DP (* 2 A)) ;OUTSIDE DIAMETER
FI (/ 0.157 DP) ;TOOTH FILLET
PR (/ DP 2.0)
) ;RADIUS OF PITCH DIAMETER
(IF (< (/ DP 8.0) TC)
(prompt "\007More teeth required!")
(progn ; ELSE
(command "LIMITS"
(list X Y)
(list XX YY)
"ZOOM"
"ALL"
"APERTURE"
"40"
"VIEWRES"
"N"
"20000"
"LAYER"
"NEW"
"RED,GREEN,BLUE"
"COLOR"
"RED"
"RED"
"COLOR"
"GREEN"
"GREEN"
"COLOR"
"BLUE"
"BLUE"
"SET"
"RED"
""
"ELEV"
0
F
)
;Draw initial circles
(command "CIRCLE"
(list 0 0)
"D"
DO
"CIRCLE"
(list 0 0)
"D"
BC
"CIRCLE"
(list 0 0)
"D"
WC
"LINE"
(list 0 (/ DR 2.0))
(list 0 (/ DO 2.0))
""
"LINE"
(list (* -1.0 (/ TC 2.0)) (/ DP 2.0))
(list (/ TC 2.0) (/ DP 2.0))
""
"LAYER"
"S"
"GREEN"
""
"ZOOM"
"W"
(list (* -1.0 (/ SD 2.0)) 0.0)
(list (/ SD 2.0) (+ (/ SD 2.0) (/ SD 4.0)))
""
"ELEV"
0
0
"CIRCLE"
(list 0 0)
"D"
(+ SD (/ SD 2.0))
"ELEV"
0
0
)
;Draw shaft
(command "CIRCLE"
(list 0 0)
"D"
(+ SD (/ SD 2.0))
"ELEV"
0
0
"CIRCLE"
(list 0 (/ SD 2.0))
"D"
(/ SD 4.0)
"ELEV"
0
0
"CIRCLE"
(list 0 0)
"D"
SD
)
;Construct keyway
(command "LINE"
(osnap (list (* -1.0 (/ SD 8.0)) (/ SD 2.0)) "APP")
(list (* -1.0 (/ SD 8.0)) (+ (/ SD 2.0) (/ SD 8.0)))
(list (/ SD 8.0) (+ (/ SD 2.0) (/ SD 8.0)))
(osnap (list (/ SD 8.0) (/ SD 2.0)) "APP")
""
"ERASE"
(osnap (list 0 (- (/ SD 2.0) (/ SD 8.0))) "NEA")
""
"BREAK"
(osnap (list 0 (/ SD 2.0)) "NEA")
"F"
(osnap (list (/ SD 8.0) (/ SD 2.0)) "APP")
(osnap (list (* -1.0 (/ SD 8.0)) (/ SD 2.0)) "APP")
"ELEV"
"0"
F
"CIRCLE"
(list 0 0)
"D"
DR
)
;Work on gear teeth
(command "ZOOM"
"E"
"ZOOM"
"W"
(list (* -1.0 (/ DP 8.0)) (- (/ DP 2.0) (/ DP 8.0)))
(list (/ DP 8.0) (+ (/ DP 2.0) (/ DP 8.0)))
"CIRCLE"
(osnap (list (* -1.0 (/ TC 2.0)) (/ DP 2.0)) "END")
(/ DP 8.0)
)
(setq ESave (entlast))
(command "CIRCLE"
(list 0 0)
"D"
Dp
"ERASE"
"L"
""
)
(setq DP2 (/ DP 2.0)
TC2 (/ TC 2.0)
DP8 (/ DP 8.0)
)
(command "CIRCLE"
(osnap (list (- DP8 TC2) (/ BC 2.0)) "APP")
DP8
)
(entdel Esave)
(command "BREAK"
(osnap (list (- DP8 TC2) (+ (/ BC 2.0) DP8)) "NEA")
"F"
(osnap (list (* -1.0 TC2) (/ DR 2.0)) "APP")
(osnap (list (* -1.0 (/ TC 4.0)) (/ DO 2.0)) "APP")
"MIRROR"
"W"
(list (* -1.0 TC) (- (/ DR 2.0) 1.0))
(list 0 (+ (/ DO 2.0) 1.0))
""
(list 0 0)
(list 0 DO)
"N"
"LINE"
(osnap (list (* -1.0 (/ TC 4.0)) (/ DO 2.0)) "APP")
(osnap (list (/ TC 4.0) (/ DO 2.0)) "APP")
""
)
;Erase the construction lines 
(command "LAYER"
"OFF"
"*"
"Y"
"ON"
"RED"
""
"ZOOM"
"E"
"ERASE"
"W"
(list (setq TT (* -1.0 (/ DO 2.0))) TT)
(list (setq TT (/ DO 2.0)) TT)
""
"LAYER"
"ON"
"GREEN"
""
)
;Finish gear teeth
(command "ARRAY"
"W"
(list (* -1.0 TC) (- (/ DR 2.0) 0.2))
(list TC (+ (/ DO 2.0) 0.2))
""
"C"
(osnap (list (/ DR 2.0) 0) "CENTER")
AB
N
"Y"
)
(setq m n)
(while (> m 0)
(command "trim"
"all"
""
(osnap (list
(* (sin (* (/ (* 2 pi) n) (- n m))) (/ DR 2))
(* (cos (* (/ (* 2 pi) n) (- n m))) (/ DR 2))
)
"NEAR"
)
""
)
(setq m (- m 1))
)
)
)
(setvar "OSMODE" osmode)
)
