(defun C:edit_join_lwpolyline ( / LINE1_ODJ LINE2_ODJ LISP-POINT1 LISP-POINT2 LIST-DILT MIN-DIST PF1 PF2 PS1 PS2 VLA-LINE1 VLA-LINE2)
  (load_global_variable)
  (setq line1_odj (car (entsel "\nВыбери полилинию: ")))
  (setq vla-line1 (vlax-ename->vla-object line1_odj))
  (while (setq line2_odj (car (entsel "\nВыбери вторую полилинию: ")))
    (setq vla-line2 (vlax-ename->vla-object line2_odj))
    (if (eq line1_odj line2_odj)
      (vla-put-Closed vla-line2 -1)
      (progn
	(setq lisp-point1 (convert-safearray-to-list-points (vlax-variant-value(vla-get-Coordinates vla-line1)) 2))
	(setq lisp-point2 (convert-safearray-to-list-points (vlax-variant-value(vla-get-Coordinates vla-line2)) 2))
	(setq ps1 (car lisp-point1))
	(setq pf1 (car (reverse lisp-point1)))
	(setq ps2 (car lisp-point2))
	(setq pf2 (car (reverse lisp-point2)))
	(setq list-dilt (list (distance ps1 ps2)(distance pf1 pf2)(distance ps1 pf2)(distance pf1 ps2)))
	(setq min-dist (apply 'min list-dilt))
	(vla-StartUndoMark active_document)
	(cond
	  ((= (distance ps1 ps2) min-dist)
	   (vla-put-Coordinates vla-line1 (convert-list-points-to-lwpolyline-safearray(append(reverse lisp-point2) lisp-point1)))
	   (vla-Delete vla-line2)
	   )
	  ((= (distance pf1 pf2) min-dist)
	   (vla-put-Coordinates  vla-line1 (convert-list-points-to-lwpolyline-safearray(append lisp-point1 (reverse lisp-point2))))
	   (vla-Delete vla-line2)
	   )
	  ((= (distance ps1 pf2) min-dist)
	   (vla-put-Coordinates vla-line1 (convert-list-points-to-lwpolyline-safearray(append lisp-point2 lisp-point1)))
	   (vla-Delete vla-line2)
	   )
	  ((= (distance pf1 ps2) min-dist)
	   (vla-put-Coordinates  vla-line1 (convert-list-points-to-lwpolyline-safearray(append lisp-point1 lisp-point2)))
	   (vla-Delete vla-line2)
	   )
	  (T
	   (alert "Случилось невозможное!"))
	  )
	(vla-EndUndoMark active_document)
	)
      )
    )
  (princ)
  )

;;;(vla-put-Closed (vlax-ename->vla-object (car (entsel))) -1)