Jump to content

Lisp lệnh HH trong chủ đề “Nhờ viết lisp: Tạo dim vuông góc giữa hai đường Polyline”

Bạn thử xem ^^

(defun c:HH(/ lst_va old mcat tenMC plDo plXanh lst_ver fn pw p2 i LastObj lst_data)
	(setq lst_va '("osmode" "cmdecho"))
	(setq old (mapcar 'getvar lst_va))
	(mapcar 'setvar lst_va '(0 0))
	(vl-load-com)
	;-----------------------
(prompt "\n Chon ten mat cat!")
(setq mcat (ssget "+.:E:S" (list (cons 0 "TEXT,MTEXT"))))
(while mcat 
	(setq tenMC (cdr (assoc 1 (entget (ssname mcat 0)))))
	(setq plDo (car(entsel "\n Chon duong mau do: ")))
	(setq plXanh (car(entsel "\n Chon duong mau xanh: ")))
	(if (and tenMC plDo plXanh)
		(progn
			(setq lst_ver (acet-geom-vertex-list plDo))
			(if (< (car (last lst_ver)) (car (car lst_ver))) (setq lst_ver (reverse lst_ver)) )
			(setq i 1)
			(foreach p1 lst_ver
				(setq p2 (vlax-curve-getClosestPointTo plXanh p1))
				(MakeText p1 (itoa i) 0.12 0 "L" nil "stt" nil nil)
				(command ".dimaligned" p1 p2 p2)
				(setq LastObj (vlax-ename->vla-object (entlast)))
				(setq lst_data (cons (strcat tenMC "," (itoa i) "," (rtos(vla-get-Measurement LastObj) 2 4) "," (rtos (car p1) 2 4) "," (rtos (cadr p1) 2 4) "," (rtos (last p1) 2 4)) lst_data))
				(setq i (1+ i))
			)
		)
		(princ "*** Chon lung tung roi! Lam lai nhe! ***")
	)	;if
	(prompt "\n Chon ten mat cat! <Enter de xuat so lieu>")
	(setq mcat (ssget "+.:E:S" (list (cons 0 "TEXT,MTEXT"))))
)	;while
(if lst_data
	(progn
		(setq lst_data (reverse lst_data))
		(setq fn (getfiled "Chon file de xuat ket qua" "" "csv" 1))
 		(setq pw (open fn "w"))
		(write-line (strcat "Mat cat, STT, K/cach, X, Y, Z") pw)
		(foreach elem lst_data
			(write-line elem pw)
		)
		(close pw)
	)
)
	(mapcar 'setvar lst_va old)
	(princ)
)
;;==================================================
(defun MakeText (point string Height Ang justify Style Layer Color xdata / Lst)
; Ang: Radial	
(setq Lst (list '(0 . "TEXT")
				(cons 8 (if Layer Layer (getvar "Clayer")))									
				(cons 62 (if Color Color 256))									
				(cons 10 point)									
				(cons 40 Height)									
				(cons 1 string)									
				(if Ang (cons 50 Ang))									
				(cons 7 (if Style Style (getvar "Textstyle")))									
				(cons -3 (if xdata (list xdata) nil)))				
				justify (strcase justify))	
				(cond ((= justify "C") (setq Lst (append Lst (list (cons 72 1) (cons 11 point)))))				
					  ((= justify "R") (setq Lst (append Lst (list (cons 72 2) (cons 11 point)))))				
					  ((= justify "M") (setq Lst (append Lst (list (cons 72 4) (cons 11 point)))))				
					  ((= justify "TL") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 3)))))				
					  ((= justify "TC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 3)))))				
					  ((= justify "TR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 3)))))					
					  ((= justify "ML") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 2)))))				
					  ((= justify "MC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 2)))))				
					  ((= justify "MR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 2)))))				
					  ((= justify "BL") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 1)))))				
					  ((= justify "BC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 1)))))				
					  ((= justify "BR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 1)))))
					  )	
					  (entmakex Lst)
)	;end

 

Cách dùng

  1. Tải tệp 421886_hh.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 HH để chạy lisp.

Mã nguồn 421886_hh.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/170671-nh%E1%BB%9D-vi%E1%BA%BFt-lisp-t%E1%BA%A1o-dim-vu%C3%B4ng-g%C3%B3c-gi%E1%BB%AFa-hai-%C4%91%C6%B0%E1%BB%9Dng-polyline/
(defun c:HH(/ lst_va old mcat tenMC plDo plXanh lst_ver fn pw p2 i LastObj lst_data)
	(setq lst_va '("osmode" "cmdecho"))
	(setq old (mapcar 'getvar lst_va))
	(mapcar 'setvar lst_va '(0 0))
	(vl-load-com)
	;-----------------------
(prompt "\n Chon ten mat cat!")
(setq mcat (ssget "+.:E:S" (list (cons 0 "TEXT,MTEXT"))))
(while mcat 
	(setq tenMC (cdr (assoc 1 (entget (ssname mcat 0)))))
	(setq plDo (car(entsel "\n Chon duong mau do: ")))
	(setq plXanh (car(entsel "\n Chon duong mau xanh: ")))
	(if (and tenMC plDo plXanh)
		(progn
			(setq lst_ver (acet-geom-vertex-list plDo))
			(if (< (car (last lst_ver)) (car (car lst_ver))) (setq lst_ver (reverse lst_ver)) )
			(setq i 1)
			(foreach p1 lst_ver
				(setq p2 (vlax-curve-getClosestPointTo plXanh p1))
				(MakeText p1 (itoa i) 0.12 0 "L" nil "stt" nil nil)
				(command ".dimaligned" p1 p2 p2)
				(setq LastObj (vlax-ename->vla-object (entlast)))
				(setq lst_data (cons (strcat tenMC "," (itoa i) "," (rtos(vla-get-Measurement LastObj) 2 4) "," (rtos (car p1) 2 4) "," (rtos (cadr p1) 2 4) "," (rtos (last p1) 2 4)) lst_data))
				(setq i (1+ i))
			)
		)
		(princ "*** Chon lung tung roi! Lam lai nhe! ***")
	)	;if
	(prompt "\n Chon ten mat cat! <Enter de xuat so lieu>")
	(setq mcat (ssget "+.:E:S" (list (cons 0 "TEXT,MTEXT"))))
)	;while
(if lst_data
	(progn
		(setq lst_data (reverse lst_data))
		(setq fn (getfiled "Chon file de xuat ket qua" "" "csv" 1))
 		(setq pw (open fn "w"))
		(write-line (strcat "Mat cat, STT, K/cach, X, Y, Z") pw)
		(foreach elem lst_data
			(write-line elem pw)
		)
		(close pw)
	)
)
	(mapcar 'setvar lst_va old)
	(princ)
)
;;==================================================
(defun MakeText (point string Height Ang justify Style Layer Color xdata / Lst)
; Ang: Radial	
(setq Lst (list '(0 . "TEXT")
				(cons 8 (if Layer Layer (getvar "Clayer")))									
				(cons 62 (if Color Color 256))									
				(cons 10 point)									
				(cons 40 Height)									
				(cons 1 string)									
				(if Ang (cons 50 Ang))									
				(cons 7 (if Style Style (getvar "Textstyle")))									
				(cons -3 (if xdata (list xdata) nil)))				
				justify (strcase justify))	
				(cond ((= justify "C") (setq Lst (append Lst (list (cons 72 1) (cons 11 point)))))				
					  ((= justify "R") (setq Lst (append Lst (list (cons 72 2) (cons 11 point)))))				
					  ((= justify "M") (setq Lst (append Lst (list (cons 72 4) (cons 11 point)))))				
					  ((= justify "TL") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 3)))))				
					  ((= justify "TC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 3)))))				
					  ((= justify "TR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 3)))))					
					  ((= justify "ML") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 2)))))				
					  ((= justify "MC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 2)))))				
					  ((= justify "MR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 2)))))				
					  ((= justify "BL") (setq Lst (append Lst (list (cons 72 0) (cons 11 point) (cons 73 1)))))				
					  ((= justify "BC") (setq Lst (append Lst (list (cons 72 1) (cons 11 point) (cons 73 1)))))				
					  ((= justify "BR") (setq Lst (append Lst (list (cons 72 2) (cons 11 point) (cons 73 1)))))
					  )	
					  (entmakex Lst)
)	;end

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Nhờ viết lisp: Tạo dim vuông góc giữa hai đường Polyline. Xem đúng bài viết #421886 để đọc hướng dẫn và trao đổi thêm.

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