-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbom-json.ulp
53 lines (46 loc) · 1.13 KB
/
bom-json.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
#require 4.1105
#usage "en: <b>Export a Bill Of Materials as JSON</b>\n"
"<p>"
" Saves a project's <i>Bill Of Material</i> as a <i>project</i>.json"
" suitable for uploading to http://solderpad.com"
" <author>Author: [email protected]</author>"
"</p>";
if (!schematic) {
dlgMessageBox(usage + "<hr><b>ERROR: No schematic!</b><p>\nThis program can only work in the schematic editor.");
exit(1);
}
string escape(string s) {
char c = '"';
int pos = 0;
string tp;
while ((pos = strchr(s, c, pos)) > -1) {
tp = strsub(s, 0, pos);
tp += "\\";
tp += strsub(s, pos);
s = tp;
pos += 2;
}
return s;
}
schematic(SCH) {
string FileName;
string json;
string sep = "";
FileName = filesetext("bom", ".json");
output(FileName, "wt") {
printf("{\n");
printf("\t\"items\": [\n");
SCH.parts(P) {
if (P.device.package) {
json = sep + "\t\t{\n"
+ "\t\t\t\"designator\": \"" + escape(P.name) + "\",\n"
+ "\t\t\t\"value\": \"" + escape(P.value) + "\",\n"
+ "\t\t\t\"description\": \"" + escape(P.device.headline) + "\"\n"
+ "\t\t}";
sep = ",\n";
printf("%s", json);
}
}
printf("\n\t]\n}\n");
}
}