几年前毕业设计做的CAD二次开发

现实总是这样,自己学的东西大部分都用不上,最后都浪费了白学了。

这是我做的毕业设计做的CAD二次开发,用的语言是AuctoLisp。当时学的挺辛苦,现在看来都是白忙。

程序的效果如图

操作的步骤是

一,准备好程序

我们举的例子是0直齿圆柱齿轮

二,点击AutoCAD的 菜单->工具->AutoLisp->加载这个菜单条

三,在下面的命令输入栏输入“0直齿圆柱齿轮”

出现上图的提示,随便输入一个值10;

又出现上图提示,输入20

在绘图窗口中用鼠标选取一点

出现上两个图,绘图完成。

源码如下,具体解释由于年代久远而记不清楚了


(defun c:0直齿圆柱齿轮()
  (setq m (getreal "\n请输入直齿圆柱齿轮的模数<默认值1.0>:"))
  (if (null m)(setq m 1.0))
  (setq z (getint "\n请输入直齿圆柱齿轮的齿数<默认值16>:"))
  (if (null 齿数)(setq z 16))
  (setq 左下角点  (getpoint "\n请选取图形的左下角点:"))
 
  (setq 齿顶圆半径 (* 0.5 m (+ z 2)))
  (setq 齿根圆半径 (* 0.5 m (- z 2.5)))
  (setq 分度圆半径 (* 0.5 m z))
  (setq 内孔半径  (* 分度圆半径 0.4 ))
  (setq 厚度      (* 分度圆半径 0.4 ))
  (setq 内槽深度  (* 内孔半径 0.2 ))
  (setq 内槽宽度  (* 内孔半径 0.4 ))
  (setq 内槽中心距(* 内孔半径 0.8 ))
 
  ;;;  创建工作图层
  (command "layer" "n" "轮廓"      "lw" "0.3" "轮廓"   "")
  (command "layer" "n" "细线"      "")
  (command "layer" "n" "剖面线"     "c" "3" "剖面线" "")
  (command "layer" "n" "中心线"    "c" "1"  "中心线"       "l"  "center2" "中心线" "")
  (command "layer" "n" "虚线"      "c" "6"  "虚线"        "l"  "hidden"   "虚线"   "")
 
;;;    取得左图的作图中心点
  (setq base_point1 (移动Y坐标得到点  左下角点    齿顶圆半径))    ;即是左下角点向上移动齿顶圆半径距离的点
  ;    取得左中心点的X,Y坐标
  (setq base_point1_x (car base_point1) base_point1_y (cadr base_point1))
  ;取得左图的右上角点
  (setq 右上角点    (list (+ (car 左下角点) 厚度) (+ (cadr 左下角点) (* 2 齿顶圆半径))))
  ;;;划左图
  (如果图层存在则切换到图层 "轮廓")        ;切换到“轮廓”层
  (command "rectang"  左下角点 右上角点 "") ;划外轮廓 ,用矩形划
 
  (setq gen_shang (移动Y坐标得到点 base_point1  齿根圆半径))
  (通过点沿着X轴划线 gen_shang 厚度)
  (setq gen_xia (移动Y坐标得到点 base_point1  (- 0 齿根圆半径)))
  (通过点沿着X轴划线 gen_xia 厚度)
 
  (setq nei_shang (移动Y坐标得到点 base_point1 内孔半径))
  (通过点沿着X轴划线 nei_shang 厚度)
  (setq nei_xia  (移动Y坐标得到点 base_point1 (- 0 内孔半径)))
  (通过点沿着X轴划线 nei_xia 厚度)

  (setq m1 (+ 内槽深度 内孔半径))
  (setq nei_shen (移动Y坐标得到点 base_point1  m1))
  (通过点沿着X轴划线 nei_shen 厚度)

;填充剖面线
;(如果图层存在则切换到图层 "轮廓")
 ; (setq gen0(list (+ base_point1_x 厚度)   (+ base_point1_y  齿根圆半径) ))
  ;(setq nei0(list (+ base_point1_x 厚度)   (+ base_point1_y  ( * 1.2 内孔半径) ) ))
 ; (command "pline" gen_shang gen0 nei0 nei_shang "")
 ; (如果图层存在则切换到图层 "剖面线")
 ; (setq ss (entlast))
 ; (command "hatch"  "ansi31" 0.15 0 ss "")
 ; (如果图层存在则切换到图层 "轮廓")
 ; (setq gen1(list (+ base_point1_x 厚度)   (- base_point1_y  齿根圆半径) ))
 ; (setq nei1(list(+ base_point1_x 厚度)    (- base_point1_y  内孔半径) ))
 ; (command "pline" gen_xia gen1 nei1 nei_xia "")
 ; (如果图层存在则切换到图层 "剖面线")
 ; (setq ss (entlast))
 ; (command "hatch"  "ansi31" 0.15 0 ss "")

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (如果图层存在则切换到图层 "中心线")
  (setq base_point2 ( 移动X坐标得到点 base_point1 厚度))
  (延伸两点X轴划线 base_point1 base_point2)

  (setq fen_shang1 (  移动Y坐标得到点 base_point1 分度圆半径))
  (setq fen_shang2 (  移动X坐标得到点 fen_shang1 厚度))
  (延伸两点X轴划线 fen_shang1 fen_shang2)
 
  (setq fen_xia1 (  移动Y坐标得到点 base_point1 (- 0 分度圆半径)))
  (setq fen_xia2 (  移动X坐标得到点 fen_xia1 厚度))
  (延伸两点X轴划线 fen_xia1 fen_xia2)

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;划右图

;取得右图的圆心 ,具体距离由图形尺寸调整后得到
  (setq center (list ( +  base_point1_x  厚度 (* 齿顶圆半径 1.5) ) base_point1_y ))  
  (command "circle" center 分度圆半径);划分度圆
  (setq move ( * 1.2 齿顶圆半径))
  (在中心点沿轴划线 center move);划中心线

   (如果图层存在则切换到图层 "轮廓")
   (command "circle" center 齿顶圆半径) ;划齿顶圆
   (setq move (/ 内槽宽度 2))
   (setq center_x (car center))
   (setq center_y (cadr center))
  (setq temp_p1 (list (- center_x move) ( + center_y 内槽中心距)))
   (setq temp_p2 (list (+ center_x move) ( + center_y 内槽中心距)))
  (setq arc_start (list (- center_x move) ( + center_y 内槽中心距 (- 0 内槽深度))))
  (setq arc_end (list (+ center_x move) ( + center_y 内槽中心距 (- 0 内槽深度))))
  (command "line" arc_start temp_p1 temp_p2 arc_end "") ;划内槽
  (command "arc" "c"   center arc_start arc_end);划内孔圆弧
  )



(defun 创建工作图层()

   (command "layer" "n" "轮廓"      "lw" "0.3" "轮廓"   "")
   (command "layer" "n" "细线"      "")
   (command "layer" "n" "剖面线"     "c" "3" "剖面线" "")
  (command "layer" "n" "中心线"    "c" "1"  "中心线"       "l"  "center2" "中心线" "")
  (command "layer" "n" "虚线"      "c" "6"  "虚线"        "l"  "hidden"   "虚线"   "")  
  )

(defun 如果图层存在则切换到图层( layer_name)
   (setq check (tblsearch "layer" layer_name))
  (if ( /= check nil)
    (command "clayer" layer_name )
  )
)


(defun  移动X坐标得到点(point1 move); point1 move
  (setq temp_x (+ move (car point1)))
  (setq temp_y (cadr point))
  (list temp_x temp_y)
  )
 
(defun  反向移动X坐标得到点(point1 move); point1 move
  (setq temp_x (- move (car point1)))
  (setq temp_y (cadr point))
  (list temp_x temp_y)
  )


(defun 移动Y坐标得到点( point1 move )
  (setq temp_x (car point1))
  (setq temp_y (+ move (cadr point1)))
  (list temp_x temp_y)
  )
 
 (defun 反向移动Y坐标得到点( point1 move )
  (setq temp_x (car point1))
  (setq temp_y (- move (cadr point1)))
  (list temp_x temp_y)
  )

(defun 在中心点沿轴划线( pointc move )
    (setq temp_x (car pointc))
  (setq temp_y( cadr pointc))

  (setq temp_p1 (list (- temp_x move) temp_y))
  (command "line" pointc temp_p1 "")

  (setq temp_p1 (list (+ temp_x move) temp_y))
  (command "line" pointc temp_p1 "")

  (setq temp_p1 (list  temp_x  (-  temp_y move )))
  (command "line" pointc temp_p1 "")

  (setq temp_p1 (list  temp_x  (+  temp_y move )))
  (command "line" pointc temp_p1 "")
 
  (princ)

  )

(defun 延伸两点X轴划线(  point1 point2)
  (setq t_x1(car point1))
  (setq t_x2 (car point2))
  (setq move ( *  0.65 (- t_x2 t_x1)))
  (setq temp_x(/ (+ t_x1 t_x2) 2))
  (setq temp_y (cadr point1))
  (setq temp_p1 (list (- temp_x move) temp_y))
  (setq temp_p2 (list (+ temp_x move) temp_y))

 (command "line" temp_p1 temp_p2 "")
  )

(defun  延伸两点Y轴划线(point1 point2)

  (setq t_y1(cadr point1))
  (setq t_y2 (cadr point2))

  (setq move ( *  0.7 (- t_y2 t_y1)))
  (setq temp_y(/ (+ t_y1 t_y2) 2))
  (setq temp_x (car point1))
  (setq temp_p1 (list  temp_x (- temp_y move)))
  (setq temp_p2 (list temp_x (+ temp_y move)))
 
  (command "line" temp_p1 temp_p2 "")
  )
(defun 通过点沿着X轴划线(point move)
    
  (setq temp_x(+ move (car point)))
  (setq temp_y (cadr point))
  (setq temp_p (list temp_x temp_y))
  (command "line" point temp_p "")
  )

(defun 通过点沿着Y轴划线(point move)
   (setq temp_x(car point))
  (setq temp_y (+ move (cadr point)))
  (setq temp_p (list temp_x temp_y))
  (command "line" point temp_p "")
  )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  (prompt "\n***************    创建 直齿圆柱齿轮 图形(请先关闭“ 对象捕捉 ”)    *************** \n")
  (princ)

原文地址:https://www.cnblogs.com/haiconc/p/2352961.html