Lisp lệnh LBL trong chủ đề “xin gúp đỡ lisp ve đường tâm”
cảm ơn anh tainguyen79 rất nhiều! bây giờ đỡ phải vẽ Pline và vẽ lại tâm nữa rồi. à có thể sửa được để chỉ cần chọn 2 mép bên đường (bằng đường Pline) là vẽ luôn cho mình tim không nhỉ?
Đây là lisp vẽ đường tâm của 2 đường cong hở, của Alan J. Thompson, khá là hay.
; Draw (LW)Polyline between two selected curves (at midpoint of vertices).
(defun c:LBL (/ foo AT:GetSel _pnts _pline _lwpline _dist e1 e2)
(vl-load-com)
(defun foo (e)
(and (wcmatch (cdr (assoc 0 (entget (car e)))) "LINE,*POLYLINE,SPLINE")
(not (vlax-curve-isClosed (car e)))))
(defun AT:GetSel (meth msg fnc / ent)
(while
(progn
(setvar 'ERRNO 0)
(setq ent (meth (cond (msg) ("\nSelect object: "))))
(cond
((eq (getvar 'ERRNO) 7) (princ "\nMissed, try again."))
((eq (type (car ent)) 'ENAME)
(if (and fnc (not (fnc ent)))
(princ "\nInvalid object!"))))))
ent)
(defun _pnts (e / p l)
(if e
(cond
((wcmatch (cdr (assoc 0 (entget e))) "ARC,LINE,SPLINE")
(list (vlax-curve-getStartPoint e) (vlax-curve-getEndPoint e)))
((wcmatch (cdr (assoc 0 (entget e))) "*POLYLINE")
(repeat (setq p (1+ (fix (vlax-curve-getEndParam e))))
(setq l (cons (vlax-curve-getPointAtParam e (setq p (1- p))) l)))))))
(defun _pline (lst)
(if
(and
(> (length lst) 1)
(entmakex '((0 . "POLYLINE") (10 0. 0. 0.) (70 . 8)))
(foreach x lst (entmakex (list '(0 . "VERTEX") (cons 10 x) '(70 . 32)))))
(cdr (assoc 330 (entget (entmakex '((0 . "SEQEND"))))))))
(defun _lwpline (lst)
(if (> (length lst) 1)
(entmakex (append
(list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") (cons 90 (length lst)) (cons 70 (* (getvar 'plinegen) 128)))
(mapcar (function (lambda (p) (list 10 (car p) (cadr p)))) lst)))))
(defun _dist (a b)
(distance (list (car a) (cadr a)) (list (car b) (cadr b))))
(if
(and
(setq e1 (_pnts (car (AT:GetSel entsel "\nSelect first open curve: " foo))))
(setq e2 (_pnts (car (AT:GetSel entsel "\nSelect next open curve: " foo))))
(not (initget 0 "Lwpolyline Polyline"))
(setq *LBL:Opt* (cond ((getkword (strcat "\nSpecify line to draw: <" (cond (*LBL:Opt*) ((setq *LBL:Opt* "Lwpolyline"))) ">: "))) (*LBL:Opt*))))
((if (eq *LBL:Opt* "Lwpolyline") _lwpline _pline)
(vl-remove nil
(mapcar (function (lambda(a b)
(if (and a b (not (grdraw (trans a 0 1) (trans b 0 1) 1 1)))
(mapcar (function (lambda (a b) (/ (+ a b) 2.))) a b))))
e1
(if (< (_dist (car e1) (car e2)) (_dist (car e1) (last e2))) e2 (reverse e2))))))
(princ))
- Tải lisp
- 247501_lbl.lsp
Cách dùng
- Tải tệp 247501_lbl.lsp về máy.
- 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 LBL để chạy lisp.
Mã nguồn 247501_lbl.lsp
;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/77029-xin-g%C3%BAp-%C4%91%E1%BB%A1-lisp-ve-%C4%91%C6%B0%E1%BB%9Dng-t%C3%A2m/
; Draw (LW)Polyline between two selected curves (at midpoint of vertices).
(defun c:LBL (/ foo AT:GetSel _pnts _pline _lwpline _dist e1 e2)
(vl-load-com)
(defun foo (e)
(and (wcmatch (cdr (assoc 0 (entget (car e)))) "LINE,*POLYLINE,SPLINE")
(not (vlax-curve-isClosed (car e)))))
(defun AT:GetSel (meth msg fnc / ent)
(while
(progn
(setvar 'ERRNO 0)
(setq ent (meth (cond (msg) ("\nSelect object: "))))
(cond
((eq (getvar 'ERRNO) 7) (princ "\nMissed, try again."))
((eq (type (car ent)) 'ENAME)
(if (and fnc (not (fnc ent)))
(princ "\nInvalid object!"))))))
ent)
(defun _pnts (e / p l)
(if e
(cond
((wcmatch (cdr (assoc 0 (entget e))) "ARC,LINE,SPLINE")
(list (vlax-curve-getStartPoint e) (vlax-curve-getEndPoint e)))
((wcmatch (cdr (assoc 0 (entget e))) "*POLYLINE")
(repeat (setq p (1+ (fix (vlax-curve-getEndParam e))))
(setq l (cons (vlax-curve-getPointAtParam e (setq p (1- p))) l)))))))
(defun _pline (lst)
(if
(and
(> (length lst) 1)
(entmakex '((0 . "POLYLINE") (10 0. 0. 0.) (70 . 8)))
(foreach x lst (entmakex (list '(0 . "VERTEX") (cons 10 x) '(70 . 32)))))
(cdr (assoc 330 (entget (entmakex '((0 . "SEQEND"))))))))
(defun _lwpline (lst)
(if (> (length lst) 1)
(entmakex (append
(list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(100 . "AcDbPolyline") (cons 90 (length lst)) (cons 70 (* (getvar 'plinegen) 128)))
(mapcar (function (lambda (p) (list 10 (car p) (cadr p)))) lst)))))
(defun _dist (a b)
(distance (list (car a) (cadr a)) (list (car b) (cadr b))))
(if
(and
(setq e1 (_pnts (car (AT:GetSel entsel "\nSelect first open curve: " foo))))
(setq e2 (_pnts (car (AT:GetSel entsel "\nSelect next open curve: " foo))))
(not (initget 0 "Lwpolyline Polyline"))
(setq *LBL:Opt* (cond ((getkword (strcat "\nSpecify line to draw: [Lwpolyline/Polyline] <" (cond (*LBL:Opt*) ((setq *LBL:Opt* "Lwpolyline"))) ">: "))) (*LBL:Opt*))))
((if (eq *LBL:Opt* "Lwpolyline") _lwpline _pline)
(vl-remove nil
(mapcar (function (lambda(a b)
(if (and a b (not (grdraw (trans a 0 1) (trans b 0 1) 1 1)))
(mapcar (function (lambda (a b) (/ (+ a b) 2.))) a b))))
e1
(if (< (_dist (car e1) (car e2)) (_dist (car e1) (last e2))) e2 (reverse e2))))))
(princ))
Thảo luận gốc
Lisp được chia sẻ trong chủ đề xin gúp đỡ lisp ve đường tâm. Xem đúng bài viết #247501 để đọc hướng dẫn và trao đổi thêm.