Jump to content

Chương 11.1 Cơ bản về hộp thoại trong Lisp - DCL

Theo "đường lối" của Nhóc, mình muốn được "thõa sức sáng tạo"

Nên mình làm cái này (code bên dưới)

Mong được góp ý & đồng ý cho qua cửa chương 11.1 để chạy đua với NHóc cho vui ! :D

p/s:

- Dạo này mình không có nhiều thời gian cho việc học nên ko theo kịp

- Thầy Két có thể thay đổi đường dẫn trong code lisp và "thử" >>>>> Mình xin dép :D :D: D

 

 

Code:

//VD2: may tinh
VD2:dialog
	{
	label = "EX_1 Calculator";
	:column
		{
		:boxed_column
			{  height = 2;
			:text
				{ key = "scr"; value = "0"; alignment = right; }
			}
		:row
			{
			:column
				{
		
				:boxed_column
					{ key = "number";
					:row
						{
						:button
							{ label = "1"; height = 2; width = 2; key = "1"; }					
						:button
							{ label = "2"; height = 2; width = 2; key = "2"; }
						:button
							{ label = "3"; height = 2; width = 2; key = "3"; }
						}
					:row
						{
						:button
							{ label = "4"; height = 2; width = 2; key = "4"; }
						:button
							{ label = "5"; height = 2; width = 2; key = "5"; }
						:button
							{ label = "6"; height = 2; width = 2; key = "6"; }
						}
					:row
						{
						:button
							{ label = "7"; height = 2; width = 2; key = "7"; }
						:button
							{ label = "8"; height = 2; width = 2; key = "8"; }
						:button
							{ label = "9"; height = 2; width = 2; key = "9"; }
						}
					:button
						{ label = "0"; height = 2; width = 2; key = "0"; }
					}
				:row
					{
					:button
						{ label = "Exit"; height = 2; width = 2; key = "cancel"; is_default = false; is_cancel = true; }
					:spacer {width = 10;}
					:toggle
						{ key = "tog1"; label = "Input Real"; value = "1"; }
					:button
						{ label = "."; height = 2; width = 2; key = "dot"; }
					}
				}
			:column
				{
				:boxed_column
					{
					:button
						{ label = "+"; height = 2; width = 2; key = "add"; }
					:button
						{ label = "-"; height = 2; width = 2; key = "sub"; }
					:button
						{ label = "x"; height = 2; width = 2; key = "mul"; }
					:button
						{ label = "/"; height = 2; width = 2; key = "div"; }
					}
				:button
					{ label = "="; height = 5; width = 2; key = "tinh"; is_default = true; }
				}
			}
		}
	}

 

 

Code lisp:

(defun c:VD2()
(vl-load-com)
(or cal (arxload "geomcal"))
(defun tinh ()
	(if (/= id t)
		(progn
			(princ 
				(setq 
					kq (vl-princ-to-string 
							(cond 
								((cal (strcat "1.0*" (get_tile "scr"))))
								(t nil)
							)
						)))
			(set_tile "scr" kq)
			(setq id t)
		)
	)
	(princ)
)
(defun input_num (num / phep_toan)
	(setq phep_toan (get_tile "scr"))
	(if (or (= "0" phep_toan) id) (setq phep_toan ""))
	(setq phep_toan (strcat phep_toan (itoa num)))
	(set_tile "scr" phep_toan)
	(setq id nil)
)
(defun input_operations (ope_code / phep_toan)
	(setq phep_toan (get_tile "scr"))
	(setq phep_toan (strcat phep_toan (chr ope_code)))
	(set_tile "scr" phep_toan)
	(setq id nil)
)
(defun set_input ( / code)
	(setq code (get_tile "tog1"))
	(if (= "1" code) (mode_tile "dot" 0) 	;enable
		(mode_tile "dot" 1)	;disable
		)
)
(setq dcl_id (load_dialog "C:/Documents and Settings/tbc/Desktop/nhap/VDS.dcl"))
(if (not (new_dialog "VD2" dcl_id)) 
	(progn
		(alert "*** Khong tom thay file DCL ! ***")
		(exit)
		)
)
(action_tile "1" "(input_num 1)")
(action_tile "2" "(input_num 2)")
(action_tile "3" "(input_num 3)")
(action_tile "4" "(input_num 4)")
(action_tile "5" "(input_num 5)")
(action_tile "6" "(input_num 6)")
(action_tile "7" "(input_num 7)")
(action_tile "8" "(input_num 8)")
(action_tile "9" "(input_num 9)")
(action_tile "0" "(input_num 0)")
(action_tile "tog1" "(set_input)")
(action_tile "add" "(input_operations 43)")
(action_tile "sub" "(input_operations 45)")
(action_tile "mul" "(input_operations 42)")
(action_tile "div" "(input_operations 47)")
(action_tile "dot" "(input_operations 46)")
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
(action_tile "tinh" "(tinh)")
(start_dialog)
(unload_dialog dcl_id) 
(if (= ddiag 1) (princ "\n*** Xong ! ***"))
(princ)
)

Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.

Cách dùng

  1. Chép mã nguồn, lưu thành tệp 331049_vd2.lsp.
  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 VD2 để chạy lisp.

Mã nguồn 331049_vd2.lsp (trích từ bài viết gốc)

//VD2: may tinh
VD2:dialog
	{
	label = "EX_1 Calculator";
	:column
		{
		:boxed_column
			{  height = 2;
			:text
				{ key = "scr"; value = "0"; alignment = right; }
			}
		:row
			{
			:column
				{
		
				:boxed_column
					{ key = "number";
					:row
						{
						:button
							{ label = "1"; height = 2; width = 2; key = "1"; }					
						:button
							{ label = "2"; height = 2; width = 2; key = "2"; }
						:button
							{ label = "3"; height = 2; width = 2; key = "3"; }
						}
					:row
						{
						:button
							{ label = "4"; height = 2; width = 2; key = "4"; }
						:button
							{ label = "5"; height = 2; width = 2; key = "5"; }
						:button
							{ label = "6"; height = 2; width = 2; key = "6"; }
						}
					:row
						{
						:button
							{ label = "7"; height = 2; width = 2; key = "7"; }
						:button
							{ label = "8"; height = 2; width = 2; key = "8"; }
						:button
							{ label = "9"; height = 2; width = 2; key = "9"; }
						}
					:button
						{ label = "0"; height = 2; width = 2; key = "0"; }
					}
				:row
					{
					:button
						{ label = "Exit"; height = 2; width = 2; key = "cancel"; is_default = false; is_cancel = true; }
					:spacer {width = 10;}
					:toggle
						{ key = "tog1"; label = "Input Real"; value = "1"; }
					:button
						{ label = "."; height = 2; width = 2; key = "dot"; }
					}
				}
			:column
				{
				:boxed_column
					{
					:button
						{ label = "+"; height = 2; width = 2; key = "add"; }
					:button
						{ label = "-"; height = 2; width = 2; key = "sub"; }
					:button
						{ label = "x"; height = 2; width = 2; key = "mul"; }
					:button
						{ label = "/"; height = 2; width = 2; key = "div"; }
					}
				:button
					{ label = "="; height = 5; width = 2; key = "tinh"; is_default = true; }
				}
			}
		}
	}

(defun c:VD2()
(vl-load-com)
(or cal (arxload "geomcal"))
(defun tinh ()
	(if (/= id t)
		(progn
			(princ 
				(setq 
					kq (vl-princ-to-string 
							(cond 
								((cal (strcat "1.0*" (get_tile "scr"))))
								(t nil)
							)
						)))
			(set_tile "scr" kq)
			(setq id t)
		)
	)
	(princ)
)
(defun input_num (num / phep_toan)
	(setq phep_toan (get_tile "scr"))
	(if (or (= "0" phep_toan) id) (setq phep_toan ""))
	(setq phep_toan (strcat phep_toan (itoa num)))
	(set_tile "scr" phep_toan)
	(setq id nil)
)
(defun input_operations (ope_code / phep_toan)
	(setq phep_toan (get_tile "scr"))
	(setq phep_toan (strcat phep_toan (chr ope_code)))
	(set_tile "scr" phep_toan)
	(setq id nil)
)
(defun set_input ( / code)
	(setq code (get_tile "tog1"))
	(if (= "1" code) (mode_tile "dot" 0) 	;enable
		(mode_tile "dot" 1)	;disable
		)
)
(setq dcl_id (load_dialog "C:/Documents and Settings/tbc/Desktop/nhap/VDS.dcl"))
(if (not (new_dialog "VD2" dcl_id)) 
	(progn
		(alert "*** Khong tom thay file DCL ! ***")
		(exit)
		)
)
(action_tile "1" "(input_num 1)")
(action_tile "2" "(input_num 2)")
(action_tile "3" "(input_num 3)")
(action_tile "4" "(input_num 4)")
(action_tile "5" "(input_num 5)")
(action_tile "6" "(input_num 6)")
(action_tile "7" "(input_num 7)")
(action_tile "8" "(input_num 8)")
(action_tile "9" "(input_num 9)")
(action_tile "0" "(input_num 0)")
(action_tile "tog1" "(set_input)")
(action_tile "add" "(input_operations 43)")
(action_tile "sub" "(input_operations 45)")
(action_tile "mul" "(input_operations 42)")
(action_tile "div" "(input_operations 47)")
(action_tile "dot" "(input_operations 46)")
(action_tile "cancel" "(setq ddiag 1)(done_dialog)")
(action_tile "tinh" "(tinh)")
(start_dialog)
(unload_dialog dcl_id) 
(if (= ddiag 1) (princ "\n*** Xong ! ***"))
(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Li] Chương 11.1 Cơ bản về hộp thoại trong Lisp - DCL. Xem đúng bài viết #331049 để đọc hướng dẫn và trao đổi thêm.

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