;;; Generate a contour that delimits contour lines
;;; Created by ronjonp
;;; Saved from: https://www.theswamp.org/index.php?topic=52076.0

(defun c:ot (/ _daisychainsort e i l ss)
(defun _daisychainsort (pt lst / tmp newlst dsort)
(defun dsort (pt lst / d1 d2)
(vl-sort lst (function (lambda (d1 d2) (< (distance pt d1) (distance pt d2)))))
)
(setq tmp (dsort pt lst))
(while (setq tmp (dsort (car tmp) (cdr tmp))) (setq newlst (cons (car tmp) newlst)))
(reverse newlst)
)
(if (setq ss (ssget '((0 . "LWPOLYLINE"))))
(progn (repeat (setq i (sslength ss))
(setq e (ssname ss (setq i (1- i))))
(setq l (cons (vlax-curve-getstartpoint e) l))
(setq l (cons (vlax-curve-getendpoint e) l))
)
(setq l (_daisychainsort (car l) l))
(entmakex (apply 'append
(list (list '(0 . "LWPOLYLINE")
'(100 . "AcDbEntity")
'(8 . "Boundary")
'(100 . "AcDbPolyline")
(cons 90 (length l))
'(70 . 1)
)
(mapcar '(lambda (x) (cons 10 x)) l)
)
)
)
)
)
(princ)
)
(c:ot)