Lisp routine

I've taken some code from an existing lisp routine and saved it in to a small lisp routine (See below). It is supposed to let the user select a number of point entities and then save the X,Y,Z data in an Ascii CSV file. (Command: WPOINT)This routine works in Autocad2002 but not in BricsCad (6.1.0006) My knowledge of Lisp is small.Can anybody explain to me why this is not working?T.i.a. Arno van Eeuwen(defun write-point (e fd prec / p) (setq p (cdr (assoc 10 (entget e)))) (write-line (strcat (rtos (car p) 2 prec) "," (rtos (cadr p) 2 prec) "," (rtos (caddr p) 2 prec) ) fd ))(defun C:WPOINT ( / ss fd file) (cond ( (not (setq ss (ssget '((0 . "POINT")))))) ( (not (setq file (getfiled "Export Points" "" "csv" 1)))) ( (not (setq fd (open file "w"))) (alert "Unable to open file for output")) (t (repeat (setq i (sslength ss)) (write-point (ssname ss (setq i (1- i))) fd 6 ) ) (close fd) ) ) (princ))

Comments

  • You can try following lisp, but I found write-line, print, princ and prin1 to file is don't work in BricsCad.(defun C:WPOINT (/ ss fd file) (if (and (setq ss (ssget (list (cons 0 "POINT")))) (setq file (getfiled "Export Points" "" "csv" 1)) (if (not (setq fd (open file "w"))) (alert "Unable to open file for output") T ) ) (repeat (setq i (sslength ss)) (write-point (ssname ss (setq i (1- i))) fd 6) ) (close fd) ) (princ))

  • That does the trick!Thank you very much.

  • Hmm, one small question:It seems that the (CLOSE FD) command doesn't close the file:all selected points are writen to the file only after closing BricsCad. The CSV file is not accessable while BricsCad (or the Lisp routine?) is still running. Is there a way to force a correct closure of the CSV file?

  • the above routine works in the new release of Bricscad. In the older versions it works crooked. The problem is with the 'cond' clause.There was a bug causing the conditions to be evaluated twice instead of once. A condition like "(=a 10)" doesn't mind being evaluated twice but if you do (= (setq a (+ a 1)) 10) then the counter increases twice as fast as expected. In the same way, if the condition includes an ssget or a getfiled, and it gets called twice, then the last call provides the values for ss , file and fd that are used to evaluate the conditions.This also explains why fd was not always closed: fd did not always point to the open file.I don't understand the remark about print to file not working for prin1, princ and print. It works. There was a minor bug that princ and prin1 added an extra space , (princ "a")(princ "b")(princ "c") resulted in "a b c " , but that is fixed too. Now the output is "abc".

  • Thanks Alexander,I just saw your release 6.2.0006 notes.

  • Alexander,I tested the LispRoutine as shown in my first message of this thread in 6.2.0006 and is works as you explained; the file gets closed correctly so all points are exported while BricsCad remains running. However, the routine as sent by Shun-Hsiang Hsiao still won't complete unless BricsCad 6.2.0006 is closed. What can be the reason for that?

This discussion has been closed.