This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbrd2json.ulp
497 lines (438 loc) · 15.3 KB
/
brd2json.ulp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
int supplylayers[];
int firstentry;
string nets = "";
int SPEC_VERSION = 1;
string TAB_STR = " ";
void Error(string s)
{
string cmd;
for(int x=0;x<argc;x++) cmd+=argv[x]+" ";
dlgMessageBox(cmd+"\n"+s);
exit(EXIT_FAILURE);
}
string tab(int n)
{
string s = "";
for(int i=0; i<n; i++)
s += TAB_STR;
return s;
}
void AddWire(UL_WIRE W, int L, int indent)
{
if(W.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
if(W.arc) {
int rot = 0;
if((W.arc.angle2 - W.arc.angle1) > 180) {rot = 1;}
printf("{\"type\": \"arc\", \"svgpath\": \"M %f %f A %f %f 0 %d 0 %f %f\", \"width\": %f}\n", u2inch(W.arc.x1), -u2inch(W.arc.y1), u2inch(W.arc.radius), u2inch(W.arc.radius), rot, u2inch(W.arc.x2), -u2inch(W.arc.y2), u2inch(W.width));
}
else printf("{\"type\": \"segment\", \"start\": [%f,%f], \"end\": [%f, %f], \"width\": %f}\n", u2inch(W.x1), -u2inch(W.y1), u2inch(W.x2), -u2inch(W.y2), u2inch(W.width));
}
void AddTrack(UL_WIRE W, int L, int indent, string net)
{
if(W.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
if(W.arc) {
printf("{ \"center\": [%f,%f], \"radius\": %f, \"startangle\": %f, \"endangle\": %f, \"width\": %f, \"net\": \"%s\"}\n", u2inch(W.arc.xc), -u2inch(W.arc.yc), u2inch(W.arc.radius), -W.arc.angle2, -W.arc.angle1, u2inch(W.arc.width), net);
} else {
printf("{ \"start\": [%f,%f], \"end\": [%f,%f], \"width\": %f, \"net\": \"%s\"}\n", u2inch(W.x1), -u2inch(W.y1), u2inch(W.x2), -u2inch(W.y2), u2inch(W.width), net);
}
}
void AddTrackVia(UL_VIA V, int L, int indent, string net)
{
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
printf("{ \"start\": [%f,%f], \"end\": [%f,%f], \"width\": %f, \"net\": \"%s\"}\n", u2inch(V.x), -u2inch(V.y), u2inch(V.x), -u2inch(V.y), u2inch(V.diameter[L]), net);
}
void AddHole(UL_HOLE H, int L, int indent)
{
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
printf("{\"type\": \"circle\", \"start\": [%f,%f], \"radius\": %f, \"width\": %f}\n", u2inch(H.x), -u2inch(H.y), u2inch(H.drill)/2, 0.005);
}
void AddCircle(UL_CIRCLE C, int L, int indent)
{
if(C.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
printf("{\"type\": \"circle\", \"start\": [%f,%f], \"radius\": %f, \"width\": %f}\n", u2inch(C.x), -u2inch(C.y), u2inch(C.radius), u2inch(C.width));
}
void AddRect(UL_RECTANGLE R, int L, int indent)
{
if(R.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
// Treat a rectangle as a polyshape, since Eagle draws them as a filled polygon (as opposed to an outline, as it does for Circles)
printf("{\"type\": \"polygon\", \"pos\": [0,0], \"angle\": 0, \"polygons\": \n");
printf("%s[ [ ",tab(indent+1));
printf("[%f,%f],",u2inch(R.x1),-u2inch(R.y1));
printf("[%f,%f],",u2inch(R.x2),-u2inch(R.y1));
printf("[%f,%f],",u2inch(R.x2),-u2inch(R.y2));
printf("[%f,%f],",u2inch(R.x1),-u2inch(R.y2));
printf("[%f,%f]",u2inch(R.x1),-u2inch(R.y1));
printf(" ] ]\n%s}\n",tab(indent));
}
void AddPolyShape(UL_POLYSHAPE P, int L, int indent)
{
int first = 1;
if(P.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
printf("{\"type\": \"polygon\", \"pos\": [0,0], \"angle\": 0, \"polygons\": \n");
printf("%s[\n%s[\n%s",tab(indent+1),tab(indent+2),tab(indent+3));
P.contours(W) {
if(!first) printf(",");
first = 0;
printf("[%f,%f]",u2inch(W.x1), -u2inch(W.y1));
}
printf("\n%s]\n%s]\n",tab(indent+2),tab(indent+1));
printf("%s}\n",tab(indent));
}
void AddPolyPour(UL_POLYPOUR P, int L, int indent, string net)
{
int firstpoly = 1;
int firstwire = 1;
if(P.layer!=L) return;
printf(tab(indent));
if(!firstentry) printf(",");
firstentry = 0;
printf("{\"polygons\": \n");
printf("%s[\n",tab(indent+1));
int x0, y0;
P.contours(W) {
if(firstwire) {
x0 = W.x1;
y0 = W.y1;
printf(tab(indent+2));
if(firstpoly) firstpoly=0;
else printf(",");
printf("[\n%s[%f,%f]\n",tab(indent+3),u2inch(W.x1), -u2inch(W.y1));
}
printf("%s,[%f,%f]\n",tab(indent+3),u2inch(W.x2), -u2inch(W.y2));
if (firstwire) firstwire=0;
else if (W.x2==x0 && W.y2==y0) {
firstwire = 1;
printf("%s]\n",tab(indent+2));
}
}
printf("\n%s]\n",tab(indent+1));
printf("%s,\"net\": \"%s\"\n",tab(indent+1),net);
printf("%s}\n",tab(indent));
}
string strcrepl(string s_in, char c_from, char c_to)
{
string a[];
int n = strsplit(a,s_in,'"');
if(n==0) return s_in;
else {
string s = a[0];
for(int ii=0; ii<n-1; ii++) s = s + "\\\"" + a[ii+1];
return s;
}
}
string ss(string s_in)
{
string s = strcrepl(s_in,'"','\"');
return s;
}
void AppendNet(string net)
{
if(strstr(nets,"\""+net+"\"")<0) {
if(strlen(nets)>0) nets = nets + ",";
nets = nets + "\""+net+"\"";
}
}
board(B)
{
int d,x,y;
int lays[];
int laycnt;
string tmpstr;
string tmpstrarr[];
string fileName;
string lname="unnamed";
string layernames[];
B.layers(L)
{
if(strsub(L.name,0,1)=="$") supplylayers[L.number]=1;
else supplylayers[L.number]=0;
layernames[L.number]=L.name;
}
sprintf(fileName,"%s.json",filesetext(B.name, ""));
int thisl;
int ilvl = 0;
output(fileName,"wt")
{
// overall 'pcbdata' object
printf("{\n");
printf(tab(ilvl) + "\"spec_version\": 1,\n");
printf(tab(ilvl++) + "\"pcbdata\": {\n");
// edges bbox
printf(tab(ilvl) + "\"edges_bbox\": {\"minx\": %f, \"miny\": %f, \"maxx\": %f, \"maxy\": %f},\n", u2inch(B.area.x1), -u2inch(B.area.y1), u2inch(B.area.x2), -u2inch(B.area.y2));
// edges (outline - from layer 20 'Dimension')
printf(tab(ilvl) + "\"edges\": [\n");
firstentry=1;
ilvl++;
B.wires(W) AddWire(W,LAYER_DIMENSION,ilvl);
B.circles(C) AddCircle(C,LAYER_DIMENSION,ilvl);
B.holes(H) AddHole(H,LAYER_DIMENSION,ilvl);
B.elements(E) E.footprint.wires(W) AddWire(W,LAYER_DIMENSION,ilvl);
B.elements(E) E.footprint.circles(C) AddCircle(C,LAYER_DIMENSION,ilvl);
B.elements(E) E.footprint.holes(H) AddHole(H,LAYER_DIMENSION,ilvl);
printf(tab(--ilvl) + "],\n"); // end edges
// drawings
printf(tab(ilvl++) + "\"drawings\": {\n");
// silkscreen
printf(tab(ilvl) + "\"silkscreen\": {\n");
int silk_layers[] = {LAYER_TPLACE,LAYER_TNAMES,LAYER_BPLACE,LAYER_BNAMES};
int e;
firstentry=1;
for(d=0;d<4;d+=2)
{
if(d==0) {
printf(tab(++ilvl) + "\"F\": [\n");
} else {
printf(tab(--ilvl) + "],\n");
printf(tab(ilvl) + "\"B\": [\n");
firstentry = 1;
}
ilvl++;
for(e=0; e<2; e++)
{
thisl=silk_layers[d+e];
B.wires(W) AddWire(W,thisl,ilvl);
B.polyShapes(P) AddPolyShape(P,thisl,ilvl);
B.rectangles(R) AddRect(R,thisl,ilvl);
B.circles(C) AddCircle(C,thisl,ilvl);
B.texts(T) T.wires(W) AddWire(W,thisl,ilvl);
B.elements(E) E.texts(T) T.wires(W) AddWire(W,thisl,ilvl);
B.elements(E) E.footprint.wires(W) AddWire(W,thisl,ilvl);
B.elements(E) E.footprint.texts(T) T.wires(W) AddWire(W,thisl,ilvl);
//B.holes(H) AddHole(H,thisl);
}
}
printf(tab(--ilvl) + "]\n");
printf(tab(--ilvl) + "},\n"); //end silkscreen
//fabrication
printf(tab(ilvl++) + "\"fabrication\": {\n");
printf(tab(ilvl) + "\"F\": [],\n");
printf(tab(ilvl) + "\"B\": []\n");
printf(tab(--ilvl) + "}\n");
printf(tab(--ilvl) + "},\n"); // end drawings
// footprints
status("Plotting elements");
printf(tab(ilvl++) + "\"footprints\": [\n");
firstentry = 1;
B.elements(E)
{
int contactcount = 0;
E.package.contacts(C) contactcount++;
if(E.populate && contactcount) {
if(firstentry)
printf(tab(ilvl++) + "{\n");
else
printf(tab(ilvl++) + ",{\n");
firstentry = 0;
//ref
printf(tab(ilvl) + "\"ref\": \"%s\",\n", E.name);
//center
printf(tab(ilvl) + "\"center\": [%f, %f],\n", u2inch(E.x), -u2inch(E.y));
// bbox
printf(tab(ilvl++) + "\"bbox\": {\n");
real xmax = -999.0, xmin = 999.0, ymax = xmax, ymin = xmin, dx, dy;
E.package.contacts(C) {
if(C.smd) {
dx = abs((u2inch(C.smd.dx)/2)*cos(C.smd.angle)) + abs((u2inch(C.smd.dy)/2)*sin(C.smd.angle));
dy = abs((u2inch(C.smd.dx)/2)*sin(C.smd.angle)) + abs((u2inch(C.smd.dy)/2)*cos(C.smd.angle));
} else {
dx = u2inch(C.pad.diameter[LAYER_TOP])/2;
dy = dx;
}
if (u2inch(C.x) + dx > xmax) xmax = u2inch(C.x) + dx;
if (u2inch(C.y) + dy > ymax) ymax = u2inch(C.y) + dy;
if (u2inch(C.x) - dx < xmin) xmin = u2inch(C.x) - dx;
if (u2inch(C.y) - dy < ymin) ymin = u2inch(C.y) - dy;
}
dx = abs(xmax - xmin);
dy = abs(ymax-ymin);
printf(tab(ilvl) + "\"pos\": [%f, %f],\n", (xmin+xmax-dx)/2, -(ymin+ymax+dy)/2);
printf(tab(ilvl) + "\"relpos\": [0, 0],\n");
printf(tab(ilvl) + "\"size\": [%f, %f],\n", dx, dy);
printf(tab(ilvl) + "\"angle\": %f\n", 0.0); // E.angle);
printf(tab(--ilvl) + "},\n"); // end bbox
// pads
printf(tab(ilvl++) + "\"pads\": [\n");
int firstpad = 1;
E.package.contacts(C) {
if(firstpad) { printf(tab(ilvl++) + "{\n"); firstpad = 0; }
else { printf(tab(ilvl++) + ",{\n"); }
printf(tab(ilvl) + "\"layers\": ");
if(C.smd) {
if(C.smd.layer == LAYER_BOTTOM) printf("[\"B\"],\n");
else printf("[\"F\"],\n");
printf(tab(ilvl) + "\"pos\": [%f, %f],\n", u2inch(C.x), -u2inch(C.y));
int dx = C.smd.dx[C.smd.layer];
int dy = C.smd.dy[C.smd.layer];
printf(tab(ilvl) + "\"size\": [%f, %f],\n", u2inch(dx), u2inch(dy));
printf(tab(ilvl) + "\"angle\": %f,\n", -C.smd.angle);
printf(tab(ilvl) + "\"shape\": ");
if(C.smd.roundness == 100 && dx == dy ) {
printf("\"circle\",\n");
}
else if (C.smd.roundness > 0) {
printf("\"roundrect\",\n");
printf("\"radius\": %f,\n", (real(C.smd.roundness)/100) * (u2inch(min(dx, dy))/2));
}
else {
printf("\"rect\",\n");
}
printf("%s\"net\": \"%s\",\n", tab(ilvl), C.signal);
printf(tab(ilvl) + "\"type\": \"smd\"\n");
if(C.smd.name == "1") printf(tab(ilvl) + ",\"pin1\": 1\n");
} else {
printf("[\"F\", \"B\"],\n");
printf(" \"pos\": [%f, %f],\n", u2inch(C.x), -u2inch(C.y));
printf(" \"size\": [%f, %f],\n", u2inch(C.pad.diameter[LAYER_TOP]), u2inch(C.pad.diameter[LAYER_TOP]));
printf(" \"angle\": %f,\n", C.pad.angle);
if(C.pad.shape[LAYER_TOP] == PAD_SHAPE_SQUARE) printf(" \"shape\": \"rect\",\n");
else if (C.pad.shape[LAYER_TOP] == PAD_SHAPE_LONG) printf(" \"shape\": \"oval\",\n");
else printf(" \"shape\": \"circle\",\n");
printf(" \"type\": \"th\",\n");
if(C.pad.name == "1") printf(" \"pin1\": 1,\n");
printf("%s\"net\": \"%s\",\n", tab(ilvl), C.signal);
printf(" \"drillshape\": \"oblong\",\n");
printf(" \"drillsize\": [%f, %f]\n", u2inch(C.pad.drill), u2inch(C.pad.drill));
}
printf(tab(--ilvl) + "}\n"); // end pad
}
printf(tab(--ilvl) + "],\n"); // end pads
printf(tab(ilvl) + "\"drawings\": [],\n");
if(E.mirror) printf(tab(ilvl) + "\"layer\": \"B\"\n");
else printf(tab(ilvl) + "\"layer\": \"F\"\n");
printf(tab(--ilvl) + "}\n"); //end footprint
}
}
printf(tab(--ilvl) + "],\n"); //end footprints
// tracks (wires on copper top/bottom copper layers)
printf(tab(ilvl++) + "\"tracks\": {\n");
printf(tab(ilvl++) + "\"F\": [\n");
firstentry = 1;
B.signals(S) {
S.wires(W) AddTrack(W,LAYER_TOP,ilvl,S.name);
S.vias(V) AddTrackVia(V,LAYER_TOP,ilvl,S.name);
}
printf(tab(--ilvl) + "],\n" + tab(ilvl++) + "\"B\": [\n");
firstentry = 1;
B.signals(S) {
S.wires(W) AddTrack(W,LAYER_BOTTOM,ilvl,S.name);
S.vias(V) AddTrackVia(V,LAYER_BOTTOM,ilvl,S.name);
}
printf(tab(--ilvl) + "]\n");
printf(tab(--ilvl) + "},\n");
// zones (copper pours on top/bottom layers)
printf(tab(ilvl++) + "\"zones\": {\n");
printf(tab(ilvl++) + "\"F\": [\n");
firstentry = 1;
B.signals(S) S.polyPours(P) AddPolyPour(P,LAYER_TOP,ilvl,S.name);
printf(tab(--ilvl) + "],\n" + tab(ilvl++) + "\"B\": [\n");
firstentry = 1;
B.signals(S) S.polyPours(P) AddPolyPour(P,LAYER_BOTTOM,ilvl,S.name);
printf(tab(--ilvl) + "]\n");
printf(tab(--ilvl) + "},\n");
// nets
printf(tab(ilvl) + "\"nets\": [");
B.signals(S) S.wires(W) AppendNet(S.name);
B.signals(S) S.vias(V) AppendNet(S.name);
printf("\"\",");
printf(nets);
printf("],\n");
/* metadata... added Dec 2021 by ei6ku, copied from iBom.ulp by "sean.alling@gmail.com"
Iterates over the board attributes looking for 'COMPANY' or 'REV' or 'REVISION' If these attribute exists
it returns the value, otherwise returns empty strings.
*/
string company = "";
string revision = "";
B.attributes(B){
if( (B.name == "COMPANY") )
{
company = B.value;
}
if( (B.name == "REV") || (B.name == "REVISION"))
{
revision = B.value;
}
}
// metadata
printf(tab(ilvl++) + "\"metadata\": {\n");
string a[];
int n = strsplit(a, B.name, '/');
strsplit(a,a[n-1],'.');
printf(tab(ilvl) + "\"title\": \"%s\",\n", a[0]);
printf(tab(ilvl) + "\"revision\": \"%s\",\n", revision); // amended ei6ku dec 2021
printf(tab(ilvl) + "\"company\": \"%s\",\n", company); // amended ei6ku dec 2021
int t = time();
sprintf(tmpstr,"%d-%d-%d %d:%02d:%02d", t2year(t), t2month(t)+1, t2day(t), t2hour(t), t2minute(t), t2second(t));
printf(tab(ilvl) + "\"date\": \"%s\"\n", tmpstr);
printf(tab(--ilvl) + "}\n");
printf(tab(--ilvl) + "}\n"); // end of pcbdata object
// components object
printf(tab(ilvl++) + ",\"components\": [\n");
firstentry = 1;
B.elements(E)
{
int contactcount = 0;
E.package.contacts(C) contactcount++;
if(E.populate && contactcount) {
if(firstentry) { printf(tab(ilvl++) + "{\n"); firstentry=0; }
else { printf(tab(ilvl++) + ",{\n"); }
printf(tab(ilvl) + "\"attr\": \"%s\",\n",""); // Note that all Attributes are added to extra_fields
printf(tab(ilvl) + "\"footprint\": \"%s\",\n",ss(E.package.name));
if(E.mirror) printf(tab(ilvl) + "\"layer\": \"B\",\n");
else printf(tab(ilvl) + "\"layer\": \"F\",\n");
printf(tab(ilvl) + "\"ref\": \"%s\",\n", E.name);
printf(tab(ilvl) + "\"val\": \"%s\"\n", E.value);
printf(tab(ilvl++) + ",\"extra_fields\": { \n");
int firstattribute = 1;
E.attributes(A) {
printf(tab(ilvl));
if(!firstattribute) { printf(","); }
firstattribute = 0;
// Escape double quotes:
string values[];
int len = strsplit(values, A.value, '"');
printf("\"%s\": \"", A.name);
for (int i = 0; i < len; ++i) {
if (i > 0) {
printf("\\\"");
}
printf("%s", values[i]);
}
printf("\"\n");
}
printf(tab(--ilvl) + "}\n"); // end extra fields
printf(tab(--ilvl) + "}\n"); // end component
}
}
printf(tab(--ilvl) + "]\n"); // end of components list
printf(tab(--ilvl) + "}\n"); // end of overall object
status("Done");
}
dlgDialog("Success") {
dlgHBoxLayout dlgSpacing(640);
dlgLabel("Generated JSON file:");
dlgTextView(fileName);
dlgHBoxLayout {
dlgStretch(1);
dlgPushButton("+OK") dlgAccept();
}
};
}