PDA

View Full Version : Help with an aray of text to CSV file



framedNlv
2009-03-25, 06:30 PM
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.

(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

T.Willey
2009-03-25, 06:47 PM
I don't have time to explain this, but it should get you close ( if not exact ) to what you want.



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

framedNlv
2009-03-26, 03:27 PM
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?

(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

T.Willey
2009-03-26, 05:16 PM
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.

framedNlv
2009-04-02, 10:18 PM
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:

(if (setq txtsel (ssget "_f" '((90 621)(168 621 )) '((0 . "TEXT") )))
(cdr (assoc 1 (entget (ssname txtsel 0))))
""
); end if

Thanks,
Chris

T.Willey
2009-04-03, 03:40 PM
I'm glad you were able to get something you could use Chris. You're welcome.