文章接近結尾了,來講講如何用AutoLisp寫刀具路徑的CNC程式,及用AutoLisp來讀取CNC程式並畫出刀具路徑。
以交線的這題來做範例,用AutoLisp來寫已求得的刀具路徑的CNC程式,AutoLisp程式如下:
(defun c:kk()
(setq r1 0 r2 0 r3 0 s1 0)
;依次要求使用者輸入圓柱半徑、圓柱長度、
;圓孔半徑、球刀半徑、倒角尺寸 (setq r1 (getreal "Enter the radius of the pillar : "))
(setq s1 (getreal "Enter the length of the pillar : "))
(setq r2 (getreal "Enter the radius of the hole: "))
(setq r3 (getreal "Enter the radius of the ball-endmilling: "))
(setq chm (getreal "Enter the size of chamfer: "))
(setq file01 (open "test.nc" "w")) ;開檔
;以下五行是程式片頭,隨人寫,也可寫G54的片頭
(princ "%\n" file01)
(princ "O0001\n" file01)
(princ "G0G17G40G49G80G90F250\n" file01)
(princ "G92X0Y0Z20.\n" file01)
(princ "S2000M03\n" file01)
(setq r3 (- r3 (/ chm 1.414)))
(setq x1 0 z1 0 x2 0 z2 0 y1 0 y2 0 i1 1 k1 0)
(command "color" "red" )
(while (< i1 362)
(setq x2 (* -1 (- r2 (/ r3 1.414)) (sin(/ (* i1 3.1415926) 180))) )
(setq y2 (+ (* 0.5 s1) (* (- r2 (/ r3 1.414)) (cos (/ (* i1 3.1415926) 180))) ))
(setq k1 (* r2 (sin(/ (* i1 3.1415926) 180))))
(setq z2 (+ (* -1 r1) (sqrt (- (* r1 r1) (* k1 k1))) (/ r3 1.414) ))
(if (> i1 1.5)
(command "line" (list x1 y1 z1) (list x2 y2 z2) "") )
(if (< i1 1.5)
(progn
(setq zx (+ z2 4))
(princ "G0 " file01)
(pp1) ;呼叫副程式
(setq zx (+ z2 1))
(princ "G1 " file01)
(pp1) )
(progn
(setq zx z2)
(princ " " file01)
(pp1) ))
(setq i1 (+ i1 1) x1 x2 y1 y2 z1 z2)
)
(princ "G0 G91 Z20.\n" file01)
(princ " X0 Y0.\n" file01)
(princ "M30" file01)
(close file01)
(command "vpoint" "-1,-1,1" "")
(command "zoom" "a" "")
)
;副程式
(defun pp1()
(setq pxs(rtos x2 2 3) pys(rtos y2 2 3) pzs(rtos zx 2 3))
(setq pxl(strlen pxs) pyl(strlen pys) pzl(strlen pzs))
(setq pxn(- 7 pxl) pyn(- 7 pyl) pzn(- 7 pzl))
(princ "X" file01)
(princ pxs file01)
(repeat pxn (princ " " file01))
(princ " " file01)
(princ "Y" file01)
(princ pys file01)
(repeat pyn (princ " " file01))
(princ " " file01)
(princ "Z" file01)
(princ pzs file01)
(princ "\n" file01)
)
以上是將刀具路徑寫成CNC程式,這樣只完成一半,最重要的後段是如何將已寫好的CNC程式化為刀具路徑模擬,這一部分我已寫好,不過檔案有點大,解說不易,我把如何使用的影像檔放下載區,可自由下載,如有興趣研究程式碼,請來信索取原文,不過,僅限個人研究用,請尊重智慧財產權,勿拿此程式做商業用,也勿拿來做任何形式的發表。
下2圖是讀取CNC程式來做刀具路徑模擬的範例,第一個圖是球面刀具路徑模擬,第二個圖是平面刀具路徑模擬,此Autolisp是個簡易型的,適用於CNC程式開端是G92的片頭,不能模擬巨集指令,不過,對於CNC程式在1000行內的都適用(因為沒有對RAM做規畫,所以要看使用者的電腦等級),CNC程式得先複製至AutoCAD的目錄之下,才可啟動此Autolisp程式 : 載入後鍵入"bt",並enter,每按一次enter,畫面上就會顯示執行一刀,如果要連續執行,則要鍵入"r",並enter,就會把所有的刀具路徑顯示在螢幕上,請參照http://web.mcvs.tp.edu.tw/cu01/並下載Autolisp之CNC程式模擬.rar(0.2k);。


留言
張貼留言