Root/
Source at commit 7eb3ac5ba29269662534a6b30b88e2b56996f05d created 13 years 9 months ago. By werner, When dragging an endpoint of an inverted measurement, we didn't consider that inst->base and inst->u.meas.end are swapped, causing fped to crash in vec_at. Also introduced a universal swap() function. | |
---|---|
1 | /* |
2 | * kicad.c - Dump objects in the KiCad board/module format |
3 | * |
4 | * Written 2009, 2010 by Werner Almesberger |
5 | * Copyright 2009, 2010 by Werner Almesberger |
6 | * |
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. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | #include <time.h> |
17 | |
18 | #include "coord.h" |
19 | #include "inst.h" |
20 | #include "kicad.h" |
21 | |
22 | |
23 | static unit_type zeroize(unit_type n) |
24 | { |
25 | return n == -1 || n == 1 ? 0 : n; |
26 | } |
27 | |
28 | |
29 | static void kicad_centric(struct coord a, struct coord b, |
30 | struct coord *center, struct coord *size) |
31 | { |
32 | struct coord min, max; |
33 | |
34 | min.x = units_to_kicad(a.x); |
35 | min.y = units_to_kicad(a.y); |
36 | max.x = units_to_kicad(b.x); |
37 | max.y = units_to_kicad(b.y); |
38 | |
39 | sort_coord(&min, &max); |
40 | |
41 | size->x = max.x-min.x; |
42 | size->y = max.y-min.y; |
43 | center->x = (min.x+max.x)/2; |
44 | center->y = -(min.y+max.y)/2; |
45 | } |
46 | |
47 | |
48 | static void do_drill(FILE *file, const struct inst *pad, struct coord *ref) |
49 | { |
50 | const struct inst *hole = pad->u.pad.hole; |
51 | struct coord center, size; |
52 | |
53 | if (!hole) |
54 | return; |
55 | |
56 | kicad_centric(hole->base, hole->u.hole.other, ¢er, &size); |
57 | |
58 | /* Allow for rounding errors */ |
59 | |
60 | fprintf(file, "Dr %d %d %d", size.x, |
61 | -zeroize(center.x-ref->x), -zeroize(center.y-ref->y)); |
62 | if (size.x < size.y-1 || size.x > size.y+1) |
63 | fprintf(file, " O %d %d", size.x, size.y); |
64 | fprintf(file, "\n"); |
65 | *ref = center; |
66 | } |
67 | |
68 | |
69 | static void kicad_pad(FILE *file, const struct inst *inst) |
70 | { |
71 | struct coord center, size; |
72 | |
73 | kicad_centric(inst->base, inst->u.pad.other, ¢er, &size); |
74 | |
75 | fprintf(file, "$PAD\n"); |
76 | |
77 | /* |
78 | * name, shape (rectangle), Xsize, Ysize, Xdelta, Ydelta, Orientation |
79 | */ |
80 | fprintf(file, "Sh \"%s\" %c %d %d 0 0 0\n", |
81 | inst->u.pad.name, inst->obj->u.pad.rounded ? 'O' : 'R', |
82 | size.x, size.y); |
83 | |
84 | /* |
85 | * Drill hole |
86 | */ |
87 | do_drill(file, inst, ¢er); |
88 | |
89 | /* |
90 | * Attributes: pad type, N, layer mask |
91 | */ |
92 | fprintf(file, "At %s N %8.8X\n", |
93 | inst->u.pad.hole ? "STD" : "SMD", (unsigned) inst->u.pad.layers); |
94 | |
95 | /* |
96 | * Position: Xpos, Ypos |
97 | */ |
98 | fprintf(file, "Po %d %d\n", center.x, center.y); |
99 | |
100 | fprintf(file, "$EndPAD\n"); |
101 | } |
102 | |
103 | |
104 | static void kicad_hole(FILE *file, const struct inst *inst) |
105 | { |
106 | struct coord center, size; |
107 | |
108 | if (inst->u.hole.pad) |
109 | return; |
110 | kicad_centric(inst->base, inst->u.hole.other, ¢er, &size); |
111 | fprintf(file, "$PAD\n"); |
112 | if (size.x < size.y-1 || size.x > size.y+1) { |
113 | fprintf(file, "Sh \"HOLE\" O %d %d 0 0 0\n", size.x, size.y); |
114 | fprintf(file, "Dr %d 0 0 O %d %d\n", size.x, size.x, size.y); |
115 | } else { |
116 | fprintf(file, "Sh \"HOLE\" C %d %d 0 0 0\n", size.x, size.x); |
117 | fprintf(file, "Dr %d 0 0\n", size.x); |
118 | } |
119 | fprintf(file, "At HOLE N %8.8X\n", (unsigned) inst->u.hole.layers); |
120 | fprintf(file, "Po %d %d\n", center.x, center.y); |
121 | fprintf(file, "$EndPAD\n"); |
122 | } |
123 | |
124 | |
125 | static void kicad_line(FILE *file, const struct inst *inst) |
126 | { |
127 | /* |
128 | * Xstart, Ystart, Xend, Yend, Width, Layer |
129 | */ |
130 | fprintf(file, "DS %d %d %d %d %d %d\n", |
131 | units_to_kicad(inst->base.x), |
132 | -units_to_kicad(inst->base.y), |
133 | units_to_kicad(inst->u.rect.end.x), |
134 | -units_to_kicad(inst->u.rect.end.y), |
135 | units_to_kicad(inst->u.rect.width), |
136 | layer_silk_top); |
137 | } |
138 | |
139 | |
140 | static void kicad_rect(FILE *file, const struct inst *inst) |
141 | { |
142 | unit_type xa, ya, xb, yb; |
143 | unit_type width; |
144 | |
145 | xa = units_to_kicad(inst->base.x); |
146 | ya = units_to_kicad(inst->base.y); |
147 | xb = units_to_kicad(inst->u.rect.end.x); |
148 | yb = units_to_kicad(inst->u.rect.end.y); |
149 | width = units_to_kicad(inst->u.rect.width); |
150 | |
151 | fprintf(file, "DS %d %d %d %d %d %d\n", |
152 | xa, -ya, xa, -yb, width, layer_silk_top); |
153 | fprintf(file, "DS %d %d %d %d %d %d\n", |
154 | xa, -yb, xb, -yb, width, layer_silk_top); |
155 | fprintf(file, "DS %d %d %d %d %d %d\n", |
156 | xb, -yb, xb, -ya, width, layer_silk_top); |
157 | fprintf(file, "DS %d %d %d %d %d %d\n", |
158 | xb, -ya, xa, -ya, width, layer_silk_top); |
159 | } |
160 | |
161 | |
162 | static void kicad_circ(FILE *file, const struct inst *inst) |
163 | { |
164 | /* |
165 | * Xcenter, Ycenter, Xpoint, Ypoint, Width, Layer |
166 | */ |
167 | fprintf(file, "DC %d %d %d %d %d %d\n", |
168 | units_to_kicad(inst->base.x), |
169 | -units_to_kicad(inst->base.y), |
170 | units_to_kicad(inst->base.x), |
171 | -units_to_kicad(inst->base.y+inst->u.arc.r), |
172 | units_to_kicad(inst->u.arc.width), |
173 | layer_silk_top); |
174 | } |
175 | |
176 | |
177 | static void kicad_arc(FILE *file, const struct inst *inst) |
178 | { |
179 | struct coord p; |
180 | double a; |
181 | |
182 | /* |
183 | * The documentation says: |
184 | * Xstart, Ystart, Xend, Yend, Angle, Width, Layer |
185 | * |
186 | * But it's really: |
187 | * Xcenter, Ycenter, Xend, Yend, ... |
188 | */ |
189 | p = rotate_r(inst->base, inst->u.arc.r, inst->u.arc.a2); |
190 | a = inst->u.arc.a2-inst->u.arc.a1; |
191 | while (a <= 0) |
192 | a += 360; |
193 | while (a > 360) |
194 | a -= 360; |
195 | fprintf(file, "DA %d %d %d %d %d %d %d\n", |
196 | units_to_kicad(inst->base.x), |
197 | -units_to_kicad(inst->base.y), |
198 | units_to_kicad(p.x), |
199 | -units_to_kicad(p.y), |
200 | (int) (a*10.0), |
201 | units_to_kicad(inst->u.arc.width), |
202 | layer_silk_top); |
203 | } |
204 | |
205 | |
206 | static void kicad_inst(FILE *file, enum inst_prio prio, const struct inst *inst) |
207 | { |
208 | switch (prio) { |
209 | case ip_pad_copper: |
210 | case ip_pad_special: |
211 | kicad_pad(file, inst); |
212 | break; |
213 | case ip_hole: |
214 | kicad_hole(file, inst); |
215 | break; |
216 | case ip_line: |
217 | kicad_line(file, inst); |
218 | break; |
219 | case ip_rect: |
220 | kicad_rect(file, inst); |
221 | break; |
222 | case ip_circ: |
223 | kicad_circ(file, inst); |
224 | break; |
225 | case ip_arc: |
226 | kicad_arc(file, inst); |
227 | break; |
228 | default: |
229 | /* |
230 | * Don't try to export vectors, frame references, or |
231 | * measurements. |
232 | */ |
233 | break; |
234 | } |
235 | } |
236 | |
237 | |
238 | static void kicad_module(FILE *file, const struct pkg *pkg, time_t now) |
239 | { |
240 | enum inst_prio prio; |
241 | const struct inst *inst; |
242 | |
243 | /* |
244 | * Module library name |
245 | */ |
246 | fprintf(file, "$MODULE %s\n", pkg->name); |
247 | |
248 | /* |
249 | * Xpos = 0, Ypos = 0, 15 layers, last modification, timestamp, |
250 | * moveable, not autoplaced. |
251 | */ |
252 | fprintf(file, "Po 0 0 0 15 %8.8lX 00000000 ~~\n", (long) now); |
253 | |
254 | /* |
255 | * Module library name again |
256 | */ |
257 | fprintf(file, "Li %s\n", pkg->name); |
258 | |
259 | #if 0 /* optional */ |
260 | /* |
261 | * Description |
262 | */ |
263 | fprintf(file, "Cd %s\n", pkg->name); |
264 | #endif |
265 | |
266 | /* |
267 | * |
268 | */ |
269 | fprintf(file, "Sc %8.8lX\n", (long) now); |
270 | |
271 | /* |
272 | * Attributes: SMD = listed in the automatic insertion list |
273 | */ |
274 | fprintf(file, "At SMD\n"); |
275 | |
276 | /* |
277 | * Rotation cost: 0 for 90 deg, 0 for 180 deg, 0 = disable rotation |
278 | */ |
279 | fprintf(file, "Op 0 0 0\n"); |
280 | |
281 | /* |
282 | * Text fields: Tn = field number, Xpos, Ypos, Xsize ("emspace"), |
283 | * Ysize ("emspace"), rotation, pen width, N (none), V = visible, |
284 | * comment layer. All dimensions are 1/10 mil. |
285 | */ |
286 | |
287 | fprintf(file, "T0 0 -150 200 200 0 40 N V %d \"%s\"\n", |
288 | layer_comment, pkg->name); |
289 | fprintf(file, "T1 0 150 200 200 0 40 N I %d \"Val*\"\n", |
290 | layer_comment); |
291 | |
292 | FOR_INST_PRIOS_UP(prio) { |
293 | for (inst = pkgs->insts[prio]; inst; inst = inst->next) |
294 | kicad_inst(file, prio, inst); |
295 | for (inst = pkg->insts[prio]; inst; inst = inst->next) |
296 | kicad_inst(file, prio, inst); |
297 | } |
298 | |
299 | fprintf(file, "$EndMODULE %s\n", pkg->name); |
300 | } |
301 | |
302 | |
303 | int kicad(FILE *file) |
304 | { |
305 | const struct pkg *pkg; |
306 | time_t now = time(NULL); |
307 | |
308 | fprintf(file, "PCBNEW-LibModule-V1 %s", ctime(&now)); |
309 | |
310 | fprintf(file, "$INDEX\n"); |
311 | for (pkg = pkgs; pkg; pkg = pkg->next) |
312 | if (pkg->name) |
313 | fprintf(file, "%s\n", pkg->name); |
314 | fprintf(file, "$EndINDEX\n"); |
315 | |
316 | for (pkg = pkgs; pkg; pkg = pkg->next) |
317 | if (pkg->name) |
318 | kicad_module(file, pkg, now); |
319 | |
320 | fprintf(file, "$EndLIBRARY\n"); |
321 | |
322 | fflush(file); |
323 | return !ferror(file); |
324 | } |
325 |
Branches:
master