Jump to content

Lisp lệnh TEST trong chủ đề “Kiểm tra hướng các đỉnh của đối tượng polyline”

Bạn cũng có thể xem qua các link sau :

http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/clockwise.htm

http://forums.autodesk.com/t5/NET/Check-the-drawing-direction-of-a-polyline/m-p/2562006/highlight/true

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/polyline-clockwise-or-counter-clockwise/m-p/2653719

 

Và tất cả các link GG liên quan đến counterclockwise ^^

Dù ở nhiều ngôn ngữ nhưng mình tin từ thuật toán bạn sẽ chuyển sang VBA được. GL

 

(defun C:Test( / @delta @cv_parse_list @polydir 2pi e ans)
  ;;-----------------------------------------------------------------------
  ;; This function returns the deflection angle (in radians) of two angles:
  ;;
  (defun @delta (a1 a2)
     (cond
        ((> a1 (+ a2 pi))
           (setq a2 (+ a2 2pi))
        )
        ((> a2 (+ a1 pi))
           (setq a1 (+ a1 2pi))
        )
     )
     (- a2 a1)
  )
  ;;-------------------------------------------------------------
  ;; Function returns a list of 3D points from a continuous list
  ;; as returned by (vlax-safearray->list (vlax-variant-value X))
  ;;
  (defun @cv_parse_list (data n / item new)
     (foreach element (reverse data)
        (setq item (cons element item))
        (if (= (length item) n)
           (setq new (cons item new) item nil)
        )
     )
     new
  )
  (defun @polydir (e / ent etype object coords flag i p1 p2 p3 sum)
     (cond
        ((/= (type e) 'ENAME) nil)
        ((not (vl-position (setq etype (cdr (assoc 0 (setq ent (entget e))))) '("AECC_CONTOUR" "LWPOLYLINE" "POLYLINE")))
           (prompt (strcat " Object selected is a(n) " etype))
        )
        ((and (setq flag (cdr (assoc 70 ent)))(> (boole 1 16 flag) 0))
           (prompt " Object selected is a 3DMESH")
        )
        (1 (setq object (vlax-ename->vla-object e)
                 coords (vlax-get object "Coordinates")
                 coords (@cv_parse_list coords (if (= etype "LWPOLYLINE") 2 3))
                 i 1
                 sum 0.0
           )
           (and
              flag
              (= (logand 1 flag) 1) ; closed
              (setq coords (reverse (cons (car coords)(reverse coords))))
           )
           (repeat (- (length coords) 2)
              (setq p1  (nth (1- i) coords)
                    p2  (nth i coords)
                    i   (1+ i)
                    p3  (nth i coords)
                    sum (+ sum (@delta (angle p1 p2)(angle p2 p3)))
              )
           )
           (if (minusp sum) (princ "\nTheo chieu Kim dong Ho") (princ "\nNguoc chieu Kim dong ho"))
        )
     )
  )
  (setvar "errno" 0)
  (setq 2pi (* pi 2))
  (while (/= (getvar "errno") 52)
     (if (setq ans (@polydir (setq e (car (entsel "\nSelect a polyline: ")))))
        (princ ans)
     )
  )
  (princ)
)

 

P/S : đó là nói về thuật toán. Còn về thủ thuật, bạn có thể mượn method offset của cad để kiểm tra.

Với offset khoảng cách dương, nếu diện tích đối tượng offset nhỏ hơn Pline gốc : thuận, ngược lại thì là ngược. Như vậy, hàm kiểm tra thuận hay nghịch chiều kim đồng hồ của bạn sẽ là :

 

Public Function isThuan(oLWP As AcadLWPolyline) As Boolean
Dim oLWPOff As AcadLWPolyline
On Error Resume Next
oLWP.Offset 1
Set oLWPOff = ThisDrawing.ModelSpace.Item(ThisDrawing.ModelSpace.Count - 1)
isThuan = (oLWP.Area > oLWPOff.Area)
oLWPOff.Delete
Set oLWPOff = Nothing
End Function

Cách dùng

  1. Tải tệp 135835_test.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 TEST để chạy lisp.

Mã nguồn 135835_test.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/36829-ki%E1%BB%83m-tra-h%C6%B0%E1%BB%9Bng-c%C3%A1c-%C4%91%E1%BB%89nh-c%E1%BB%A7a-%C4%91%E1%BB%91i-t%C6%B0%E1%BB%A3ng-polyline/
(defun C:Test( / @delta @cv_parse_list @polydir 2pi e ans)
  ;;-----------------------------------------------------------------------
  ;; This function returns the deflection angle (in radians) of two angles:
  ;;
  (defun @delta (a1 a2)
     (cond
        ((> a1 (+ a2 pi))
           (setq a2 (+ a2 2pi))
        )
        ((> a2 (+ a1 pi))
           (setq a1 (+ a1 2pi))
        )
     )
     (- a2 a1)
  )
  ;;-------------------------------------------------------------
  ;; Function returns a list of 3D points from a continuous list
  ;; as returned by (vlax-safearray->list (vlax-variant-value X))
  ;;
  (defun @cv_parse_list (data n / item new)
     (foreach element (reverse data)
        (setq item (cons element item))
        (if (= (length item) n)
           (setq new (cons item new) item nil)
        )
     )
     new
  )
  (defun @polydir (e / ent etype object coords flag i p1 p2 p3 sum)
     (cond
        ((/= (type e) 'ENAME) nil)
        ((not (vl-position (setq etype (cdr (assoc 0 (setq ent (entget e))))) '("AECC_CONTOUR" "LWPOLYLINE" "POLYLINE")))
           (prompt (strcat " Object selected is a(n) " etype))
        )
        ((and (setq flag (cdr (assoc 70 ent)))(> (boole 1 16 flag) 0))
           (prompt " Object selected is a 3DMESH")
        )
        (1 (setq object (vlax-ename->vla-object e)
                 coords (vlax-get object "Coordinates")
                 coords (@cv_parse_list coords (if (= etype "LWPOLYLINE") 2 3))
                 i 1
                 sum 0.0
           )
           (and
              flag
              (= (logand 1 flag) 1) ; closed
              (setq coords (reverse (cons (car coords)(reverse coords))))
           )
           (repeat (- (length coords) 2)
              (setq p1  (nth (1- i) coords)
                    p2  (nth i coords)
                    i   (1+ i)
                    p3  (nth i coords)
                    sum (+ sum (@delta (angle p1 p2)(angle p2 p3)))
              )
           )
           (if (minusp sum) (princ "\nTheo chieu Kim dong Ho") (princ "\nNguoc chieu Kim dong ho"))
        )
     )
  )
  (setvar "errno" 0)
  (setq 2pi (* pi 2))
  (while (/= (getvar "errno") 52)
     (if (setq ans (@polydir (setq e (car (entsel "\nSelect a polyline: ")))))
        (princ ans)
     )
  )
  (princ)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Kiểm tra hướng các đỉnh của đối tượng polyline. Xem đúng bài viết #135835 để đọc hướng dẫn và trao đổi thêm.

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