PDA

View Full Version : list-write to file??



stoobydale
6th Jun 2005, 04:19 pm
Hi, Is it possible to write a list to a file? I am trying to write a programme (for college course) that will get all the start point and endpoint info from a drawing on the screen and will then write that info to a file. I have managed to get it to select all the entities and pick out the info I need, put it into a list but now I can't get it to write that info to a file. Here is a copy of the programme so far. (a bit long winded I know but I'm only a novice :oops: ) any help would be much appreciated. Thanks.

(defun c:ssaa ()
(setq ss1 (ssget))
(setq ent1 (ssname ss1 0))
(setq ent2 (ssname ss1 1))
(setq ent3 (ssname ss1 2))
(setq ent4 (ssname ss1 3))
(setq ent5 (ssname ss1 4))
(setq ent6 (ssname ss1 5))
(setq ent7 (ssname ss1 6))
(setq ent8 (ssname ss1 7))
(princ "the first entity is: ")(prin1 ent1)
(princ "\nthe second entity is: ")(prin1 ent2)
(princ "\nthe third entity is: ")(prin1 ent3)
(princ "\nthe fourth entity is: ")(prin1 ent4)
(princ "\nthe fifth entity is: ")(prin1 ent5)
(princ "\nthe sixth entity is: ")(prin1 ent6)
(princ "\nthe seventh entity is: ")(prin1 ent7)
(princ "\nthe eighth entity is: " )(prin1 ent8)
(princ)
(princ "\n")
(setq ent9 (entget ent1))
(setq ent10 (entget ent2))
(setq ent11 (entget ent3))
(setq ent12 (entget ent4))
(setq ent13 (entget ent5))
(setq ent14 (entget ent6))
(setq ent15 (entget ent7))
(setq ent16 (entget ent8))
(princ)
(setq item1 (list (cdr (assoc 0 ent9))(list (nth 1(assoc 10 ent9))(nth 2(assoc 10 ent9)))(list (nth 1(assoc 11 ent9))(nth 2(assoc 11 ent9)))))
(setq item2 (list (cdr (assoc 0 ent10))(list (nth 1(assoc 10 ent10))(nth 2(assoc 10 ent10)))(list (nth 1(assoc 11 ent10))(nth 2(assoc 11 ent10)))))
(setq item3 (list (cdr (assoc 0 ent11))(list (nth 1(assoc 10 ent11))(nth 2(assoc 10 ent11)))(list (nth 1(assoc 11 ent11))(nth 2(assoc 11 ent11)))))
(setq item4 (list (cdr (assoc 0 ent12))(list (nth 1(assoc 10 ent12))(nth 2(assoc 10 ent12)))(list (nth 1(assoc 11 ent12))(nth 2(assoc 11 ent12)))))
(setq item5 (list (cdr (assoc 0 ent13))(list (nth 1(assoc 10 ent13))(nth 2(assoc 10 ent13))) (cdr (assoc 40 ent13))))
(setq item6 (list (cdr (assoc 0 ent14))(list (nth 1(assoc 10 ent14))(nth 2(assoc 10 ent14)))(list (nth 1(assoc 11 ent14))(nth 2(assoc 11 ent14)))))
(setq item7 (list (cdr (assoc 0 ent15))(list (nth 1(assoc 10 ent15))(nth 2(assoc 10 ent15)))(list (nth 1(assoc 11 ent15))(nth 2(assoc 11 ent15)))))
(setq item8 (list (cdr (assoc 0 ent16))(list (nth 1(assoc 10 ent16))(nth 2(assoc 10 ent16)))(list (nth 1(assoc 11 ent16))(nth 2(assoc 11 ent16)))))
(princ)
;(setq fd (open "G:/prog5b.dat" "w")
;(write-line (item1) fd)
;(close fd)
;(print)
;)

)

Cheers,
Stu.

David Bethel
6th Jun 2005, 05:47 pm
Instead of


(write-line item1 fd)

Try:


(prin1 item1 fd)
(write-line "" fd)


(item1) and item1 are 2 totally different things The parenthesis instruct the processor to evaluate the statement. The lack of tells it to look up the variable's value. -David

stoobydale
6th Jun 2005, 10:25 pm
Cheers Dave, thanks for the reply. I had the right answer before I tried the "write-line" command but hadn't spotted that I had missed a ")". Here is my original answer.

(setq fd (open "G:/hnd lisps/prog5b.dat" "w"));opens file
(princ item1 fd)(princ "\n" fd);prints relevant info and moves to next line
(princ item2 fd)(princ "\n" fd)
(princ item3 fd)(princ "\n" fd)
(princ item4 fd)(princ "\n" fd)
(princ item5 fd)(princ "\n" fd)
(princ item6 fd)(princ "\n" fd)
(princ item7 fd)(princ "\n" fd)
(princ item8 fd)(princ "\n" fd)
(close fd);close file
(print)
)

probably not the prettiest bit of lisp, but it does the job :?

Cheers.

fuccaro
7th Jun 2005, 07:06 am
Stoobydale
You may wish to see this lisp routine too:
http://cadtutor.net/forum/viewtopic.php?t=1785&