跳到主要內容

錐面孔倒角(一)

文章日期:2009-02-18 11:05



有一陣子沒講AutoLISP了,本文試著藉錐面軸向孔之倒角來講解如何使用AutoLISP繪製錐面軸向孔輪廓,並寫出倒角的CNC程式
       

錐面的繪製
假設錐面的上頂面半徑為r1,底面半徑為r2,高度是h1,則需使用者輸入的AutoLISP寫法如下所示,當,AutoCAD讀到這三行時,其螢幕底下的字幕行就會出現雙引號內的文字,以(setq r1 (getreal "Enter the top diameter : \n") )來說,螢幕底下的字幕就是會出現Enter the top diameter : ,此時AutoCAD會等待使用者輸入半徑之數值後,才會繼續讀下行的AutoLISP
    (setq r1 (getreal "Enter the top diameter : \n") )
    (setq r2 (getreal "Enter the bottom diameter : \n"))
    (setq h1 (getreal "Enter the height : \n"))
但是,一般來說,圖面上通常標示直徑,所以,為了方便性,讓使用者直接輸入直徑值,而程式內部再除以2,以轉化成半徑值,則AutoLISP寫法如下所示:
    (setq r1 (/ (getreal "Enter the top diameter : \n") 2 ))
    (setq r2 (/ (getreal "Enter the bottom diameter : \n") 2 ))
    (setq h1 (getreal "Enter the height : \n"))
當上述資料經使用者輸入之後,便可以開始繪製錐面,如果要以每度畫一次錐面直線,則需寫一個可以跑360次迴圈,此時可以用while這個函式,寫法如下:
  (setq  i1 1 )
  (while (< i1 361)
        (setq i1 (+ 1 i1))
  )
那麼要如何在每次迴圈之內畫一次錐面直線呢? AutoLISP程式如下所示:
(defun c:bt()
  (setq   r1 0  r2 0  h1 0 )
    (setq r1 (/ (getreal "Enter the top diameter : \n") 2 ))
    (setq r2 (/ (getreal "Enter the bottom diameter : \n") 2 ))
    (setq h1 (getreal "Enter the height : \n"))
  (setq  x2 0  y2 0  x22 0   y22 0 )
  (setq  i1 1  cta (/ 3.1415926 180.0)  ctaWk 0  h1 (* -1 (abs h1)) )
  (while (< i1 361)
    (setq ctaWk (* cta i1))
    (setq x2   (* r1 (cos ctaWk))    y2 (* r1 (sin ctaWk )) )
    (setq x22  (* r2 (cos ctaWk))  y22 (* r2 (sin ctaWk )) )
    (command "line" (list x2 y2 0) (list x22 y22 h1) "" )
    (setq i1 (+ 1 i1))
  )
  (command "zoom" "e" "")
  (command "vpoint" "-1,-1,1"  "")
)

使用者輸入錐面的上頂面直徑10,底面直徑為30,高度是5時,則AutoCAD就會自動畫出下面圖形
   

這錐面的繪製只是用來講解AutoLISP的暖身用並為了下一個步驟-錐面孔的繪製來做視覺上的驗證,看看畫出來的錐面孔的輪廓是否有在錐面上,到底錐面孔的輪廓是甚麼樣子?我還沒畫之前也不清楚,不過有個高人是這樣說 :

""放在斜面就變橢圓放在錐柱上就變雞蛋狀,上尖下圓 還折彎在錐柱上,就是洋竽片...

ㄏㄏ,真是有想像力的洋竽片,下回再分曉了...
 http://tw.myblog.yahoo.com/cu01joe/article?mid=1960&prev=1970&next=1956

留言