;Find and create polylines from selected objects
;Stefan M. 07.03.2014
(defun c:obo ( / *error* break_object l2p ms ss i lst segments pa dr aa ce reg ea)
(or acDoc (setq acDoc (vla-get-activedocument (vlax-get-acad-object))))
(setq ms (vlax-get acDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
(vla-startundomark acDoc)
(setq pa (getvar 'peditaccept)
dr (getvar 'draworderctl)
ce (getvar 'cmdecho)
aa 0.00
)

(defun *error* (msg)
(and
msg
(not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*"))
(princ (strcat "\nError: " msg))
)
(foreach x segments (vla-delete x))
(setvar 'peditaccept pa)
(setvar 'draworderctl dr)
(setvar 'cmdecho ce)
(vla-endundomark acDoc)
(princ)
)
(defun l2p (l) (if l (cons (list (car l) (cadr l) (caddr l)) (l2p (cdddr l)))))

(defun break_object (e points / object_type start center end radius normal arc res)
(if points
(progn
(setq points
(vl-sort points
(function
(lambda (a b)
( (distance start (car points)) 1e-8)
(setq res (cons (vlax-invoke ms 'addline start (setq start (car points))) res))
)
(setq points (cdr points))
)
)
(T
(if
(eq object_type "AcDbArc")
(setq start (vlax-curve-getStartParam e))
(setq start (vlax-curve-getparamatpoint e (car points))
points (reverse (cons (car points) (reverse (cdr points))))
)
)
(setq center (vla-get-Center e)
radius (vla-get-Radius e)
normal (vla-get-Normal e)
)
(while points
(if (not (equal start (setq end (vlax-curve-getparamatpoint e (car points))) 1e-8))
(progn
(setq arc (vla-AddArc ms center radius start end))
(vla-put-Normal arc normal)
(setq res (cons arc res))
)
)
(setq points (cdr points)
start end)
)
)
)
)
)
res
)

(if
(setq ss (ssget ":L" '((0 . "LINE,LWPOLYLINE,ARC,CIRCLE"))))
(progn
(repeat (setq i (sslength ss))
(setq i (1- i)
e (ssname ss i)
e (vlax-ename->vla-object e)
)
(if
(eq (vla-get-ObjectName e) "AcDbPolyline")
(foreach x (vlax-invoke e 'Explode)
(setq lst (cons x lst))
)
(setq lst (cons (vla-copy e) lst))
)
)
(if
(setq segments
(apply 'append
(mapcar
(function
(lambda (e / l)
(break_object e
(progn
(foreach other (vl-remove e lst)
(foreach x (l2p (vlax-invoke e 'intersectwith other acExtendNone))
(setq l (cons x l))
)
)
(if
(eq (vla-get-ObjectName e) "AcDbCircle")
l
(cons (vlax-curve-getendpoint e) l)
)
)
)
)
)
lst
)
)
)
(progn
(setq reg (vlax-invoke ms 'AddRegion segments))
(foreach x (append segments lst) (vla-delete x))
(setvar 'peditaccept 1)
(setvar 'draworderctl 0)
(setvar 'cmdecho 0)
(foreach x reg
(setq x (vlax-vla-object->ename x))
(command "_explode" x)
(command "_pedit" "_m" "_p" "" "_j" "" "")
(if (> (vlax-curve-getArea (entlast)) aa) (setq aa (vlax-curve-getArea (setq ea (entlast)))))
)
(if (> (length reg) 1) (entdel ea))
(setvar 'peditaccept pa)
(setvar 'draworderctl dr)
(setvar 'cmdecho ce)
)
(princ "\nValid region(s) not found")
)
)
)
(vla-endundomark acDoc)
(princ)
)
(c:obo)