2017年12月29日 星期五

AutoLISP&DCL基礎篇 (AutoCAD程序設計魔法書)

AutoLISP&DCL基礎篇 (AutoCAD程序設計魔法書)這本書是公司很久以前買的;有沒有附光碟已不可考??現將練習範例 做成可編輯檔以供後輩參考用!!

第11章 AutoLISP學習範例
EX01:(lsp_01.lsp) 11-1 凹形圖
(defun c:lsp_01()
(setvar "cmdecho" 0);
;cmdecho:為控制使用AutoLISP(command)函數,回應提示與輸入資料。0 為不回應,1 為回應。
(setq pt1 (getpoint "\n 選取圖形左下角:"))
(setq ww (getdist pt1 "\n 寬度<100.0>"))
 ;getdist 可用滑鼠拉一段距離(數值均為正);亦可用鍵盤輸入數值
 (if (null ww) (setq ww 100.0));當ww未賦值時,定義ww為100
(setq hh (getdist pt1 "\n 高度<50.0>"))
(if (null hh) (setq hh 50.0));當hh未賦值時,定義hh為100
 (setq ww3 (/ ww 3))
(setq hh2 (/ hh 2))
;以下為求得各點座標
(setq pt2 (polar pt1 0 ww))
(setq pt3 (polar pt2 (/ pi 2) hh))
(setq pt4 (polar pt3 pi ww3))
(setq pt5 (polar pt4 (* pi 1.5) hh2))
(setq pt6 (polar pt5 pi ww3))
(setq pt7 (polar pt6 (/ pi 2) hh2))
(setq pt8 (polar pt7 pi ww3))
(command "pline" pt1 pt2 pt3 pt4 pt5 pt6 pt7 pt8 "c")
(prin1)
;(prin1)消除執行完後所返回的nil
)




















11-1 四圓孔
(defun c:lsp_02()
(setvar "cmdecho" 0)
;cmdecho:為控制使用AutoLISP(command)函數,回應提示與輸入資料。0 為不回應,1 為回應。
(setq pt1 (getpoint "\n 第一角點:"))
(setq pt3 (getcorner pt1 "\另一角點:"))
;getcorner:由螢幕上的第一點(pt1)為基礎, 選取第二點(pt3),以便畫出矩形.
    (setq pt2 (list (car pt3)(cadr pt1)))
;(car 串列/pt3):傳回串列/pt3中的第一個元素(通常用來取得 X 座標)
 ;(cadr 串列/pt1):傳回串列/pt1中的第二個元素(通常用來取得 Y 座標)
(setq pt4 (list (car pt1)(cadr pt3)))
    (setq rr (getdist pt1 "\n圓半徑<5>:"))
;getdist:可用鼠標拉一段距離(數值均為正);亦可用鍵盤輸入數值
(if (null rr) (setq rr 5.0));當rr未賦值時,定義rr為5.0
    (command "circle" pt1 rr)
(setq en1 (entlast))
;entlast:取出圖形資料中的最後一個圖元
(command "line" (polar pt1 pi (+ rr 3)) (polar pt1 0 (+ rr 3)) "")
(setq en2 (entlast))
(command "line" (polar pt1 (* pi 1.5) (+ rr 3)) (polar pt1 (/ pi 2) (+ rr 3)) "")
(setq en3 (entlast))
(command "copy" en1 en2 en3 "" "m" pt1 pt2 pt3 pt4 "")
(prin1)
;prin1:消除執行完後所返回的nil
)



EX 練習函數 entsel、car、cdr、entget
<<選取圖>>

指令:(setq en1 (entsel))
選取物件(碰選圓):(<圖元名稱: 7ffffb04ea0> (49.729 7.14407 0.0))
    其中<圖元名稱: 7ffffb04ea0> <--圖元名稱
    (49.729 7.14407 0.0)<---滑鼠點選圓點之該點座標

指令:(car en1)
<圖元名稱: 7ffffb04ea0> <--串列的第一元素  '

指令:(cadr en1)
(49.729 7.14407 0.0) <--串列的第二元素  '

指令:(setq en1_data (entget (car en1)))
((-1 . <圖元名稱: 7ffffb04ea0>) (0 . "CIRCLE") (330 . <圖元名稱: 7ffffb039f0>) (5 . "27A") (100 .

"AcDbEntity") (67 . 0) (410 . "Model") (8 . "STR") (100 . "AcDbCircle") (10 60.0 15.0 0.0) (40 .

12.5) (210 0.0 0.0 1.0))

<<選取線>>

指令:(setq en2 (entsel))
選取物件(碰選線):(<圖元名稱: 7ffffb04e90> (24.7951 17.6415 0.0))

指令:(setq en2_data (entget (car en2)))
((-1 . <圖元名稱: 7ffffb04e90>) (0 . "LINE") (330 . <圖元名稱: 7ffffb039f0>) (5 . "279") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "STR") (100 . "AcDbLine") (10 15.0 10.0 0.0) (11 65.0 50.0 0.0) (210 0.0 0.0 1.0))

<<選取文字>>

指令:(setq en3 (entsel))
選取物件(碰選文字): (<圖元名稱: 7ffffb04ed0> (90.6104 7.64996 0.0))

指令:(setq en3_data (entget (car en3))):'顯示聯合資料串列
((-1 . <圖元名稱: 7ffffb04ed0>) (0 . "TEXT") (330 . <圖元名稱: 7ffffb039f0>) (5 . "27D") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "TXT") (100 . "AcDbText") (10 90.0055 6.48393 0.0) (40 . 8.0) (1 . "AutoCAD") (50 . 0.0) (41 . 1.0) (51 . 0.0) (7 . "Standard") (71 . 0) (72 . 0) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . "AcDbText") (73 . 0))

聯合資料串列中個子串列群碼分析說明參考 - DXF 群組碼

EX02:(lsp_02.lsp)
(defun c:lsp_02()
     (setvar "cmdecho" 0)
;cmdecho:為控制使用AutoLISP(command)函數,回應提示與輸入資料。0 為不回應,1 為回應。
(setq pt1 (getpoint "\n 第一角點:"))
(setq pt3 (getcorner pt1 "\另一角點:"))
;getcorner:由螢幕上的第一點(pt1)為基礎, 選取第二點(pt3),以便畫出矩形.
      (setq pt2 (list (car pt3)(cadr pt1)))
;(car 串列/pt3):傳回串列/pt3中的第一個元素(通常用來球 X 座標)
;(cadr 串列/pt1):傳回串列/pt1中的第二個元素(通常用來球 Y 座標)
(setq pt4 (list (car pt1)(cadr pt3)))
      (setq rr (getdist pt1 "\n圓半徑<5>:"))
;getdist:可用鼠標拉一段距離(數值均為正);亦可用鍵盤輸入數值
(if (null rr) (setq rr 5.0));當rr未賦值時,定義rr為5.0
    (command "circle" pt1 rr)
(setq en1 (entlast))
;entlast:取出圖形資料中的最後一個圖元
(command "line" (polar pt1 pi (+ rr 3)) (polar pt1 0 (+ rr 3)) "")
(setq en2 (entlast))
(command "line" (polar pt1 (* pi 1.5) (+ rr 3)) (polar pt1 (/ pi 2) (+ rr 3)) "")
(setq en3 (entlast))
(command "copy" en1 en2 en3 "" "m" pt1 pt2 pt3 pt4 "")
(prin1)
;prin1:消除執行完後所返回的nil
)

EX03:(lsp_03.lsp)
(defun c:lsp_03()
(setvar "cmdecho" 0)
(setq pt1 (getpoint "\n第一角點:"))
(setq pt3 (getcorner pt1 "\另一角點"))
(setq pt2 (list (car pt3) (cadr pt1)))
(setq pt4 (list (car pt1) (cadr pt3)))
(command "Pline" pt1 pt3 pt2 pt4 "c")
(setq dd (/ (distance pt1 pt4) 2))
(setq ang1 (angle pt2 pt3))
(setq ang2 (angle pt4 pt1))
;繪製兩側半弧
(command "arc" pt2 "e" (polar pt2 ang1 dd ) "a" 180)
(command "arc" (polar pt2 ang1 dd ) "e" pt3 "a" 180)
(command "arc" pt4 "e" (polar pt4 ang2 dd ) "a" 180)
(command "arc" (polar pt4 ang2 dd ) "e" pt1 "a" 180)
(prin1)
)

EX04:(lsp_04.lsp)
(defun c:lsp_04()
(setvar "cmdecho" 0)
(setq pt1 (getpoint "\n 輸入第一點:"))
(setq pt2 (getpoint pt1 "\n 以pt1為準輸入第二點(需在第四象限):\n"))
(setq LL (distance pt1 pt2))
(setq ang (angle pt1 pt2))
(setq pt3 (polar pt2 (+ ang (/ (* 120 pi) 180)) LL))
(command "polygon" 3 "e" pt1 pt2)
(command "arc" pt1 "e" pt2 "a" 180)
(setq en1 (entlast))
(setq mid1 (polar pt1 ang (/ LL 2)))
(command "circle" "2p" mid1 (polar mid1 (+ ang (* pi 1.5)) (/ LL 2)))
;------
(setq en2 (entlast))
(setq mid2 (polar pt2 (+ ang (/ (* 120 pi) 180)) (/ LL 2)))
(setq cen (inters pt3 mid1 pt1 mid2))
(command "array" en1 en2 "" "p" cen 3 "" "")
(command "line" pt1 cen pt2 cen pt3 cen);自己另加
(prin1)
)

EX05-比較最常與最短 lsp_05.lsp
(defun c:lsp_05()
(setvar "cmdecho" 0)
(setq pt1 (getpoint "\n 輸入起點:"))
(command "pline" pt1)
(setq pt2 (getpoint "\n 輸入第二點:"))
(command pt2)
(setq pt3 (getpoint "\n 輸入第三起點:"))
(command pt3)
(setq pt4 (getpoint "\n 輸入第四起點:"))
(command pt4)
(setq pt5 (getpoint "\n 輸入第五起點:"))
(command pt5 "c")
;----計算每段長度 -----
(setq dist1-2 (distance pt1 pt2))
(setq dist2-3 (distance pt2 pt3))
(setq dist3-4 (distance pt3 pt4))
(setq dist4-5 (distance pt4 pt5))
(setq dist5-1 (distance pt5 pt1))
(setq cc '())
(setq cc (cons (list dist1-2 pt1 pt2) cc))
(setq cc (cons (list dist2-3 pt2 pt3) cc))
(setq cc (cons (list dist3-4 pt3 pt4) cc))
(setq cc (cons (list dist4-5 pt4 pt5) cc))
(setq cc (cons (list dist5-1 pt5 pt1) cc))
(setq max_dd (max dist1-2 dist2-3 dist3-4 dist4-5 dist5-1))
(setq min_dd (min dist1-2 dist2-3 dist3-4 dist4-5 dist5-1))
(setq max_pt (assoc max_dd cc))
(setq max_pt1 (cadr max_pt))
(setq max_pt2 (caddr max_pt))
(setq max_ptm (polar max_pt1 (angle max_pt1 max_pt2) (/ max_dd 2)))
(setq min_pt (assoc min_dd cc))
(setq min_pt1 (cadr min_pt))
(setq min_pt2 (caddr min_pt))
;-------------------------
(command "arc" max_pt1 "e" max_ptm "a" 180)
(command "arc" max_ptm "e" max_pt2 "a" 180)
(command "arc" min_pt1 "e" min_pt2 "a" 180)
(prompt "max:" );自行加入測試用
(prin1)
)

沒有留言:

張貼留言