Lisp lệnh TUYEN trong chủ đề “Viết Lisp theo yêu cầu”
Bạn thử lisp này, nếu có trường hợp nào chưa đúng yêu cầu thì
- Post bản vẽ các trường hợp đó
- Chú thích phần sai khác của lisp so với yêu cầu
- Dùng font unicode để chú thích hoặc post font của style TieuDe
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 66597_tuyen.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 TUYEN để chạy lisp.
Mã nguồn 66597_tuyen.lsp (trích từ bài viết gốc)
(vl-load-com)
(defun c:Tuyen( / n id esc ss Obj e el els pt om l tl f li s da fuz)
(defun SplitStr (str / s n)
(setq s (list))
(while (setq n (vl-string-search "," str))
(setq s (append s (list (substr str 1 n)) ))
(setq str (substr str (+ n 2)))
)
(setq s (append s (list str) ))
)
(defun Draw2Circle ( / a hs)
(if (= 0 (nth 1 e))
(setq hs 1)
(setq hs -1 pos (- l pos))
)
(if (equal pos (nth 2 e) 1.e-8)
(setq pt (vlax-curve-getEndPoint Obj))
(setq pt (vlax-curve-getPointAtDist Obj pos))
)
(if pt
(progn
(setq a (+ (* hs da)(angle '(0 0)(vlax-curve-getFirstDeriv Obj (vlax-curve
-getParamAtPoint Obj pt)))))
(command "CIRCLE" (polar pt a (atof (nth 2 s))) 1.5)
(command "CIRCLE" (polar pt a (- (atof (nth 3 s)))) 1.5)
)
)
)
(setq fuz 1.e-8 )
(setq om (getvar "osmode") )
(princ "\n Chon cac duong tam:")
;(setq ss (ssget (list (cons 0 "LINE,POLYLINE,LWPOLYLINE,ARC,SPLINE")))) ; Truong hop tong quat
(setq ss (ssget (list (cons 0 "POLYLINE")))) ; Ban ve mau
(setq pt (getpoint "\nDiem dau:") )
(setq tl 0 i 0 n (sslength ss))
(repeat n
(setq Obj (ssname ss i) i (1+ i))
(setq el (append el (list (list Obj (vlax-curve-getStartPoint Obj) (vlax-curve-getEndPoint
Obj) ))))
)
(while el
(setq el (vl-sort el
(function
(lambda (e1 e2)
(< (min (distance pt (nth 1 e1)) (distance pt (nth 2 e1)))
(min (distance pt (nth 1 e2)) (distance pt (nth 2 e2)))
)
)
)
))
(setq e (nth 0 el) Obj (car e) l (vlax-curve-getDistAtParam Obj (vlax-curve-getEndParam
Obj)))
(setq id (if (< (distance pt (nth 1 e)) (distance pt (nth 2 e))) 0 1) tl (+ tl l))
(setq els (append els (list(list Obj id l))) el (cdr el) )
(setq pt (nth (- 2 id) e))
)
(if (not (setq f (open (getfiled "Data file" "" "csv" 0) "r"))) (exit))
(setq da (/ pi 2))
(setq li (read-line f) )
(while (/= (setq li (read-line f) )"EOF")
(setq s (SplitStr li))
(if (= 4 (length s))
(progn
(setq esc nil i 0 pos (atof (nth 1 s)))
(if (or (< pos 0) (> pos tl)) (setq b T))
(while (and (< i n )(not esc))
(progn
(setq e (nth i els) Obj (nth 0 e) l (nth 2 e))
(if (<= pos l)
(progn
(Draw2Circle)
(setq esc T)
)
)
(setq i (1+ i) pos (- pos l) )
)
)
)
)
)
(close f)
(setvar "osmode" om)
(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 #66597 để đọc hướng dẫn và trao đổi thêm.