Jump to content

Chương 6 : Bài Tập

- nhoc gôm lại vậy đc không anh Ket ^^

;; ham luu gia tri
(defun getvalue ( a giatri dongnhac / astr) 
(or a (setq a giatri))
(cond
	((= (type a) 'INT) (setq a (cond ((getint (strcat "\n" dongnhac "(" (itoa a) ") :")))(a))))
	((= (type a) 'REAL) (setq a (cond ((getreal (strcat "\n" dongnhac "(" (rtos a 2) ") :")))(a))))
	((= (type a) 'STR) (setq a (cond ((= "" (setq astr (getstring T (strcat "\n" dongnhac " (" a "): ")))) a) (astr))))
))
;;;;
;;;tao textstyle
(defun emk_style (MyStyle MyFont)
(entmake (list    (cons 0 "STYLE")    
(cons 100 "AcDbSymbolTableRecord")    
(cons 100 "AcDbTextStyleTableRecord")    
(cons 2 MyStyle)    (cons 3  MyFont)    
(cons 70 0))))
;;;;;    
;;;ham ve cau thang
(defun kkkv ( diem cao rong sbac / pt1 pt2 e f)
(setvar "cmdecho" 0)
(command ".Pline" diem (setq pt1 (polar diem (/ pi 2) cao)) (setq pt2 (polar pt1 0 rong)) "")
(setq e (entlast))
(command ".line" diem pt2 "")
(setq f (entlast))
(command ".ucs" "e" f)
(command "-array" e "" "R" 1 sbac (distance diem pt2))
(command ".erase" f "")
(command ".ucs" "w")
(setvar "cmdecho" 0)
)
;;;;
(defun danhso (/ codanh)
(initget "Y N")
(setq codanh (Xstrcase (getkword "\nBan co mun danh so bac < Y / N > :")))
(if (= codanh "N") (setq codanh nil) (setq codanh t))
(if codanh
						  (progn
		                    (repeat sb
						      (command "text" (polar pt (/ pi 2) (+ cb 0.05)) (/ cb 1.5) 0 (itoa (setq i (1+ i))))
						      (command ".pline" pt (polar pt (/ pi 2) cb) (setq pt (polar (polar pt (/ pi 2) cb) 0 rb)) "")
						   
						     )
						   )
						 (kkkv pt cb rb sb)
						 )
)


;;;; bai 7 + 8 : ve cau thang them lua chon co danh so bac hay ko ^^
(defun c:vktt (/ lst_va old key )
(setq lst_va '("osmode" "cmdecho" "ANGDIR" "ANGBASE" "AUNITS"))
(setq old (mapcar 'getvar lst_va))
(mapcar 'setvar lst_va '(1 0 0 0 0))
(emk_style "danhso" "Ariali.ttf")
(setvar "textstyle" "danhso")
(setq i 0)
;(initget "Y N")
;(setq codem (Xstrcase (getkword "\nBan co mun danh so bac <Enter=Yes/No>: ")))
;(if (= codem "N") (setq codem nil) (setq codem t))
(initget 1 "A B C")
(setq key (Xstrcase (getkword "\nAsobacktbac Bcaonhasobacrongbac Ccaonhasobacgocnghieng <A/B/C>:")))
(cond 
           ((= key "A")
		        (if (and 
				        (setq pt (getpoint "\nchon diem dat:"))
						(setvar "osmode" 0)
				         (setq sb (getvalue sb 7 "nhap so bac")
				               rb (getvalue rb 0.25 "nhap chieu rong bac")
						       cb (getvalue cb 0.2 "nhap chieu cao bac")))
						(danhso)
                 )
            )
			((= key "B")
			     (if (and 
				        (setq pt (getpoint "\nchon diem dat:"))
						(setvar "osmode" 0)
						(setq cnha (getvalue cnha 3.5 "nhap do cao nha")
						      sb (getvalue sb 9 "nhap so bac")
							  rb (getvalue rb 0.25 "nhap chieu rong bac")
							  cb (/ cnha sb)))
					 (danhso)
				  )
			)
			((= key "C")
			       (if (and   
				          (setq pt (getpoint "\nchon diem dat:"))
						  (setvar "osmode" 0)
						  (setq cnha (getvalue cnha 4.0 "nhap chieu cao nha")
						        sb (getvalue sb 11 "nhap so bac thang:")
								gocng (getvalue gocng 45.0 "nhap goc nghieng")
								cb (/ cnha sb)
								pt4 (polar pt (* PI (/ 90 180.0)) cb)
								pt5 (polar pt4 0 100)
								pt6 (polar pt (* PI (/ gocng 180.0)) 100)
								pt7 (inters pt pt6 pt4 pt5 nil)
								rb (sqrt (- (expt (distance pt pt7) 2) (expt cb 2)))))
						(danhso)
					)
				)
)
(command ".zoom" "e")
(mapcar 'setvar lst_va old)
(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 313847_vktt.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 VKTT để chạy lisp.

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

;; ham luu gia tri
(defun getvalue ( a giatri dongnhac / astr) 
(or a (setq a giatri))
(cond
	((= (type a) 'INT) (setq a (cond ((getint (strcat "\n" dongnhac "(" (itoa a) ") :")))(a))))
	((= (type a) 'REAL) (setq a (cond ((getreal (strcat "\n" dongnhac "(" (rtos a 2) ") :")))(a))))
	((= (type a) 'STR) (setq a (cond ((= "" (setq astr (getstring T (strcat "\n" dongnhac " (" a "): ")))) a) (astr))))
))
;;;;
;;;tao textstyle
(defun emk_style (MyStyle MyFont)
(entmake (list    (cons 0 "STYLE")    
(cons 100 "AcDbSymbolTableRecord")    
(cons 100 "AcDbTextStyleTableRecord")    
(cons 2 MyStyle)    (cons 3  MyFont)    
(cons 70 0))))
;;;;;    
;;;ham ve cau thang
(defun kkkv ( diem cao rong sbac / pt1 pt2 e f)
(setvar "cmdecho" 0)
(command ".Pline" diem (setq pt1 (polar diem (/ pi 2) cao)) (setq pt2 (polar pt1 0 rong)) "")
(setq e (entlast))
(command ".line" diem pt2 "")
(setq f (entlast))
(command ".ucs" "e" f)
(command "-array" e "" "R" 1 sbac (distance diem pt2))
(command ".erase" f "")
(command ".ucs" "w")
(setvar "cmdecho" 0)
)
;;;;
(defun danhso (/ codanh)
(initget "Y N")
(setq codanh (Xstrcase (getkword "\nBan co mun danh so bac < Y / N > :")))
(if (= codanh "N") (setq codanh nil) (setq codanh t))
(if codanh
						  (progn
		                    (repeat sb
						      (command "text" (polar pt (/ pi 2) (+ cb 0.05)) (/ cb 1.5) 0 (itoa (setq i (1+ i))))
						      (command ".pline" pt (polar pt (/ pi 2) cb) (setq pt (polar (polar pt (/ pi 2) cb) 0 rb)) "")
						   
						     )
						   )
						 (kkkv pt cb rb sb)
						 )
)


;;;; bai 7 + 8 : ve cau thang them lua chon co danh so bac hay ko ^^
(defun c:vktt (/ lst_va old key )
(setq lst_va '("osmode" "cmdecho" "ANGDIR" "ANGBASE" "AUNITS"))
(setq old (mapcar 'getvar lst_va))
(mapcar 'setvar lst_va '(1 0 0 0 0))
(emk_style "danhso" "Ariali.ttf")
(setvar "textstyle" "danhso")
(setq i 0)
;(initget "Y N")
;(setq codem (Xstrcase (getkword "\nBan co mun danh so bac <Enter=Yes/No>: ")))
;(if (= codem "N") (setq codem nil) (setq codem t))
(initget 1 "A B C")
(setq key (Xstrcase (getkword "\nAsobacktbac Bcaonhasobacrongbac Ccaonhasobacgocnghieng <A/B/C>:")))
(cond 
           ((= key "A")
		        (if (and 
				        (setq pt (getpoint "\nchon diem dat:"))
						(setvar "osmode" 0)
				         (setq sb (getvalue sb 7 "nhap so bac")
				               rb (getvalue rb 0.25 "nhap chieu rong bac")
						       cb (getvalue cb 0.2 "nhap chieu cao bac")))
						(danhso)
                 )
            )
			((= key "B")
			     (if (and 
				        (setq pt (getpoint "\nchon diem dat:"))
						(setvar "osmode" 0)
						(setq cnha (getvalue cnha 3.5 "nhap do cao nha")
						      sb (getvalue sb 9 "nhap so bac")
							  rb (getvalue rb 0.25 "nhap chieu rong bac")
							  cb (/ cnha sb)))
					 (danhso)
				  )
			)
			((= key "C")
			       (if (and   
				          (setq pt (getpoint "\nchon diem dat:"))
						  (setvar "osmode" 0)
						  (setq cnha (getvalue cnha 4.0 "nhap chieu cao nha")
						        sb (getvalue sb 11 "nhap so bac thang:")
								gocng (getvalue gocng 45.0 "nhap goc nghieng")
								cb (/ cnha sb)
								pt4 (polar pt (* PI (/ 90 180.0)) cb)
								pt5 (polar pt4 0 100)
								pt6 (polar pt (* PI (/ gocng 180.0)) 100)
								pt7 (inters pt pt6 pt4 pt5 nil)
								rb (sqrt (- (expt (distance pt pt7) 2) (expt cb 2)))))
						(danhso)
					)
				)
)
(command ".zoom" "e")
(mapcar 'setvar lst_va old)
(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [LI]Chương 6 : Bài Tập. Xem đúng bài viết #313847 để đọc hướng dẫn và trao đổi thêm.

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