draw.c: Always draw pin / hole names when in pinout preview
[geda-pcb/pcjc2.git] / tools / gerbertotk.c
blob5b2f03ae8665e7b860abfe10238bb546697e6127
1 /*
2 * COPYRIGHT
4 * gerbertotk, simple Gerber file to tcl/tk program converter
5 * Copyright (C) 1996 Albert John FitzPatrick III
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Contact addresses for paper mail and Email:
22 * Albert John FitzPatrick III/ 8803 Humming Bird Lane/ Quinton, VA 23141/
23 * USA.
24 * ajf_nylorac@acm.org
28 /* WARNING: This is an alpha version. Use it with many grains of salt. */
30 static char Copyright[] = "Copyright (C) 1996 Albert John FitzPatrick III";
33 * Example Usage:
34 * cat *.gbr | gerbertotk | wish
37 #include <string.h>
38 #include <stdio.h>
40 typedef long GerberValue;
42 typedef struct {
43 int gc_inComment;
44 GerberValue gc_d;
45 GerberValue gc_x1;
46 GerberValue gc_y1;
47 GerberValue gc_x2;
48 GerberValue gc_y2;
49 GerberValue gc_i;
50 GerberValue gc_j;
51 } GerberContext;
53 GerberContext gc;
55 #define gbMaxX 10000L
56 #define gbMaxY 10000L
57 #define tkScale 4L
58 #define tkX(x) (800L * (x) * tkScale / gbMaxX)
59 #define tkY(y) ((600L * (gbMaxY - (y)) * tkScale / gbMaxY))
62 do_cmd(GerberContext *gc, int cmd, char *buffer)
64 GerberValue value = atol(buffer);
66 switch (cmd) {
67 case 'G':
68 switch (value) {
69 case 4L:
70 gc->gc_inComment = 1;
71 break;
73 break;
74 case 'X':
75 gc->gc_x2 = value;
76 break;
77 case 'Y':
78 gc->gc_y2 = value;
79 break;
80 case 'I':
81 gc->gc_i = value;
82 break;
83 case 'J':
84 gc->gc_j = value;
85 break;
86 case 'D':
87 switch (value) {
88 case 1L:
89 /* Move with light on. */
90 printf(".f.c create line %ld %ld %ld %ld\n",
91 (long) tkX(gc->gc_x1),
92 (long) tkY(gc->gc_y1),
93 (long) tkX(gc->gc_x2),
94 (long) tkY(gc->gc_y2)
96 gc->gc_x1 = gc->gc_x2;
97 gc->gc_y1 = gc->gc_y2;
98 break;
99 case 2L:
100 /* Move with light off. */
101 gc->gc_x1 = gc->gc_x2;
102 gc->gc_y1 = gc->gc_y2;
103 break;
104 case 3L:
105 /* Move with light off. Flash light on. Turn light off. */
106 printf(".f.c create line %ld %ld %ld %ld\n",
107 (long) tkX(gc->gc_x2),
108 (long) tkY(gc->gc_y2),
109 (long) tkX(gc->gc_x2),
110 (long) tkY(gc->gc_y2)
112 break;
113 default:
114 gc->gc_d = value;
115 break;
117 break;
118 case 'M':
119 break;
120 case '*':
121 gc->gc_inComment = 0;
122 break;
123 case '\0':
124 /* FIXME: what if missing on first time?
125 fprintf(stderr, "Missing Gerber command character.\n");
127 break;
132 main(int argc, char *argv[])
134 int ch;
135 int cmd;
136 char buffer[1024];
137 char *bp;
139 if (argc != 1) {
140 fprintf(stderr, "%s: %s\n",
141 argv[0],
142 Copyright
144 fprintf(stderr, "%s: Usage: %s\n",
145 argv[0],
146 argv[0]
148 exit(1);
151 bp = buffer;
152 cmd = '\0';
153 gc.gc_inComment = 0;
154 gc.gc_d = 0L;
155 gc.gc_x1 = 0L;
156 gc.gc_y1 = 0L;
157 gc.gc_x2 = 0L;
158 gc.gc_y2 = 0L;
159 gc.gc_i = 0L;
160 gc.gc_j = 0L;
162 printf("button .exit \\\n");
163 printf("\t-command exit \\\n");
164 printf("\t-text Exit\n");
165 printf("frame .f\n");
166 printf("canvas .f.c \\\n");
167 printf("\t-width 800 \\\n");
168 printf("\t-height 600 \\\n");
169 printf("\t-scrollregion { 0 0 10000 10000 } \\\n");
170 printf("\t-xscroll { .f.h set } \\\n");
171 printf("\t-yscroll { .f.v set }\n");
172 printf("scrollbar .f.v \\\n");
173 printf("\t-orient vertical \\\n");
174 printf("\t-command { .f.c yview }\n");
175 printf("scrollbar .f.h \\\n");
176 printf("\t-orient horizontal \\\n");
177 printf("\t-command { .f.c xview }\n");
179 printf("\n");
180 printf("pack .exit .f \\\n");
181 printf("\t-side top \\\n");
182 printf("\t-expand yes \\\n");
183 printf("\t-fill both\n");
184 printf("pack .f.v \\\n");
185 printf("\t-side right \\\n");
186 printf("\t-fill y\n");
187 printf("pack .f.h \\\n");
188 printf("\t-side bottom \\\n");
189 printf("\t-fill x\n");
190 printf("pack .f.c \\\n");
191 printf("\t-side left\n");
193 printf("\n");
195 while ((ch = getchar()) != EOF) {
196 if (gc.gc_inComment && ch != '*')
197 continue;
199 switch (ch) {
200 case 'D':
201 case 'G':
202 case 'X':
203 case 'Y':
204 case 'I':
205 case 'J':
206 case 'M':
207 do_cmd(&gc, cmd, buffer);
208 cmd = ch;
209 bp = buffer;
210 break;
211 case '*':
212 /* FIXME: Handle other EOB characters. */
213 if (gc.gc_inComment)
214 do_cmd(&gc, ch, buffer);
215 else
216 do_cmd(&gc, cmd, buffer);
217 cmd = '\0';
218 bp = buffer;
219 break;
220 case '\r':
221 /* FIXME: only after EOB. */
222 cmd = '\0';
223 bp = buffer;
224 break;
225 case '\012':
226 /* FIXME: only after EOB. */
227 cmd = '\0';
228 bp = buffer;
229 break;
230 case '+':
231 case '-':
232 case '0':
233 case '1':
234 case '2':
235 case '3':
236 case '4':
237 case '5':
238 case '6':
239 case '7':
240 case '8':
241 case '9':
242 *bp++ = (char) ch;
243 *bp = '\0';
244 break;
245 default:
246 do_cmd(&gc, cmd, buffer);
247 cmd = '\0';
248 bp = buffer;
249 if (! gc.gc_inComment)
250 fprintf(stderr, "Unknown Gerber command '%c' (%d).\n",
251 (char) ch,
254 break;
258 exit(0);