Need a lisp for export text to csv

Need a lisp for export text to csv

kajanthangavel
Advocate Advocate
5,360 Views
9 Replies
Message 1 of 10

Need a lisp for export text to csv

kajanthangavel
Advocate
Advocate

I want a lisp for export text to csv like this

Screenshot.jpg

I want csv like this format. (I need yellow highlighted values only) . every row values will be put side by side.

 

Note: CSV title (row 1 & 2)  and Chainage (Column A) not necessary

I attach sample DWG and CSV.

Someone help me.

Thanks.

0 Likes
Accepted solutions (2)
5,361 Views
9 Replies
Replies (9)
Message 2 of 10

pbejse
Mentor
Mentor

Answer this for us first: Just my observation if you may,

 

Has the company considered using attributes to store the values? If its not tool late, its wise to convert it now if the company you're working for will continue with that format, easier to extract the values with Data Extraction.

 

If they like the idea, and this is going to be an continuing thing, might as well do the conversion now. If you like we can start with that.

 

 

0 Likes
Message 3 of 10

pbejse
Mentor
Mentor
Accepted solution

Have time to kill.

 

 

(Defun c:Text2CSVAgain  ( /  CollectedDAta csvFile values Collected row opf)
;;;	[	pBe Jun 2020	     	;;;
(and (= 1 (getvar 'Dwgtitled))
     (setq CollectedDAta nil
                csvFile (strcat (getvar 'dwgprefix)
				(vl-filename-base (getvar 'dwgname)) ".csv"))
     (progn
	(while (and
                     (princ "Select LHS and RHS columns per sheet")
                     (setq values (ssget '((0 . "TEXT")))))
	      (if (zerop (rem (setq i (sslength values)) 2))
	          (progn               	                 
			(setq Collected (mapcar '(lambda (d) (setq ent (entget d))
			                    (list (cdr (assoc 10 ent))
                                                  	(strcat "," (cdr (assoc 1 ent)))))
			                         (vl-remove-if 'listp
			                               (mapcar 'cadr (ssnamex values)))
			                         )
			              )
			 (setq Row (mapcar 'cadr
	                                  (vl-sort
	                                      Collected '(lambda (a b)
	                          		(cond
	                                              ((> (cadar a) (cadar b)))
	                                              ((equal (cadar a) (cadar b)  0.1)
	                                                    (< (caar a) (caar b))
	                                                    )
	                                               )
	                                            )
	                            		)
	                                  )
	                       )
                        )
                  )
              	(setq collectedData (cons row collectedData))
                (prompt "\nPress Enter to end or ")
              )
	        collectedData
	        )
		(Setq opf (open csvFile "w"))
	        (foreach rowValue collectedData
	              (write-line  (apply 'strcat rowValue) opf)
	              )
		(not (close opf))
	     	(startapp "notepad" csvFile)
	)
      (princ)
      )

 

 

command: Text2CSVAgain 

Select LHS and RHS columns per sheet
Select objects:

t2ca.png

 

 

 

 

Specify opposite corner: 36 found

Select objects:  [ Enter  to accept selection ]


Press Enter to end or Select LHS and RHS columns per sheet
Select objects: Specify opposite corner: 36 found

 

 

HTH

 

BTW: I assure you, it will a lot easier if you are using attributes the user just needs to select the title block to gather the station number and data.

 

I also left the first column as blank.

Message 4 of 10

roland.r71
Collaborator
Collaborator

A more sophisticated way to create csv's 😎

 

 

 

; --- writeCSV ----------------------------------------------------------------
   (defun writeCSV ( csvfile dblist dlm / file_w lines text record val)

      ; function to write .csv file: By Roland.R71
      ; Follows csv standards, as far as there are any.

      ; csvfile = path+filename to use. example: c:\\temp\\mydata.csv
      ; dblist  = a 'database' list of values. (a list of lists)
      ; dlm     = the delimiter to use. example: , ; \t (tab) etc.
      ; Function checks for delimiter inside values, adds quotes if found.
      ;
      ; Example code:
      ; (setq lst (list '("1" "2" "3") '("4" "5,1" "6") '("7" "8.1" "9") '("" "" "")))
      ; (writeCSV "c:/temp/test.csv" lst ",")
      ;
      ; example csv file:
      ; 1,2,3
      ; 4,"5,1",6
      ; 7,8.1,9
      ; ,,

      (setq file_w (open csvfile "w"))
      (foreach record dblist
         (setq i 0 text "")
         (while (< i (length record))
            (setq val  (cond ((nth i record))(""))
                  val  (cond ((vl-string-search dlm val)(strcat "\"" val "\""))(val))
                  text (strcat text val)
                  i    (1+ i)
            )
            (if (< i (length record))
               (setq text (strcat text dlm))
            )
         )
         (write-line text file_w)
      )
      (close file_w)
   )

 

 

Message 5 of 10

kajanthangavel
Advocate
Advocate

Our company not will provide any offer. I trying to finish easily.

Thank you.

0 Likes
Message 6 of 10

pbejse
Mentor
Mentor

@kajanthangavel wrote:

Our company not will provide any offer. I trying to finish easily.

Thank you.


What offer? I think you misunderstood  my message @kajanthangavel . I'm merely making a suggestion on how the company  should prepare their drawing sheets. Instead of plain TEXT for the LHS and RHS values, use ATTRIBUTES. The company will be doing the conversion in-house. That way its easier to extract the target values onto an excel sheet.

 

I did provide a code to export the KHS/RHS values onto a csv file right after my 'observation" message. All i'm saying The process could be a LOT easier if its not setup the way it is now.

 

Hope that it is clear now.

 

0 Likes
Message 7 of 10

kajanthangavel
Advocate
Advocate

Wow.. Working well. but small problem. it write lines bottom to top.

thanks @pbejse 

0 Likes
Message 8 of 10

kajanthangavel
Advocate
Advocate

Now Understood.

but I completed more than 3000 drawings. I can't change as ATTRIBUTES.

thank you for your advice.

Thanks @pbejse 

0 Likes
Message 9 of 10

pbejse
Mentor
Mentor
Accepted solution

@kajanthangavel wrote:

Wow.. Working well. 

thanks @pbejse 


Glad you were able to figure out how it works

 


@kajanthangavel wrote:

but small problem. it write lines bottom to top.


Easy. replace this 

(foreach rowValue collectedData
    (write-line  (apply 'strcat rowValue) opf)
	)

with

(foreach rowValue (reverse collectedData)
    (write-line  (apply 'strcat rowValue) opf)
	)

HTH

 

Message 10 of 10

Sea-Haven
Mentor
Mentor

Like you Pbe but I would suggest learn about using a table, the format lends itself to one.

 

Yes I have pit schedules done as 1 line multi atts blocks, and the smarts to update pulling answers from all over the dwg model layouts etc the advantage of it being a block.

 

Re original poster to many exist to update could be redone as a table as long as the pattern is the same, a custom lisp. Or as block with attributes. Only update when opened for some reason.

0 Likes