Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

selected text(s) to CSV files

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
aqdam1978
5110 Views, 13 Replies

selected text(s) to CSV files

Hi friends,

 

I need a LISP program that prompts: "select text(s)" then I select for example 100 text in open drawing file.

after that this program creat a file (LIST.CSV) and write each selected text in line by line of this file:

 

result text file (LIST.CSV ) should be:

 

Text1

Text2

Text3

...

...

Text99

Text100

 

 

ofcourse Text1 is the content of a text object.

 

Thank you in advance

 

 

Tags (1)
13 REPLIES 13
Message 2 of 14
pbejse
in reply to: aqdam1978

How would you want it, write the textstring to CSV file as you select the text? or all in one go and sorted?

 

Message 3 of 14
CADaSchtroumpf
in reply to: aqdam1978

Can it you help?

 

(defun c:text_value2csv ( / js dxf_cod mod_sel n lremov file_name cle f_open ename l_pt l_pr nbs)
  (princ "\nChoice of an object models for filtration: ")
  (while
    (null
      (setq js
        (ssget "_+.:E:S"
          (list
            '(0 . "*TEXT")
            (cons 67 (if (eq (getvar "CVPORT") 1) 1 0))
            (cons 410 (if (eq (getvar "CVPORT") 1) (getvar "CTAB") "Model"))
          )
        )
      )
    )
    (princ "\nIt is not a valid object for this function!")
  )
  (vl-load-com)
  (setq dxf_cod (entget (ssname js 0)))
  (foreach m (foreach n dxf_cod (if (not (member (car n) '(0 67 410 8 6 62 48 420 70))) (setq lremov (cons (car n) lremov))))
    (setq dxf_cod (vl-remove (assoc m dxf_cod) dxf_cod))
  )
  (initget "Single All Manual")
  (if (eq (setq mod_sel (getkword "\nMode of filtered selection, choice[Single/All/Manual]<Manual>: ")) "Single")
    (setq n -1)
    (if (eq mod_sel "All")
        (setq js (ssget "_X" dxf_cod) n -1)
        (setq js (ssget dxf_cod) n -1)
    )
  )
  (setq file_name (getfiled "Name of the file has create?: " (strcat (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 3)) "csv") "csv" 37))
  (if (null file_name) (exit))
  (if (findfile file_name)
    (progn
      (prompt "\nFile already exists!")
      (initget "Add Replace Undo")
      (setq cle
        (getkword "\nDatas in file? [Add/Replace/Undo] <Replace>: ")
      )
      (cond
        ((eq cle "Add")
          (setq cle "a")
        )
        ((or (eq cle "Replace") (eq cle ()))
          (setq cle "w")
        )
        (T (exit))
      )
      (setq f_open (open file_name cle))
    )
    (setq f_open (open file_name "w"))
  )
  (repeat (sslength js)
    (setq ename (vlax-ename->vla-object (ssname js (setq n (1+ n)))) l_pt nil)
    (setq l_pr (list 'TextString) nbs 0)
    (foreach n l_pr
      (if (vlax-property-available-p ename n)
        (setq l_pt
          (cons (vlax-get ename n) l_pt)
        )
      )
    )
    (foreach n l_pt
      (write-line n f_open)
    )
    (write-line "" f_open)
  )
  (close f_open)
  (prin1)
)

 

 

 

Message 4 of 14
3wood
in reply to: aqdam1978

Please also try attached file.

Message 5 of 14
aqdam1978
in reply to: pbejse

Hi my friend,

 

I just want to select text then program filter text with "/" symbole. if selected text has one "/" character in content then accepted it and finally sort all of this selected text and put them as a Text File with same name of drwaing file.

for example, if I open "MyCAD23.dwg" file and select texts , then LISP program creat "MyCAD23.CSV" file and put all of selected text into this CSV file line by line. (one line for one text)

 

Text1

Text2

...

Text100

 

Thank you so much

 

Message 6 of 14
Hallex
in reply to: aqdam1978

Try this code

(defun C:TCSV (/  textdata txt file fname)
  
(vl-load-com)

(command "_zoom" "_e")
(setq textdata
       (mapcar '(lambda(x)(cdr (assoc 1 x)))	
       (mapcar 'entget
	       (vl-remove-if
		 'listp
		 (mapcar 'cadr
			 (ssnamex (ssget "_X"
					 (list
					   (cons -4 "<and")
					   (cons 0 "TEXT")					   
					   (cons 1 "*/*")		
					   (cons 410 (getvar 'ctab))
					   (cons -4 "and>")
					   )
					 )
				  )
			 )
		 )
	       )
      )
)
  (command "_zoom" "_p")

  (if textdata
    (progn

  
  (setq fname (strcat (getvar "dwgprefix") (vl-filename-base (getvar "dwgname")) ".csv"))
  
   (if (setq file (open fname "w"))
        (progn
            (setq i 0)
            (foreach txt textdata

                (write-line (strcat (itoa (setq i (1+ i))) "\t" txt)  file)
            )
            (close file)
           )
        )

  (alert (strcat "Texts saved to file: \n" fname))
  
   )
    )
  (princ)
  )
(princ "\n\t\t---   Type TCSV to execute.    ---")
(prin1)

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 7 of 14
aqdam1978
in reply to: Hallex

Hi my friend,

Thank you for your help, But this is not exactly what I want!

May you customize it as:

1- did not add number before texts

2-prompt "select object(s)" and apply filter for selected objects.

3-before write to file, sorting them, for e.g. by vl-sort '< function

3-did not shows any  message or alert

 

Thank you so much in advance

 

 

Message 8 of 14
pbejse
in reply to: aqdam1978


@aqdam1978 wrote:

 

1- did not add number before texts

 


What do you mean by this?  say found string are /Banana and /Mango, what would be the result?

Banana1

Mango2

or

Banana 1?

Mango 2?

 


 

3-did not shows any  message or alert 


An alert or message informing the user the porgram is done processing?

 

 

 

 

 

Message 9 of 14
aqdam1978
in reply to: pbejse

Hi Pbejse,

in previous lisp program when I selected these text:

xc/56

45/cdv-f

ffg/hg67

ggh/gh-045

 

result is (filename.CSV):

1 xc/56

2 45/cdv-f

3 ffg/hg67

4 ggh/gh-045

 .....

but I want exactly text without prefix or suffix added. exactly these:

45/cdv-f

ffg/hg67

ggh/gh-045

xc/56

 

(Sorted result in CSV file) 

 

So, I don't like to added sequential numbers before texts!! I want texts in line by line!

 

I prefer output csv file opens by Notepad and don't like to see a message!

 

Thank you so much

 

 

Message 10 of 14
pbejse
in reply to: aqdam1978

a couple questions:

 

 "if selected text has one "/" character in content then accepted.."

don't include strings with two or more "/"?

 

"prompt "select object(s)" and apply filter for selected objects..."

If you'll be prompted to select text why the need for filter?

 

Why the need for CSV format?

 

Quickly written code

 

(defun c:w2csv (/ strs fname file i)
  (if (and (= (getvar 'DwgTitled) 1)
	   (setq strs nil
		 ss   (ssget (list '(0 . "TEXT") (cons 1 "*/*")))
	   )
	   (setq fname (strcat (getvar "dwgprefix")
			       (vl-filename-base (getvar "dwgname"))
			       ".csv"
		       )
	   )
	   (setq file (open fname "w"))
      )
    (progn
      (repeat (setq i (sslength ss))
	(setq strs
	       (cons
		 (cdr
		   (assoc 1
			  (entget (ssname ss (setq i (1- i))))
		   )
		 )
		 strs
	       )
	)
      )
      (foreach itm (vl-sort strs '<)
	(write-line itm file)
      )
      (close file)
      (startapp "Notepad" fname)
    )
  )
  (princ)
)

 

 

Message 11 of 14
aqdam1978
in reply to: pbejse

Hi, Pbejse,

 

 "if selected text has one "/" character in content then accepted.."

don't include strings with two or more "/"?

-> one or more "/" is acceptable. (at least one)

 

 

"prompt "select object(s)" and apply filter for selected objects..."

If you'll be prompted to select text why the need for filter?

-> because I need to export a part of texts inside drawings.

some of them have not "/" or even some of them are not text in my selection set.

 

 

Why the need for CSV format?

--> because I want to import this file into another software that it just can import CSV format.

 

Thank you for your time and consideration

 

 

 

Message 12 of 14
pbejse
in reply to: aqdam1978


@aqdam1978 wrote:

Hi, Pbejse,

 

Thank you for your time and consideration

 


Refer to my previous post aqdam for the test code and tell me what else is missing

 

 

Message 13 of 14
aqdam1978
in reply to: pbejse

Hi Pbejse,

 

as always, your program works well.

 

Thank you so much.

 

Message 14 of 14
MD81GM
in reply to: CADaSchtroumpf

Hi CADaStroumph,

 

This is an excellent tool, does exactly what I need.

 

Two questions however, the csv file puts a blank line between the data that is extracted, can this be deleted via the code, before the data is extracted?

 

And secondly, can the data be written to view horizontally not vertically in the csv file?

 

Thanks

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost