Author Topic: Getfiled manipulation  (Read 699 times)

0 Members and 1 Guest are viewing this topic.

Jim2018

  • Mosquito
  • Posts: 16
Getfiled manipulation
« on: February 26, 2023, 12:50:48 PM »
It is a routine that reads consecutive files of num,x,y format and creates a 2d lwpolyline by simultaneously printing a point and its name on the vertices. The question is whether there is a way to integrate the algorithm, especially in the getfiled command, so as to avoid splitting the algorithm between the first time and the following ones. Splitting is done for work in the same folder.
Attached is a data file (-0.txt) to test.

Code - Auto/Visual Lisp: [Select]
  1. ;;; txt2lwpo.lsp
  2. ;;; --------------------------------------------------------------------------
  3. ;;; 2023_02_25
  4. ;;; Reads files num,x,y format and creates a 2d lwpolyline by simultaneously
  5. ;;; printing a point and its name on the vertices (as text).
  6. ;;; --------------------------------------------------------------------------
  7. (defun c:txt2lwpo ( / f_name f_line list_all count_s p_num p_xcord p_ycord p_h )
  8.  (setvar "cmdecho" 0)
  9.  (setq ff (getfiled "Select a file to import import .... : " "" "txt" 16))
  10.  (setq ff_dir (vl-filename-directory ff))
  11.  (setq f_name (open ff "r"))
  12.    (setq list_all '())
  13.    (setq count_s 0)
  14.    (While
  15.      (setq f_line (read-line f_name))
  16.      (setq f_line_list(strtoli (strcat f_line ",") ","))
  17.      (setq p_num(nth 0 f_line_list))  
  18.      (setq p_xcord(nth 1 f_line_list))
  19.      (setq p_ycord(nth 2 f_line_list))
  20.      (setq point_new (list (atof p_xcord) (atof p_ycord) 0))
  21.      (setq list_all (append list_all (list (list p_num point_new))))
  22.      (setq count_s (1+ count_s))
  23.    );;;end While
  24.    (close f_name)
  25.    (makepline list_all "0" 1)
  26.    (mapcar '(lambda (pt)(command "point" (cadr pt))
  27.                         (command "text" (cadr pt)  1  0 (car pt))
  28.             ) list_all
  29.    );;;end mapcar
  30.    (while
  31.      (setq f_name (open (getfiled "Select a file to import .... : " ff_dir "txt" 16) "r"))
  32.      (setq list_all '())
  33.      (setq count_s 0)
  34.      (While
  35.        (setq f_line (read-line f_name))
  36.        (setq f_line_list(strtoli (strcat f_line ",") ","))
  37.        (setq p_num(nth 0 f_line_list))
  38.        (setq p_xcord(nth 1 f_line_list))
  39.        (setq p_ycord(nth 2 f_line_list))
  40.        (setq point_new (list (atof p_xcord) (atof p_ycord) 0))
  41.        (setq list_all (append list_all (list (list p_num point_new))))
  42.        (setq count_s (1+ count_s))
  43.      );;;end While
  44.      (close f_name)
  45.      (makepline list_all "0" 1)
  46.      (mapcar '(lambda (pt)(command "point" (cadr pt))
  47.                         (command "text" (cadr pt)  1  0 (car pt))
  48.               ) list_all
  49.      );;;end mapcar
  50.    );;;end while
  51. );;;end function
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  53. (defun strtoli (str symb / count s li )
  54.     (setq count 1)
  55.     (setq li  '())
  56.     (while (/= (setq s (substr str count 1)) "")
  57.            (if (= s symb)
  58.                (progn
  59.                   (setq li (append li (list (substr str 1 (- count 1)))))
  60.                   (setq str (substr str (+ count 1)))
  61.                   (setq count 0)
  62.                )
  63.            )
  64.            (setq count (1+ count))
  65.     )
  66.     li
  67. );;;end function
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. (defun makepline (plst lay clsd)  
  70.    (entmakex
  71.       (append
  72.         (list  '(0 . "LWPOLYLINE")
  73.                '(100 . "AcDbEntity")
  74.                '(100 . "AcDbPolyline")
  75.                (cons 8 lay)
  76.                (cons 90 (length plst))
  77.                (cons 70 clsd)              ;;; 1 for closed polyline 0 otherwise
  78.         );;;end list
  79.         (mapcar '(lambda (pt) (cons 10 (cadr pt)))  plst)  
  80.       );;;end append
  81.    );;;end entmakex
  82. );;;;;;end function
  83. ;;;
  84. (princ "\n\tTYPE <txt2lwpo> to start")


EDIT (John): Added code tags.
« Last Edit: February 27, 2023, 09:58:37 AM by JohnK »

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Getfiled manipulation
« Reply #1 on: February 26, 2023, 05:20:10 PM »
Could be done with lisp also so open the files and make a new one use a blank line as the end marker for each pline. You use the "A" option append to a file. The other and again make a list of file names. If all the csv are in one directory then should only have to point to that directory rather than a file. Trying to remember the get all files say csv in directory, maybe a Lee-mac function. Pretty sure a VL function.

The other thing is say for a CIV3D user you would add a description, this way the plines are not joined together but strung together.

1,219387.14,4348711.49,p1
2,219383.53,4348702.56,p1
3,219373.46,4348677.68,p1
..................
384,219394.62,4348737.32,p2 start of new pline

CIV3D would do the labelling, layer control etc. Other products like Stringer.
« Last Edit: February 26, 2023, 05:27:56 PM by BIGAL »
A man who never made a mistake never made anything

Jim2018

  • Mosquito
  • Posts: 16
Re: Getfiled manipulation
« Reply #2 on: February 27, 2023, 04:00:21 AM »
Thank you BIGAL

I have thought about the process of creating a file from the individual files, but the question concerns the improvement of the routine, with the same data, consider it a search for a better algorithm, which I don't know!

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Getfiled manipulation
« Reply #3 on: February 27, 2023, 10:09:35 AM »
Getting all the files, of a type (for example), is easy.

I haven't looked at your code yet but here is a general File list getter routine (this was in one of my old files). Please claim this routine if it is yours but I will assume it's one of mine.
Code - Auto/Visual Lisp: [Select]
  1. (defun files-p ( path pattern ) ;; /*{{{*/
  2.   ;;
  3.   ;; returns a list of all the files in a given path
  4.   ;; with a given search pattern. If a nill pattern
  5.   ;; to search for is not given, defaults to ``*.*''.
  6.   ;;
  7.   ;; (files-p "C:\\" nil)
  8.   ;;
  9.   ;; returns all files in directory specified.
  10.   ;;
  11.   (
  12.    (lambda ( a e )
  13.      (if (null a)
  14.        nil
  15.        (cond
  16.          ((null e)
  17.           (vl-directory-files a "*.*" 1))
  18.          ((vl-directory-files a e 1)))) )
  19.    path ; a
  20.    pattern ; e
  21.    ) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Getfiled manipulation
« Reply #4 on: February 27, 2023, 06:54:48 PM »
Why are they individual files ? Survey style files are generally only 1 big one.
A man who never made a mistake never made anything