Jump to content

Lisp lệnh CAN trong chủ đề “SỬA GIÚP MÌNH LISP DÙNG HỘP THOẠI DIALOG?”

Mình có sửa lisp theo sự giúp đỡ của bạn @Thaistreetz nhưng có vài điều mình không hiểu, mong được các bạn giúp đỡ.

Trong biến danh sách dùng *canopy* và canopy thì khác nhau chổ nào vậy? Mục đích của đoạn code này là gì:  (foreach l dcl_code (write-line l file_dcl)).

Với thêm nhờ các bạn bổ sung giúp mình khi nhập dữ liệu tại edit_box này xong thì enter sẽ nhảy con trỏ xuống edit_box dưới, mình sửa hoài mà không được.

Ah, thêm cái nữa là sao mình dùng lệnh: (setq i (atof  (get_tile "slope"))) thì lại không lấy được giá trị của "slope trong hộp thoại nhỉ? Cám ơn các bạn nhiều!

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/170843-s%E1%BB%ADa-gi%C3%BAp-m%C3%ACnh-lisp-d%C3%B9ng-h%E1%BB%99p-tho%E1%BA%A1i-dialog/
(defun mc (/ p q1 q2 q3 q4 q5 q6 q7 oldos tana a sec1 sec2 c d1 d2)
(setq p (getpoint "\nNhap chon vi tri dat:"))
(setq tana (/ i 100))
(setq a (atan tana))
	(setq p1 (polar p 0 b))
	(setq p2 (polar p1 (/ (* pi 90) 180) 200))
	(setq p0 (polar p2 pi 150))
	(setq p3 (polar p0 (/ (* pi 90) 180) 200))
	(setq p4 (polar p3 (/ (* (- 180 (/ (* a 180) pi)) pi) 180) (/ (- b 150) (cos a))))
  	(command "pline" p p1 p2 "a" p3 "l" p4 "c")
	(setq sec1 (entlast))
	(command "change" sec1 "" "p" "la" "thay" "")
	(command "hatch" "ANSI32" 600 45 sec1 "")
	(command "change" "l" "" "p" "la" "hatch" "")    
    (setq n1 1 ch1 "canopy")
    (setq bl1 (strcat ch1 (itoa n1)))
    (while (tblsearch "block" bl1)
	(progn
        (setq n1 (1+ n1))
        (setq bl1 (strcat ch1 (itoa n1)))
        )
    )
	(command "-block" bl1 p "c" "non" p1 "non" p4 "")
	(command "-insert" bl1 p "" "" "" )
)
(defun md (/ p h h1 q1 q2 q3 q4 q5 q6 q7 oldos tana a sec1 sec2 c d1 d2)
(setq p (getpoint "\nNhap chon vi tri dat:"))
(setq tana (/ i 100))
(setq a (atan tana))
   	
	(setq h (+ 400 (* tana (- b 150))))
	(setq p1 (polar p 0 l))
	(setq p2 (polar p1 (/ (* pi 90) 180) h))
	(setq p3 (polar p 0 150))
	(setq p4 (polar p3 (/ (* pi 90) 180) h))
	(setq p5 (polar p1 pi 150))
	(setq p6 (polar p5 (/ (* pi 90) 180) h))
	(setq p7 (polar p3 (/ (* pi 90) 180) 200))
	(setq p8 (polar p5 (/ (* pi 90) 180) 200))
	(setq p9 (polar p7 (/ (* pi 45) 180) 200))
	(command "rectangle" p p2)
  	(command "line" p3 p4 "")
	(command "line" p5 p6 "")
	(command "line" p7 p8 "")
	(command "-hatch" "p" "ANSI32" 600 45 p9 "")
	(command "change" "l" "" "p" "la" "hatch" "")
    (setq n2 1 ch2 "canopy")
    (setq bl2 (strcat ch2 (itoa n2)))
    (while (tblsearch "block" bl2)
	(progn
        (setq n2 (1+ n2))
        (setq bl2 (strcat ch2 (itoa n2)))
        )
    )
	(command "-block" bl2 p "c" "non" p2 "non" p "")
	(command "-insert" bl2 p "" "" "" )
)
(defun mb (/ p p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 sec1 n3 ch3 bl3)
(setq p (getpoint "\nNhap chon vi tri dat:"))
	(setq p1 (polar p 0 l))
	(setq p2 (polar p1 (/ (* pi 90) 180) b))
	(setq p3 (polar p 0 150))
	(setq p4 (polar p3 (/ (* pi 90) 180) b))
	(setq p5 (polar p1 pi 150))
	(setq p6 (polar p5 (/ (* pi 90) 180) b))
	(setq p7 (polar p3 (/ (* pi 45) 180) 300))
	(setq p8 (polar p (/ (* pi 90) 180) b))
	(setq p9 (polar p8 (/ (* pi 270) 180) 150))
	(setq p10 (polar p9 (/ (* pi 270) 180) 200))
	(setq p11 (polar p10 (/ (* pi 270) 180) 300))
	(setq p12 (polar p11 (/ (* pi 270) 180) 500))
	(command "rectangle" p p2)
  	(command "line" p3 p4 "")
	(command "line" p5 p6 "")
	(command "-hatch" "p" "ANSI32" 700 45 p7 "")
	(command "change" "l" "" "p" "la" "hatch" "")
	(command "line" p4 p6 "")
	(setq sec1 (entlast))
	(command "move" sec1 "" p8 p9)
	(command "copy" "l" "" p9 p10 "")
	(command "copy" "l" "" p10 p11 "")
	(command "copy" "l" "" p11 p12 "")
    (setq n3 1 ch3 "canopy")
    (setq bl3 (strcat ch3 (itoa n3)))
    (while (tblsearch "block" bl3)
	(progn
        (setq n3 (1+ n3))
        (setq bl3 (strcat ch3 (itoa n3)))
        )
    )
	(command "-block" bl3 p "c" "non" p2 "non" p "")
	(command "-insert" bl3 p "" "" "" )
)
(defun c:can ( / dcl_code dcl_id file_dcl temp)
(setq oldos (getvar "osmode"))
(setvar "osmode" 0)
(setq old_layer (getvar "clayer"))
(setq lay1 (tblsearch "layer" "roof"))
(if (= lay1 nil) (command "-layer" "n" "roof" "c" "252" "roof" "lw" 0.15 "roof" ""))
(setq lay2 (tblsearch "layer" "hatch"))
(if (= lay2 nil) (command "-layer" "n" "hatch" "c" "8" "hatch" "lw" 0.09 "hatch" ""))
(setq lay3 (tblsearch "layer" "thay"))
(if (= lay3 nil) (command "-layer" "n" "thay" "c" "4" "thay" "lw" 0.15 "thay" ""))
(setq lay4 (tblsearch "layer" "block"))
(if (= lay4 nil) (command "-layer" "n" "block" "c" "21" "block" "lw" 0.09 "block" ""))
(setq lay5 (tblsearch "layer" "gutter"))
(if (= lay5 nil) (command "-layer" "n" "gutter" "c" "6" "gutter" "lw" 0.3 "gutter" ""))
(setvar "clayer" "roof")
 (if (not *canopy*) (setq *canopy* (list "1" "0" "0" "0" "0" "0")))
 (setq dcl_code (list (strcat
"canopy : dialog { label = \"&Lisp ve canopy\";"
" : boxed_radio_row { label = \"Select drawing type\"; key = \"dt\";"
"		 : radio_button { label = \"&Side elevation\"; key = \"se\";}"
"		 : radio_button { label = \"&Front elevation\"; key = \"fe\";}"
"		 : radio_button { label = \"&plan\"; key = \"pl\";}}"
"	: column {"
"	   : edit_box { label = \"Roof slope (%)\"; edit_width = 6; key = \"slope\";}"
"	   : edit_box { label = \"Canopy length\"; edit_width = 6; key = \"length\";}"
"	   : edit_box { label = \"Canopy width\"; edit_width = 6; key = \"width\";}}"
" ok_cancel;}")))
 (setq temp (vl-filename-mktemp "canopy.dcl")	file_dcl (open temp "W"))
 (foreach l dcl_code (write-line l file_dcl))
 (close file_dcl)
 (setq dcl_id (load_dialog temp))
 (vl-file-delete temp)
 (new_dialog "canopy" dcl_id)
 (mapcar 'set_tile (list "se" "fe" "pl" "slope" "length" "width") canopy)
 (cond ((= (car *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(0 1 0)))
			 ((= (cadr *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(0 0 0)))
			 ((= (caddr *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(1 0 0))))
 (action_tile "se" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"slope\") '(0 1 0 2))")
 (action_tile "fe" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"slope\") '(0 0 0 2))")
 (action_tile "pl" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"length\") '(1 0 0 2))")
 (action_tile "accept" "(setq *canopy* (mapcar 'get_tile '(\"se\" \"fe\" \"pl\" \"slope\" \"length\" \"width\"))) (done_dialog)")
 (action_tile "cancel" "(done_dialog) (exit)")
 (start_dialog) (unload_dialog dcl_id)
	(setq i (atof (cadddr *canopy*)))
	(setq l (atof (nth 4 *canopy*)))
	(setq b (atof (last *canopy*)))
 (cond ((= (car *canopy*) "1") (mc))
			 ((= (cadr *canopy*) "1") (md))
			 ((= (caddr *canopy*) "1") (mb)))
(setvar "clayer" old_layer)
(setvar "osmode" oldos)
 (princ))

 

Cách dùng

  1. Tải tệp 422532_can.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh CAN để chạy lisp.

Mã nguồn 422532_can.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/170843-s%E1%BB%ADa-gi%C3%BAp-m%C3%ACnh-lisp-d%C3%B9ng-h%E1%BB%99p-tho%E1%BA%A1i-dialog/
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/170843-s%E1%BB%ADa-gi%C3%BAp-m%C3%ACnh-lisp-d%C3%B9ng-h%E1%BB%99p-tho%E1%BA%A1i-dialog/
(defun mc (/ p q1 q2 q3 q4 q5 q6 q7 oldos tana a sec1 sec2 c d1 d2)
(setq p (getpoint "\nNhap chon vi tri dat:"))
(setq tana (/ i 100))
(setq a (atan tana))
	(setq p1 (polar p 0 b))
	(setq p2 (polar p1 (/ (* pi 90) 180) 200))
	(setq p0 (polar p2 pi 150))
	(setq p3 (polar p0 (/ (* pi 90) 180) 200))
	(setq p4 (polar p3 (/ (* (- 180 (/ (* a 180) pi)) pi) 180) (/ (- b 150) (cos a))))
  	(command "pline" p p1 p2 "a" p3 "l" p4 "c")
	(setq sec1 (entlast))
	(command "change" sec1 "" "p" "la" "thay" "")
	(command "hatch" "ANSI32" 600 45 sec1 "")
	(command "change" "l" "" "p" "la" "hatch" "")    
    (setq n1 1 ch1 "canopy")
    (setq bl1 (strcat ch1 (itoa n1)))
    (while (tblsearch "block" bl1)
	(progn
        (setq n1 (1+ n1))
        (setq bl1 (strcat ch1 (itoa n1)))
        )
    )
	(command "-block" bl1 p "c" "non" p1 "non" p4 "")
	(command "-insert" bl1 p "" "" "" )
)
(defun md (/ p h h1 q1 q2 q3 q4 q5 q6 q7 oldos tana a sec1 sec2 c d1 d2)
(setq p (getpoint "\nNhap chon vi tri dat:"))
(setq tana (/ i 100))
(setq a (atan tana))
   	
	(setq h (+ 400 (* tana (- b 150))))
	(setq p1 (polar p 0 l))
	(setq p2 (polar p1 (/ (* pi 90) 180) h))
	(setq p3 (polar p 0 150))
	(setq p4 (polar p3 (/ (* pi 90) 180) h))
	(setq p5 (polar p1 pi 150))
	(setq p6 (polar p5 (/ (* pi 90) 180) h))
	(setq p7 (polar p3 (/ (* pi 90) 180) 200))
	(setq p8 (polar p5 (/ (* pi 90) 180) 200))
	(setq p9 (polar p7 (/ (* pi 45) 180) 200))
	(command "rectangle" p p2)
  	(command "line" p3 p4 "")
	(command "line" p5 p6 "")
	(command "line" p7 p8 "")
	(command "-hatch" "p" "ANSI32" 600 45 p9 "")
	(command "change" "l" "" "p" "la" "hatch" "")
    (setq n2 1 ch2 "canopy")
    (setq bl2 (strcat ch2 (itoa n2)))
    (while (tblsearch "block" bl2)
	(progn
        (setq n2 (1+ n2))
        (setq bl2 (strcat ch2 (itoa n2)))
        )
    )
	(command "-block" bl2 p "c" "non" p2 "non" p "")
	(command "-insert" bl2 p "" "" "" )
)
(defun mb (/ p p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 sec1 n3 ch3 bl3)
(setq p (getpoint "\nNhap chon vi tri dat:"))
	(setq p1 (polar p 0 l))
	(setq p2 (polar p1 (/ (* pi 90) 180) b))
	(setq p3 (polar p 0 150))
	(setq p4 (polar p3 (/ (* pi 90) 180) b))
	(setq p5 (polar p1 pi 150))
	(setq p6 (polar p5 (/ (* pi 90) 180) b))
	(setq p7 (polar p3 (/ (* pi 45) 180) 300))
	(setq p8 (polar p (/ (* pi 90) 180) b))
	(setq p9 (polar p8 (/ (* pi 270) 180) 150))
	(setq p10 (polar p9 (/ (* pi 270) 180) 200))
	(setq p11 (polar p10 (/ (* pi 270) 180) 300))
	(setq p12 (polar p11 (/ (* pi 270) 180) 500))
	(command "rectangle" p p2)
  	(command "line" p3 p4 "")
	(command "line" p5 p6 "")
	(command "-hatch" "p" "ANSI32" 700 45 p7 "")
	(command "change" "l" "" "p" "la" "hatch" "")
	(command "line" p4 p6 "")
	(setq sec1 (entlast))
	(command "move" sec1 "" p8 p9)
	(command "copy" "l" "" p9 p10 "")
	(command "copy" "l" "" p10 p11 "")
	(command "copy" "l" "" p11 p12 "")
    (setq n3 1 ch3 "canopy")
    (setq bl3 (strcat ch3 (itoa n3)))
    (while (tblsearch "block" bl3)
	(progn
        (setq n3 (1+ n3))
        (setq bl3 (strcat ch3 (itoa n3)))
        )
    )
	(command "-block" bl3 p "c" "non" p2 "non" p "")
	(command "-insert" bl3 p "" "" "" )
)
(defun c:can ( / dcl_code dcl_id file_dcl temp)
(setq oldos (getvar "osmode"))
(setvar "osmode" 0)
(setq old_layer (getvar "clayer"))
(setq lay1 (tblsearch "layer" "roof"))
(if (= lay1 nil) (command "-layer" "n" "roof" "c" "252" "roof" "lw" 0.15 "roof" ""))
(setq lay2 (tblsearch "layer" "hatch"))
(if (= lay2 nil) (command "-layer" "n" "hatch" "c" "8" "hatch" "lw" 0.09 "hatch" ""))
(setq lay3 (tblsearch "layer" "thay"))
(if (= lay3 nil) (command "-layer" "n" "thay" "c" "4" "thay" "lw" 0.15 "thay" ""))
(setq lay4 (tblsearch "layer" "block"))
(if (= lay4 nil) (command "-layer" "n" "block" "c" "21" "block" "lw" 0.09 "block" ""))
(setq lay5 (tblsearch "layer" "gutter"))
(if (= lay5 nil) (command "-layer" "n" "gutter" "c" "6" "gutter" "lw" 0.3 "gutter" ""))
(setvar "clayer" "roof")
 (if (not *canopy*) (setq *canopy* (list "1" "0" "0" "0" "0" "0")))
 (setq dcl_code (list (strcat
"canopy : dialog { label = \"&Lisp ve canopy\";"
" : boxed_radio_row { label = \"Select drawing type\"; key = \"dt\";"
"		 : radio_button { label = \"&Side elevation\"; key = \"se\";}"
"		 : radio_button { label = \"&Front elevation\"; key = \"fe\";}"
"		 : radio_button { label = \"&plan\"; key = \"pl\";}}"
"	: column {"
"	   : edit_box { label = \"Roof slope (%)\"; edit_width = 6; key = \"slope\";}"
"	   : edit_box { label = \"Canopy length\"; edit_width = 6; key = \"length\";}"
"	   : edit_box { label = \"Canopy width\"; edit_width = 6; key = \"width\";}}"
" ok_cancel;}")))
 (setq temp (vl-filename-mktemp "canopy.dcl")	file_dcl (open temp "W"))
 (foreach l dcl_code (write-line l file_dcl))
 (close file_dcl)
 (setq dcl_id (load_dialog temp))
 (vl-file-delete temp)
 (new_dialog "canopy" dcl_id)
 (mapcar 'set_tile (list "se" "fe" "pl" "slope" "length" "width") canopy)
 (cond ((= (car *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(0 1 0)))
			 ((= (cadr *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(0 0 0)))
			 ((= (caddr *canopy*) "1") (mapcar 'mode_tile '("slope" "length" "width") '(1 0 0))))
 (action_tile "se" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"slope\") '(0 1 0 2))")
 (action_tile "fe" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"slope\") '(0 0 0 2))")
 (action_tile "pl" "(mapcar 'mode_tile '(\"slope\" \"length\" \"width\" \"length\") '(1 0 0 2))")
 (action_tile "accept" "(setq *canopy* (mapcar 'get_tile '(\"se\" \"fe\" \"pl\" \"slope\" \"length\" \"width\"))) (done_dialog)")
 (action_tile "cancel" "(done_dialog) (exit)")
 (start_dialog) (unload_dialog dcl_id)
	(setq i (atof (cadddr *canopy*)))
	(setq l (atof (nth 4 *canopy*)))
	(setq b (atof (last *canopy*)))
 (cond ((= (car *canopy*) "1") (mc))
			 ((= (cadr *canopy*) "1") (md))
			 ((= (caddr *canopy*) "1") (mb)))
(setvar "clayer" old_layer)
(setvar "osmode" oldos)
 (princ))

Thảo luận gốc

Lisp được chia sẻ trong chủ đề SỬA GIÚP MÌNH LISP DÙNG HỘP THOẠI DIALOG?. Xem đúng bài viết #422532 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 298)