免费CAD教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.mfcad.com提供.

齿轮二次开发源代码下载

作者:无极限发布时间:2012-02-11浏览:3714
所需积分:
免费资料
教程格式:
文件大小:
6 KB
教程介绍

简介:齿轮二次开发源代码下载,利用该代码编辑成lsp外挂程序后,只需输入齿轮的参数就可以轻松绘制出齿轮,希望本代码能为您学习cad二次开发有所帮助

源代码:

(defun C:gear ()
  (setq numt nil
           diap nil
            prsa nil
            pnts nil
            test nil
            pwd nil
  )
 
  (setq numt (getint "齿数:<24>"))
  (if (= numt nil)
    (setq numt 24)
  )
  (setq diap (getreal "齿轮模数:<0.5> "))
  (if (= diap nil)
    (setq diap (/ 1 0.5))
    (setq diap (/ 1 diap))
  )
  (setq prsa (getreal "压力角:<20.0> "))
  (if (= prsa nil)
    (setq prsa 20.0)
  )
  (setq pnts (getint "曲线上的点的数量:<40> "))
  (if (= pnts nil)
    (setq pnts 40)
  )

;  (setq pwd (getreal "Please input password:"))
;     (if (/= pwd 8833)
;        (setq numt 0)
;  )
;     (if (= pwd nil)
;        (setq numt 0)
;  )
;
  (command "osnap" "non")
  (setvar "cmdecho" 0)
  (setq oldvar (getvar "pickbox"))
  (setvar "pickbox" 0)
  (setvar "aperture" 1)
  (command "osmode" "0" )
;
  (setq prsa (/ (* prsa pi) 180.0))
  (setq pitd (/ numt diap))
  (setq outd (/ (+ numt 2) diap))
  (setq basr (/ (* pitd (cos prsa)) 2))
  (setq orad (/ outd 2.0))
  (setq z (- (expt orad 2.0) (expt basr 2.0)))
  (setq x (sqrt z))
  (setq paodd (atan (/ x basr)))
  (setq incr (/ paodd pnts))
  (setq p 0.0)
  (setq pitr (/ pitd 2.0))
  (setq pang (/ 360. (* numt 4.0)))
  (setq pang (/ (* pang pi) 180.0))
  (graphscr)
  (setq p2 (getpoint "齿轮中心:"))
  (setq y2 (cadr p2))
  (setq x2 (car p2))
  (setq r0 (/ (/ (- numt 2.5) diap) 2))
  (setq r1 (/ 0.2 diap))
  (setq h (sqrt (- (* (+ r1 r0) (+ r1 r0)) (* r1 r1))))
  (setq ang0 (/ (* pi 2) numt))

  (setq y5 (+ y2 basr))
  (setq p5 (list x2 y5))
  (setq y55 (+ y2 r0))
  (setq p55 (list x2 y55))
  (setq p88 (list (+ x2 2) (+ y5 2)))
  (setq a3 (/ (* pi 5) 4))
  (setq a4 (/ pi 4))
  (setq pz3 (polar p2 a3 (* orad 1.5)))
  (setq pz4 (polar p2 a4 (* orad 1.5)))
  (command "zoom" "w" pz3 pz4)
;
  (setq clay (getvar "CLAYER"))
  (setq sblip (getvar "BLIPMODE"))
  (setq ts (tblsearch "LAYER" "CEN"))
   (if (null ts)
      (progn
       (prompt "\nCreating new layer - CEN. ")
       (setvar "BLIPMODE" 0)
       (command "LAYER" "M" "CEN" "LT" "CENTER" "CEN" "C" "RED" "CEN" "")    
       )
      (progn
       (if (> (cdr (assoc 70 ts)) 0) (command "LAYER" "T" "CEN" "ON" "CEN" "U" "CEN" ""))
       (command "LAYER" "S" "cen" "")
       )
    )
  (command "circle"  p2 pitr)
  (setvar "BLIPMODE" sblip)
  (command "LAYER" "S" clay "")
;
  (setq a1 (- (/ pi 2 ) 0.1))
  (setq a2 (+ (/ pi 2) 0.1))
  (setq pz1 (polar p2 a1 basr))
  (setq pz2 (polar p2 a2 orad))
  (command "zoom" "w" pz1 pz2)
  (setq s (ssadd))
  (setq le (entlast))
  (setq test 0)

  (command "pline" p5)
  (setq p (+ incr p ))
  (while (> pnts 0)
   (setq e1 (sin p))
   (setq e2 (cos p))
   (setq e (/ e1 e2))
   (setq j (- e p))
   (setq x1 (* (/ (sin j) (cos p)) basr))
   (setq y1 (* (/ (cos j) (cos p)) basr))
   (setq x3 (+ x2 x1))
   (setq y3 (+ y2 y1))
   (setq p3 (list x3 y3))
   (command p3)
   (setq p (+ incr p))
   (setq pnts  (- pnts 1))
   (if (/= test 1)
    (progn
    (setq hyp (sqrt (+ (expt x1 2) (expt y1 2))))
     (if (> hyp pitr)
       (progn
         (setq pint p3)
         (setq test 1)
        )
      )
     );endif
    );endif
    )
    (command "")
    (setq L2 (ssget "L"))
    (initget "Y y N n")
    (setq ans (getkword "\n 完成齿轮 ?:<Y> "))
    (if (/= ans "N")
      (progn
        (command "zoom" "w" pz1 pz2)
        (setq p11 (osnap pint "inter"))
        (setq ang (angle p2 p11))
        (setq angi (- ang pang))
        (setq p12 (polar p2 angi 1.0))
;
  (if (< (* 0.94 numt (/ 1 diap)) (* h 2.0005))
      (progn
            (if (< numt 42)
            (progn
               (setq p56 (list x2 (+ y2 r0)))
               (command "line" p56 p5 "")
               (setq L33 (entlast))
               (command "zoom" "w" p77 p88)
               (command "mirror" L2 L33 "" p2 p12 "")
               (setq adj1 (- angi (/ pi 2)))
               (setq adj2 (- (/ pi 2)(* pang 4)))
               (setq p17 (polar p2 (+ angi adj1) r0))
               (setq p16 (polar p2 adj2 r0))
               (command "arc" p16 "c" p2 p17)
              ) ;end progn
            (progn
               (command "zoom" "w" p77 p88)
               (command "mirror" L2 "" p2 p12 "")
               (setq pL1 (entlast))
               (setq adj1 (- angi (/ pi 2)))
               (setq adj2 (- (/ pi 2)(* pang 4)))
               (setq p17 (polar p2 (+ angi adj1) r0))
               (setq p16 (polar p2  adj2 r0))
             (if (> numt 101)
               (command "arc" p17 "c" p2 p16)
               (command "arc" p16 "c" p2 p17))
               (setq arc4 (entlast))
               (setq p171 (polar p17 0.7854 (/ 0.4 diap)))
               (setq p172 (polar p17 3.9 (/ 0.4 diap)))
             (if (> numt 101)
               (setq p18 (polar p2 (+ angi adj1 ang0) r0))
               (setq p18 (polar p2 (+ adj2 ang0) r0)))
               (setq p181 (polar p18 2.3 (/ 0.4 diap)))
               (setq p182 (polar p18 5.5 (/ 0.4 diap)))
               (command "zoom" "w" p171 p172)
             (if (> numt 101)
               (command "extend" pL1 "" p16 "")
               (command "extend" pL1 "" p17 ""))
               (setq ang0 (/ (* ang0 180) pi))
               (command "rotate" arc4 "" p2 ang0)
               (command "zoom" "w" p181 p182)
               (command "extend" L2 "" p18 "")
               (command "zoom" "w" pz1 pz2)
               (command "trim" arc4 "" p5 "")
               (command "erase" pl1 "")
               (command "mirror" L2 "" p2 p12 "")
             ) ;end progn
           ) ;end if
         ) ;end progn
      (progn
        (setq ang12 (- (/ pi 2) (angle p2 p12)))
        (setq ang57 (atan (/ r1 h)))
        (setq ang58 (- ang0 (* ang12 2) (* ang57 2)))
        (setq ang577 (+ (/ pi 2) ang57))
        (setq ang588 (+ ang577 ang58))
        (setq p57 (polar p2 ang577 (+ r1 r0)))
        (setq p577 (polar p2 ang577 r0))
        (setq p588 (polar p2 ang588 r0))       
        (setq p56 (list x2 (+ y2 h)))
        (command "arc" p577 "c" p57 p56)
        (setq arc1 (entlast))
        (command "arc" p577 "c" p2 p588)
        (setq arc2 (entlast))
        (command "line" p56 p5 "")
        (setq L33 (entlast))
        (command "zoom" "w" p77 p88)
        (command "mirror" arc1 arc2 L2 L33 ""  p2  p12 "")
        (command "erase" arc2 "")
       ) ;end progn   
    ) ;end if

        (setq beta (angle p2 p3))
        (setq ang2 (- (* angi 2) beta))
        (setq p15 (polar p2 ang2 orad))
        (command "arc" p15 "c" p2 p3)
        (while (setq le (entnext le))
          (ssadd le s)
       )
       (command "array" s "" "p" p2 numt "" "")
       (setq q1 (nth 0 p2))
       (setq q2 (nth 1 p2))
       (setq q5 (+ q1 pitr 2))
       (setq q6 (- q2 pitr 2))
       (setq q3 (- q1 pitr 2))
       (setq q4 (+ q2 pitr 2))
       (setq q1 (list q5 q6))
       (setq q2 (list q3 q4))
       (command "zoom" "w" q1 q2)
     )
     )
    (setvar "pickbox" 5)
    (setvar "aperture" 5)
    (setvar "osmode" 37)
   
    (princ "Finish gear ")
    (princ numt)
    (princ "T")
    (princ)
   )

代码使用方法:

第一步 将下载好代码解压后,将其中的GEAR.lsp拖到autocad绘图窗口中(或者将上述代码复制到一个txt文档当中,然后将扩展名该为lsp,并将其拖进autocad绘图区中)

第二步 输入命令【gear】回车,根据提示,输入齿轮各个参数,

下图就是利用该代码编制成了lsp程序绘制的齿轮

齿轮
    最新评论
    0/300
    发表评论
    全部评论
    暂无评论
    相关内容