Jump to content

Lisp lệnh ADDVERTEX trong chủ đề “Thêm node vào đường Pline”

Bạn chạy thử LISP này nhé.

(defun c:AddVertex (/ Sel Pt Obj CoordList cnt ParmPt cnt2 Ang tmpPt1 tmpPt2)
(if
(and
 (setq Sel (entsel "\n Chon vi tri can them Node tren Polyline : "))
 (= (cdr (assoc 0 (entget (car Sel)))) "LWPOLYLINE")
 (setq Pt (trans (vlax-curve-getClosestPointTo (car Sel) (trans (cadr Sel) 1 0)) 0 (car Sel)))
 (setq Obj (vlax-ename->vla-object (car Sel)))
 (setq CoordList (vlax-get Obj 'Coordinates))
)
(progn
 (setq cnt 0)
 (setq ParmPt 1)
 (while (< (1+ cnt) (length CoordList))
  (setq cnt2
   (if (>= (setq cnt2 (+ 2 cnt)) (length CoordList))
    (- cnt2 (length CoordList))
    cnt2
   )
  )
  (setq Ang
   (angle
    (setq tmpPt1
     (list
      (nth cnt CoordList)
      (nth (1+ cnt) CoordList)
     )
    )
    (setq tmpPt2
     (list
      (nth cnt2 CoordList)
      (nth (1+ cnt2) CoordList)
     )
    )
   )
  )
  (if
   (or
    (equal (angle Pt tmpPt1) Ang 0.000001)
    (equal (angle Pt tmpPt2) Ang 0.000001)
   )
   (setq cnt (length CoordList))
   (progn
    (setq ParmPt (1+ ParmPt))
    (setq cnt (+ 2 cnt))
   )
  )
 )
 (vlax-invoke Obj 'AddVertex ParmPt (list (car Pt) (cadr Pt)))
)
)
(princ)
)

lisp này cũng tốt đấy chứ. Chỉ có điều nếu tách nhập điểm riêng thì người dùng bắt điểm chính xác hơn.

Cách dùng

  1. Tải tệp 57971_addvertex.lsp về máy.
  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 ADDVERTEX để chạy lisp.

Mã nguồn 57971_addvertex.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/15016-th%C3%AAm-node-v%C3%A0o-%C4%91%C6%B0%E1%BB%9Dng-pline/
(defun c:AddVertex (/ Sel Pt Obj CoordList cnt ParmPt cnt2 Ang tmpPt1 tmpPt2)
(if
(and
 (setq Sel (entsel "\n Chon vi tri can them Node tren Polyline : "))
 (= (cdr (assoc 0 (entget (car Sel)))) "LWPOLYLINE")
 (setq Pt (trans (vlax-curve-getClosestPointTo (car Sel) (trans (cadr Sel) 1 0)) 0 (car Sel)))
 (setq Obj (vlax-ename->vla-object (car Sel)))
 (setq CoordList (vlax-get Obj 'Coordinates))
)
(progn
 (setq cnt 0)
 (setq ParmPt 1)
 (while (< (1+ cnt) (length CoordList))
  (setq cnt2
   (if (>= (setq cnt2 (+ 2 cnt)) (length CoordList))
    (- cnt2 (length CoordList))
    cnt2
   )
  )
  (setq Ang
   (angle
    (setq tmpPt1
     (list
      (nth cnt CoordList)
      (nth (1+ cnt) CoordList)
     )
    )
    (setq tmpPt2
     (list
      (nth cnt2 CoordList)
      (nth (1+ cnt2) CoordList)
     )
    )
   )
  )
  (if
   (or
    (equal (angle Pt tmpPt1) Ang 0.000001)
    (equal (angle Pt tmpPt2) Ang 0.000001)
   )
   (setq cnt (length CoordList))
   (progn
    (setq ParmPt (1+ ParmPt))
    (setq cnt (+ 2 cnt))
   )
  )
 )
 (vlax-invoke Obj 'AddVertex ParmPt (list (car Pt) (cadr Pt)))
)
)
(princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Thêm node vào đường Pline. Xem đúng bài viết #57971 để đọc hướng dẫn và trao đổi thêm.

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