Jump to content

Lisp lấy các thông số của đối tượng line, polyline, circle .....

Hề hể hề,

Đây là code bổ sung vào lisp trên với chức năng thêm xử lý cả khi đối tượng là circle đồng thời hiển thị luôn các đối tượng được chọn trên màn hình


(defun c:sldb (/ oldos ent ent1 elst ss d)
(vl-load-com)
(setq oldos (getvar "osmode"))
(setvar "osmode" 0)
(command "undo" "be")
(setq ent (car (entsel "\n Chon doi tuong goc")))
(command "offset" (getreal "\n Nhap khoang cach can offset: ") ent (getpoint "\n Chon phia offset ") "")
(setq ent1 (entlast)
         elst (entget ent1)  )
(if (and (= (cdr (assoc 0 elst)) "LWPOLYLINE") (= (cdr (assoc 70 elst)) 1))
   (setq ss (ssget "CP" (acet-geom-vertex-list ent1)))
)
(if (= (cdr (assoc 0 elst)) "CIRCLE")
   (setq d (getreal "\n Nhap khoang cach giua hai diem lien ke: ")
             ss (ssget "CP" (makelist ent1 d)) )
)

(command "undo" "e")
(setvar "osmode" oldos)
(sssetfirst nil ss)
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;-------------------------------------------------------------
(defun makelist ( e d1 / ps pe d d2 p2);;;Make points list along curve e. Length of 1 segment = d1
(vl-load-com);;;Load Visual LISP extensions before use vlax-xxxx functions
(setq
ps (vlax-curve-getStartPoint e);;;Start point
pe (vlax-curve-getEndPoint e);;;End point
d (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e));;;Length of curve e
d2 d1;;;Init variable distance
plst (list)
)
;;;;;;;(command "pline");;;Call pline command
(setq plst (append plst (list ps)));;;Start point
(while (<= d2 d);;;While not over end point pe
(setq p2 (vlax-curve-getPointAtDist e d2));;;Variable point at d2 = length along curve
(setq plst (append plst (list p2)));;;Continue point list with current point  p2
(setq d2 (+ d2 d1));;;Increase distance d2 by d1
);;;End while
;;;;;(if (not (equal pe ps 0.01))
(setq plst (append plst (list pe)));;;Continue points list with pe and finish command
;;;;(command "c" )
;;;)
plst
)

Trong đoạn code trên có sử dụng một phần lisp biến đường tròn thành lwpolyline của bác SSG nhưng thay vì vẽ pline thì mình chuyển lại chỉ tạo points list cho đỡ phải vẽ rồi lại xóa đi.

Bạn nhớ cám ơn bác ấy nếu thấy dùng được cái lisp này nhé.

 

Cam ơn bạn nhé.

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 207883_sldb.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 SLDB để chạy lisp.

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

(defun c:sldb (/ oldos ent ent1 elst ss d)
(vl-load-com)
(setq oldos (getvar "osmode"))
(setvar "osmode" 0)
(command "undo" "be")
(setq ent (car (entsel "\n Chon doi tuong goc")))
(command "offset" (getreal "\n Nhap khoang cach can offset: ") ent (getpoint "\n Chon phia offset ") "")
(setq ent1 (entlast)
         elst (entget ent1)  )
(if (and (= (cdr (assoc 0 elst)) "LWPOLYLINE") (= (cdr (assoc 70 elst)) 1))
   (setq ss (ssget "CP" (acet-geom-vertex-list ent1)))
)
(if (= (cdr (assoc 0 elst)) "CIRCLE")
   (setq d (getreal "\n Nhap khoang cach giua hai diem lien ke: ")
             ss (ssget "CP" (makelist ent1 d)) )
)

(command "undo" "e")
(setvar "osmode" oldos)
(sssetfirst nil ss)
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;-------------------------------------------------------------
(defun makelist ( e d1 / ps pe d d2 p2);;;Make points list along curve e. Length of 1 segment = d1
(vl-load-com);;;Load Visual LISP extensions before use vlax-xxxx functions
(setq
ps (vlax-curve-getStartPoint e);;;Start point
pe (vlax-curve-getEndPoint e);;;End point
d (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e));;;Length of curve e
d2 d1;;;Init variable distance
plst (list)
)
;;;;;;;(command "pline");;;Call pline command
(setq plst (append plst (list ps)));;;Start point
(while (<= d2 d);;;While not over end point pe
(setq p2 (vlax-curve-getPointAtDist e d2));;;Variable point at d2 = length along curve
(setq plst (append plst (list p2)));;;Continue point list with current point  p2
(setq d2 (+ d2 d1));;;Increase distance d2 by d1
);;;End while
;;;;;(if (not (equal pe ps 0.01))
(setq plst (append plst (list pe)));;;Continue points list with pe and finish command
;;;;(command "c" )
;;;)
plst
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Yêu cầu] Lisp lấy các thông số của đối tượng line, polyline, circle ...... Xem đúng bài viết #207883 để đọc hướng dẫn và trao đổi thêm.

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