2 exec guile -l parse-gerber.scm -e main -s "$0" "$@"
5 ; gEDA - GNU Electronic Design Automation
7 ; Copyright (C) 2000-2001 Stefan Petersen (spe@stacken.kth.se)
11 ; This program is free software; you can redistribute it and/or modify
12 ; it under the terms of the GNU General Public License as published by
13 ; the Free Software Foundation; either version 2 of the License, or
14 ; (at your option) any later version.
16 ; This program is distributed in the hope that it will be useful,
17 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ; GNU General Public License for more details.
21 ; You should have received a copy of the GNU General Public License
22 ; along with this program; if not, write to the Free Software
23 ; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
26 (define *last-aperture-type* '())
30 (define (ps-preamble port)
32 (display "%generated by gerbv\n\n" port)
33 (display "/inch {72 mul} def\n" port)
34 (display "/mm {2.54 div} def\n" port)
36 (display "1.5 inch 3 inch translate\n" port)
38 (if (equal? (assoc-ref *image* 'polarity) 'positive)
40 (display "/Black {0 setgray} def\n" port)
41 (display "/White {1 setgray} def\n" port))
43 (display "/Black {1 setgray} def % Polarity reversed\n" port)
44 (display "/White {0 setgray} def\n" port)))
46 (display "/circle { % x y id od\n" port)
47 (display " gsave\n" port)
48 (display " 3 index 3 index moveto\n" port)
49 (display " 3 index 3 index 3 2 roll % Fix arguments\n" port)
50 (display " 2 div % d given, need r\n" port)
51 (display " 0 360 arc Black fill % outer\n" port)
52 (display " 2 div % d given, need r\n" port)
53 (display " 0 360 arc White fill %inner\n" port)
54 (display "grestore\n" port)
55 (display "} def\n" port)
57 (display "/rectangle { % x y xl yl\n" port)
58 (display " gsave\n" port)
59 (display " newpath\n" port)
60 (display " 1 setlinewidth\n" port)
61 (display " 3 index 2 index 2 div sub\n" port)
62 (display " 3 index 2 index 2 div add moveto\n" port)
63 (display " 1 index 0 rlineto\n" port) ; ->
64 (display " dup -1 mul 0 exch rlineto\n" port) ; \!/
65 (display " 1 index -1 mul 0 rlineto\n" port) ; <-
66 (display " dup 0 exch rlineto\n" port) ; /!\
67 (display " pop pop pop pop closepath Black fill\n" port)
68 (display " grestore\n" port)
69 (display "} def\n" port)
71 (set! *max-x* (+ *max-x* 100))
72 (set! *max-y* (+ *max-y* 100))
73 (set! *min-x* (- *min-x* 100))
74 (set! *min-y* (- *min-y* 100))
75 (display "gsave 72 setlinewidth newpath\n" port)
76 (display (string-append (number->string *max-x*) " 1000 div inch ") port)
77 (display (string-append (number->string *max-y*) " 1000 div inch moveto\n") port)
78 (display (string-append (number->string *max-x*) " 1000 div inch ") port)
79 (display (string-append (number->string *min-y*) " 1000 div inch lineto\n") port)
80 (display (string-append (number->string *min-x*) " 1000 div inch ") port)
81 (display (string-append (number->string *min-y*) " 1000 div inch lineto\n") port)
82 (display (string-append (number->string *min-x*) " 1000 div inch ") port)
83 (display (string-append (number->string *max-y*) " 1000 div inch lineto\n") port)
84 (display "closepath White fill grestore\n" port)
87 (define (print-ps-element element port)
88 (let ((x (car element))
90 (aperture-type (car (caddr element)))
91 (aperture-state (cdr (caddr element))))
92 (cond ((eq? aperture-state 'exposure-off)
93 (handle-line-aperture aperture-type port)
94 (print-position x y port)
95 (display "moveto\n" port))
96 ((eq? aperture-state 'exposure-on)
97 (handle-line-aperture aperture-type port)
98 (print-position x y port)
99 (display "lineto\n" port))
100 ((eq? aperture-state 'exposure-flash)
101 (print-position x y port)
102 (print-flash-aperture aperture-type port)))
106 (define (print-position x y port)
107 (display x port) ; X axis
108 (display " 1000 div " port)
109 (display (assoc-ref *image* 'unit) port)
111 (display y port) ; Y axis
112 (display " 1000 div " port)
113 (display (assoc-ref *image* 'unit) port)
116 (define (handle-line-aperture aperture-type port)
117 (cond ((null? *last-aperture-type*) ; First time
118 (set! *last-aperture-type* aperture-type)
119 (display (get-aperture-size aperture-type) port)
120 (display " inch setlinewidth\n" port))
121 ((not (eq? *last-aperture-type* aperture-type)) ; new aperture
122 (display "stroke\n" port)
123 (display *last-x* port) ; X Axis
124 (display " 1000 div " port)
125 (display (assoc-ref *image* 'unit) port)
127 (display *last-y* port)
128 (display " 1000 div " port)
129 (display (assoc-ref *image* 'unit) port)
130 (display " moveto\n" port)
131 (display (get-aperture-size aperture-type) port)
132 (display " inch setlinewidth\n" port)
133 (set! *last-aperture-type* aperture-type))))
135 (define (print-flash-aperture aperture-type port)
136 (let* ((aperture-description (assv aperture-type *aperture-description*))
137 (symbol (cadr aperture-description))
138 (sizes (cddr aperture-description)))
142 (display (car sizes) port)
143 (display " inch " port))
145 (display (car sizes) port)
146 (display " inch " port)
147 (display (cadr sizes) port)
148 (display " inch " port)))
151 (display " circle" port))
153 ; (display " pop pop moveto " port))
154 (display " rectangle " port))
156 (display " moveto %unknown" port))))
159 (define (get-aperture-size type)
160 (let ((desc (assv type *aperture-description*)))
165 (define (generate-ps netlist port)
167 (for-each (lambda (element)
168 (print-ps-element element port))
170 (display "stroke\nshowpage\n" port))
174 (define *min-x* 30000)
175 (define *min-y* 30000)
177 (define (find-boundaries netlist)
180 (let ((x (string->number (caar netlist)))
181 (y (string->number (cadar netlist))))
190 (find-boundaries (cdr netlist)))))
193 (let ((infile 'stdin) ; this doesn't work
194 (outfile "foo.ps")) ; this doesn't work
197 (set! infile (cadr args)))
199 (set! infile (cadr args))
200 (set! outfile (caddr args)))
202 (display "Wrong number of arguments.\n ")
204 (display " infile [outfile]")
207 (call-with-input-file infile parse-gerber)
208 (find-boundaries (reverse *netlist*))
209 (call-with-output-file outfile
211 (generate-ps (reverse *netlist*) port)))))