Just wanna add something to the discussion.
My solution to export UTM Coordinates to KML Format (CAD to GPS Receiver and Google Earth)
;; Zoning and Datum are hard coded. (My region is Zone 48, North Hemisphere)
;; Line Description on KML File is based on Layer Name
;; Needs to deal with:
;; - Closed Polyline
;; - Automatic LINE COLOR
(defun C:VRTX2KML ( / olderr )
(ShowTM "Vertices to File/GPS/Google Earth")
(NewErrTrap)
(getmode '("cmdecho" "blipmode" "osmode"))
(mapcar 'setvar
'("cmdecho" "blipmode" "osmode")
'(0 1 0)
)
(princ "\nSelect Polyline: ")
(setq sset (ssget '((0 . "LWPOLYLINE,POLYLINE"))))
(if sset
(progn
;;
;; PREFIX FOR GPS RECEIVER
;;
(setq prefx (getstring "\n. Prefix for Polyline: "))
(setq flnm (getstring "\n. Output Filename: "))
(initget 1)
(setq wdth (getint "\n. POLYLINE Width [1-10]: "))
;;
;; MANUAL LINE COLOR
;;
(initget "Blue B Red R Green G Yellow Y")
(setq answr (getkword "\n. POLYLINE Color <Red>/Green/Blue/Yellow: "))
;;Color and opacity (alpha) values are expressed in hexadecimal notation.
;;The range of values for any one color is 0 to 255 (00 to ff). For alpha, 00 is fully transparent and ff is fully opaque.
;;The order of expression is aabbggrr, where aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); rr=red (00 to ff).
;;For example, if you want to apply a blue color with 50 percent opacity to an overlay, you would specify the following:
;;<color>7fff0000</color>,
;;where alpha=0x7f, blue=0xff, green=0x00, and red=0x00.
(cond ((or (= answr "Red") (= answr "R") (= answr nil))
(setq clr "7F0000FF")
)
((or (= answr "Green") (= answr "G"))
(setq clr "7F00FF00")
)
((or (= answr "Blue") (= answr "B"))
(setq clr "7FFF0000")
)
((or (= answr "Yellow") (= answr "Y"))
(setq clr "7F00FFFF")
)
);end cond
(setq dwgprefx (getvar "DWGPREFIX"))
(setq flnmCartesian (strcat dwgprefx flnm "-XY.CSV"))
(setq flnmLatlong (strcat dwgprefx flnm "-LongLat.CSV"))
(setq flnmMap (strcat dwgprefx flnm ".KML"))
(setq flCartesian (open flnmCartesian "w"))
(setq flLatLong (open flnmLatlong "w"))
(setq flMap (open flnmMap "w"))
(vrtx2GPSstakeout sset)
(close flCartesian)
(close flLatLong)
(close flMap)
(princ (strcat "\n. Data saved to: " flnmCartesian "!"))
(princ (strcat "\n. Data saved to: " flnmLatlong "!"))
(princ (strcat "\n. Data saved to: " flnmMap "!"))
);end progn
);end if
(setmode *mod2)
(OldErrTrap)
(princ)
)
(defun vrtx2GPSstakeout ( sset / cntr n)
(WRITESTARTKML clr wdth)
(setq jumlhpoly (sslength sset) ;; Jumlah Polyline
n 0
)
;; Konter
(setq idxpoly 1)
(repeat jumlhpoly
(setq enm (ssname sset n))
(setq etype (dxf-code 0 enm))
;;
;; Cek Polyline is Close (Google Earth)
;;
(if (= (logand (dxf-code 70 enm) 1) 1)
(setq Pclose T)
(setq Pclose nil)
);end if LOGAND
(setq desc (dxf-code 8 enm)) ;; Layer AS DESCRIPTION
(setq ptlst (getcoords enm))
(WRITESTARTPLINE desc)
;; Konter
(setq idxvertex 1)
(setq idx 0)
(setq ptcartesian '())
(setq ptlatlong '())
(cond ((= etype "LWPOLYLINE")
(repeat (/ (length ptlst) 2)
(setq x (nth idx ptlst))
(setq y (nth (+ idx 1) ptlst))
(vrtx2GPSWrite)
(setq idxvertex (+ idxvertex 1))
(setq idx (+ idx 2))
);end repeat
(setq idxpoly (+ idxpoly 1))
)
((= etype "POLYLINE")
(repeat (/ (length ptlst) 3)
(setq x (nth idx ptlst))
(setq y (nth (+ idx 1) ptlst))
(vrtx2GPSWrite)
(setq idxvertex (+ idxvertex 1))
(setq idx (+ idx 3))
);end repeat
(setq idxpoly (+ idxpoly 1))
)
);end cond
(WRITEENDPLINE)
(setq n (+ n 1))
);end repeat
(WRITEENDKML)
(princ)
);end defun
(defun vrtx2GPSWrite ()
(setq ptxy (list x y))
(setq ptcartesian (append ptcartesian (list (list ptxy (strcat prefx (itoa idxpoly) "-" (itoa idxvertex))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tulis ke FILE X - Y - DESKRIPSI
(write-line (strcat
(rtos x 2 3)
","
(rtos y 2 3)
(strcat "," prefx (itoa idxpoly) "-" (itoa idxvertex))
)
flCartesian
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Latitude Longitude
(setq ptgeo (UTM2LatLong x y 48 F))
(setq ptlatlong (append ptlatlong (list (list ptgeo (strcat prefx (itoa idxpoly) "-" (itoa idxvertex))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILE (LONG-LAT-ELEVATION)
(write-line (strcat "\t" "\t"
(rtos (Y-OF ptgeo) 2 9)
","
(rtos (X-OF ptgeo) 2 9)
",0" ;; 2D
)
flMap
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GPS FORMAT (LONG-LAT-DESCRIPTION)
(write-line (strcat "\t" "\t"
(rtos (Y-OF ptgeo) 2 9)
","
(rtos (X-OF ptgeo) 2 9)
(strcat "," prefx (itoa idxpoly) "-" (itoa idxvertex))
)
flLatLong
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITESTARTKML ( clr wdth )
(setq AWALKML (strcat
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\">
<Document>
<!-- Begin Style Definitions -->
<Style id=\"line1\">
<LineStyle>
<color>"
clr
"</color>
<width>"
(itoa wdth) ;; WIDTH
"</width>
</LineStyle>
</Style>
<Folder>
<name>Line Features</name>
<description>Line Features</description>"
)
)
(write-line AWALKML flMap)
(princ)
);END DEFUN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITESTARTPLINE ( desc )
(setq AWALPline (strcat
" <Placemark>
<description>"
desc ;; DESCRIPTION
" </description>
<styleUrl>#line1</styleUrl>
<LineString>
<coordinates>"
)
)
(write-line AWALPLINE flMap)
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITEENDPLINE ( )
(setq AKHIRPLINE (strcat
" </coordinates>
</LineString>
</Placemark>"
)
)
(write-line AKHIRPLINE flMap)
(princ)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun WRITEENDKML ( )
(setq AKHIRKML (strcat
" </Folder>
</Document>
</kml>"
)
)
(write-line AKHIRKML flMap)
(princ)
)
;;
;;This function will return a list of coordinates. If the polyline is a LWPOLYLINE entity,
;; the list will be in the form of (x y x y x y x y), and the number of vertices can be determined by ;; dividing the length of the list by
;;If the polyline is a POLYLINE entity (2D or 3D), then the list will be in the form of (x y z x y z x y z),
;; and the number of vertices can be determined by dividing the length of the list by 3.
;;
;;
(defun getcoords (ent)
(vlax-safearray->list
(vlax-variant-value
(vlax-get-property
(vlax-ename->vla-object ent)
"Coordinates"
)
)
)
)
;;
;;(UTM2LatLong 781496 2900000 48 T)
(defun UTM2LatLong ( X Y Zone NORTHFLAG ) ;; NORTHFLAG = TRUE/FALSE
(if NORTHFLAG
(setq latd Y)
(setq latd (- 10000000.00 Y))
)
(setq lond (- 500000.0 X))
;; Zone Central Longitude
(if (> Zone 0)
(setq ZCL (- (* 6.0 Zone) 183.0))
(setq ZCL 3.0)
)
;;
;;Constant
;;
(setq b 6356752.3142 ) ;; Polar Axis
(setq a 6378137 ) ;; Equator Radius
(setq ec 0.0818191909289069 ) ;; Ecc
(setq e1sq 0.006739496756587 ) ;; E1sq
(setq K0 0.9996 ) ;; K0
;;
;; FootPrint Latitude
;;
(setq arc (/ latd K0) ) ;; Arch Length
(setq mu (/ arc (* a (- 1.0 (/ (* 1.0 (expt ec 2)) 4.0)
(/ (* 3.0 (expt ec 4)) 64.0)
(/ (* 5.0 (expt ec 6)) 256.0)
)
)
)
) ;; =arc/(a*(1-ec^2/4-3*ec^4/64-5*ec^6/256))
(setq e0 (sqrt (- 1.0 (expt ec 2)))) ;; =(1-ec^2)^0.5
(setq e1 (/ (- 1.0 e0) (+ 1.0 e0))) ;; =(1-e0)/(1+e0)
(setq C1 (- (* 3.0 e1 0.5) (/ (* 27.0 (expt e1 3)) 32.0))) ;; =3*ei/2-27*ei^3/32
(setq C2 (- (/ (* 21.0 e1 e1) 16.0) (/ (* 55.0 e1 e1 e1 e1) 32.0))) ;; =21*ei^2/16-55*ei^4/32
(setq C3 (/ (* 151.0 e1 e1 e1) 96.0)) ;; =151*ei^3/96
(setq C4 (/ (* 1097.0 e1 e1 e1 e1) 512.0)) ;; =1097*ei^4/512
(setq phi1 (+ mu (* c1 (sin (* 2.0 mu)))
(* c2 (sin (* 4.0 mu)))
(* c3 (sin (* 6.0 mu)))
(* c4 (sin (* 8.0 mu)))
)
) ;; =mu+c1*SIN(2*mu)+cb*SIN(4*mu)+ccc*SIN(6*mu)+cd*SIN(8*mu)
;;
;; FINALE FOOTPRINT LATITUDE
;;
(setq lat (rad2deg phi1)) ;;=phi1*180/PI()
(setq latdeg (fix lat))
(setq latmin (fix (* (- lat latdeg) 60.0)))
(setq latsec (* 3600.0 (- lat latdeg (/ latmin 60.0))))
;;
(setq Q0 (* e1sq (cos phi1) (cos phi1))) ;;=eisq*COS(phi1)^2
(setq T0 (* (Tan phi1) (Tan phi1))) ;;=TAN(phi1)^2
(setq N0 (/ a
(sqrt (- 1.0 (expt (* ec (sin phi1)) 2.0)))
)
) ;;=a/(1-(ec*SIN(phi1))^2)^(1/2)
(setq R0 (/ (* a (- 1.0 (* ec ec)))
(expt
(- 1.0 (expt (* ec (sin phi1)) 2.0))
(/ 3.0 2.0)
)
)
) ;;=a*(1-ec*ec)/(1-(ec*SIN(phi1))^2)^(3/2)
(setq D0 (/ lond N0 K0)) ;;=lond/(n0*k0)
;;
;;Coefficients for Calculating Latitude
;;
(setq Fact1 (* N0 (/ (Tan Phi1) R0))) ;;=n0*TAN(phi1)/r0
(setq Fact2 (* D0 D0 0.5)) ;;=dd0*dd0/2
(setq Fact3 (/ (* (+ 5.0 (* 3.0 T0)
(* 10.0 Q0)
(* -4.0 Q0 Q0)
(* -9.0 e1sq)
)
D0 D0 D0 D0
)
24.0
)
) ;;=(5+3*t0+10*Q0-4*Q0*Q0-9*eisq)*dd0^4/24
(setq Fact4 (/ (* (+ 61.0 (* 90.0 T0)
(* 298.0 Q0)
(* 45.0 T0 T0)
(* -252.0 e1sq)
(* -3.0 Q0 Q0)
)
D0 D0 D0 D0 D0 D0
)
720.0
)
) ;;=(61+90*t0+298*Q0+45*t0*t0-252*eisq-3*Q0*Q0)*dd0^6/720
;;
;;Coefficients for Calculating Longitude
;;
(setq LoFact1 D0)
(setq LoFact2 (/ (* (+ 1.0 (* 2.0 T0) Q0)
D0 D0 D0
)
6.0
)
) ;; =(1+2*t0+Q0)*dd0^3/6
(setq LoFact3 (/ (* (+ 5.0 (* -2.0 Q0)
(* 28.0 T0)
(* -3.0 Q0 Q0)
(* 8.0 e1sq)
(* 24.0 T0 T0)
)
D0 D0 D0 D0 D0
)
120.0
)
) ;;=(5-2*Q0+28*t0-3*Q0^2+8*e1sq+24*t0^2)*dd0^5/120
(setq DeltaLongRad (/ (+ LoFact1 (* -1.0 LoFact2) Lofact3)
(Cos Phi1)
)
) ;; =(lof1-lof2+lof3)/COS(phi1)
(setq DeltaLongDec (rad2deg DeltaLongRad))
(setq LatRad (- phi1 (* Fact1 (+ Fact2 Fact3 Fact4)))) ;;=(phi1-fact1*(fact2+fact3+fact4))
(setq LatitudeIs (rad2deg LatRad))
(setq LongitudeIs (- ZCL DeltaLongDec))
; (princ (rtos LatitudeIs 2 8))
; (princ "\t")
; (princ (rtos LongitudeIs 2 8))
(list (* -1.0 LatitudeIs) LongitudeIs) ;;
)
(defun showTM ( txt ) (graphscr)
(princ (strcat "\n. " txt))
(princ ". (Copyright of ARY HIMAWAN S. 1990)")
)
(defun NewErrTrap () (setq olderr *error* *error* @hs_err) (princ))
(defun OldErrTrap () (setq *error* olderr olderr nil) (princ))
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
;; Save Restore System variable setting
;; Example : (getmode '("cmdecho" "blipmode" "osmode" etc...etc....))
;; Example : (setmode *mod2)
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
(defun getmode (mod1)
(setq *mod2 '()) ; create global variable to store setting
(repeat (length mod1)
(setq *mod2
(append *mod2 (list (list (car mod1) (getvar (car mod1)))))
)
(setq mod1 (cdr mod1)) ; go to next element in list
)
)
(defun setmode (mod1)
(repeat (length mod1)
(setvar (caar mod1) (cadar mod1)) ; extract setting and reset
(setq mod1 (cdr mod1))
)
)
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
;; FIND DXF GROUP
;;o~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~o
(defun dxf-code (code enm /) (cdr (assoc code (entget enm))))