-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgroup-movedup-place-suffix.ulp
226 lines (194 loc) · 6.04 KB
/
group-movedup-place-suffix.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
#usage "<b>Add/Remove a Suffix to a group of part names</b> - v1.00 (07/06/2011)<p>"
"This ULP generates a script to add or remove a suffix to a group of parts "
"selected in the schematic editor. No checking is peformed for duplicates."
"<p>"
"<author>http://www.bobstarr.net</author>";
//////////////////////////////////////////////////////////////////////////////
//
// NORMALIZE ALL R/C/L PART VALUES
//
// Copyright (C) 2011, Robert E. Starr (http://www.bobstarr.net)
//
// REVISION HISTORY:
//
// v1.00 - 07/06/2011 (RES) - Initial Release
//
//////////////////////////////////////////////////////////////////////////////
//
// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED.
//
//////////////////////////////////////////////////////////////////////////////
string Version = "1.00";
string fileName;
string partNameOld[];
string partNameNew[];
string strSuffix = "-1";
int nCount = 0;
int fOption = 0;
int fScriptExec = 1; // execute script automatically after generating
//////////////////////////////////////////////////////////////////////////////
// trim all spaces from a string
string trimSpaces(string s)
{
if (!s)
return s;
string result;
int i;
int cnt=0;
for (i=0; s[i]; i++) {
if (isspace(s[i]))
continue;
result[cnt] = s[i];
cnt++;
}
return result;
}
// Trim suffix from from a string
string trimSuffix(string s, string suffix)
{
if (!s)
return s;
int lensuffix = strlen(suffix);
int lenstr = strlen(s);
if (lenstr > lensuffix)
{
if (strstr(s, suffix) > 0)
s = strsub(s, 0, lenstr - lensuffix);
}
return s;
}
// Replace suffix from from a string
string replaceSuffix(string s, string replace)
{
if (!s)
return s;
int pos = strrchr(s, '-');
if (pos >= 0)
{
s = strsub(s, 0, pos);
s += replace;
}
return s;
}
string removeSuffixChar(string s, int ch)
{
if (!s)
return s;
string result;
int i;
int cnt=0;
for (i=0; s[i]; i++) {
if (s[i] == ch)
continue;
result[cnt] = s[i];
cnt++;
}
return result;
}
//////////////////////////////////////////////////////////////////////////////
// Script Entry Point
if (schematic)
{
schematic(SCH)
{
fileName = filesetext(SCH.name, "_PartIDSuffx~~~.scr");
int Result = dlgDialog("PartID Suffix")
{
string title = "Add Remove PartID Suffix v" + Version;
dlgVBoxLayout
{
dlgGroup(title)
{
dlgVBoxLayout
{
dlgGridLayout
{
dlgCell(1,0) dlgLabel("Suffix");
dlgCell(1,1) dlgStringEdit(strSuffix);
}
}
dlgVBoxLayout
{
dlgRadioButton("&Add Suffix", fOption);
dlgRadioButton("&Remove Suffix", fOption);
dlgRadioButton("&Replace Suffix", fOption);
dlgRadioButton("&Remove Suffix Dash", fOption);
}
}
}
dlgHBoxLayout
{
dlgPushButton("OK") dlgAccept();
dlgPushButton("-Cancel") dlgReject();
dlgCheckBox("&Execute Script", fScriptExec);
}
};
if (!Result)
exit(0);
output(fileName, "wtD")
{
printf("# 'generated by partid-suffix.ulp v%s, exported from;\n", Version);
printf("# '%s at %s;\n", SCH.name, t2string(time()));
printf("# '%s;\n\n", EAGLE_SIGNATURE);
SCH.parts(P)
{
P.instances(I)
{
if (ingroup(I))
{
// make sure it has a package and name
if (P.device.package && P.name)
{
string suffix = strupr(trimSpaces(strSuffix));
string name;
if (fOption == 0)
{
// Add the suffix
name = P.name + suffix;
printf("NAME %s %s;\n", P.name, name);
}
else if (fOption == 1)
{
// Remove the suffix
if (strlen(P.name) > strlen(suffix))
{
printf("NAME %s %s;\n", P.name, trimSuffix(P.name, suffix));
}
}
else if (fOption == 2)
{
if (strlen(P.name) > strlen(suffix))
{
printf("NAME %s %s;\n", P.name, replaceSuffix(P.name, suffix));
}
}
else if (fOption == 3)
{
if (strlen(P.name) > strlen(suffix))
{
printf("NAME %s %s;\n", P.name, removeSuffixChar(P.name, '-'));
}
}
++nCount;
}
}
}
}
}
if (!nCount)
{
dlgMessageBox("No Parts Selected in Group");
exit(0);
}
if (fScriptExec)
exit ("SCRIPT '" + fileName + "';");
else
exit(0);
}
}
else
{
dlgMessageBox("Start this ULP in a Schematic!", "OK");
}
// End-Of-File