Polyline direction (clockwise or counterclockwise)

Polyline direction (clockwise or counterclockwise)

Anonymous
Not applicable
3,628 Views
10 Replies
Message 1 of 11

Polyline direction (clockwise or counterclockwise)

Anonymous
Not applicable

Hi guys,

is there a command that tells you if a polyline is drawn clockwise or counter clockwise?

I want to use the reverse command, but only if the polyline is clockwise.

I saw some threads of VBA codes, is there a lisp for that?

 

Thanks

0 Likes
Accepted solutions (1)
3,629 Views
10 Replies
Replies (10)
Message 2 of 11

marko_ribar
Advisor
Advisor

The main key is subfunction (Listclockwise-p ptlst)...

Here is something based on it (if it's not exactly what you're looking for) :

 

(defun c:setplsplhelclockw  ( / unit mxv v^v transptucs transptwcs 3dplv hplv ohplv lplv olplv splv helv ListClockwise-p
                                ss cmde i c ch ent l ll clw
                            )

  (defun unit ( v )
    (mapcar '(lambda ( x ) (/ x (distance '(0.0 0.0 0.0) v))) v)
  )

  (defun mxv ( m v )
    (mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
  )

  (defun v^v ( u v )
    (mapcar '(lambda ( n ) (- (* (nth (car n) u) (nth (cadr n) v)) (* (nth (cadr n) u) (nth (car n) v)))) '((1 2) (2 0) (0 1)))
  )

  (defun transptucs ( pt p1 p2 p3 / ux uy uz )
    (setq uz (unit (v^v (mapcar '- p2 p1) (mapcar '- p3 p1))))
    (setq ux (unit (mapcar '- p2 p1)))
    (setq uy (unit (mapcar '- p3 p1)))
    
    (mxv (list ux uy uz) (mapcar '- pt p1))
  )

  (defun transptwcs ( pt pt1 pt2 pt3 / pt1n pt2n pt3n )
    (setq pt1n (transptucs '(0.0 0.0 0.0) pt1 pt2 pt3))
    (setq pt2n (transptucs '(1.0 0.0 0.0) pt1 pt2 pt3))
    (setq pt3n (transptucs '(0.0 1.0 0.0) pt1 pt2 pt3))
    (transptucs pt pt1n pt2n pt3n)
  )

  (defun 3dplv ( pl / v vl )
    (if (and (eq (cdr (assoc 0 (entget pl))) "POLYLINE") (< 7 (cdr (assoc 70 (entget pl))) 14))
      (progn
        (setq v pl)
        (while (eq (cdr (assoc 0 (entget (setq v (entnext v))))) "VERTEX")
          (setq vl (cons (cdr (assoc 10 (entget v))) vl))
        )
        (reverse vl)
      )
      (progn
        (prompt "\nNot valid pl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun hplv ( pl / el uz v vl ux uy )
    (if (and (eq (cdr (assoc 0 (entget pl))) "POLYLINE") (< -1 (cdr (assoc 70 (entget pl))) 6))
      (progn
        (setq el (last (cdr (assoc 10 (entget pl)))))
        (setq uz (cdr (assoc 210 (entget pl))))
        (setq v pl)
        (while (eq (cdr (assoc 0 (entget (setq v (entnext v))))) "VERTEX")
          (setq vl (cons (list (car (cdr (assoc 10 (entget v)))) (cadr (cdr (assoc 10 (entget v)))) el) vl))
        )
        (if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
        (if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
        (if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
        (if (not uy) (setq uy (unit (v^v uz ux))))
        (setq vl (mapcar '(lambda ( p ) (transptwcs p '(0.0 0.0 0.0) ux uy)) vl))
        (reverse vl)
      )
      (progn
        (prompt "\nNot valid pl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun ohplv ( pl / v vl )
    (if (and (eq (cdr (assoc 0 (entget pl))) "POLYLINE") (< -1 (cdr (assoc 70 (entget pl))) 6))
      (progn
        (setq v pl)
        (while (eq (cdr (assoc 0 (entget (setq v (entnext v))))) "VERTEX")
          (setq vl (cons (cdr (assoc 10 (entget v))) vl))
        )
        (reverse vl)
      )
      (progn
        (prompt "\nNot valid pl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun lplv ( pl / el uz vl ux uy )
    (if (eq (cdr (assoc 0 (entget pl))) "LWPOLYLINE")
      (progn
        (setq el (cdr (assoc 38 (entget pl))))
        (setq uz (cdr (assoc 210 (entget pl))))
        (setq vl (mapcar 'cdr (vl-remove-if-not '(lambda ( p ) (= (car p) 10)) (entget pl))))
        (setq vl (mapcar '(lambda ( p ) (list (car p) (cadr p) el)) vl))
        (if (equal uz '(0.0 0.0 1.0) 1e-8) (setq ux '(1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
        (if (equal uz '(0.0 0.0 -1.0) 1e-8) (setq ux '(-1.0 0.0 0.0) uy '(0.0 1.0 0.0)))
        (if (not (or (equal uz '(0.0 0.0 1.0) 1e-8) (equal uz '(0.0 0.0 -1.0) 1e-8))) (setq ux (unit (v^v '(0.0 0.0 1.0) uz))))
        (if (not uy) (setq uy (unit (v^v uz ux))))
        (setq vl (mapcar '(lambda ( p ) (transptwcs p '(0.0 0.0 0.0) ux uy)) vl))
        vl
      )
      (progn
        (prompt "\nNot valid pl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun olplv ( pl / vl )
    (if (eq (cdr (assoc 0 (entget pl))) "LWPOLYLINE")
      (progn
        (setq vl (mapcar 'cdr (vl-remove-if-not '(lambda ( p ) (= (car p) 10)) (entget pl))))
        vl
      )
      (progn
        (prompt "\nNot valid pl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun splv ( spl / vl )
    (if (eq (cdr (assoc 0 (entget spl))) "SPLINE")
      (progn
        (setq vl (mapcar 'cdr (vl-remove-if-not '(lambda ( p ) (= (car p) 10)) (entget spl))))
        vl
      )
      (progn
        (prompt "\nNot valid spl agument supplied to function") 
        (princ)
      )
    )
  )

  (defun helv ( hel / vl )
    (if (eq (cdr (assoc 0 (entget hel))) "HELIX")
      (progn
        (setq vl (mapcar 'cdr (vl-remove-if-not '(lambda ( p ) (= (car p) 10)) (entget hel))))
        vl
      )
      (progn
        (prompt "\nNot valid hel agument supplied to function") 
        (princ)
      )
    )
  )

  (defun ListClockwise-p (lst / z vlst)
    (vl-catch-all-apply 'minusp 
      (list
        (if 
          (not 
            (equal 0.0
              (setq z
                (apply '+
                  (mapcar 
                    (function
                      (lambda (u v)
                        (- (* (car  u) (cadr  v)) (* (car  v) (cadr  u)))
                      )
                    )
                    (setq vlst
                      (mapcar
                        (function
                          (lambda (a b) (mapcar '- b a))
                        )
                        (mapcar (function (lambda (x) (car lst))) lst) 
                        (cdr (reverse (cons (car lst) (reverse lst))))
                      )
                    )
                    (cdr (reverse (cons (car vlst) (reverse vlst))))
                  )
                )
              ) 1e-6
            )
          )
          z
          (progn
            (prompt "\n\nChecked vectors are colinear - unable to determine clockwise-p of list")
            nil
          )
        )
      )
    )
  )

  (prompt "\nSelect 2DPOLYLINES, 3DPOLYLINES, SPLINES and HELIXES for setting CW/CCW direction")
  (setq ss (ssget "_:L" '((0 . "POLYLINE,LWPOLYLINE,SPLINE,HELIX"))))
  (setq cmde (getvar 'cmdecho))
  (setvar 'cmdecho 0)
  (setq i -1)
  (initget "CW CCW")
  (setq c (getkword "\nSet 2DPOLYLINES, 3DPOLYLINES, SPLINES and HELIXES clock-wise / counter-clock-wise ( CW / CCW ) <CW> : "))
  (if (eq c "CCW")
    (progn
      (initget "UCS OCS")
      (setq ch (getkword "\nSet 3DPOLYLINES, SPLINES, HELIXES clockwise viewed from UCS and 2DPOLYLINES viewed from (UCS / OCS) <OCS> : "))
      (if (eq ch "UCS")
        (progn
          (while (setq ent (ssname ss (setq i (1+ i))))
            (cond
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< 7 (cdr (assoc 70 (entget ent))) 14))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (3dplv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "3DPOLYLINE" l)
                      (setq l (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" l)))) (assoc "3DPOLYLINE" l) l))
                      (setq l (cons (cons "3DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "3DPOLYLINE" ll)
                        (setq ll (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" ll)))) (assoc "3DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "3DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< -1 (cdr (assoc 70 (entget ent))) 6))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (hplv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HEAVY2DPOLYLINE" l)
                      (setq l (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" l)))) (assoc "HEAVY2DPOLYLINE" l) l))
                      (setq l (cons (cons "HEAVY2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HEAVY2DPOLYLINE" ll)
                        (setq ll (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" ll)))) (assoc "HEAVY2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "HEAVY2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (lplv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "LIGHTWEIGHT2DPOLYLINE" l)
                      (setq l (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" l)))) (assoc "LIGHTWEIGHT2DPOLYLINE" l) l))
                      (setq l (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "LIGHTWEIGHT2DPOLYLINE" ll)
                        (setq ll (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" ll)))) (assoc "LIGHTWEIGHT2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "SPLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (splv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "SPLINE" l)
                      (setq l (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" l)))) (assoc "SPLINE" l) l))
                      (setq l (cons (cons "SPLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "SPLINE" ll)
                        (setq ll (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" ll)))) (assoc "SPLINE" ll) ll))
                        (setq ll (cons (cons "SPLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "HELIX")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (helv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HELIX" l)
                      (setq l (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" l)))) (assoc "HELIX" l) l))
                      (setq l (cons (cons "HELIX" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HELIX" ll)
                        (setq ll (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" ll)))) (assoc "HELIX" ll) ll))
                        (setq ll (cons (cons "HELIX" 1) ll))
                      )
                    )
                  )
                )
              )
            )
          )
        )
        (progn
          (while (setq ent (ssname ss (setq i (1+ i))))
            (cond
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< 7 (cdr (assoc 70 (entget ent))) 14))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (3dplv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "3DPOLYLINE" l)
                      (setq l (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" l)))) (assoc "3DPOLYLINE" l) l))
                      (setq l (cons (cons "3DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "3DPOLYLINE" ll)
                        (setq ll (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" ll)))) (assoc "3DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "3DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< -1 (cdr (assoc 70 (entget ent))) 6))
                (if (eq (setq clw (ListClockwise-p (ohplv ent))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HEAVY2DPOLYLINE" l)
                      (setq l (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" l)))) (assoc "HEAVY2DPOLYLINE" l) l))
                      (setq l (cons (cons "HEAVY2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HEAVY2DPOLYLINE" ll)
                        (setq ll (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" ll)))) (assoc "HEAVY2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "HEAVY2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
                (if (eq (setq clw (ListClockwise-p (olplv ent))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "LIGHTWEIGHT2DPOLYLINE" l)
                      (setq l (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" l)))) (assoc "LIGHTWEIGHT2DPOLYLINE" l) l))
                      (setq l (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "LIGHTWEIGHT2DPOLYLINE" ll)
                        (setq ll (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" ll)))) (assoc "LIGHTWEIGHT2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "SPLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (splv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "SPLINE" l)
                      (setq l (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" l)))) (assoc "SPLINE" l) l))
                      (setq l (cons (cons "SPLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "SPLINE" ll)
                        (setq ll (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" ll)))) (assoc "SPLINE" ll) ll))
                        (setq ll (cons (cons "SPLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "HELIX")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (helv ent)))) T)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HELIX" l)
                      (setq l (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" l)))) (assoc "HELIX" l) l))
                      (setq l (cons (cons "HELIX" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HELIX" ll)
                        (setq ll (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" ll)))) (assoc "HELIX" ll) ll))
                        (setq ll (cons (cons "HELIX" 1) ll))
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
    (progn
      (initget "UCS OCS")
      (setq ch (getkword "\nSet 3DPOLYLINES, SPLINES clockwise viewed from UCS and 2DPOLYLINES viewed from (UCS / OCS) <OCS> : "))
      (if (eq ch "UCS")
        (progn
          (while (setq ent (ssname ss (setq i (1+ i))))
            (cond
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< 7 (cdr (assoc 70 (entget ent))) 14))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (3dplv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "3DPOLYLINE" l)
                      (setq l (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" l)))) (assoc "3DPOLYLINE" l) l))
                      (setq l (cons (cons "3DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "3DPOLYLINE" ll)
                        (setq ll (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" ll)))) (assoc "3DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "3DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< -1 (cdr (assoc 70 (entget ent))) 6))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (hplv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HEAVY2DPOLYLINE" l)
                      (setq l (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" l)))) (assoc "HEAVY2DPOLYLINE" l) l))
                      (setq l (cons (cons "HEAVY2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HEAVY2DPOLYLINE" ll)
                        (setq ll (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" ll)))) (assoc "HEAVY2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "HEAVY2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (lplv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "LIGHTWEIGHT2DPOLYLINE" l)
                      (setq l (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" l)))) (assoc "LIGHTWEIGHT2DPOLYLINE" l) l))
                      (setq l (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "LIGHTWEIGHT2DPOLYLINE" ll)
                        (setq ll (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" ll)))) (assoc "LIGHTWEIGHT2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "SPLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (splv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "SPLINE" l)
                      (setq l (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" l)))) (assoc "SPLINE" l) l))
                      (setq l (cons (cons "SPLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "SPLINE" ll)
                        (setq ll (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" ll)))) (assoc "SPLINE" ll) ll))
                        (setq ll (cons (cons "SPLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "HELIX")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (helv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HELIX" l)
                      (setq l (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" l)))) (assoc "HELIX" l) l))
                      (setq l (cons (cons "HELIX" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HELIX" ll)
                        (setq ll (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" ll)))) (assoc "HELIX" ll) ll))
                        (setq ll (cons (cons "HELIX" 1) ll))
                      )
                    )
                  )
                )
              )
            )
          )
        )
        (progn
          (while (setq ent (ssname ss (setq i (1+ i))))
            (cond
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< 7 (cdr (assoc 70 (entget ent))) 14))
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (3dplv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "3DPOLYLINE" l)
                      (setq l (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" l)))) (assoc "3DPOLYLINE" l) l))
                      (setq l (cons (cons "3DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "3DPOLYLINE" ll)
                        (setq ll (subst (cons "3DPOLYLINE" (1+ (cdr (assoc "3DPOLYLINE" ll)))) (assoc "3DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "3DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (and (eq (cdr (assoc 0 (entget ent))) "POLYLINE") (< -1 (cdr (assoc 70 (entget ent))) 6))
                (if (eq (setq clw (ListClockwise-p (ohplv ent))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HEAVY2DPOLYLINE" l)
                      (setq l (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" l)))) (assoc "HEAVY2DPOLYLINE" l) l))
                      (setq l (cons (cons "HEAVY2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HEAVY2DPOLYLINE" ll)
                        (setq ll (subst (cons "HEAVY2DPOLYLINE" (1+ (cdr (assoc "HEAVY2DPOLYLINE" ll)))) (assoc "HEAVY2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "HEAVY2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "LWPOLYLINE")
                (if (eq (setq clw (ListClockwise-p (olplv ent))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "LIGHTWEIGHT2DPOLYLINE" l)
                      (setq l (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" l)))) (assoc "LIGHTWEIGHT2DPOLYLINE" l) l))
                      (setq l (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "LIGHTWEIGHT2DPOLYLINE" ll)
                        (setq ll (subst (cons "LIGHTWEIGHT2DPOLYLINE" (1+ (cdr (assoc "LIGHTWEIGHT2DPOLYLINE" ll)))) (assoc "LIGHTWEIGHT2DPOLYLINE" ll) ll))
                        (setq ll (cons (cons "LIGHTWEIGHT2DPOLYLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "SPLINE")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (splv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "SPLINE" l)
                      (setq l (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" l)))) (assoc "SPLINE" l) l))
                      (setq l (cons (cons "SPLINE" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "SPLINE" ll)
                        (setq ll (subst (cons "SPLINE" (1+ (cdr (assoc "SPLINE" ll)))) (assoc "SPLINE" ll) ll))
                        (setq ll (cons (cons "SPLINE" 1) ll))
                      )
                    )
                  )
                )
              )
              ( (eq (cdr (assoc 0 (entget ent))) "HELIX")
                (if (eq (setq clw (ListClockwise-p (mapcar '(lambda ( p ) (trans p 0 1)) (helv ent)))) nil)
                  (progn
                    (command "_.reverse" ent "")
                    (if (assoc "HELIX" l)
                      (setq l (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" l)))) (assoc "HELIX" l) l))
                      (setq l (cons (cons "HELIX" 1) l))
                    )
                  )
                  (progn
                    (if (vl-catch-all-error-p clw)
                      (if (assoc "HELIX" ll)
                        (setq ll (subst (cons "HELIX" (1+ (cdr (assoc "HELIX" ll)))) (assoc "HELIX" ll) ll))
                        (setq ll (cons (cons "HELIX" 1) ll))
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )
  (foreach e l
    (prompt "\n\nTotal reversement in ") (if (eq c "CCW") (prompt "counter-clock-wise direction of \"") (prompt "clock-wise direction of \"")) (prompt (car e)) (prompt "\" entity(ies) is(are) : ") (princ (itoa (cdr e)))
  )
  (prompt "\n")
  (foreach e ll
    (prompt "\nTotal unknown direction of vertices - no reversement processed of \"") (prompt (car e)) (prompt "\" entity(ies) is(are) : ") (princ (itoa (cdr e)))
  )
  (setvar 'cmdecho cmde)
  (princ)
)

(defun c:sclw nil (c:setplsplhelclockw))

HTH, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 11

Kent1Cooper
Consultant
Consultant

Do a Search here -- this has come up several times before, and there are quite a few routines out there to determine in which direction a Polyline was drawn.

Kent Cooper, AIA
0 Likes
Message 4 of 11

Anonymous
Not applicable
Search where?
0 Likes
Message 5 of 11

Anonymous
Not applicable
Accepted solution

Thanks Kent1Cooper,

 

I found a flawless lsp by Evgeniy Elpanov.

It does everything i need.

 

Thanks everyone

Message 6 of 11

marko_ribar
Advisor
Advisor

@Anonymous wrote:

Thanks Kent1Cooper,

 

I found a flawless lsp by Evgeniy Elpanov.

It does everything i need.

 

Thanks everyone


It's flawless like dead horse... Collinear or point like polylines or any other entities likewise aren't neither clockwise nor counterclockwise oriented...

I offered you real solution and you're fooling us all... Check subfunction (ListClockwise-p ptlst) again and be person that can appreciate posted examples by giving solution or at least a kudo to those that really deserve...

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 7 of 11

Anonymous
Not applicable
Marko I am sorry, I did not intent to offend you, please accept my apologies.
your solution is also good no doubt, for my use, what I posted as the solution was exactly what I needed. Be sure i also saved your code
0 Likes
Message 8 of 11

lahaghi
Community Visitor
Community Visitor

Thanks for your Lisp Code. that is very useful. Is that possible that we have VBA Code?

0 Likes
Message 9 of 11

john.uhden
Mentor
Mentor

@lahaghi :

Here is my offering (use the @polydir function in combination with the others):

  ;;-----------------------------------------------------------------------
  ;; This function returns the deflection angle (in radians) of two angles:
  ;;
  (defun @delta (a1 a2)
    (cond
      ((> a1 (+ a2 pi))
        (+ a2 pi pi (- a1))
      )
      ((> a2 (+ a1 pi))
        (- a2 (+ a1 pi pi))
      )
      (1 (- a2 a1))
    )
  )
  ;; Function to group a list of items into a list of
  ;; multiple lists, each of length N, e.g.
  ;; '(A B C D E F G H I) -> '((A B C)(D E F)(G H I))
  (defun @group (lst n / item new)
    (foreach element (reverse lst)
      (setq item (cons element item))
      (if (= (length item) n)
        (setq new (cons item new) item nil)
      )
    )
    new
  )
  (defun @polydir (object / e ent etype coords flag i p1 p2 p3 sum)
    (setq e      (vlax-vla-object->ename object)
          ent    (entget e)
          etype  (cdr (assoc 0 ent))
          flag   (cdr (assoc 70 ent))
          coords (vlax-get object 'Coordinates)
          i 1
          sum 0.0
    )
    (if (= etype "LWPOLYLINE")
      (setq coords (@group coords 2))
      (setq coords (@group coords 3))
    )
    (if (= (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) "CW" "CCW")
  )

John F. Uhden

0 Likes
Message 10 of 11

Kent1Cooper
Consultant
Consultant

As I think I may have pointed out before, the @polydir function can be thrown off by certain configurations involving arc segments.  It gets this "Pacman" shape wrong, no matter in which order its vertices are:

Kent1Cooper_0-1693994485838.png

That's one reason I prefer the Offset-and-compare-areas approach, such as in OffsetInOrOut.lsp , which also uses far less code.  It does, however, have its own possibility for incorrect results, in the case of a shape that results in more than one object from Offsetting:

Kent1Cooper_1-1693994835459.png

 

Kent Cooper, AIA
0 Likes
Message 11 of 11

john.uhden
Mentor
Mentor

Thanks @Kent1Cooper ,

Your PACMAN figure reveals what you say (at least to me).

I'll have to come  with a modified or different approach.

John F. Uhden

0 Likes