經讀者偷天詢問,突然想起以前寫的這個抓螢幕圖點的lsp檔,手寫程式者在使用AutoCAD當畫好圖時,常有需要抓螢幕的點來寫CNC程式,本文旨在以Autolisp來將AutoCAD螢幕上所需的點寫出成一個檔案。
autolisp的語法比MACRO還簡單,就是:(函數 ..  ..  ..),這樣而已,至於AutoLisp提供的函數則有17大類,請參考http://tw.myblog.yahoo.com/cu01joe/article?mid=611&prev=613&l=f&fid=17,一個程式的好壞就靠函數與程式寫作者的交互作用,讀者寫Autolisp檔時,只要用Windows裡所附的記事本來寫就可,寫好後存:XXX.lsp(XXX是指檔名),並在AutoCAD環境內載入這個lsp檔,如下圖所示,就可使用。
lsp檔全文如下
;;;;This file is for writing out milling points.;;;;;;;;;;;;;;;;;
(defun c:co()          ;此lsp定義的指令名稱為"co"
  (setq  i1 1 s1 0 counter 0 p1 nil dalst nil)
  (while (< i1 100)  ;此迴圈用於讀取螢幕上滑鼠所抓的點
    (setq p1 (getpoint "Choose the point : \n"))
    (setq dalst (append dalst (list p1)) )
    (if (= p1 nil)
      (setq i1 150)
      (progn
      (setq counter (+ 1 counter ))
        (setq i1 (+ i1 1)) ))
  )
;底下的程式用來將抓取滑鼠點取的點來寫出成檔案
  (setq file01 (open "c:\\coor.txt" "w"))
  (setq x1 13.1313 y1 13.1313 z1 13.1313)
  (setq x2 0  y2 0  z2 0  i1 0)
  (while (< i1 counter)
     (setq p2 (nth i1 dalst) x2 (car p2) y2 (cadr p2) z2 (caddr p2))
     (pp1)    ;呼叫副程式
     (setq i1 (+ i1 1))
  )
  (princ "Please open c:\\coor.txt file.\n" ) 
  (close file01)
)
;副程式的宣告與內容如下
(defun pp1()
  (setq pxs(rtos x2 2 3) pys(rtos y2 2 3) pzs(rtos z2 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)
  (if ( or (< (abs (- (fix x2) x2)) 0.001) (< (- 1 (abs (- (fix x2) x2))) 0.001) )
    (princ "." file01) )
  (repeat pxn (princ " " file01))
  (princ " " file01)
  (princ "Y" file01)
  (princ pys file01)
  (if ( or (< (abs (- (fix y2) y2)) 0.001) (< (- 1 (abs (- (fix y2) y2))) 0.001) )
    (princ "." file01) )
  (repeat pyn (princ " " file01))
  (princ " " file01)
  (princ "Z" file01)
  (if ( or (< (abs (- (fix z2) z2)) 0.001) (< (- 1 (abs (- (fix z2) z2))) 0.001) )
    (princ "." file01) )
  (princ pzs file01)
  (princ "\n" file01)
)
留言
張貼留言