From: Darius Blaszijk
Subject: problems exporting data to file
Date: 
Message-ID: <40efe058$0$2745$fb624cd1@morenews.zeelandnet.nl>
Hello,

I'm writing an export macro in autocad. What I want to do is to export
certain data for all lines in a perticular layer. The filtering works fine,
but I have problems of writing the data to file. The problem is that nothing
happens. The file is created, but is always zero bytes. What's going wrong.
Another problem I have is when I uncomment alle the serialization code, I
get the message

"error: too many arguments: (IF (= "LINE" (CDR (A <snip> .. <snip>
"),Color(16711680)") (PRINT F))"

The funny thing is that when I comment about 80% there is no error reported.
But it doesn't matter which lines I comment. So it seems that there is too
many code in the IF statement??

Anyway, I hope I'm at the right address here.

Kind regards, Darius Blaszijk

(defun exportdata()

  ;open file for output
  (setq f (open "E:/Develop/temp.dat" "w"))

  ;get all entities from database
  (setq a (ssget))

  ;loop counter
  (setq b 0)

  ;loop through all entities
  (repeat (sslength a)

    ;if layer is PARTS then process entities
    (if (= "PARTS" (cdr (assoc 8 (entget (ssname a b)))))

      ;if entity type is LINE then process entities
      (if (= "LINE" (cdr (assoc 0 (entget (ssname a b)))))

 ;next the format the serialization shouls look like
 ;LINE = X1(10),Y1(10),X2(20),Y2(20),Color(100)
 ;process line data

 ;(prompt "LINE = X1(" f)
 (setq x1 (car (cdr (assoc 10 (entget (ssname a b))))))
 (prompt x1 f)
 ;(prompt "),Y1(" f)
 ;(setq y1 (cadr(cdr (assoc 10 (entget (ssname a b))))))
 ;(prompt y1 f)
 ;(prompt "),X2(" f)
 ;(setq x2 (car (cdr (assoc 11 (entget (ssname a b))))))
 ;(prompt x2 f)
 ;(prompt "),Y2(" f)
 ;(setq y2 (cadr(cdr (assoc 11 (entget (ssname a b))))))
 ;(prompt y2 f)
 ;(setq color "),Color(16711680)")
 ;(print f)
      )
    )
    ; increase loop counter
    (setq b (+ b 1))
  )
  (close f)
)

From: Frank Buss
Subject: Re: problems exporting data to file
Date: 
Message-ID: <ccopre$ivm$1@newsreader2.netcologne.de>
"Darius Blaszijk" <···········@zeelandnet.nl> wrote:

> The problem is
> that nothing happens. The file is created, but is always zero bytes.
> What's going wrong.

I don't know AutoCAD Lisp, but at least in Common Lisp (CL) you can use 
write-line, princ etc. for writing to a file.

> Another problem I have is when I uncomment alle
> the serialization code, I get the message
> 
> "error: too many arguments: (IF (= "LINE" (CDR (A <snip> .. <snip>
> "),Color(16711680)") (PRINT F))"

In CL the special operator "if" has the syntax "if test-form then-form 
[else-form] => result*" 
(http://www.lisp.org/HyperSpec/Body/speope_if.html). If you want to 
evaluate more than one form for the then-branch, then you can use progn:

(if t
    (progn
      (princ 1)
      (princ 2)
      (princ 3))
    (princ 4))

This prints "123".

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Darius Blaszijk
Subject: Re: problems exporting data to file
Date: 
Message-ID: <40f02150$0$2728$fb624cd1@morenews.zeelandnet.nl>
Frank,

I switched to princ and use progn. It works!!

Thank you very much.

Kind regards, Darius Blaszijk

"Frank Buss" <··@frank-buss.de> schreef in bericht
·················@newsreader2.netcologne.de...
> "Darius Blaszijk" <···········@zeelandnet.nl> wrote:
>
> > The problem is
> > that nothing happens. The file is created, but is always zero bytes.
> > What's going wrong.
>
> I don't know AutoCAD Lisp, but at least in Common Lisp (CL) you can use
> write-line, princ etc. for writing to a file.
>
> > Another problem I have is when I uncomment alle
> > the serialization code, I get the message
> >
> > "error: too many arguments: (IF (= "LINE" (CDR (A <snip> .. <snip>
> > "),Color(16711680)") (PRINT F))"
>
> In CL the special operator "if" has the syntax "if test-form then-form
> [else-form] => result*"
> (http://www.lisp.org/HyperSpec/Body/speope_if.html). If you want to
> evaluate more than one form for the then-branch, then you can use progn:
>
> (if t
>     (progn
>       (princ 1)
>       (princ 2)
>       (princ 3))
>     (princ 4))
>
> This prints "123".
>
> --
> Frank Bu�, ··@frank-buss.de
> http://www.frank-buss.de, http://www.it4-systems.de
From: Frank Buss
Subject: Re: problems exporting data to file
Date: 
Message-ID: <ccp6p4$ikg$1@newsreader2.netcologne.de>
"Darius Blaszijk" <···········@zeelandnet.nl> wrote:

> (defun exportdata()
> 
>   ;open file for output
>   (setq f (open "E:/Develop/temp.dat" "w"))
> 
>   ;get all entities from database
>   (setq a (ssget))
> 
>   ;loop counter
>   (setq b 0)
> 
>   ;loop through all entities
>   (repeat (sslength a)
> 
>     ;if layer is PARTS then process entities
>     (if (= "PARTS" (cdr (assoc 8 (entget (ssname a b)))))
> 
>       ;if entity type is LINE then process entities
>       (if (= "LINE" (cdr (assoc 0 (entget (ssname a b)))))
> 
>  ;next the format the serialization shouls look like
>  ;LINE = X1(10),Y1(10),X2(20),Y2(20),Color(100)
>  ;process line data
> 
>  ;(prompt "LINE = X1(" f)
>  (setq x1 (car (cdr (assoc 10 (entget (ssname a b))))))
>  (prompt x1 f)
>  ;(prompt "),Y1(" f)
>  ;(setq y1 (cadr(cdr (assoc 10 (entget (ssname a b))))))
>  ;(prompt y1 f)
>  ;(prompt "),X2(" f)
>  ;(setq x2 (car (cdr (assoc 11 (entget (ssname a b))))))
>  ;(prompt x2 f)
>  ;(prompt "),Y2(" f)
>  ;(setq y2 (cadr(cdr (assoc 11 (entget (ssname a b))))))
>  ;(prompt y2 f)
>  ;(setq color "),Color(16711680)")
>  ;(print f)
>       )
>     )
>     ; increase loop counter
>     (setq b (+ b 1))
>   )
>   (close f)
> )

Ok, I try to re-write it in CL. For this I need some functions and 
macros, because they are not defined in CL. The AutoCAD repeat-macro     
looks like it can be defined like this:

(defmacro repeat (count form) `(loop repeat ,count do ,form))

It's my first macro, so perhaps there might be some side-effects by using 
it, but it works. It would be easier, if AutoCAD lisp has the loop macro, 
because then you could write:

(loop for b from 0 to (1- (sslength a)) do (your code))

Accessing the name could be still more simply, if "ssget" returns a list 
or vector.

Some other definitions which I assume:

(defun ssname (selection index) (nth index selection))
(defun sslength (selection) (length selection))
(defun entget (name) (gethash name *entities*))
(defun ssget () '("line1" "line3"))

and some example data I assume:

(defvar *entities* (make-hash-table :test 'equal))

(setf (gethash "line1" *entities*)
      '((0 . "LINE")
       (8 . "PARTS")
       (10 1 2 3 4)
       (11 5 6 7 8)))

(setf (gethash "line2" *entities*)
      '((0 . "LINE")
       (8 . "NO_PART")
       (10 9 10 11 12)
       (11 13 14 15 16)))

(setf (gethash "line3" *entities*)
      '((0 . "LINE")
       (8 . "PARTS")
       (10 17 18 19 20)
       (11 21 22 23 24)))

now I can write the "exportdata" function (substituting "=" with 
"string=" and using "with-open-file" instead of "open")

(defun exportdata ()
  (with-open-file (f "c:/tmp/test.txt" :direction :output)
    (let ((a (ssget)) (b 0))
      (repeat (sslength a)
              (let* ((entity (entget (ssname a b)))
                     (layer (cdr (assoc 8 entity)))
                     (type (cdr (assoc 0 entity))))
                (if (and (string= layer "PARTS") (string= type "LINE"))
                    (let* ((line-start (cdr (assoc 10 entity)))
                           (line-end (cdr (assoc 11 entity)))
                           (x1 (nth 0 line-start))
                           (y1 (nth 1 line-start))
                           (x2 (nth 2 line-end))
                           (y2 (nth 3 line-end)))
                      (format f (concatenate 'string
                                             "LINE = X1(~F),Y1(~F),"
                                             "X2(~F),Y2(~F),"
                                             "Color(16711680)~%"
                                             "process line data~%")
                              x1 y1 x2 y2)
                      (setq b (+ b 1)))))))))

If this doesn't help you, you can try to post your question in 
comp.cad.autocad again.

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de