This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadd_ordercodes.ulp
152 lines (137 loc) · 4.26 KB
/
add_ordercodes.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
#require 6.0000
string tr(string s)
{
return s;
}
if (!schematic)
{
dlgMessageBox (usage + tr ("<hr><b>ERROR: No schematic!</b><p>\nThis program can only work in"
" the schematic editor."));
exit (1);
}
int RefDefsColumn = 0;
string csvPath;
int ocsSize = 0;
string ocNames[];
int ocIndexes[];
int selectedSeparatorIndex = 1;
string separators[] = { tr("comma"), tr("semicolon"), tr("tabulator")};
string StripWhiteSpace (string s)
{
while (s && isspace (s [0]))
s = strsub (s, 1);
while (s && isspace (s [strlen (s) - 1]))
s = strsub (s, 0, strlen (s) - 1);
return s;
}
int findSheetIndex(string refdef)
{
schematic(S) {
S.parts(P) {
P.instances(I) {
if (P.name == refdef)
return I.sheet;
}
}
}
return -1;
}
string import(string csvPath, int selectedSeparatorIndex, int refdefsIndex)
{
string ret = "";
string lines[];
int CSVSeparator = 0;
int nLines = fileread(lines, csvPath);
string cols[];
int sheetIndex = -1;
switch (selectedSeparatorIndex) {
case 0:
CSVSeparator = ',';
break;
case 1:
CSVSeparator = ';';
break;
case 2:
CSVSeparator = '\t';
break;
}
for (int lineIndex = 0; lineIndex < nLines; lineIndex++) {
int colC = 0;
if (CSVSeparator == ',') {
} else {
colC = strsplit(cols, lines[lineIndex], CSVSeparator);
}
if (lineIndex == 0) {
// first line shall contain the OC_fields
for (int col = 0; col<colC; col++) {
string prefix = strsub(cols[col], 0, 3);
if (prefix == "OC_" || prefix == "oc_") {
ocNames[ocsSize] = cols[col];
ocIndexes[ocsSize] = col;
ocsSize++;
printf("Oredercode column found in %d col\n", col);
}
}
} else {
string refdefs[];
int refdefsC = strsplit(refdefs, cols[refdefsIndex], ',');
for (int ri = 0; ri<refdefsC; ri++) {
string refdes = StripWhiteSpace(refdefs[ri]);
if (refdes == "")
continue;
printf("Looking ordercodes for %s\n", refdes);
for (int ocIndex = 0; ocIndex < ocsSize; ocIndex++) {
printf("OC in col %d == %s\n", ocIndexes[ocIndex], cols[ocIndexes[ocIndex]]);
if (cols[ocIndexes[ocIndex]] != "") {
int sheetIndexTmp = findSheetIndex(refdes);
if (sheetIndexTmp != -1) {
if (sheetIndexTmp != sheetIndex)
sprintf(ret, "%sEDIT .s%d;\n", ret, sheetIndexTmp);
sheetIndex = sheetIndexTmp;
sprintf(ret, "%sCHANGE DISPLAY OFF;\n", ret);
sprintf(ret, "%sATTRIBUTE %s %s '%s';\n", ret, refdes, ocNames[ocIndex], cols[ocIndexes[ocIndex]]);
} else {
printf("Unable to find part %s\n", refdes);
}
}
}
}
}
}
printf(ret);
return ret;
}
output("/tmp/test.txt", "wt") {
dlgDialog (tr ("Import ordercodes from Bill Of Material"))
{
dlgLabel(tr("This ULP requires a CSV file (exported by bom_w_attr.ulp for e.g.).\n"
"The script will pull the attributes from columns named OC_* and add it to the devices in the schematic."));
dlgGroup (tr ("BoM file"))
{
dlgStringEdit (csvPath);
dlgPushButton("Browse") {
csvPath = dlgFileOpen("Select the BoM file", "", "*.csv");
}
}
dlgHBoxLayout
{
dlgGroup (tr ("Reference designators colum (0 based)"))
{
// TODO create a dlgComboBox with A-Z...
dlgSpinBox (RefDefsColumn);
}
dlgGroup (tr ("Column separator"))
{
dlgComboBox(separators, selectedSeparatorIndex);
}
dlgStretch (1);
}
dlgHBoxLayout
{
dlgStretch (1);
dlgPushButton (tr ("Import")) exit(import(csvPath, selectedSeparatorIndex, RefDefsColumn));
dlgPushButton (tr ("-Close")) dlgAccept();
dlgStretch(1);
}
};
}