(defun C:convert_loc_to_kml (/ patch-file list-all-points list-current-point list-file list-strings coordinates coordinates-str description)
  (if(setq patch-file (getfiled "Выбор файла loc" (getvar "DWGPREFIX") "loc" 0))
    (progn
      (setq list-file (file_read_to_list patch-file))
      (setq list-file (reverse (cddddr (reverse (cddddr list-file)))))
      (repeat (/(length list-file)14)
	(setq list-current-point (append list-current-point (list (get-record-id-loc-point (car list-file)))))
	(setq list-file (cdr list-file))
	(repeat 12
	  (setq list-current-point (append list-current-point (list (get-record-value-loc-point (car list-file)))))
	  (setq list-file (cdr list-file))
	  )
	(setq list-all-points (append list-all-points (list list-current-point)))
	(setq list-current-point nil)
	(setq list-file (cdr list-file))
	)
      (setq description "<![CDATA[имя план высота H<br>")
      (foreach x list-all-points
	(setq description (strcat
			    description
			    (strcat (get-item-by-name-in-list x "Description")" "
				    (get-item-by-name-in-list x "Use_Horizontal")" "
				    (get-item-by-name-in-list x "Use_Vertical")" "
				    (get-item-by-name-in-list x "Local_Z")"<br>")))
	)
      (setq description (strcat description "]]>"))
      (foreach y list-all-points
	(setq coordinates (append coordinates (list (list (atof (get-item-by-name-in-list y "Lon")) (atof (get-item-by-name-in-list y "Lat")) ))))
	)
      (setq coordinates (convert-list-points-to-poligon coordinates))
      (setq coordinates-str "")
      (foreach z coordinates
	(setq coordinates-str (strcat coordinates-str (rtos (nth 0 z) 2 13)"," (rtos (nth 1 z) 2 13) ",0 "))
	)
      (setq list-strings (list
			   "<?xml version=\"1.0\" encoding=\"windows-1251\"?>"
			   "<kml xmlns=\"http://earth.google.com/kml/2.2\">"
			   "<Document>"
			   "<Placemark>"
			   (strcat "<name>" (vl-filename-base patch-file)"</name>")
			   (strcat "<description>"description"</description>")
			   "<Style>"
			   "<LineStyle>"
			   (strcat "<color>" "A60000FF" "</color>")
			   "<width>2</width>"
			   "</LineStyle>"
			   "<PolyStyle>"
			   (strcat "<color>" "33FFFF00" "</color>")
			   "<fill>1</fill>"
			   "</PolyStyle>"
			   "</Style>"
			   "<Polygon>"
			   "<extrude>1</extrude>"
			   "<outerBoundaryIs>"
			   "<LinearRing>"
			   (strcat "<coordinates>" coordinates-str "</coordinates>")
			   "</LinearRing>"
			   "</outerBoundaryIs>"
			   "</Polygon>"
			   "</Placemark>"
			   "</Document>"
			   "</kml>"
			   )
	    )
      
      
      (if(setq patch-file (getfiled "Имя сохраняемого файла..." (strcat (vl-filename-directory patch-file)"\\"(vl-filename-base patch-file)) "kml" 1))
	(file_write_list_to_file patch-file list-strings))
      
      )
    )
  (princ)
  )

(defun get-record-id-loc-point (str / list-record)
  (setq list-record (convert-string-to-list-be-delimiter (vl-string-right-trim " >"(vl-string-left-trim "<record " str))"="))
  (list (nth 0 list-record) (vl-string-trim "\""(nth 1 list-record)))
  )

;;;(get-record-id-loc-point "<record id=\"Point 3\" >")


(defun get-record-value-loc-point (str / list-record list-record-1 list-record-2)
  (setq list-record (convert-string-to-list-be-delimiter (vl-string-right-trim "></value>"(vl-string-left-trim "<value " str))" "))
  (setq list-record-1 (convert-string-to-list-be-delimiter (nth 0 list-record)"="))
  (setq list-record-2 (convert-string-to-list-be-delimiter (nth 1 list-record)"="))
  (list (vl-string-trim "\"" (cadr list-record-1)) (vl-string-trim "\""(cadr list-record-2)))
  )

;;;(get-record-value-loc-point "<value name=\"Lat\" value=\"51.5421066792194\"></value>")


(defun convert-list-points-to-poligon (list-points / start-point flag list-angle result-poligon)
  (setq start-point (assoc (apply 'max (mapcar 'car list-points)) list-points))
  (foreach x (cdr list-points)
    (setq list-angle (append list-angle (list (calculation_angle_be_3points (mapcar '+ start-point (list 0.0 1.0)) start-point x))))
    )
  (setq result-poligon (list start-point (nth (analysis-number-item-in-list (apply 'min list-angle)list-angle) (cdr list-points))))
  (setq n 0)
  (while (not flag)
    (setq list-angle nil)
    (setq current-point (last result-poligon))
    (setq current-angle-point (cadr (reverse result-poligon)))
    (setq clear-list-points (vl-remove current-angle-point (vl-remove current-point list-points)))
    (foreach y clear-list-points
      (setq list-angle (append list-angle (list (calculation_angle_be_3points current-angle-point current-point y))))
      )
    (setq result-poligon (append result-poligon (list (nth (analysis-number-item-in-list (apply 'min list-angle)list-angle) clear-list-points))))
    (if (or (and (eq (car (last result-poligon)) (car start-point))(eq (cadr (last result-poligon)) (cadr start-point))) (> n (length list-points)))
      (setq flag T)
      )
    (setq n (1+ n))
    )
  result-poligon
  )