Jump to content

Extracting Texts to a comma delimated txt or csv file?


Recommended Posts

Posted

Hi everybody,

 

I have a dwg file which contains a coordinate table of points (x,y,z) but not in a autocad table format (just only text).

 

I need to export these data to a comma or tab delimated text file or a csv, xls file. Due to lack of knowledge in lisp I need your help.

 

If anybody has a routine for this or can help me to create it, I will be galdful.

 

Here is an attached example table of coordinates.

 

coordinates_example.dwg

 

Many thanks in advance

Posted

Try this:

(defun c:test (/)
(vl-load-com)
(defun prc (txt)(write-line (vl-princ-to-string txt) fn))
(setq dwgsname (getvar 'dwgname))
(setq dwgsnamelen (strlen dwgsname))
(setq dwgsname (substr dwgsname 1 (- dwgsnamelen 4)))
(setq fname (Strcat (getvar 'dwgprefix) dwgsname ".txt" ))  
(setq fn (open  fname "w"))
(setq txtlst (ssget (list '(0 . "TEXT"))))
(setq txtnum (sslength txtlst))
(setq tcnt 0) 
(while (< tcnt txtnum)
(progn
(setq en(ssname txtlst tcnt))
(setq enlist(entget en))
(setq NomeTxt (strcat (cdr(assoc 1 enlist))";") )
(prc NomeTxt)
(setq tcnt (1+ tcnt)))) 
 (PRINC)
(prompt (strcat "\nBlock data written to file: " fname))
(startapp "notepad" (findfile  Fname))

 (close fn)
 );defun

Posted
Hi everybody,

 

I have a dwg file which contains a coordinate table of points (x,y,z) but not in a autocad table format (just only text).

 

I need to export these data to a comma or tab delimated text file or a csv, xls file. Due to lack of knowledge in lisp I need your help.

 

If anybody has a routine for this or can help me to create it, I will be galdful.

 

Here is an attached example table of coordinates.

 

[ATTACH]19101[/ATTACH]

 

Many thanks in advance

 

 

Try this one too

 

;; Import the multi column plain text table ;;
;; to .csv file ;;
;;local defuns:
;; remove duplicates  
;; published by hutch
(defun rem-dups  (mylist / newlst)
 (foreach item  mylist
   (and (null (member item newlst))
 (setq newlst (cons item newlst))
 )
   )
 newlst
 )

(defun data->csv  (datalist fname / fdesc)
 (setq fdesc (open fname "w"))
 (foreach line  datalist
   (write-line line fdesc)
   )
 (close fdesc)
 (prin1)
 )



;; *** main part *** ;;
(defun C:TCSV (/ csv_list csv_name data en i ip item line output
         p1 p2 rowlist ss str tmp txt ylist)
(alert
 "Select plain text table by window selection\nwithout title and headers")

(setq p1 (getpoint "\nSpecify the first corner point of window ")
     p2 (getcorner p1
     "\nSpecify the opposite corner point of window ")
     )

(setq ss (ssget "W" p1 p2 (list (cons 0 "TEXT")))
     i  -1
     )

(repeat (sslength ss)
 (setq en   (ssname ss (setq i (1+ i)))
ip   (cdr (assoc 10 (entget en)))
txt  (cdr (assoc 1 (entget en)))
tmp  (cons txt ip)
data (cons tmp data)
)
 )
(setq ylist (mapcar 'caddr data)
     ylist (rem-dups ylist)
     ylist (vl-sort ylist (function (lambda (a b) (> a b))))
     )
(repeat (length ylist)
 (setq rowlist (vl-remove-if-not
   (function (lambda (x)
        (equal (caddr x) (car ylist) 0.01)
        )
      )
   data
   )
rowlist (vl-sort rowlist
   (function (lambda (a b) (< (cadr a) (cadr b))))
   )
)
 (setq output (append output (list rowlist)))
 (setq ylist (cdr ylist))
 )
(setq output (mapcar (function (lambda (x)
    (mapcar 'car x)
    )
         )
      output
      )
     )
(setq csv_list
      (mapcar '(lambda (item) (substr item 2))
       (mapcar '(lambda (line) (apply 'strcat line))
        (mapcar '(lambda (str)
     (mapcar '(lambda (i) (strcat "," i)) str))
         output)
        )
       )
     )
(setq csv_name (strcat (getvar "dwgprefix")
        (vl-filename-base (getvar "dwgname"))
        ".csv")
     )
(data->csv csv_list csv_name)
(alert (strcat "File saved as \"" csv_name "\""))    
 (princ)
)
(princ "\n\t*** Type TCSV to execute...")
(prin1)

 

~'J'~

Posted

I know the selection is a little cumbersome, but for points I wanted to be as safe as possible.

 

(defun c:PointColumnExport (/ _ss Num North East Elev lst)
 ;; Alan J. Thompson, 04.22.10

 (vl-load-com)

 (defun _ss (/ l)
   ((lambda (ss i)
      (if ss
        (progn
          (while (setq e (ssname ss (setq i (1+ i))))
            (setq l (cons (cons (caddr (assoc 10 (setq e (entget e)))) (cdr (assoc 1 e))) l))
          )
          (mapcar (function cdr) (vl-sort l (function (lambda (a b) (> (car a) (car b))))))
        )
      )
    )
     (ssget '((0 . "MTEXT,TEXT")))
     -1
   )
 )


 ;; Write list to file
 ;; #File - file to write list to (must be in form "c:\\File.txt")
 ;; #ListToWrite - list to write to file
 ;; #Overwrite - If T, will overwrite; nil to append
 ;; Alan J. Thompson, 04.28.09
 (defun AT:WriteToFile (#File #ListToWrite #Overwrite / #FileOpen)
   (cond ((and (vl-consp #ListToWrite)
               (setq #FileOpen (open #File
                                     (if #Overwrite
                                       "W"
                                       "A"
                                     ) ;_ if
                               ) ;_ open
               ) ;_ setq
          ) ;_ and
          (foreach x #ListToWrite
            (write-line (vl-princ-to-string x) #FileOpen)
          ) ;_ foreach
          (close #FileOpen)
          #File
         )
   ) ;_ cond
 ) ;_ defun

 (if (and (princ "\nSelect point number text: ")
          (setq Num (_ss))
          (princ "\nSelect Northing text: ")
          (setq North (_ss))
          (princ "\nSelect Easting text: ")
          (setq East (_ss))
          (princ "\nSelect Elevation text: ")
          (setq Elev (_ss))
     )
;;;(apply
;;;  (function and)
;;;  (mapcar
;;;    (function
;;;      (lambda (p v)
;;;        (princ p)
;;;        (set v (ssget '((0 . "MTEXT,TEXT"))))))
;;;    '("\nSelect numbers: " "\nSelect Northing text: ")
;;;    '(ssNum ssNor)))
   (progn
     (setq lst (mapcar
                 (function cdr)
                 (vl-sort
                   (mapcar
                     (function
                       (lambda (# n e z)
                         (cons (atoi #) (strcat # "," n "," e "," z))
                       )
                     )
                     Num
                     North
                     East
                     Elev
                   )
                   (function (lambda (a b) (< (car a) (car b))))
                 )
               )
     )
     (startapp
       "NOTEPAD"
       (AT:WriteToFile
         (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname)) "_PointList.txt")
         lst
         T
       )
     )
   )
 )
 (princ)
)

Posted

Nice work Fixo!

I should have gone against my judgment and done a little better job of sorting.

I guess I was just nervous...

 

Your submission works fine, so I won't bother fixing mine.

Posted

Many thanx to all you for all your helps, these routines are great.

and many thanx to Lee, I am always using your lisps and they are very useful.

Posted
Nice work Fixo!

I should have gone against my judgment and done a little better job of sorting.

I guess I was just nervous...

 

Your submission works fine, so I won't bother fixing mine.

Thanks for the compliment, Alan,

it was written in 2007 for one of the friends of mine

Happy coding :)

 

~'J'~

Posted
Thanks for the compliment, Alan,

it was written in 2007 for one of the friends of mine

Happy coding :)

 

~'J'~

Likewise. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...