Author Topic: Convert coordinate files to x,y  (Read 815 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17000
  • brevity != aggression
Re: Convert coordinate files to x,y
« Reply #15 on: August 30, 2017, 03:08:19 pm »
Quick & Dirty concept code.

Code: [Select]
(defun _TSV->List ( tsvfilename / foo i handle s result )

    (defun foo ( s / result )
        (while (setq i (vl-string-position 9 s nil t))
            (setq
                result (cons (substr s (+ 2 i)) result)
                s      (substr s 1 i)
            )
        )
        (cons s result)
    )

    (if (setq handle (open tsvfilename "r"))
        (progn
            (while (setq s (read-line handle))
                (setq result (cons (foo s) result))
            )
            (close handle)
            (vl-remove 'nil (reverse result))
        )
    )
)

Code: [Select]
(defun _List->CSV ( lst tsvfilename / handle )

    (if (setq handle (open tsvfilename "w"))
        (progn
            (foreach r lst
                (princ (car r) handle)
                (foreach x (cdr r) (princ "," handle) (princ x handle))
                (princ "\n" handle)
            )
            (null (close handle))
        )
    )
)

Code: [Select]
;;  read it in
(setq lst (_TSV->List "c:\\docs\\tsv.txt"))
;;  abuse it
(setq alt (mapcar (function (lambda (r) (reverse (mapcar 'set '(x y) (cdr r))))) lst))
;;  write it out
(_List->CSV alt "c:\\docs\\csv_abused.txt")

;;   orig

0    318349.74    4231547.73
1    318445.61    4231568.79
2    318446.37    4231561.59
3    318443.36    4231549.82
4    318438.82    4231539.87
5    318428.56    4231532.33
...

;;   abused

4231547.73,318349.74
4231568.79,318445.61
4231561.59,318446.37
4231549.82,318443.36
4231539.87,318438.82
...


/2˘
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

VovKa

  • Swamp Rat
  • Posts: 850
  • Ukraine
Re: Convert coordinate files to x,y
« Reply #16 on: August 30, 2017, 03:30:22 pm »
no foo
Code: [Select]
(setq result (cons (read (strcat "(" s ")")) result))

MP

  • Seagull
  • Posts: 17000
  • brevity != aggression
Re: Convert coordinate files to x,y
« Reply #17 on: August 30, 2017, 05:01:27 pm »
If the data hosts non numerical items that technique would (undesirably) yield symbols instead of strings.

Cheers.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

fools

  • Newt
  • Posts: 63
  • China
Re: Convert coordinate files to x,y
« Reply #18 on: August 30, 2017, 06:57:20 pm »
I attach a *.txt file with the coordinates.I try fools code but the *.pl file is empty

My code is written very rough, just to show the way.
Change space to tab in function F_Str2Lst can convert correctly.

Code - Auto/Visual Lisp: [Select]
  1. (DEFUN c:test (/ FF FFN TXT TXTS)
  2.  (SETQ ffn  (GETFILED "Select file" "" "txt" 0)
  3. ff   (OPEN ffn "r")
  4. txts nil
  5.  )
  6.  (WHILE (SETQ txt (READ-LINE ff)) (SETQ txts (CONS txt txts)))
  7.  (CLOSE ff)
  8.  ;;(VL-FILE-RENAME ffn (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "bak"))
  9.  (SETQ txts (MAPCAR 'CDR (MAPCAR 'F_Str2Lst (MAPCAR 'F_NoExtraSpaces (REVERSE txts)))))
  10.  (SETQ ff (OPEN (STRCAT (SUBSTR ffn 1 (- (STRLEN ffn) 3)) "pl") "w"))
  11.  (FOREACH item txts (WRITE-LINE (STRCAT (CAR item) "," (CADR item)) ff))
  12.  (CLOSE ff)
  13. )
  14.  
  15. (DEFUN F_NoExtraSpaces (str / pos)
  16.  (SETQ str (VL-STRING-TRIM " " str))
  17.  (WHILE (SETQ pos (VL-STRING-SEARCH "  " str pos)) (SETQ str (VL-STRING-SUBST " " "  " str pos)))
  18.  str
  19. )
  20.  
  21. (DEFUN F_Str2Lst (str / pos)
  22.  (IF (SETQ pos (VL-STRING-SEARCH "\t" str))
  23.    (CONS (SUBSTR str 1 pos) (F_Str2Lst (SUBSTR str (+ (STRLEN "\t") pos 1))))
  24.    (LIST str)
  25.  )
  26. )
Good good study , day day up . Sorry about my Chinglish .

Topographer

  • Bull Frog
  • Posts: 492
Re: Convert coordinate files to x,y
« Reply #19 on: August 30, 2017, 07:08:35 pm »
Thank you   :smitten: :smitten:

fools

  • Newt
  • Posts: 63
  • China
Re: Convert coordinate files to x,y
« Reply #20 on: August 30, 2017, 07:10:47 pm »
no foo
Code: [Select]
(setq result (cons (read (strcat "(" s ")")) result))

Using read function maybe change data.

Code: [Select]
(SETQ str " 0   318349.74  4231547.73 ")
(read (strcat "(" str ")"))

result:
(0 318350.0 4.23155e+006)
Good good study , day day up . Sorry about my Chinglish .

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 981
  • class keyThumper<T>:ILazy<T>
Re: Convert coordinate files to x,y
« Reply #21 on: August 30, 2017, 11:53:10 pm »
hI. I am searching for a lisp to convert coordinate files files to x,y  (*.txt)

1) p,x,y,z --->x,y
2) p,x,y ---->x,y
3) p x y z ---> x,y
4) p x y ----> x,y


i know that i can do it with excel but i need something faster.

Thanks

to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.


0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81


just saying ...
called Kerry in my other life

Sometimes the question is more important than the answer.

fools

  • Newt
  • Posts: 63
  • China
Re: Convert coordinate files to x,y
« Reply #22 on: August 31, 2017, 01:20:40 am »
to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.

Thanks for remind. I change a function to increase versatility.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test (/ FF FFN TXT TXTS)
  2.  (setq ffn  (getfiled "Select file" "" "txt" 0)
  3. ff   (open ffn "r")
  4. txts nil
  5.  )
  6.  (while (setq txt (read-line ff)) (setq txts (cons txt txts)))
  7.  (close ff)
  8.  (setq txts (mapcar '(lambda (x) (STD-STRSPLIT x "\t,; ")) (reverse txts)))
  9.  (setq txts
  10.   '(lambda (x) (vl-remove-if '(lambda (y) (and (not (equal "0" y)) (zerop (atof y)))) x))
  11.   txts
  12. )
  13.  )
  14.  (setq ff (open (strcat (substr ffn 1 (- (strlen ffn) 3)) "pl") "w"))
  15.  (foreach item txts (write-line (strcat (cadr item) "," (caddr item)) ff))
  16.  (close ff)
  17.  (princ)
  18. )
  19.  
  20. ;;; The order of chars in delim is not important.
  21. ;;; keeping null tokens, not as with std-strtok.
  22. ;;; Might be renamed to std-string-split
  23. ;;; by Vladimir Nesterowsky
  24. (defun STD-STRSPLIT (s delims / len s1 i c lst)
  25.  (setq delims (vl-string->list delims) ; fixed
  26. len    (strlen s)
  27. s1     ""
  28. i      (1+ len)
  29.  )
  30.  (while (> (setq i (1- i)) 0)
  31.    (setq c (substr s i 1))
  32.    (if (member (ascii c) delims)
  33.      (if (/= i len) ; "1,2," -> ("1" "2") and not ("1" "2" "")
  34. (setq lst (cons s1 lst)
  35.      s1  ""
  36. )
  37.      )
  38.      (setq s1 (strcat c s1))
  39.    )
  40.  )
  41.  (cons s1 lst) ; ",1,2" -> ("" "1" "2")
  42. )


1.txt
0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81

1.pl
300000.00,4000000.00
318445.61,4231568.79
318446.37,4231561.59
318443.36,4231549.82
318438.82,4231539.87
318428.56,4231532.33
318425.25,4231523.57
318428.55,4231513.05
318442.57,4231516.64
318455.87,4231506.94
318473.12,4231495.81
Good good study , day day up . Sorry about my Chinglish .

anhquang1989

  • Newt
  • Posts: 47
Re: Convert coordinate files to x,y
« Reply #23 on: August 31, 2017, 02:14:16 am »
This is the norm of my work. It may be right for you. please try
Import txt file, csv with the format: P Y Y Z Code
- different separators like comma, space, tab
- Still works well with other formats like
+ P X Y
+ P X Y Z
+ P Y X
+ P Y X Z
......

MP

  • Seagull
  • Posts: 17000
  • brevity != aggression
Re: Convert coordinate files to x,y
« Reply #24 on: August 31, 2017, 10:53:24 am »
to be more in line with the original requirements the translators would need to work successfully on this list.
with the addition of TABS as per the provided file.

0   300000.00   4000000.00 0.0
1   318445.61   4231568.79
2 318446.37,4231561.59,123
3   318443.36,4231549.82
4   318438.82      4231539.87    0.0
5   318428.56,   4231532.33,      0.0
6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0
8   318442.57   4231516.64
9   318455.87   4231506.94
10   318473.12   4231495.81


just saying ...

Eschewing the important but I don't have time for "importance of well formed data" discussion I'll ante up this 2 minute quickie:

Code: [Select]
(defun _Normalize ( str )
    (foreach delim '(" " "," "\t")
        (foreach pattern (list (strcat " " delim) (strcat delim " "))
            (while (vl-string-search pattern str)
                (setq str (vl-string-subst delim pattern str))
            )
        )
    )         
    (vl-string-translate " \t" ",," str)
)

Code: [Select]
(progn
    (foreach test
       '(
            "0   300000.00   4000000.00 0.0"
            "1   318445.61   4231568.79"
            "2 318446.37,4231561.59,123"
            "3   318443.36,4231549.82"
            "4   318438.82      4231539.87    0.0"
            "5   318428.56,   4231532.33,      0.0"
            "6,318425.25,4231523.57,0.0"
            "7,   318428.55,   4231513.05,   0.0"
            "8 \t  318442.57   4231516.64"
            "9   318455.87 \t  4231506.94"
            "10   318473.12   4231495.81"   
        )
        (princ (strcat "\n" test " >> " (_Normalize test)))
    )   
    (princ)
)

0   300000.00   4000000.00 0.0        >> 0,300000.00,4000000.00,0.0
1   318445.61   4231568.79            >> 1,318445.61,4231568.79
2 318446.37,4231561.59,123            >> 2,318446.37,4231561.59,123
3   318443.36,4231549.82              >> 3,318443.36,4231549.82
4   318438.82      4231539.87    0.0  >> 4,318438.82,4231539.87,0.0
5   318428.56,   4231532.33,      0.0 >> 5,318428.56,4231532.33,0.0
6,318425.25,4231523.57,0.0            >> 6,318425.25,4231523.57,0.0
7,   318428.55,   4231513.05,   0.0   >> 7,318428.55,4231513.05,0.0
8   318442.57   4231516.64            >> 8,318442.57,4231516.64
9   318455.87    4231506.94           >> 9,318455.87,4231506.94
10   318473.12   4231495.81           >> 10,318473.12,4231495.81


Cheers.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Lee Mac

  • Seagull
  • Posts: 11831
  • AutoCAD 2015 Windows 7 London, England
Re: Convert coordinate files to x,y
« Reply #25 on: August 31, 2017, 01:36:25 pm »
Just to offer an alternative approach:
Code - Auto/Visual Lisp: [Select]
  1. (defun NormaliseList ( src dst lst / rgx rtn )
  2.    (if (setq rgx (vlax-create-object "vbscript.regexp"))
  3.        (progn
  4.            (vl-catch-all-apply
  5.               '(lambda nil
  6.                    (vlax-put-property rgx 'global actrue)
  7.                    (vlax-put-property rgx 'pattern src)
  8.                    (setq rtn (mapcar '(lambda ( x ) (vlax-invoke rgx 'replace x dst)) lst))
  9.                )
  10.            )
  11.            (vlax-release-object rgx)
  12.            rtn
  13.        )
  14.    )
  15. )
Code - Auto/Visual Lisp: [Select]
  1.    (NormaliseList
  2.        "[^\\s\\,]+[\\s\\,]+([^\\s\\,]+)[\\s\\,]+([^\\s\\,]+).*"
  3.        "$1,$2"
  4.       '(
  5.            "0   300000.00   4000000.00 0.0"
  6.            "1   318445.61   4231568.79"
  7.            "2 318446.37,4231561.59,123"
  8.            "3   318443.36,4231549.82"
  9.            "4   318438.82      4231539.87    0.0"
  10.            "5   318428.56,   4231532.33,      0.0"
  11.            "6,318425.25,4231523.57,0.0"
  12.            "7,   318428.55,   4231513.05,   0.0"
  13.            "8 \t  318442.57   4231516.64"
  14.            "9   318455.87 \t  4231506.94"
  15.            "10   318473.12   4231495.81"  
  16.        )
  17.    )
  18. )

Yields:

"300000.00,4000000.00"
"318445.61,4231568.79"
"318446.37,4231561.59"
"318443.36,4231549.82"
"318438.82,4231539.87"
"318428.56,4231532.33"
"318425.25,4231523.57"
"318428.55,4231513.05"
"318442.57,4231516.64"
"318455.87,4231506.94"
"318473.12,4231495.81"

MP

  • Seagull
  • Posts: 17000
  • brevity != aggression
Re: Convert coordinate files to x,y
« Reply #26 on: August 31, 2017, 02:39:54 pm »
Nicely done Lee, cheers.
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Lee Mac

  • Seagull
  • Posts: 11831
  • AutoCAD 2015 Windows 7 London, England
Re: Convert coordinate files to x,y
« Reply #27 on: August 31, 2017, 05:42:42 pm »
Cheers Michael, same to you. :-)

fools

  • Newt
  • Posts: 63
  • China
Re: Convert coordinate files to x,y
« Reply #28 on: September 01, 2017, 09:39:54 pm »
Thanks Lee and MP, I learned a lot.
Good good study , day day up . Sorry about my Chinglish .

DEVITG

  • Bull Frog
  • Posts: 425
Re: Convert coordinate files to x,y
« Reply #29 on: September 02, 2017, 12:38:32 am »
Just put , or upload, or whats ever, your sample dwg and your sample xyz value file, as you do it by hand.

Keep it simple sir .. just KISS

Location @ Córdoba Argentina<br /><br />using acad 2008 under win XP