【转载】:Autolisp:利用AuoCAD之Lisp编程案例之智能加工齿轮的演示程序-----一个处女座程序猿
程序员文章站
2022-05-22 10:37:27
...
利用AuoCAD之Lisp编程案例之智能加工齿轮的演示程序
-
(defun dlg1();初始化对话一
-
(set_tile ”_m” ”5”);模数列表第6项
-
(set_tile ”_z” ”20”);齿数20
-
(set_tile ”_alf” ”2”);压力角第3项
-
(set_tile ”s_z” ”20”);齿数滑动条
-
(set_tile ”cut” ”2”);齿廓列表第3项
-
(action_tile ”_m” ”(setq msh_ $value)"</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="8"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(action_tile <span class="hljs-string"><span class="hljs-string">"s_z"</span></span> <span class="hljs-string"><span class="hljs-string">"(fsz)"</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="9"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(action_tile <span class="hljs-string"><span class="hljs-string">"_z"</span></span> <span class="hljs-string"><span class="hljs-string">"(fz)"</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="10"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(action_tile <span class="hljs-string"><span class="hljs-string">"s_x"</span></span> <span class="hljs-string"><span class="hljs-string">"(fs_x)"</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(action_tile <span class="hljs-string"><span class="hljs-string">"_x"</span></span> <span class="hljs-string"><span class="hljs-string">"(f_x)"</span></span>)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="12"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(action_tile <span class="hljs-string"><span class="hljs-string">"_alf"</span></span> <span class="hljs-string"><span class="hljs-string">"(setq ylj_ $value)”)
-
(action_tile ”prec0” ”(setq qxcsh 5)”)
-
(action_tile ”prec1” ”(setq qxcsh 10)”)
-
(action_tile ”prec2” ”(setq qxcsh 15)”)
-
(action_tile ”accept” ”(getd)(done_dialog 1)”)
-
(action_tile ”cancel” ”(done_dialog 0)”)
-
(action_tile ”help” ”(dlg3)”)
-
)
-
;定义获取控件当前数据的函数
-
(defun getd()
-
(setq msh_ (get_tile ”_m”))
-
(setq msh (atof(nth (atoi msh_) m_lst)));模数
-
(setq ylj_ (get_tile ”_alf”))
-
(setq ylj (atof(nth (atoi ylj_) alf_lst)));压力角
-
(setq bwxsh(atof(get_tile ”_x”)));变位系数
-
(setq chsh(atoi(get_tile ”_z”)));齿数
-
)
-
;定义齿数编辑框的活动的函数
-
(defun fz(/ gz)
-
(setq gz(atoi $value))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="32"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">or</span></span> (< gz <span class="hljs-number"><span class="hljs-number">9</span></span>)(> gz <span class="hljs-number"><span class="hljs-number">100</span></span>))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="33"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (progn</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="34"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (set_tile $key (get_tile ”s_z”))
-
(setq chsh(atoi $value))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="36"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> )</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="37"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (progn</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="38"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (set_tile <span class="hljs-string"><span class="hljs-string">"s_z"</span></span> (itoa gz))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="39"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (setq chsh gz)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="40"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> )</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="41"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="42"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="43"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">;定义齿数滑动条的活动的函数</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="44"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(defun fsz()</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="45"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (set_tile <span class="hljs-string"><span class="hljs-string">"_z"</span></span> $value)
-
(setq chsh (atoi $value))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="47"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="48"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">;定义变位系数滑动条的活动的函数</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="49"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(defun fs_x(/ xx)</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="50"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (setq xx (atof $value))
-
(setq bwxsh(* 0.01 xx))
-
(setq xx(rtos bwxsh 2 2))
-
(set_tile ”_x” xx)
-
)
-
;定义变位系数编辑框的活动的函数
-
(defun f_x(/ xx )
-
(setq xx(atof $value))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="58"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line">(<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (<span class="hljs-keyword"><span class="hljs-keyword">or</span></span> (< xx <span class="hljs-number"><span class="hljs-number">-1</span></span>)(> xx <span class="hljs-number"><span class="hljs-number">1</span></span>))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="59"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (progn</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="60"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (setq xx (get_tile <span class="hljs-string"><span class="hljs-string">"s_x"</span></span>))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="61"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (setq xx(atof xx))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="62"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (setq bwxsh(* <span class="hljs-number"><span class="hljs-number">0.01</span></span> xx))</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="63"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> (set_tile $key (rtos bwxsh 2 2))
-
)
-
(progn
-
(setq bwxsh xx)
-
(setq xx(* 100 xx))
-
(set_tile ”s_x” (itoa xx))
-
)))
-
;定义齿轮加工的函数
-
(defun makegear()
-
(setq chlzhx ’(0 0));齿轮中心
-
(setq ylj (* pi (/ ylj 180.0)));度转换为弧度
-
(setq chgao (* msh (+ chdgaoxsh bwxsh)));齿高=1
-
(setq fdybj (* 0.5 (* msh chsh)));分度圆半径
-
(setq dybj (+ fdybj (* msh (+ chdgaoxsh bwxsh))));齿顶圆半径
-
(setq kbj (* 0.25 dybj));轮孔半径
-
(setq lm1 (list (- (car chlzhx) (* 2 dybj)) (+ (last chlzhx) dybj)));图纸界限的左下角
-
(setq lm2 (list (+ (* 5 msh) (car chlzhx) dybj) (- (last chlzhx) (+ 10.0 (* chdjx msh)) dybj)));图纸界限的右上角
-
(command ”zoom” lm1 lm2)
-
(setq jybj (+ fdybj (* bwxsh msh)));节圆半径=分度圆半径+变位量
-
(setq temy1 (* msh (+ chdjx chdgaoxsh)));齿顶高1+齿顶间隙0.25
-
(setq chtgen (+ jybj temy1));齿轮中心到齿条根部的距离
-
(setq chtdi (+ chtgen 3.0));齿轮中心到齿条底根部的距离
-
(setq chtdi (- (cadr chlzhx) chtdi));齿条下底的y
-
(setq chtzuo (- (car chlzhx) (* 2 jybj)));齿条左边x
-
(setq p01 (list chtzuo chtdi))
-
(setq chw (* pi msh));周节
-
(setq chtgeny(+ chtdi 3.0));齿条根y
-
(setq p0 (list chtzuo chtgeny))
-
(setq chh (* 2.0 temy1));齿条全齿高
-
(setq chb (/ (* chh (sin ylj)) (cos ylj)))
-
(setq cha (* 0.25 (- chw (* 2 chb))))
-
(setq chnu(* 0.5 chsh));齿条的齿数
-
(setq chnum 0.0)
-
(command ”color” ”green”)
-
(setq chtemp p0)
-
(command ”pline” p01 p0 )
-
(while(< chnum chnu);绘制齿条轮廓线
-
(setq chnum (1+ chnum))
-
(command (setq p1 (list (+ cha (car p0)) (cadr p0))))
-
(command (setq p2 (list (+ chb (car p1)) (+ chh (cadr p1)))))
-
(command (setq p3 (list (+ (* 2.0 cha) (car p2)) (cadr p2))))
-
(command (setq p4 (list (+ chb (car p3)) (- (cadr p3) chh))))
-
(command (setq p5 (list (+ cha (car p4)) (cadr p4))))
-
(setq p0 p5)
-
)
-
(setq p02 (list (+ chtzuo (* chnum chw)) chtdi))
-
(command p02 p01 ”c”);齿条轮廓线绘制完毕
-
(setq e1(entlast));获取齿条的图元名
-
(command ”region” e1 ”“);将齿条转换为面域
-
(setq tiao(entlast));获取转换为面域的齿条的图元名
-
(command ”color” ”white”)
-
(command ”circle” chlzhx dybj);绘制齿顶圆
-
(setq c1(entlast));获取齿顶圆的图元名
-
(command ”region” c1 ”“);将齿顶圆转换为面域
-
(setq c1(entlast));获取转换为面域的齿顶圆的图元名
-
(command ”circle” chlzhx kbj);绘制轮孔圆
-
(setq c2(entlast));获取轮孔圆的图元名
-
(command ”region” c2 ”“);将轮孔圆转换为面域
-
(setq c2(entlast));获取转换为面域的轮孔圆的图元名
-
(command ”subtract” c1 ”“ c2 ”“);齿顶圆的面域减去轮孔圆的面域
-
(setq lun(entlast));获取齿轮毛坯的图元名
-
(setq dphi(/ 360.0 (* chsh qxcsh)));齿轮毛坯每次旋转角度
-
(setq chtydjl (/ chw qxcsh));齿条每次移动距离
-
(setq shjqxcsh 0);实际切削次数
-
(setq ydjl 0.0);累计移动距离
-
(while (<= shjqxcsh (* qxcsh chsh))
-
(setq ydjl (+ ydjl chtydjl))
-
(setq dx chtydjl);齿条移动距离
-
(if (>= ydjl chw)
-
(progn;ydjl大于等于周节chw
-
(setq ydjl 0.0)
-
(setq dx (* (- 1 qxcsh) chtydjl));齿条退回一个周节
-
)
-
)
-
(command ”copy” tiao ”“ ’(0 0) ‘(0 0));原地复制齿条
-
(setq tiao1(entlast))
-
(command ”subtract” lun ”“ tiao1 ”“);齿轮毛坯减去齿条
-
(command ”rotate” lun ”“ chlzhx dphi);齿轮毛坯旋转dphi
-
(command ”move” tiao ”“ ’(0.0 0.0) (list dx 0.0));齿条移动dx
-
(setq shjqxcsh (1+ shjqxcsh))
-
)
-
)
-
;定义齿轮参数显示的函数
-
(defun showdata()
-
(if (not (new_dialog ”gear_list” id))(exit))
-
(setq x1(dimx_tile ”image2”)) ;设置x1为图象宽
-
(setq y1(dimy_tile ”image2”)) ;设置y1为图象高
-
(start_image ”image2”) ;开始建立图象
-
(slide_image -10 -10 (+ x1 20) y1 ”d:/example/sld/13-2-21”) ;-10 -10 (+ x1 20) y1
-
(end_image) ;图象建立毕
-
(set_tile ”t1” (strcat ”模数m: “ (rtos msh 2 2)))
-
(set_tile ”t2” (strcat ”齿数z: “ (itoa chsh)))
-
(setq ylj(* 180 (/ ylj pi)))
-
(set_tile ”t3” (strcat ”压力角a: “ (rtos ylj 2 2) ”?” ));jlj0
-
(set_tile ”t4” (strcat ”变位系数x: “ (rtos bwxsh 2 2)))
-
(set_tile ”t5” (strcat ”分度圆直径d: “ (rtos (* 2 fdybj) 2 2)))
-
(set_tile ”t6” (strcat ”齿顶圆直径da: “ (rtos (* 2 dybj) 2 2)))
-
(set_tile ”t7” (strcat ”齿根圆直径df: “ (rtos (* 2 (- dybj (* 2.25 msh))) 2 2)))
-
(action_tile ”accept” ”(done_dialog 1)”)
-
(action_tile ”xwj” ”(done_dialog 2)”)
-
(if (> (start_dialog) 1)(fwfile))
-
)
-
;定义将齿轮参数写到指定文件的函数
-
(defun fwfile()
-
(setq fname(getfiled ”输入存放齿轮参数的文件” ”“ ”txt” 1))
-
(setq fp(open fname ”w”))
-
(write-line (strcat ”模数m: “ (rtos msh 2 2)) fp)
-
(write-line (strcat ”齿数z: “ (itoa chsh))fp)
-
(write-line (strcat ”压力角a: “ (rtos ylj 2 2) ”?” )fp)
-
(write-line (strcat ”变位系数x: “ (rtos bwxsh 2 2))fp)
-
(write-line (strcat ”分度圆直径d: “ (rtos (* 2 fdybj) 2 2))fp)
-
(write-line (strcat ”齿顶圆直径da: “ (rtos (* 2 dybj) 2 2))fp);
-
(write-line (strcat ”齿根圆直径df: “ (rtos (* 2 (- dybj (* 2.25 msh))) 2 2))fp)
-
(close fp)
-
)
-
; 主函数
-
(defun c:niu( / bwxsh c1 c2 cha chb chdgaoxsh chdjx chgao chh chnu chnum chsh chlzhx chtdi chtemp chtgen chtgeny chtydjl chtzuo chw dphi dx dybj e1 fdybj fname fp jybj kbj lm1 lm2 lun msh msh_ p0 p01 p02 p1 p2 p3 p4 p5 qxcsh shjqxcsh temy1 tiao tiao1 x0 x1 y0 y1 ydjl ylj ylj_ zhxx zhxy)
-
(setvar ”cmdecho” 0)
-
(setvar ”blipmode” 0)
-
(setvar ”osmode” 0)
-
(setq chdgaoxsh 1.0);齿高系数
-
(setq chdjx 0.25);齿间隙
-
(setq qxcsh 15)
-
(setq id(load_dialog ”d:/example/dcl/13-2”)) ;装入对话框文件
-
(if (< id 0)(exit))
-
(setq what 3)
-
(while (> what 1)
-
(if (not (new_dialog ”mgear” id))(exit))
-
(setq x1(dimx_tile ”image1”)) ;设置x1为图象宽
-
(setq y1(dimy_tile ”image1”)) ;设置y1为图象高
-
(start_image ”image1”) ;开始处理图象
-
(slide_image -10 -25 (+ x1 100) y1 ”d:/example/sld/13-2-1”) ;-10 -25 (+ x1 20) y1
-
(end_image) ;图象处理完毕
-
(setq m_lst(list ”1” ”1.25” ”1.5” ”2” ”2.5” ”3” ”4” ”5” ”6” ”8” ”10”))
-
(start_list ”_m”) ;开始处理模数列表
-
(mapcar ’add_list m_lst)
-
(end_list);模数列表处理完毕
-
(setq alf_lst(list ”14.5” ”15” ”20” ”22.5” ”25”))
-
(start_list ”_alf”);开始处理压力角列表
-
(mapcar ’add_list alf_lst)
-
(end_list);压力角列表处理完毕
-
(dlg1) ;初始化对话框一
-
(if (/= what 2) (setq what (start_dialog)))
-
) ;while结束
-
(if (= what 1)
-
(progn
-
(makegear);调用加工齿轮的函数
-
(showdata);调用显示齿轮参数的函数
-
)
-
)
-
(unload_dialog id);卸载对话框文件
-
)
</div>
上一篇: PHP stdClass类 使用
下一篇: 根据JDK生成对应jre