Results 1 to 6 of 6

Thread: Help with an aray of text to CSV file

  1. #1
    I could stop if I wanted to
    Join Date
    2001-10
    Location
    Defragging the internets.
    Posts
    297

    Default Help with an aray of text to CSV file

    I would like to create a lisp routine that can go convert a dwg schedule to a csv (or similar) file. I have attached a snapshot of the schedule. It looks like I will need to loop threw the code 21 times for each line of data. Each line should have the text value + followed with a tab (chr 9) if found or replaced with 2 tabs (chr 9) if no text is found. I have the distances between the text in autocad starting at the first column and the rows are 1"
    1", 1", 1", 10", 1", 1", 1", 2", 2", 2", 5", 2", 2", 2", 1", 1", 1", 1", 10", 1", 1"

    So far all I was able to do is a simple lisp to select a piece of text and output it to a file.
    Code:
    (setq DN (getvar "DWGNAME"))
    (setq DP (getvar "DWGPREFIX"))
    (substr DN 1 (- (strlen DN) 4)) 
    (setq filename (strcat (substr DN 1 (- (strlen DN) 4)) ".csv" ))
    (setq txt (car (entsel "Pick Text")))
    (if txt
    (progn
    (setq elist (entget txt)); entity list
    (setq IP (setq TM (strcat(cdr (assoc 1 elist))(chr 9)))); text
    ); progn
    ); if
    (setq file (open filename "w"))
    (write-line IP file)
    (close file)
    Even with the above code I don't know how to select from a specific location (IE: 1,2).

    If someone could help me get started I would appriciate it.

    Thanks,
    Chris
    Attached Images Attached Images

  2. #2
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043

    Default Re: Help with an aray of text to CSV file

    I don't have time to explain this, but it should get you close ( if not exact ) to what you want.

    Code:
    (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)
    )

  3. #3
    I could stop if I wanted to
    Join Date
    2001-10
    Location
    Defragging the internets.
    Posts
    297

    Default Re: Help with an aray of text to CSV file

    T. Willey,
    Thanks for the help, but the output was not exactly what I expected (see attached).

    I ran the lisp on the whole dwg and couldn't see where it was putting things. Then I numbered the first row so I could see what was going on. A single row from autocad is being spread out between five rows in the CSV file.

    I don't know how to work with the VLA code, so I can't follow what is going on. I think I would rather work with autolisp functions/code.

    As a start could you help me this part of the code?
    Code:
    (setq txt (car (entsel "Pick Text")))
    I need to use coordinates (a point or a crossing) with-out user input as some of the results that are 'nil would be replace with a tab or ",". I will also be adding other data locations that is not shown in my example.

    Thanks,
    Chris
    Attached Images Attached Images

  4. #4
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043

    Default Re: Help with an aray of text to CSV file

    I'm sorry Chris, but I don't have the time to rewrite the code for you to meet your specific needs, as the code works for mine already. If you can't use it, then forget it, and go a route you are comfortable with.

    (setq txt (car (entsel "Pick Text")))

    This will set the variable ' txt ' to the entity name of the entity selected. If you need something more specific, then I suggest you read the help files for those built in functions of AutoLisp.

  5. #5
    I could stop if I wanted to
    Join Date
    2001-10
    Location
    Defragging the internets.
    Posts
    297

    Default Re: Help with an aray of text to CSV file

    T.Willey

    Thanks for the help. I did use ExportTextTable to help with some of the work. It would be nice if the code could put in a fuzz factor for text that is justified middle or left. I think that would have solved some of the issues I had with it.

    In the end I got what I wanted useing:
    Code:
    (if (setq txtsel (ssget "_f" '((90 621)(168 621 )) '((0 . "TEXT") )))
    (cdr (assoc 1 (entget (ssname txtsel 0))))
    ""
    ); end if
    Thanks,
    Chris

  6. #6
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043

    Default Re: Help with an aray of text to CSV file

    I'm glad you were able to get something you could use Chris. You're welcome.

Similar Threads

  1. parametric aray troubles
    By kafka in forum Revit Architecture - Families
    Replies: 1
    Last Post: 2011-07-15, 10:16 PM
  2. Replies: 3
    Last Post: 2009-08-11, 12:25 PM
  3. Replies: 10
    Last Post: 2007-10-09, 01:11 PM
  4. Replies: 6
    Last Post: 2007-04-13, 09:38 PM
  5. Multiline Text Editor truncating imported text file
    By zellingj in forum AutoCAD General
    Replies: 3
    Last Post: 2006-12-06, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •