Lisp lấy tọa độ của các đỉnh của 1 polyline của bác ssg
À, mình thì yên tâm rồi nhưng CAD nhiều lúc nó chẳng yên tâm, giải quyết bài toán thường tốt hơn nếu làm tổng quát :) Ví dụ, như hình trên, việc giải quyết cũng không đơn giản lắm. Theo mình hiểu, việc so sánh thuận nghịch trong ý bạn chỉ là so sánh vị trí point đầu và cuối Pline ?
Nếu đúng thì code có thể giống thế này :
(Chú ý : cách chọn hướng mình gợi ý bạn pick chọn 2 điểm, như thế thao tác sẽ dùng thuần chuột, nhanh hơn là pick, pick , tay trái chọn Y/N, tay phải nhấn Space )
;; by ssg, ketxu update ;; Danh so Pline ;;;PUBLIC FUNCTIONS ;;;------------------------------------------------------------------------------- (Defun DTR(x) (/ (* x pi) 180) ) ;;;change degree to radian, return REAL ;;;------------------------------------------------------------------------------- (defun lineP (p0 a r / p1) ;;;Line polar: point, degree angle, radius (setq p1 (polar p0 (dtr a) r)) (command "line" p0 p1 "") ) ;;;------------------------------------------------------------------------------- (defun linePX (p0 x) (lineP p0 0 x)) ;;;Horizontal line: length x, from p0 ;;;------------------------------------------------------------------------------- (defun linePY (p0 y) (lineP p0 90 y)) ;;;Vertical line: length y, from p0 ;;;------------------------------------------------------------------------------- (defun getVert (e / i L) ;;;Return list of all vertex from pline e (setq i 0 L nil) (vl-load-com) (repeat (fix (+ (vlax-curve-getEndParam e) 1)) (setq L (append L (list (vlax-curve-getPointAtParam e i)))) (setq i (1+ i)) ) L ) ;;;------------------------------------------------------------------------------- (defun wtxtMC (txt p h) ;;;Write text Middle Center, specify text, point, height (entmake (list (cons 0 "TEXT") (cons 7 (getvar "textstyle"))(cons 8 (if (tblsearch "Layer" "Defpoints") "Defpoints" (getvar "clayer"))) (cons 1 txt) (cons 10 p) (cons 11 p) (cons 40 h) (cons 72 1) (cons 73 2))) ) ;;;------------------------------------------------------------------------------- (defun Collect(e / e2 SS) ;;;Selection set from e to entlast (setq SS (ssadd)) (ssadd e SS) (while (setq e2 (entnext e)) (ssadd e2 SS) (setq e e2)) SS ) ;;;------------------------------------------------------------------------------- (defun Collect1(e / ss) ;;;Selection set after e to entlast. If e nil, select all from fist entity of drawing. (if (= e nil) (setq ss (collect (entnext))) (progn (setq ss (collect e)) (ssdel e ss)) ) ) ;;;------------------------------------------------------------------------------- ;;;PRIVATE FUNCTIONS ;;;------------------------------------------------------------------------------- (defun txt1(txtL / p1 p2 p3 p4 pL i) ;;;Write texts in 1 row (setq p1 (list (+ (car p0) (* 2 h)) (- (cadr p0) (* 1.5 h))) p2 (polar p1 0 (* 6 h)) p3 (polar p2 0 (* 8 h)) p4 (polar p3 0 (* 8 h)) pL (list p1 p2 p3 p4) i 0 ) (repeat 3 (wtxtMC (nth i txtL) (nth i pL) h) (setq i (1+ i)) ) ) ;;;------------------------------------------------------------------------------- (defun mesh1() ;;;Make 1 mesh unit (linepy p0 (* -3 h)) (command "copy" "L" "" p0 (list (+ (car p0) (* 4 h)) (cadr p0))) (command "array" "L" "" "r" 1 3 (* 8 h)) (linepx (polar p0 (* 1.5 pi) (* 3 h)) (* 20 h)) ) ;;;------------------------------------------------------------------------------- ;;;MAIN PROGRAM ;;;------------------------------------------------------------------------------- (defun C:VC( / h p et p0 p00 pvL oldos j pv num txtL ss bn p1 p2 pvlT) ;;;Vertex Co-ordinate ;;;GET TEXT HEIGHT (if (not h0) (setq h0 1)) (setq h (getreal (strcat "\nChon chieu cao text <" (rtos h0) ">:"))) (if (not h) (setq h h0) (setq h0 h)) (setvar "osmode" 0) ;;;PICK & BASE POINT (setq et (car (entsel))) (setq p1 (getpoint "\n Huong danh so") p2 (getpoint p1 "\n Huong danh so") p00 (getpoint "\nDiem chuan bang toa do (phia tren ben trai):") p0 p00 pvLt (getvert et) pvL (cond ((>(setq dau (*(-(car (car pvLt))(car (last pvlT)))(- (car p1)(car p2)))) 0) pvLt) ((< dau 0) (reverse pvlT))) oldos (getvar "osmode") ) ;;;HEADER (linepx p0 (* 20 h)) (mesh1) (txt1 (list "TT" "X" "Y")) (setq p0 (polar p0 (* 1.5 pi) (* 3 h))) ;;;MAKE RECORDS (setq j 0) (repeat (1- (length pvL)) (mesh1) (setq pv (nth j pvL) num (itoa (1+ j)) txtL (list num (rtos (car pv)) (rtos (cadr pv)) ) ) (txt1 txtL) ;(wtxtMC num (polar pv 0 h) h) (setq p0 (polar p0 (* 1.5 pi) (* 3 h))) (setq j (1+ j)) ) ;;;MAKE BLOCK (setq ss (collect1 et)) (setq bn "1") (while (tblsearch "block" bn) (setq bn (itoa (1+ (atoi bn))))) (command "block" bn p00 ss "") (command "insert" bn p00 "" "" "") ;;;WRITE POINT NAME (setq j 0) (repeat (length pvL) (setq pv (nth j pvL) num (itoa (1+ j)) ) (wtxtMC num (polar pv 0 h) h) (setq j (1+ j)) ) ;;;FINISH (setvar "osmode" oldos) (princ) ) ;;;-------------------------------------------------------------------------------
Mình không thấy lisp lấy tọa độ của điểm cuối cùng, ví dụ polyline có 4 đỉnh thì lisp chỉ lấy tọa độ từ đỉnh 1 đến đỉnh 3 thôi, lạ quá ketxu à. Một điểm nữa là mỗi khi dùng lisp thì tất cả Object snap bị tắt hết, mình phải bấm F3 rồi chọn select all thì mới pick điểm cho hướng đánh số được. :blink:


- Tải lisp
- 162843_vc.lsp
Cách dùng
- Tải tệp 162843_vc.lsp về máy.
- 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ẽ.
- Gõ lệnh VC để chạy lisp.
Mã nguồn 162843_vc.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/51107-nh%E1%BB%9D-ch%E1%BB%89nh-s%E1%BB%ADa-lisp-l%E1%BA%A5y-t%E1%BB%8Da-%C4%91%E1%BB%99-c%E1%BB%A7a-c%C3%A1c-%C4%91%E1%BB%89nh-c%E1%BB%A7a-1-polyline-c%E1%BB%A7a-b%C3%A1c-ssg/
;; by ssg, ketxu update
;; Danh so Pline
;;;PUBLIC FUNCTIONS
;;;-------------------------------------------------------------------------------
(Defun DTR(x) (/ (* x pi) 180) ) ;;;change degree to radian, return REAL
;;;-------------------------------------------------------------------------------
(defun lineP (p0 a r / p1) ;;;Line polar: point, degree angle, radius
(setq p1 (polar p0 (dtr a) r))
(command "line" p0 p1 "")
)
;;;-------------------------------------------------------------------------------
(defun linePX (p0 x) (lineP p0 0 x)) ;;;Horizontal line: length x, from p0
;;;-------------------------------------------------------------------------------
(defun linePY (p0 y) (lineP p0 90 y)) ;;;Vertical line: length y, from p0
;;;-------------------------------------------------------------------------------
(defun getVert (e / i L) ;;;Return list of all vertex from pline e
(setq i 0 L nil)
(vl-load-com)
(repeat (fix (+ (vlax-curve-getEndParam e) 1))
(setq L (append L (list (vlax-curve-getPointAtParam e i))))
(setq i (1+ i))
)
L
)
;;;-------------------------------------------------------------------------------
(defun wtxtMC (txt p h) ;;;Write text Middle Center, specify text, point, height
(entmake (list (cons 0 "TEXT") (cons 7 (getvar "textstyle"))(cons 8 (if (tblsearch "Layer" "Defpoints") "Defpoints" (getvar "clayer")))
(cons 1 txt) (cons 10 p) (cons 11 p) (cons 40 h) (cons 72 1) (cons 73 2)))
)
;;;-------------------------------------------------------------------------------
(defun Collect(e / e2 SS) ;;;Selection set from e to entlast
(setq SS (ssadd))
(ssadd e SS)
(while (setq e2 (entnext e)) (ssadd e2 SS) (setq e e2))
SS
)
;;;-------------------------------------------------------------------------------
(defun Collect1(e / ss)
;;;Selection set after e to entlast. If e nil, select all from fist entity of drawing.
(if (= e nil) (setq ss (collect (entnext)))
(progn (setq ss (collect e)) (ssdel e ss))
)
)
;;;-------------------------------------------------------------------------------
;;;PRIVATE FUNCTIONS
;;;-------------------------------------------------------------------------------
(defun txt1(txtL / p1 p2 p3 p4 pL i) ;;;Write texts in 1 row
(setq
p1 (list (+ (car p0) (* 2 h)) (- (cadr p0) (* 1.5 h)))
p2 (polar p1 0 (* 6 h))
p3 (polar p2 0 (* 8 h))
p4 (polar p3 0 (* 8 h))
pL (list p1 p2 p3 p4)
i 0
)
(repeat 3
(wtxtMC (nth i txtL) (nth i pL) h)
(setq i (1+ i))
)
)
;;;-------------------------------------------------------------------------------
(defun mesh1() ;;;Make 1 mesh unit
(linepy p0 (* -3 h))
(command "copy" "L" "" p0 (list (+ (car p0) (* 4 h)) (cadr p0)))
(command "array" "L" "" "r" 1 3 (* 8 h))
(linepx (polar p0 (* 1.5 pi) (* 3 h)) (* 20 h))
)
;;;-------------------------------------------------------------------------------
;;;MAIN PROGRAM
;;;-------------------------------------------------------------------------------
(defun C:VC( / h p et p0 p00 pvL oldos j pv num txtL ss bn p1 p2 pvlT)
;;;Vertex Co-ordinate
;;;GET TEXT HEIGHT
(if (not h0) (setq h0 1))
(setq h (getreal (strcat "\nChon chieu cao text <" (rtos h0) ">:")))
(if (not h) (setq h h0) (setq h0 h))
(setvar "osmode" 0)
;;;PICK & BASE POINT
(setq et (car (entsel)))
(setq
p1 (getpoint "\n Huong danh so")
p2 (getpoint p1 "\n Huong danh so")
p00 (getpoint "\nDiem chuan bang toa do (phia tren ben trai):")
p0 p00
pvLt (getvert et)
pvL
(cond ((>(setq dau (*(-(car (car pvLt))(car (last pvlT)))(- (car p1)(car p2)))) 0) pvLt)
((< dau 0) (reverse pvlT)))
oldos (getvar "osmode")
)
;;;HEADER
(linepx p0 (* 20 h))
(mesh1)
(txt1 (list "TT" "X" "Y"))
(setq p0 (polar p0 (* 1.5 pi) (* 3 h)))
;;;MAKE RECORDS
(setq j 0)
(repeat (1- (length pvL))
(mesh1)
(setq
pv (nth j pvL)
num (itoa (1+ j))
txtL (list num (rtos (car pv)) (rtos (cadr pv)) )
)
(txt1 txtL)
;(wtxtMC num (polar pv 0 h) h)
(setq p0 (polar p0 (* 1.5 pi) (* 3 h)))
(setq j (1+ j))
)
;;;MAKE BLOCK
(setq ss (collect1 et))
(setq bn "1")
(while (tblsearch "block" bn) (setq bn (itoa (1+ (atoi bn)))))
(command "block" bn p00 ss "")
(command "insert" bn p00 "" "" "")
;;;WRITE POINT NAME
(setq j 0)
(repeat (length pvL)
(setq
pv (nth j pvL)
num (itoa (1+ j))
)
(wtxtMC num (polar pv 0 h) h)
(setq j (1+ j))
)
;;;FINISH
(setvar "osmode" oldos)
(princ)
)
;;;-------------------------------------------------------------------------------
Thảo luận gốc
Lisp được chia sẻ trong chủ đề [Nhờ chỉnh sửa] Lisp lấy tọa độ của các đỉnh của 1 polyline của bác ssg. Xem đúng bài viết #162843 để đọc hướng dẫn và trao đổi thêm.