Lisp lệnh EXPL trong chủ đề “Viết Lisp theo yêu cầu”
Đoạn code của bạn mới chỉ đúng cho 1 trường hợp polyline dạng LWPOLYLINE, với đối tượng dạng POLYLINE thì lệnh sẽ gặp lỗi. Và bạn nên viết thêm đoạn code để người dùng tuỳ chọn tên file, nơi lưu trữ thay vì xuất ra một file với tên cố định, khi làm nhiều lần, các file dữ liệu sẽ bị ghi đè mất.
Tôi xin đưa ra một đoạn code sau, các bác dùng thử 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
- Chép mã nguồn, lưu thành tệp 29306_expl.lsp.
- 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 EXPL để chạy lisp.
Mã nguồn 29306_expl.lsp (trích từ bài viết gốc)
;;;=======================================================
;;; Ham nhan list dinh tu Polyline bao gom ca cao do
;;;
(defun Getvpl1 (ename stn / Elist rep e1 dp c)
(setq rep "C"
Elist '()
c stn
)
(setq ename (entnext ename))
(while (/= rep "SEQEND")
(setq e1 (entget ename))
(setq rep (cdr (assoc 0 e1)))
(if (/= rep "VERTEX")
(princ)
(progn
(setq dp (cdr (assoc 42 e1)))
(setq
elist (cons (cons c
(cdr (assoc 10 e1)
)
)
elist
)
)
)
)
;;end if
(setq ename (entnext ename)
c (1+ c)
)
)
;;end while
(setq elist (reverse elist))
;;kthuc
)
;;;=============================================================================
=======
;;; Ham nhan list dinh tu LWPolyline bao gom ca cao do
(defun Getvlw1 (ename stn / di i e1 elist dp c evl)
(setq e1 (entget ename))
(setq elist '()
c 1
c1 stn
evl (cdr (assoc 38 e1))
)
(while e1
(setq di (car e1))
(if (/= 10 (car di))
(princ)
(progn
(setq vlap 1
dp nil
)
(while (and e1 (/= vlap 42))
;;never null
(setq i (car e1))
(setq vlap (car i))
(if (/= vlap 42)
(setq e1 (cdr e1))
(setq dp (cdr i))
)
)
;;end while
(if (null dp)
(alert "Error bulge in the LWPOLYLINE")
;;nerver appear
(setq elist (cons (cons c1 (append (cdr di) (list evl))) elist)
c1 (1+ c1)
)
)
;;end if
)
)
;;end if
(setq e1 (cdr e1)
c (1+ c)
)
)
;;end while
(setq elist (reverse elist))
)
(Defun Intro ()
(prompt "\nCopyright by Nguyen Gia Dat - 0915169886")
)
;;;==================================================================
(defun C:Expl (/ sspl txtfile stt cter count txtline polist pltype)
(command "undo" "begin")
(setvar "cmdecho" 0)
(intro)
(setq sspl (ssget '((-4 . "<OR")
(0 . "Polyline")
(0 . "LWpolyline")
(-4 . "OR>")
)
)
cter 0
stt 1
txtfile '()
)
(while (< cter (sslength sspl))
(setq objpl (ssname sspl cter)
entpl (entget objpl)
pltype (cdr (assoc 0 entpl))
cter (1+ cter)
)
(cond
((= pltype "POLYLINE")
(setq
polist (getvpl1 objpl stt)
stt (+ stt (length polist))
)
)
((= pltype "LWPOLYLINE")
(setq polist (getvlw1 objpl stt)
stt (+ stt (length polist))
)
)
)
(setq txtfile (append txtfile polist))
)
;;; Chon file luu so lieu
(Setq FileDir (getfiled "File luu so lieu:" "" "txt" 1)
)
(setq count1 1
FileID (open FileDir "w")
)
(write-line "B¶ng sè liÖu" FileID)
(write-line
"Stt\tTo¹ ®é X\tTo¹ ®é Y\tTo¹ ®é Z"
FileID
)
(while (<= count1 (length txtfile))
(setq Xid (car (cdr (assoc count1 Txtfile)))
Yid (cadr (cdr (assoc count1 Txtfile)))
Zid (cadr (cdr (cdr (assoc count1 Txtfile))))
Txtline (strcat (itoa count1)
"\t"
(rtos Xid)
"\t"
(rtos Yid)
"\t"
(rtos Zid)
)
count1 (1+ count1)
)
(write-line Txtline FileID)
)
; (write-line Cpright FileID)
(close FileID)
(command "undo" "end")
(setvar "cmdecho" 1)
(princ)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề Viết Lisp theo yêu cầu. Xem đúng bài viết #29306 để đọc hướng dẫn và trao đổi thêm.