有了Autolisp的基本概念之後,現在來做個小練習,假設我們要1+2+3+…,一直加到200,可以寫成 : (+ 1 2 3 4 …) ,一直寫到200,不過這樣實在太費事,我們可以寫個小迴圈並將整個程式化成AutoCAD的一個指令來執行,程式如下:
(defun c:sum()
(setq total 0 i1 1)
(while (< i1 201)
(setq total (+ total i1))
(setq i1 (+ i1 1)) ) (princ "\ntotal=" )
(princ total )
(princ )
)
以上之程式解釋如下:
(defun c:sum() |
( 定義一個名叫sum的指令
|
(setq total 0 i1 1) |
設定total = 0, i1 = 1
|
(while (< i1 201) |
(迴圈,當i1< 201,則進入迴圈之內,否則離開迴圈
|
(setq total (+ total i1)) |
迴圈內:設定 total 之值為 total+i1
|
(setq i1 (+ i1 1)) ) |
迴圈內:設定 i1 之值為 i1+1 ,並跳到迴圈第一行)
|
(princ "\ntotal=" ) |
\n:跳一行,並在螢幕上列出total=
|
(princ total ) |
在螢幕上列出算出來的total的值
|
(princ ) |
蓋掉回傳值,以免螢幕重列了兩次total的值
|
) |
完結定義 )
|
將這程式寫在電腦的筆記本裡,並存檔且命名為test01.lsp,注意存檔類型要選"所有檔案",以免存成txt檔,存好之後,進入AutoCAD裡,用滑鼠點選"工具"=>"載入應用程式"並點選檔案的所在路徑及檔名後,按"載入"並"離開",最後回到AutoCAD底下文字列裡,用鍵盤輸入"sum"這個我們已經做好的這個指令名稱,就可啟動這個程式。
知道如何寫迴圈之後,再來學用Autolisp畫圖,先做個半徑為80的半球面的圖形試試,範例如下:
(defun c:draw()
(setq x1 0 z1 0 x2 0 z2 0 i1 1)
(while (< i1 91)
(setq x2 (+ (* 80 (sin (/ (* i1 3.1415926) 180)) ) 0))
(setq z2 (+ -80 (* 80 (cos (/ (* i1 3.1415926) 180)))))
(if (= i1 1 )
(command "line" (list 0 0 20) (list x2 0 3) (list x2 0 z2) "")
(command "line" (list x1 0 z1) (list x2 0 z2) "")
)
(command "circle" (list 0 0 z2) x2 "")
(setq i1 (+ i1 1) x1 x2 z1 z2)
)
(command "vpoint" "1,1,1" "")
(command "zoom" "a" "")
)
將這程式寫在電腦的筆記本裡,並存檔且命名為test02.lsp,注意存檔類型要選所有檔案,程式也可以至http://web.mcvs.tp.edu.tw/cu01/下載Autolisp程式;,有了程式之後,進入AutoCAD裡,就可以將寫好的Autolisp載入AutoCAD裡,化成一個指令來使用,操作過程如下 : 用滑鼠點選AutoCAD上方的"工具"=>"載入應用程式"並點選檔案的所在路徑及檔名後,按"載入"後離開對話框,然後在AutoCAD底下文字列裡,打入"draw",並按Enter鍵,就可啟動這個Autolisp程式,執行結果如下圖所示,Autolisp的載入與驅動也可以下載Autolisp的使用(0.2k);來參考。

此圓球的最高點為Z0,這個程式的迴圈內有個判斷式if,它會判斷i1是否等於1,如果是,則執行第一敘述 (command "line" (list 0 0 20) (list x2 0 3) (list x2 0 z2) ""),若否,則則執行第二敘述(command "line" (list x1 0 z1) (list x2 0 z2) ""),第一敘述在畫兩條線(可做為下刀路徑),從座標(0 0 20) 經(x2 0 3) 到(x2 0 z2),第二敘述在畫一條線,從座標(x1 0 z1) (x2 0 z2),每跑一次迴圈,i1值都會增加1,第二敘述都會被執行(除了第一次跑迴圈是執行第一敘述),同時也都會畫一個圓,i1從1跑到90,所以迴圈總共跑了90次,當i1跑到91時,就跳出迴圈。
球銑刀銑削
如果要用球銑刀來銑這樣的一個球面,要如何利用Autolisp來畫刀具路徑並寫CNC程式呢?如下圖所示,此球面與球銑刀的接處點繞Z軸旋轉一圈,可以得到銑削路徑圓,這個球面可以用這種圓來顯示,但是CNC程式是控制球銑刀的球心來銑削,也就是將球心繞Z軸旋轉一圈,就可以得到刀具路徑圓,這個球面就可以用這種圓來銑削,我們做一個跑90次的迴圈,角a是從Z軸到銑刀接觸點的角度,角a從1度開始,每跑一次迴圈,就增加1度(在迴圈裡可用i1來代替角a),並重新計算刀具路徑圓與銑削路徑圓。

畫刀具路徑之程式也可以至http://web.mcvs.tp.edu.tw/cu01/下載Autolisp程式;,至於程式內容如下 :
(defun c:sph01()
(setq r0 (getreal "Enter the radius of the sphere : "))
(setq r1 (getreal "Enter the radius of the ball-endmill : "))
(setq x1 0 z1 0 x2 0 z2 0 x11 0 z11 0 x22 0 z22 0 i1 1)
(while (< i1 91)
(setq x2 (+ (* r0 (sin (/ (* i1 3.1415926) 180)) ) 0))
(setq z2 (+ (* -1 r0) (* 80 (cos (/ (* i1 3.1415926) 180)))))
(command "color" "bylayer" )
(if (> i1 1)
(command "line" (list x11 0 z11) (list x22 0 z22) "") )
(command "circle" (list 0 0 z2) x2 "")
(setq x22 (* (+ r0 r1) (sin (/ (* i1 3.1415926) 180))) )
(setq z22 (+ -80 (* (+ r0 r1) (cos (/ (* i1 3.1415926) 180))) ))
(command "color" "green" )
(if (> i1 1)
(command "line" (list x11 0 z11) (list x22 0 z22) "") )
(command "circle" (list 0 0 z22) x22 "")
(setq i1 (+ i1 1) x1 x2 z1 z2 x11 x22 z11 z22 ) )
(command "vpoint" "1,1,1" "")
(command "zoom" "a" "")
)

等角圖

側視圖
刀具路徑圓與銑削路徑圓檢查無誤之後,便可以用Autolisp來寫CNC程式,程式如下。
(defun c:kk() | (定義指令kk |
(setq r0 (getreal "Enter the radius of the sphere : ")) (setq r1 (getreal "Enter the radius of the ball-endmill : ")) | 顯示字幕並取得由鍵盤輸入之實數 |
(setq file01 (open "test.nc" "w")) | 開啟可寫入的檔案檔名為test.nc |
(princ "%\n" file01) (princ "O0002\n" file01) (princ "G 0G 17G 40G 49G 80G 90\n" file01) (princ "G92X0Y0Z20.\n" file01) (princ "S 2500M 03\n" file01) | 寫CNC程式的標頭到test.nc檔案裡。 |
(setq x1 0 z1 0 x2 0 z2 0 i1 1) | 設定參數 |
(while (< i1 91) | (迴圈開始,當i1 小於91則進入迴圈 |
(setq x2 (* (+ r0 r1) (sin (/ (* i1 3.1415926) 180))) ) (setq z2 (+ -80 (* (+ r0 r1) (cos (/ (* i1 3.1415926) 180))) )) | 求取並設定x2及z2參數 x2=(r0+r1)*sin( i1*3.14/180) z2=(r0+r1)*cos( i1*3.14/180) |
(command "circle" (list 0 0 z2) x2 "") | 畫一個圓 |
(if (= i1 1 ) | (判斷式,i1是否等於1 |
(progn (princ "G00 X" file01) (princ x2 file01) (princ " Z" file01) (princ "3.\n" file01) (princ "G01 Z" file01) (princ z2 file01) (princ "\n" file01) ) | 判斷式之第一敘述,走兩條路徑 |
(progn (princ "G01 X" file01) (princ x2 file01) (princ " Z" file01) (princ z2 file01) (princ "\n" file01) ) | 判斷式之第二敘述,走1條路徑 |
) | 判斷式終了) |
(princ "G03 X-" file01) (princ x2 file01) (princ " R" file01) (princ x2 file01) (princ "\n" file01) | 逆時針銑半圓 |
(princ "G03 X" file01) (princ x2 file01) (princ " R" file01) (princ x2 file01) (princ "\n" file01) | 逆時針再銑半圓 |
(setq i1 (+ i1 1) x1 x2 z1 z2) | 設定參數 |
) | 迴圈終了) |
(close file01) | 關閉檔案 |
) | 程式終了) |
留言
張貼留言