Exporting Text to excel

Exporting Text to excel

Anonymous
Not applicable
6,899 Views
6 Replies
Message 1 of 7

Exporting Text to excel

Anonymous
Not applicable

Hi All,

 

I have a little problem with modifying some code I found on Lee Mac's website.  I hope someone on here well versed on autocad programming can help me out.  Are there any ways to modify the code so that it lists the texts/mtxt/attr in an order from left to right as it appears in the drawing (also adding an x,y coordinate next to each text)? Also, are there ways to add a condition to only list texts/mtxt/attr that has 3 decimal places?

 

;; Text 2 CSV  -  Lee Mac
;; Writes all Text, MText & Attribute content from all layouts and within
;; all blocks and nested blocks to a selected CSV file.

(defun c:txt2csv ( / data file )
    (cond
        (   (not
                (progn
                    (vlax-for block (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                        (if (eq :vlax-false (vla-get-isxref block))
                            (vlax-for obj block
                                (cond
                                    (   (wcmatch (vla-get-objectname obj) "AcDb*Text")
                                        (setq data (cons (vla-get-textstring obj) data))
                                    )
                                    (   (and
                                            (eq "AcDbBlockReference" (vla-get-objectname obj))
                                            (eq :vlax-true (vla-get-hasattributes obj))
                                        )
                                        (foreach att (vlax-invoke obj 'getattributes)
                                            (setq data (cons (vla-get-textstring att) data))
                                        )
                                    )
                                )
                            )
                        )
                    )
                    data
                )
            )
            (princ "\nNo Text, MText or Attributes found.")
        )
        (   (not (setq file (getfiled "Create CSV file" "" "csv" 1)))
            (princ "\n*Cancel*")
        )
        (   (setq file (open file "w"))
            (foreach x data (write-line x file))
            (setq file (close file))
            (princ (strcat "\n" (itoa (length data)) " strings written to file."))
        )
        (   (princ "\nUnable to open CSV file for writing."))
    )
    (princ)
)
(vl-load-com) (princ)

Thanks you for reading!

0 Likes
6,900 Views
6 Replies
Replies (6)
Message 2 of 7

TheCADnoob
Mentor
Mentor

your question may be better fielded in the section which commonly deals with lisps here : http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/bd-p/130

 

Im not sure listing the elements in the order as they are seen in the drawing is going to be that simple, but as i wount be able to get what you need ill let those who are in the know make that determination.

 

Also have you tried using DATAEXTRACTION

CADnoob

EESignature

0 Likes
Message 3 of 7

3wood
Advisor
Advisor

To convert texts into an Excel file, you can try TEXT2CSV.

Message 4 of 7

sameerulcdc
Contributor
Contributor

Hi 
can this lsp modify to user selection option particular area   

Thank you 

0 Likes
Message 5 of 7

ec-cad
Collaborator
Collaborator

Very 'old' thread here, but attached Lisp will extract Attributes, Mtext & Text to a .csv file called

Text_to_csv.csv in the current folder. It (doesn't Unformat the Mtext), you can find that Lisp here.

Will allow Window / Crossing pick of what you want to export.

 

ECCAD

Message 6 of 7

Sea-Haven
Mentor
Mentor

One of the requests was do a sort on the objects so they are bottom left sorted. With X & Y. You can do a sort on X & Y. So would make a list then sort it, then say write the csv, so would have 3 lists Atts, Mtext & Text. 

 

 

; sorts on 1st two items
(vl-sort lst
	 '(lambda (a b)
	    (cond
	      ((< (car a) (car b)))
	      ((= (car a) (car b)) (< (cadr a) (cadr b)))
	    )
	  )
)

 

An example but depends on how you make the list, expects X & Y as items 1 and 2. 

 

For me I would write direct to Excel and remove need for a csv step. Your welcome to use "Alan excel library.lsp", if a table is wanted also can either make a table then use a lisp I have, Export table to excel, or do both in one code set.

 

You are right about remove Mtext formatting also. Multi line required single cell in Excel ?

 

Really need a sample dwg from Anonymous with a required resulting Excel, before doing any code.

Message 7 of 7

Sea-Haven
Mentor
Mentor

"Add an option to restrict to 3 decimal places."

Are you talking about the X & Y values of the text then just use (rtos x 2 3) (rtos Y 2 3) I won't go into padding for moment.

 

 

 

0 Likes