Jump to content

Lisp lệnh 3DP trong chủ đề “[ hỏi ] cách nối đường 3D để có 1 đường 3d kín”

Đây là cái lisp của "người ta", dùng để nối các 3D Line thành 3D Polyline. Tôi đã test thử trên bản vẽ của bạn, thấy OK, nhưng lâu thì khỏi nói, vì polyline của bạn vô vàn điểm!
Cách dùng:
1). Vẽ 2 LINE nối 2 đầu của cặp POLYLINE.
2). EXPLODE 2 POLYLINE ra thành 1 mớ LINE.
3). Load và dùng lisp: chọn tất cả đối tượng gồm: 2 line + đống line do explode sinh ra => pha cà phê nhâm nhi ít phút => có 1 POLYLINE liền nhau.

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 220261_3dp.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 3DP để chạy lisp.

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

;; Routine to convert 3d lines to as few as possable 3d polylines
;;  CAB 12/23/03
;;
;;   Collect all lines into a selection set
;;   Create a list of end points
;;   Walk through end points and find matches
;;   Create a list of points then make into plines
;;
(defun c:3dp (/ sslines lc ssl lep en end end1 end2 ptr 3dll pt)

 (if (setq sslines (ssget '((0 . "LINE"))))
   (prompt "\nWorking, Please Wait.")
   (progn
     (prompt "\nNo Lines Found.    ")
     (exit)
   )
 )
 (setq usercmd (getvar "CMDECHO"))
 (setvar "CMDECHO" 0)
 (setq useros (getvar "osmode"))
 (setvar "osmode" 0)
 (setq lc    0
ssl   (sslength sslines)
lep   (list)
3dcnt 0 ; # of 3d lines
fuzz  0 ; change as needed
 )
 (prompt (strcat "\n" (rtos (/ ssl 2) 2 0) " lines found."))
 (command "undo" "begin")
 (while (< lc ssl) ; make a list of line end points
   (setq en  (entget (ssname sslines lc))
lep (cons (cdr (assoc 11 en)) lep)
lep (cons (cdr (assoc 10 en)) lep)
lc  (1+ lc)
   ) ; inc pointer
 ) ; end while

 (while lep
   ;; lep is a collection of line end points
   (setq end1 (car lep) ; get one end
end2 (cadr lep) ; get the other end
lep  (cddr lep) ; remove that line from list
3dll (list)
3dll (cons end2 3dll)
3dll (cons end1 3dll); 3dll is the first segment of 3d poly line
ptr  0 ; pointer
   )
   ;;  
   (while (<= (1+ ptr) (length lep))
     (setq end (nth ptr lep)) ; get a line end point from list
     (cond
((equal end end1 fuzz) ; found a matching point
(if (/= (/ ptr 2.0) (fix (/ ptr 2.0))) ; end of line
 ;; get pevious point
 (setq 3dll (cons (nth (1- ptr) lep) 3dll); add to begining of 3dll
end1 (car 3dll)
lep  (remove_pt (1- ptr) ptr)
 ) ; remove the end points from the list
 ;; else begining of line
 ;; get next point
 (setq 3dll (cons (nth (1+ ptr) lep) 3dll)
end1 (car 3dll)
lep  (remove_pt ptr (1+ ptr))
 ) ; remove the end points from the list
) ; endif
(setq ptr 0) ; reset pointer
) ; end cond 1

((equal end end2 fuzz)
(if (/= (/ ptr 2.0) (fix (/ ptr 2.0))) ; end of line
 ;; get pevious point
 (setq 3dll (append 3dll (list (nth (1- ptr) lep))); add to end of 3dll
end2 (nth (1- ptr) lep)
lep  (remove_pt (1- ptr) ptr)
 )
 ;; else begining of line
 ;; get next point
 (setq 3dll (append 3dll (list (nth (1+ ptr) lep)))
end2 (nth (1+ ptr) lep)
lep  (remove_pt ptr (1+ ptr))
 )
) ; endif
(setq ptr 0) ; reset pointer
) ; end cond 2
(t
(setq ptr (1+ ptr)) ; inc pointer
)
     ) ; end cond stmt
   ) ; end while
;;;=====================================================
;;;  Draw 3d poly here using points collected in 3dll
;;;=====================================================
   (if 3dll
     (progn
(command "3dpoly")
(foreach pt 3dll
(command pt)
)
(command "")
(setq 3dcnt (1+ 3dcnt))
     )
   ); endif
 ) ; end while
 (command "_erase" sslines "")
 (command "undo" "end")
 (prompt (strcat "\n" (rtos 3dcnt 2 0) " 3dPoly lines created."))
 (prompt "\nProcess complete.\n")
 (setvar "osmode" useros)
 (setvar "CMDECHO" usercmd)
) ; end defun main

(defun remove_pt (first sec / cnt llen newlist)
 (setq cnt 0
llen (length lep)
newlist (list)
 )
 (while (< cnt llen)
   (if (and (/= cnt first) (/= cnt sec))
     (setq newlist (cons (nth cnt lep) newlist))
   )
   (setq cnt (1+ cnt))
 ); end while
 (reverse newlist)
); end defun

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [ hỏi ] cách nối đường 3D để có 1 đường 3d kín. Xem đúng bài viết #220261 để đọc hướng dẫn và trao đổi thêm.

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