Lisp lệnh TAO trong chủ đề “nhờ kết hợp 2 lisp”
Thật ra cái này tuy tôi viết cũng lâu rồi nhưng chỉ mang tính thử nghiệm (làm cho biết), chưa có ứng dụng thực tế.
Bạn muốn dùng thì down cái lsp này về.
Trong file bạn phải có sẵn pline và text cho từng đoạn của pline (đặt text gần cái đoạn của nó).
Đánh lệnh "tao", chọn pline, quét các text của pline. XOng vẫn chưa thấy có gì khác, tuy nhiên nếu bạn thay đổi chiều dài đoạn thì text sẽ đổi.
Làm xong nhớ save file.
Lần sau mở file lại load cái lsp này vào để cad hiểu các hàm của nó, nhưng không cần đánh lệnh "tao" lại (lệnh này chỉ làm 1 lần cho 1 pline thôi).
(defun print-length (notifier-object reactor-object parameter-list / lvt pl n tm) (setq lvt (getLength (setq pl (vlax-vla-object->ename notifier-object)))) (mapcar '(lambda(x) (setq obj (vla-HandleToObject adoc (last x))) (vla-put-textstring obj (nth (car x) lvt)) (setq tm (laydinhdoan pl (car x))) (vla-put-rotation obj (if (< (caar tm) (caadr tm)) (angle (car tm) (last tm)) (angle (last tm) (car tm) ))) (vla-put-TextAlignmentPoint obj (vlax-3d-point (midp (car tm) (last tm))))) (vlax-ldata-get "Rdict" (dxf 5 pl))) (princ) ) (defun laydoan(pl pt / obj) (fix (vlax-curve-getParamAtPoint pl (vlax-curve-getClosestPointTo pl pt)))) (defun laydinhdoan(pl n / obj) (list (vlax-curve-getPointAtParam pl n) (vlax-curve-getPointAtParam pl (1+ n)))) (defun getLength(pl / vt l obj) (setq vt 0 l nil) (repeat (fix (vlax-curve-getEndParam pl)) (setq l (append l (list (rtos (- (vlax-curve-getDistAtParam pl (setq vt (1+ vt))) (vlax-curve-getDistAtParam pl (1- vt))) 2 0))))) l ) (defun C:tao (/ mypLine tt5 pl) (setq mypLine (vlax-ename->vla-object (setq pl (car (entsel "\nChon Polyline:")))) tt5 (dxf 5 pl)) (prompt "\nChon text ghi chieu dai:") (if (vlax-ldata-get "Rdict" tt5) (vlax-ldata-delete "Rdict" tt5)) (vlax-ldata-put "Rdict" tt5 (mapcar '(lambda(x) (list (laydoan pl (vlax-curve-getClosestPointTo mypLine (dxf 10 x))) (vla-get-Handle (vlax-ename->vla-object x)))) (ssto (ssget '((0 . "TEXT")))))) (setq plineReactor (vlr-object-reactor (list mypLine) "PLine Reactor" '((:vlr-modified . print-length)))) (vlr-pers plineReactor) (princ) )
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 308369_tao.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 TAO để chạy lisp.
Mã nguồn 308369_tao.lsp (trích từ bài viết gốc)
(defun print-length (notifier-object reactor-object parameter-list / lvt pl n tm)
(setq lvt (getLength (setq pl (vlax-vla-object->ename notifier-object))))
(mapcar '(lambda(x)
(setq obj (vla-HandleToObject adoc (last x)))
(vla-put-textstring obj (nth (car x) lvt))
(setq tm (laydinhdoan pl (car x)))
(vla-put-rotation obj (if (< (caar tm) (caadr tm)) (angle (car tm) (last tm))
(angle (last tm) (car tm) )))
(vla-put-TextAlignmentPoint obj (vlax-3d-point (midp (car tm) (last tm)))))
(vlax-ldata-get "Rdict" (dxf 5 pl)))
(princ)
)
(defun laydoan(pl pt / obj)
(fix (vlax-curve-getParamAtPoint pl (vlax-curve-getClosestPointTo pl pt))))
(defun laydinhdoan(pl n / obj)
(list (vlax-curve-getPointAtParam pl n) (vlax-curve-getPointAtParam pl (1+ n))))
(defun getLength(pl / vt l obj)
(setq vt 0 l nil)
(repeat (fix (vlax-curve-getEndParam pl))
(setq l (append l (list (rtos (- (vlax-curve-getDistAtParam pl (setq vt (1+ vt)))
(vlax-curve-getDistAtParam pl (1- vt))) 2 0)))))
l
)
(defun C:tao (/ mypLine tt5 pl)
(setq mypLine (vlax-ename->vla-object (setq pl (car (entsel "\nChon Polyline:"))))
tt5 (dxf 5 pl))
(prompt "\nChon text ghi chieu dai:")
(if (vlax-ldata-get "Rdict" tt5) (vlax-ldata-delete "Rdict" tt5))
(vlax-ldata-put "Rdict" tt5
(mapcar '(lambda(x) (list (laydoan pl (vlax-curve-getClosestPointTo mypLine (dxf 10 x)))
(vla-get-Handle (vlax-ename->vla-object x)))) (ssto (ssget '((0 . "TEXT"))))))
(setq plineReactor (vlr-object-reactor (list mypLine) "PLine Reactor" '((:vlr-modified . print-length))))
(vlr-pers plineReactor)
(princ)
)Thảo luận gốc
Lisp được chia sẻ trong chủ đề nhờ kết hợp 2 lisp. Xem đúng bài viết #308369 để đọc hướng dẫn và trao đổi thêm.