一、齒輪輪齒漸開漸開線

(defun c:gear(/)
(
setq
p0 (getpoint"輸入齒輪中心p0: ")
m (getreal"輸入齒輪模數(shù)M: ")
z (getint"輸入齒輪齒數(shù)Z: ")
ha 1
c 0.25
a (/ pi 9)
ra(/ (* (+ z (+ ha ha)) m) 2)
rf(/ (* (- z (+ (+ ha ha) c)) m) 2)
r(/ ( * m z) 2)
rb ( * r (cos a))
ri rb
ang 0
g (polar p0 ang rb)
)
(command "circle" p0 ra)
(command "circle" p0 rf)
(command "circle" p0 r)
(command "line" g)

(while (< ri ra)
(setq
ang ( + ang(/ pi 360))
ri ( / rb (cos ang))
cta ( - (/ (sin ang) (cos ang)) ang)
g(polar p0 cta ri)
)

(command g)
)
(command )
)


二、螺旋源程序

(defun errMsg (s)
(if (/= s "function cancelled") (princ (strcat "
Error: " s)))
                                            ; 當命令執(zhí)行時出現(xiàn)錯誤
(setvar "cmdecho" ocmdold)                         ; 例如用戶按下了CTRL + C
(setvar "osmode" osmold)
(setq *error* olderr)                                 ; 恢復舊的錯誤處理
(princ)
)

(defun c:3Dthread (/ radouter threadpitch threadlength threadangle
            ptStart innerdiafactor radmid radinner ocmdold osmold 4H 5H 6H h6 g6)
  ;-------------------------------------------------------------------
  ; 獲取公制外徑大小、螺距總長
  ; 然后計算一系列幾何點
  ; 并且關閉對象捕捉、命令回顯
  ;-------------------------------------------------------------------
  (setq 4H (list 0.0015 0.002 0.002 0.0025 0.003 0.0035 0.004 0.005 0.006 0.007 0.008 0.009 0.010))  
  (setq 5H (list 0.002 0.0025 0.003 0.004 0.0045 0.0055 0.0065 0.0075 0.009 0.010 0.0115 0.0125 0.0135))  
  (setq 6H (list 0.003 0.004 0.0045 0.0055 0.0065 0.008 0.0095 0.011 0.0125 0.0145 0.016 0.018 0.020))  
  (setq h6 (list -0.003 -0.004 -0.0045 -0.0055 -0.0065 -0.008 -0.0095 -0.011 -0.0125 -0.0145 -0.016 -0.018 -0.020))  
  (setq g6 (list -0.005 -0.008 -0.0095 -0.0115 -0.0135 -0.017 -0.0195 -0.023 -0.0265 -0.0295 -0.033 -0.036 -0.0515))  

  (setq osmold (getvar "osmode"))                             ; 保存調(diào)用前的osmode系統(tǒng)變量值
  (setq ocmdold (getvar "cmdecho"))                             ; 保存調(diào)用前的cmdecho系統(tǒng)變量值
  (setvar "osmode" 0)                                       ; 關閉對象捕捉      
  (setvar "cmdecho" 0)                                   ; 關閉命令的回顯

  (setq innerdiafactor 1.5)                               ; 設置內(nèi)徑系數(shù)
  (initget 7)                                         ; radmid 必須非零、非空、非負
  (setq radmid (getdist "
公制外徑: "))
  (initget 7)                                         ; threadpitch 必須非零、非空、非負
  (setq threadpitch (getreal "
螺距: "))
  (initget 1)                                         ; ptStart 必須非空
  (setq ptStart (getpoint "
起始點: "))
  (initget 3)                                         ; threadlength 必須非零、非空、非負
  (setq threadlength (getdist "
螺紋總長(Y方向): "))

; 對公制外徑添加公差
  (if (<= radmid 3) (setq order 0)                         ; 根據(jù)公制外徑確定公差帶中位置
  (if (<= radmid 6) (setq order 1)
  (if (<= radmid 10) (setq order 2)
  (if (<= radmid 18) (setq order 3)
  (if (<= radmid 30) (setq order 4)
  (if (<= radmid 50) (setq order 5)
  (if (<= radmid 80) (setq order 6)
  (if (<= radmid 120) (setq order 7)
  (if (<= radmid 180) (setq order 8)
  (if (<= radmid 250) (setq order 9)
  (if (<= radmid 315) (setq order 10)
  (if (<= radmid 400) (setq order 11)
  (if (<= radmid 500) (setq order 12)
  )))))))))))))

  (if (< threadpitch 0.25) (setq radmid (+ radmid (nth order 4H)))           ; 根據(jù)螺距確定公差帶
  (if (< threadpitch 0.35) (setq radmid (+ radmid (nth order 5H)))
  (if (>= threadpitch 0.35) (setq radmid (+ radmid (nth order 6H)))
  )))
 
  (setq h (* 0.866025 threadpitch))                               ; 計算齒高
  (setq radouter (+ radmid (/ h 4)))                             ; 計算外徑
  (setq radinner (- radouter (* h innerdiafactor)))                   ; 計算內(nèi)徑
  (setq threadangle (+ 30 0))                                   ; 計算齒頂角

  (auxithread radouter radmid radinner threadpitch threadlength threadangle ptStart)     ; 調(diào)用繪制三維螺紋的子函數(shù)

  (princ "
三維螺紋創(chuàng)建完成")
  (setvar "osmode" osmold)                                       ; 恢復調(diào)用前的osmode系統(tǒng)變量值
  (setvar "cmdecho" ocmdold)                                       ; 恢復調(diào)用前的cmdecho系統(tǒng)變量值
  (princ)
)

(defun auxithread (radouter radmid radinner threadpitch threadlength threadangle ptStart / ttal pt1z ang pt1a
pt1az pt3a pt1b pt1bz pt3b pttmp1 pttmp2 pttmp3 pttmp4 pttmp5 pttmp6 pttmp7 pttmp8 pttmp9 pttmp10 pttmp11 pttmp12 tstmp startcone endcone)

  ;(command "undo" "begin")                                   ; 開始undo步驟

  (setq ttal (+ (fix (/ (abs threadlength) threadpitch)) 3)
    pttmp1 (list (- (car ptStart) (/ radouter 2.0)) (cadr ptStart) (caddr ptStart))
    pt1z (list (- (car ptStart) (/ radouter 2.0)) (cadr ptStart) (+ (caddr pttmp1) 1.0))
    pttmp2 (polar pttmp1 (/ (* threadangle pi) 180.0) 1)
    pttmp3 (list (+ (car pttmp1) radouter) (+ (cadr pttmp1) (/ threadpitch 2.0)) (caddr ptStart))
    ang (angle pttmp1 pttmp3)
    pt1a (polar pttmp1 (+ ang (/ pi 2.0)) threadpitch)
    pt1az (list (car pt1a) (cadr pt1a) (+ (caddr pt1a) 1.0))
    pt3a (polar pt1a ang radouter)
    pt1b (polar pttmp1 (- ang (/ pi 2.0)) threadpitch)
    pt1bz (list (car pt1b) (cadr pt1b) (+ (caddr pt1b) 1.0))
    pt3b (polar pt1b ang radouter)
    pttmp4 (polar pttmp3 (/ (* (- 180 threadangle) pi) 180.0) 1)
    pttmp5 (inters pttmp1 pttmp2 pttmp3 pttmp4 nil)
    pttmp6 (list (car pttmp5) (cadr ptStart) (caddr ptStart))
    pttmp7 (polar pttmp1 (/ (* (- 360 threadangle) pi) 180.0) 1)
    pttmp8 (polar pttmp3 (/ (* (+ 180 threadangle) pi) 180.0) 1)
    pttmp9 (inters pttmp1 pttmp7 pttmp3 pttmp8 nil)
    pttmp10 (list (car pttmp9) (cadr pttmp3) (caddr pttmp3))
    pttmp11 (polar ptStart (/ pi 2.0) threadpitch)
    pttmp12 (polar pttmp11 (/ pi 2.0) (abs threadlength))
  )

  ;-------------------------------------------------------------------
  ; 繪制兩個倒置的并偏移1/2螺距的圓錐
  ; 這兩個圓錐都以中剖面剖分
  ; 進行并集運算
  ;-------------------------------------------------------------------

(SETQ startcone "order")
(SETQ endcone "Y")
  (command "zoom" "w" (list (car pt1a) (+ (cadr pt1a) (abs threadlength)) (caddr pt1a)) pt3b)

  (princ "
正在繪制三維螺紋,請等待")
  (command "pline" pttmp1 pttmp5 pttmp6 "c")
  (command "revolve" "l" "" pttmp5 pttmp6 "")
  (command "slice" "l" "" pttmp1 pttmp3 pt1z pttmp5)
  (command "slice" "l" "" pt1a pt3a pt1az pttmp3)
  (setq tstmp (ssadd (entlast)))
  (command "pline" pttmp3 pttmp9 pttmp10 "c")
  (command "revolve" "l" "" pttmp9 pttmp10 "")
  (command "slice" "l" "" pttmp1 pttmp3 pt1z pttmp9)
  (command "slice" "l" "" pt1b pt3b pt1bz pttmp3)
  (setq tstmp (ssadd (entlast) tstmp))
  (command "union" tstmp "")

  ;-------------------------------------------------------------------
  ; 上面的實體被剖切成兩份,然后進行鏡像以獲得螺旋線的螺旋
  ; 單線的高度實際上等于兩倍螺距,但是超出或者是在內(nèi)部或者是
  ; 在最后一步被切除
  ;-------------------------------------------------------------------

  (command "slice" tstmp "" "xy" ptStart "b")
  (setq tstmp (ssadd (entlast) tstmp))
  (command "mirror" "l" "" pttmp1 "@10<0" "y")
  (command "union" tstmp "")

  ;-------------------------------------------------------------------
  ; 螺旋進行陣列操作,然后再作并集(可能會耗費一定時間)
  ; 得到的實體再切除到指定的螺旋高度
  ;-------------------------------------------------------------------

  (setq e (entlast))
  (command "array" tstmp "" "r" ttal 1 threadpitch)
  (repeat (1- ttal)
    (setq e (entnext e)
      tstmp (ssadd e tstmp)
    )
  )
  (command "union" tstmp "")

; 若開始創(chuàng)建45度的圓錐
  (if (/= startcone "order")
  (progn (setq e (entlast))
  (command "cone" (list (car ptStart) (+ (cadr ptStart) threadpitch) (caddr ptStart)) "d" radmid "a" (list (car ptStart) (+ (+ (cadr ptStart) (/ radmid 2)) threadpitch) (caddr ptStart)))
  (command "union" "l" e "")
  )
  )

  (command "slice" "l" "" "zx" pttmp11 pttmp12)
  (command "slice" "l" "" "zx" pttmp12 pttmp11)
  (command "move" "l" "" ptStart (list (car ptStart) (- (cadr ptStart) threadpitch) (caddr ptStart)))

; 創(chuàng)建最小直徑的圓柱體,然后與螺旋作交集
  (setq e (entlast))
  (command "cylinder" ptStart "d" radinner "c" (list (car ptStart) (+ (cadr ptStart) (abs threadlength)) (caddr ptStart)))
  (command "union" "l" e "")
  (setq tstmp (entlast))

; 創(chuàng)建中空的圓柱體
  (command "cylinder" ptStart "d" radmid "c" (list (car ptStart) (+ (cadr ptStart) (abs threadlength)) (caddr ptStart)))   ; minor dia
  (setq e (entlast))

; 若最后創(chuàng)建45度的圓錐
  (if (/= endcone "order")
  (progn (command "move" "l" "" ptStart (list (car ptStart) (- (cadr ptStart) (/ (- radmid radinner) 2)) (caddr ptStart)))    
  (command "cone" ptStart "d" radmid "a" (list (car ptStart) (+ (cadr ptStart) (/ radmid 2)) (caddr ptStart)))
  (command "move" "l" "" ptStart (list (car ptStart) (- (+ (cadr ptStart) (abs threadlength)) (/ (- radmid radinner) 2)) (caddr ptStart)))
  (command "union" "l" e "")          
  (setq e (entlast))
  )
  )

; 從大圓柱中減去小直徑圓柱
  (command "cylinder" ptStart "d" (* radmid 1.5) "c" (list (car ptStart) (+ (cadr ptStart) (abs threadlength)) (caddr ptStart)))
  (command "subtract" "l" "" e "")
  (setq e (entlast))

; 從螺旋中減去圓柱
  (command "subtract" tstmp "" e "")

; 如果螺旋長度為負然后鏡像
  (setq e (entlast))
  (if (< threadlength 0) (mirror3d e "zx" ptStart "y"))

  (command "zoom" "p")
;   (command "undo" "end")                       ; 結束undo步驟
)

;;;---------------------------------------------------------------------------------------------------------------------;
(arxload "geom3d" nil)
(princ "
3DThread 已加載。 ")
(princ)

三、彈簧源程序

(defun errMsg (s)             ; 當命令執(zhí)行時出現(xiàn)錯誤
                        ; 例如用戶按下了CTRL + C
(if (/= s "Function cancelled")
  (princ (strcat "
Error: " s))
)
(setvar "cmdecho" cmdch)         ; 恢復調(diào)用前的cmdecho系統(tǒng)變量值
(setvar "blipmode" blpmd)         ; 恢復調(diào)用前的blipmode系統(tǒng)變量值
(setq *error* olderror)         ; 恢復舊的錯誤處理
(princ)
)

(defun spring (nRepeat cntPoint bhfac lr strad bvfac
          / angle distnc tp aGrw dhGrw dvGrw Pi2 dv)

(setvar "blipmode" 0)           ; 關閉 blipmode
(setvar "cmdecho" 0)           ; 關閉 cmdecho
(setq Pi2 (* 3.14159265 2))
(setq aGrw (/ Pi2 lr))
(setq dhGrw (/ bhfac lr))
(if bvfac (setq dvGrw (/ bvfac lr)))
(setq angle 0.0)
(if bvfac
  (setq distnc strad dv 0.0)
  (setq distnc 0.0)
)
(if bvfac
  (command "3dpoly")                 ; 開始繪制彈簧
  (command "pline" cntPoint)       ; 從基點開始繪制彈簧
)
(repeat nRepeat
  (repeat lr
    (setq tp (polar cntPoint (setq angle (+ angle aGrw))
              (setq distnc (+ distnc dhGrw))
          )
    )
    (if bvfac
      (setq tp (list (car tp) (cadr tp) (+ dv (caddr tp)))
          dv (+ dv dvGrw)
      )
    )
    (command tp)             ; 繼續(xù)輸入下一點
  )
)
(command "")                 ; 輸入結束
(princ)
)

;;;
;;;     生成三維彈簧函數(shù)調(diào)用接口
;;;

(defun C:3DSPRING (/ olderror cmdch blpmd cntPt RottCnt horiGrw vertGrw bgnRdtn ptCntPerCircle)
(setq cmdch (getvar "cmdecho"))     ; 獲取cmdecho值,以便調(diào)用結束后恢復
(setq blpmd (getvar "blipmode"))   ; 獲取blipmode值,以便調(diào)用結束后恢復
(setvar "cmdecho" 0)               ; 關閉 cmdecho    
(initget 1)                 ; cntPt 必須非空
(setq cntPt (getpoint "
請輸入底部中心點: "))
(initget 7)                 ; RottCnt 必須非零、非空,不可忽略
(setq RottCnt (getint "
旋轉的圈數(shù): "))
(initget 7)                 ; bgnRdtn 必須非零、非空,不可忽略
(setq bgnRdtn (getdist cntPt "
起始半徑: "))
(initget 1)                 ; cf 必須非零、非空
(setq horiGrw (getdist "
每一圈的水平增長距離: "))
(initget 3)                 ; cf 必須非零、非空
(setq vertGrw (getdist "
每一圈的垂直增長距離: "))
(initget 6)                 ; ptCntPerCircle 必須非零、不可忽略
(setq ptCntPerCircle (getint "
每一圈的插值點數(shù)<35>: "))
(cond ((null ptCntPerCircle) (setq ptCntPerCircle 35)))
(spring RottCnt cntPt horiGrw ptCntPerCircle bgnRdtn vertGrw)
(setvar "cmdecho" cmdch)           ; 恢復調(diào)用前的cmdecho系統(tǒng)變量值
(setvar "blipmode" blpmd)           ; 恢復調(diào)用前的blipmode系統(tǒng)變量值
(setq *error* olderror)           ; Restore old *error* handler
(princ)

)