-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgroup-movedup-place.ulp
399 lines (325 loc) · 10.3 KB
/
group-movedup-place.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
#usage "<b>Generate a MOVE script of part locations selected via the group command</b>\n"
"<p>"
"This program generates a script of move commands for parts selected via the group command "
"which can then be used move or place a group or multiple groups of other parts using the selected "
"group as a template and adding an offset. The primary purpose of this to allow placing groups of parts "
"to place other groups of parts in the same placement at a new offset. This is useful for placing "
"duplicate circuits groups of the same type. Note all groups parts must have the same part ID suffixed with "
"a dash and group number (eg first group = C1-1, R1-1, R2-1, and second group = C1-2, R1-2, R2-2, etc). "
"Also see the utility group-movedup-place-suffix.ulp for adding suffixes in the schematic.<p>"
"<author>http://www.bobstarr.net</author>"
//////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011, Robert E. Starr (http://www.bobstarr.net)
//
// REVISION HISTORY:
//
// v1.00 - 02/13/2010 (RES) - Initial Release
//
//////////////////////////////////////////////////////////////////////////////
//
// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED. IF YOU DON'T LIKE IT, DON'T USE IT!
//
//////////////////////////////////////////////////////////////////////////////
string Version = "1.00";
string fileName;
string partNameOld[];
string partNameNew[];
string configFilename = "group-movedup-place.cfg";
// Variables below are stored/recalled from config file
string strSuffixFirst = "-1";
string strSuffixLast = "-1";
real offsetX = 0.0;
real offsetY = 0.0;
real incX = 100.0;
real incY = 100.0;
int fScriptExec = 1; // execute script automatically after generating
int fMultiple = 0;
//////////////////////////////////////////////////////////////////////////////
// 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 "";
int pos = strrchr(s, '-');
if (pos >= 0)
{
s = strsub(s, 0, pos);
s += replace;
}
return s;
}
int getSuffixValue(string s)
{
int val = 0;
if (!s)
return 0;
int pos = strrchr(s, '-');
if (pos >= 0)
{
s = strsub(s, pos+1);
val = strtol(s);
}
return val;
}
void LoadConfigSettings()
{
string a[];
int n;
// Should be 8 lines of data in config file
if ((n = fileread(a, filedir(argv[0])+configFilename)) == 8)
{
int i = 0;
strSuffixFirst = a[i++];
strSuffixLast = a[i++];
offsetX = strtod(a[i++]);
offsetY = strtod(a[i++]);
incX = strtod(a[i++]);
incY = strtod(a[i++]);
fScriptExec = strtol(a[i++]);
fMultiple = strtol(a[i++]);
}
}
void SaveConfigSettings()
{
// save the config settings
output(filedir(argv[0])+configFilename, "wt")
{
printf("%s\n", strSuffixFirst);
printf("%s\n", strSuffixLast);
printf("%f\n", offsetX);
printf("%f\n", offsetY);
printf("%f\n", incX);
printf("%f\n", incY);
printf("%d\n", fScriptExec);
printf("%d\n", fMultiple);
}
}
int MoveSingleGroup(UL_BOARD B)
{
int cnt = 0;
B.elements(E)
{
if (ingroup(E))
{
real x,y;
switch (B.grid.unit)
{
case GRID_UNIT_MIC:
x = u2mic(E.y);
y = u2mic(E.y);
break;
case GRID_UNIT_MIL:
x = u2mil(E.x);
y = u2mil(E.y);
break;
case GRID_UNIT_MM:
x = u2mm (E.x);
y = u2mm (E.y);
break;
case GRID_UNIT_INCH:
x = u2inch(E.x);
y = u2inch(E.y);
break;
}
string name = E.name;
if (strSuffixFirst)
name = replaceSuffix(name, strSuffixFirst);
// issue the actual move command and account for rotation/mirror
printf("MOVE %s (%5.4f %5.4f);\n", name, x + offsetX, y + offsetY);
printf("ROTATE =R%f %s;\n", E.angle, name);
if (E.mirror)
printf("MIRROR %s;\n", name);
++cnt;
}
}
return cnt;
}
int MoveMultipleGroups(UL_BOARD B)
{
int start = getSuffixValue(strSuffixFirst);
int end = getSuffixValue(strSuffixLast);
if (end < start)
{
dlgMessageBox("Invalid First/Last Suffix Ranges");
return 0;
}
if (!start)
{
dlgMessageBox("Invalid First Suffix");
return 0;
}
if (!end)
{
dlgMessageBox("Invalid Last Suffix");
return 0;
}
int i;
int cnt = 0;
real offX = offsetX;
real offY = offsetY;
for (i=start; i <= end; i++)
{
printf("#\n# Move Group Suffix %d\n#\n", i);
B.elements(E)
{
if (ingroup(E))
{
real x, y;
switch (B.grid.unit)
{
case GRID_UNIT_MIC:
x = u2mic(E.y);
y = u2mic(E.y);
break;
case GRID_UNIT_MIL:
x = u2mil(E.x);
y = u2mil(E.y);
break;
case GRID_UNIT_MM:
x = u2mm (E.x);
y = u2mm (E.y);
break;
case GRID_UNIT_INCH:
x = u2inch(E.x);
y = u2inch(E.y);
break;
}
string name = E.name;
string suffix;
sprintf(suffix, "-%d", i);
name = replaceSuffix(name, suffix);
// issue the actual move command and account for rotation/mirror
printf("MOVE %s (%5.4f %5.4f);\n", name, x + offX, y + offY);
printf("ROTATE =R%f %s;\n", E.angle, name);
if (E.mirror)
printf("MIRROR %s;\n", name);
++cnt;
}
}
offX += incX;
offY += incY;
}
return cnt;
}
int MainAppDialog()
{
int nResult = dlgDialog("Group Move/Duplicate Placement")
{
string title = "Group Move/Duplicate Placement v" + Version;
dlgVBoxLayout
{
dlgSpacing(5);
dlgGroup(title)
{
dlgVBoxLayout
{
dlgGroup("Part Name Suffixes")
{
dlgGridLayout
{
dlgCell(1,0) dlgLabel("First Part ID Suffix");
dlgCell(1,1) dlgStringEdit(strSuffixFirst);
dlgCell(1,2) dlgRadioButton("Move Single Group of Parts", fMultiple);
dlgCell(2,0) dlgLabel("Last Part ID Suffix");
dlgCell(2,1) dlgStringEdit(strSuffixLast);
dlgCell(2,2) dlgRadioButton("Move Multiple Groups of Parts", fMultiple);
}
}
dlgGridLayout
{
dlgCell(1,0) dlgLabel("X-Offset");
dlgCell(1,1) dlgRealEdit(offsetX);
dlgCell(1,2) dlgLabel("X-Increment");
dlgCell(1,3) dlgRealEdit(incX);
dlgCell(1,4) dlgPushButton("++") { offsetX += incX; }
dlgCell(1,5) dlgPushButton("--") { offsetX -= incX; };
dlgCell(2,0) dlgLabel("Y-Offset");
dlgCell(2,1) dlgRealEdit(offsetY);
dlgCell(2,2) dlgLabel("Y-Increment");
dlgCell(2,3) dlgRealEdit(incY);
dlgCell(2,4) dlgPushButton("++") { offsetY += incY; }
dlgCell(2,5) dlgPushButton("--") { offsetY -= incY; };
}
}
}
}
dlgHBoxLayout
{
dlgPushButton("OK") dlgAccept();
dlgPushButton("-Cancel") dlgReject();
dlgCheckBox("&Execute Script", fScriptExec);
}
};
return nResult;
}
//////////////////////////////////////////////////////////////////////////////
// Script Entry Point
if (board) board(B)
{
LoadConfigSettings();
if (MainAppDialog() == 0)
{
SaveConfigSettings();
exit(0);
}
fileName = filesetext(B.name, "_GroupMoveDupPlace.scr");
output(fileName)
{
printf ("GRID ");
if (B.grid.unit == GRID_UNIT_MIC)
printf ("MIC\n");
else if (B.grid.unit == GRID_UNIT_MM)
printf ("MM\n");
else if (B.grid.unit == GRID_UNIT_MIL)
printf ("MIL\n");
else
printf ("INCH\n");
int cnt = 0;
if (fMultiple)
cnt = MoveMultipleGroups(B);
else
cnt = MoveSingleGroup(B);
SaveConfigSettings();
if (fScriptExec && cnt)
exit("SCRIPT '" + fileName + "';");
else
exit(0);
}
}
else
{
dlgMessageBox("\n Start this ULP in a Board \n");
exit (0);
}