TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Matt__W on August 25, 2010, 04:41:09 PM

Title: Export text to Excel
Post by: Matt__W on August 25, 2010, 04:41:09 PM
I thought there was a thread (or two) about exporting text to Excel and based on the coordinates of the text it would put it in different cells rather than one tall column.  I found a few threads but they used VBA.  :\
Title: Re: Export text to Excel
Post by: T.Willey on August 25, 2010, 04:59:20 PM
Here is an old one I have that I think will do what you want.  Not sure though, as I haven't used it in awhile.

Code: [Select]
(defun c:ExportTextTable (/ tempPt XValList PtValList tempList tempStr EndList XValCnt StrCnt MaxCnt StrList Opened *error*)
   
    (defun *error* (msg)
   
        (if Opened (close Opened))
        (if msg (prompt (strcat "\n Error-> " msg)))
    )
    ;--------------------------------------------------------------
    (if (ssget '((0 . "TEXT")))
        (vlax-for obj (vla-get-ActiveSelectionSet (vla-get-ActiveDocument (vlax-get-Acad-Object)))
            (setq tempPt
                (vlax-get
                    obj
                    (if (equal (vla-get-Alignment obj) 0)
                        'InsertionPoint
                        'TextAlignmentPoint
                    )
                )
            )
            (if (not (vl-position T (mapcar '(lambda (x) (equal (car tempPt) x 0.00001)) XValList)))
                (setq XValList (cons (car tempPt) XValList))
            )
            (setq PtValList
                (cons
                    (cons
                        (vl-string-translate "," ";" (vla-get-TextString obj))
                        tempPt
                    )
                    PtValList
                )
            )
        )
    )
    (foreach lst PtValList
        (if (setq tempList (assoc (setq tempStr (rtos (caddr lst) 2 10)) EndList))
            (setq EndList
                (subst
                    (list
                        tempStr
                        (cons lst (cadr tempList))
                    )
                    tempList
                    EndList
                )
            )
            (setq EndList (cons (list tempStr (list lst)) EndList))
        )
    )
    (if EndList
        (progn
            (setq EndList
                (vl-sort
                    EndList
                    '(lambda (a b)
                        (> (distof (car a)) (distof (car b)))
                    )
                )
            )
            (setq XValList (vl-sort XValList '<))
            (foreach lst EndList
                (setq lst
                    (vl-sort
                        (cadr lst)
                        '(lambda (a b)
                            (< (cadr a) (cadr b))
                        )
                    )
                )
                (setq XValCnt 0)
                (setq StrCnt 0)
                (setq MaxCnt (length XValList))
                (setq tempStr "")
                (repeat (length lst)
                    (while (not (equal (cadr (nth StrCnt lst)) (nth XValCnt XValList) 0.0000001))
                        (setq tempStr (strcat tempStr ","))
                        (setq XValCnt (1+ XValCnt))
                    )
                    (setq tempStr (strcat tempStr (car (nth StrCnt lst))))
                    (if (< StrCnt MaxCnt)
                        (setq tempStr (strcat tempStr ","))
                    )
                    (setq StrCnt (1+ StrCnt))
                    (setq XValCnt (1+ XValCnt))
                )
                (setq StrList (cons tempStr StrList))
            )
            (setq Opened (open "c:/test/ExportedText.csv" "a"))
            (foreach str (reverse StrList)
                (write-line str Opened)
            )
        )
    )
    (*error* nil)
    (princ)
)
Title: Re: Export text to Excel
Post by: Matt__W on August 26, 2010, 08:09:26 AM
I'll have a look.  Thanks!
Title: Re: Export text to Excel
Post by: fixo on August 26, 2010, 09:50:27 AM
Check this thread as well
http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Create-table-from-plain-geometry-text/td-p/2755529

Oleg