forked from lachlanA/eagle-to-kicad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenumber-sheet.ulp
586 lines (532 loc) · 19.2 KB
/
renumber-sheet.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#usage "<b>Renumber the parts of a schematic</b><p>"
"This version renumbers only devices with packages (no supply) "
"sorted by sheets and coordinates (vertical/descending, horizontal/ascending).<br>"
"Optional: The starting point for the numeration of parts on the first sheet "
"defines an offset value for the following pages.<br>"
"Example:<br>"
" - 0 = R1 ... Rn<br>"
" - 100 sheet 1: R101..R199 sheet 2: R201..R299 ...<br>"
" - 500 sheet 1: R501..R999 sheet 2: R1001..R1499 ...<br>"
" - 1000 sheet 1: R1001..R1999 sheet 2: R2001..R2999 ...<br>"
"<p>"
"<author>Author: [email protected]</author>"
// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED
string Version = "ULP Version 4.4.2";
/*
* CHANGELOG================================================
* 27.02.2015: Programed moded to only add refance numbers
* to parts which have no refance numbers
* Thanks to: Lachlan "lachlanusa (at) gmail.com"
*
* 2006.08.23 [email protected]
* correct counter if crossing page by same prefix
* 2008.01.30 [email protected]
* renumber only the current sheet by start sheet-number * numerical_order
* 2012-06-21 - corrected sort by Weighting of emphasis in Y-direction
*/
string Info = "ATTENTION<p>" +
"Please verify that the corresponding pcb layout (if it existing) " +
"has been loaded, and is consistant with the PCB.<p>" +
"Otherwise renumbering of parts wont work, KiCad sch part to pcb part match wont work !";
string Infoplus =
" You can change the following sorting parameters:<p>" +
" descx = 0 (X ascending [left >> right])<br>" +
" descx = 1 (X descending [right >> left])<br>" +
" descy = 0 (Y ascending [bottom >> top])<br>" +
" descy = 1 (Y descending [top >> bottom])<br>";
int descy = 1; // set to 0 sorting ascending
int descx = 0; // set to 1 sorting descending
int numerical_order = 0; // sort from sheet 1, or sort by start counter for sheet
int emphases = 0; // weighting of emphases first X second Y or first Y second X
int actual_sheet = 0; // 2008.01.30
int only_actual_sheet = 0; // 2008.01.30
string sheet_info = "";
numeric string OldNames[], NewNames[], Prefix[];
int x[], y[], Index[], Sheet[];
int nrNames = 0;
numeric string SymNames[]; // Device-Name of Symbol
int symsh[];
int SymX[], SymY[];
int Snr = 0;
int Dnr = 0;
string Error = "";
string SymPrefix[]; // holds Refance/prefix - any traling number
string DevPrefix[];
string DevName[]; // holds Refnace/prefix with traling number
string SymDevName[]; // holds Refnace/prefix with traling number
string NoPrefixRenumber = ""; // Prefix do not renumber Testpoints
int ckx[], cky[], cksh[];
string ckname[];
string cmd; // holds the script to renumbing in sch
string c;
real Grid = 100; // in 100 Mil
string lbr[], dev[], sym[];
int maxRefNumber = 0; // Hold the maxium refance number on this schmatic
// this will be the start of the number parts which dont have a refance
//--------------------------------------------------------------------------------
// Returns the index of the first digit of the numeric part of Name
// -1 indicates there is no numeric part in Name
int GetNumberIndex(string Name)
{
int l = strlen(Name) - 1;
for (int Index = l; Index >= 0; --Index) {
if (!isdigit(Name[Index]))
return Index < l ? Index + 1 : -1;
}
return 0;
}
//--------------------------------------------------------------------------------
// return Refance with out traling number
//
string prefix(string name) { // Prefix of Device
int num = GetNumberIndex(name);
if (num < 1)
return name;
else {
string pfx = name;
pfx[num] = 0; // put in 0 to termant string
return pfx;
}
}
void DescendingY(void) {
for (int ny = 0; ny < nrNames ; ny++) {
y[ny] = 0 - y[ny];
}
return;
}
void DescendingX(void) {
for (int nx = 0; nx < nrNames ; nx++) {
x[nx] = 0 - x[nx];
}
return;
}
void SortElements(void) {
// Sorts the elements according to their location, first by ascending
// x coordinates, then by ascending y coordinates.
// If you prefer a different kind of sorting, you can implement this here.
// As a result, the integer array Index[] must contain the new sequence
// in which to renumber the elements.
// 2008-07-24 alf, weighting of emphases first X second Y or first Y second X
if (descy) DescendingY();
if (descx) DescendingX();
if(!numerical_order)
{
if (!emphases)
sort(nrNames, Index, NewNames, Sheet, x, y);
else
sort(nrNames, Index, NewNames, Sheet, y, x);
}
else {
if (!emphases) sort(nrNames, Index, Sheet, NewNames, x, y);
if (emphases) sort(nrNames, Index, Sheet, NewNames, y, x); // 2012-06-21 correct sort on emphases
}
if (descy) DescendingY();
if (descx) DescendingX();
return;
}
void CheckSameOrigin(int chk) { // eagle can not rename an element
// if another element is on the same coordinate
int index[];
string checklist, h;
sort(chk, index, cksh, ckx, cky);
for (int n = 0; n < nrNames; n++) {
if(ckx[index[n]] == ckx[index[n+1]] && cky[index[n]] == cky[index[n+1]] && cksh[index[n]] == cksh[index[n+1]]) {
sprintf(h, "%s & %s on same coordinate (%d %d) mil in sheet %d\n",
ckname[index[n]], ckname[index[n+1]],
ckx[index[n]],
cky[index[n]],
cksh[index[n]]);
checklist += h;
}
}
if (checklist) {
dlgDialog("Check coordinates") {
dlgLabel("Eagle can not rename elements that are placed at the same position!");
dlgHBoxLayout {
dlgSpacing(300);
}
dlgTextView(checklist);
dlgHBoxLayout {
dlgPushButton("Break") dlgAccept();
dlgStretch(1);
}
};
exit(0);
}
return;
}
//--------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------
void GenerateNames(void) {
string memprefix = "";
int mem_sh = 0;
if(!numerical_order) {
// Generates new numeric parts to the element names in NewNames
int k;
for (int n = 0; n <= nrNames - 1; ++n)
{
if (memprefix != NewNames[Index[n]]) // Start of new Refance prefix name ?
{
memprefix = NewNames[Index[n]];
// k = 0; // Reset to zero
k = maxRefNumber;
}
sprintf(NewNames[Index[n]], "%s%d", NewNames[Index[n]], ++k);
}
}
else { // renumber sheets by 100.. 200.. 300..
string h;
int newdevnr;
for(int n = 0; n < nrNames ; ++n) {
if (memprefix != NewNames[Index[n]])
{
memprefix = NewNames[Index[n]];
newdevnr = numerical_order * Sheet[Index[n]] +1;
}
if (mem_sh != Sheet[Index[n]])
{ // a new Sheet is starting by old prefix *** 2006.08.23 [email protected]
mem_sh = Sheet[Index[n]];
newdevnr = numerical_order * Sheet[Index[n]] +1;
}
sprintf(NewNames[Index[n]], "%s%d", NewNames[Index[n]], newdevnr);
newdevnr++;
if (newdevnr-(Sheet[Index[n]]*numerical_order) >= numerical_order)
{
sprintf(h, "<qt><nobr>More parts with prefix '%s' than starting point %d on sheet %d<br>Start the ulp with <b>numerical order</b> >= %d</nobr>",
memprefix, numerical_order, Sheet[Index[n]], numerical_order*10);
dlgMessageBox(h, "Break");
exit(0);
}
}
}
return;
}
//--------------------------------------------------------------------------------
// output script for renumber part
//--------------------------------------------------------------------------------
void Rename(int x, int y, string New) {
// Generates the EAGLE command necessary to change element name Old to New
sprintf(c, "Name '%s' (%d %d);#209\n", New, x, y);
cmd += c;
return;
}
//--------------------------------------------------------------------------------
// Generates an EAGLE script file that does the whole renumbering.
// The tricky part here is that we cannot rename an element to a name
// that already exists in the schematic (which, e.g. might be necessary if
// we had to swap the names of two elements). Therefore we have to
// use a ScratchName wherever this is necessary.
// If there is nothing to do, the resulting script file will be empty.
void GenerateScript(void)
{
string ScratchName;
int sch = 0;
int n;
for ( n = 0; n < nrNames; ++n) {
if (Sheet[Index[n]] != sch) {
sch = Sheet[Index[n]]; // *** change sheet
sprintf(c, "Edit .s%d;\n", sch);
cmd += c;
}
sprintf( ScratchName, "$%d_%d_$", sch, n);
Rename(x[Index[n]],y[Index[n]], ScratchName); // output script for renumber part
}
for ( n = 0; n < nrNames; ++n) {
if (Sheet[Index[n]] != sch) {
sch = Sheet[Index[n]]; // *** change sheet
sprintf(c, "Edit .s%d;\n", sch);
cmd += c;
}
Rename(x[Index[n]],y[Index[n]], NewNames[Index[n]]); // output script for renumber part
}
return;
}
//--------------------------------------------------------------------------------
// *** check collision before rename ***
//--------------------------------------------------------------------------------
string CheckNames(void) {
string new_name = ";";
string h;
for (int Dn = 0; Dn < Dnr; Dn++ ) {
for (int Sn = 0; Sn < Snr; Sn++) {
if (DevPrefix[Dn] == SymPrefix[Sn]) {
sprintf(h, "# Do not use Prefix %s on Device with Package (%s) and Device without Package (%s)\n",
SymPrefix[Sn], DevName[Dn], SymDevName[Sn]);
Error += h;
break;
}
}
}
for (int n = 0; n < nrNames - 1; ++n) { // make a long string
new_name += NewNames[n] + ";";
}
for (int xx = 0; xx < Snr - 1; xx++) {
string sd = SymNames[xx];
if(sd[0] == '$') { // if first character is a $ on Symbolname
Error += "# Do not use $ character at first position in device names\n";
sprintf(h, "# RENAME %s at (%.4f %.4f) - sheet %d before running this ULP again' (%.4f %.4f)\n",
SymNames[xx], SymX[xx] / 1000.0, SymY[xx] / 1000.0, symsh[xx], SymX[xx] / 1000.0, SymY[xx] / 1000.0);
Error += h;
}
int s;
int pos = strrstr(new_name, ";" + SymNames[xx] + ";");
if (pos > 0 ) {
for (s = 0; s < nrNames - 1; s++)
{
if(NewNames[s] == SymNames[xx])
{
break;
}
}
Error += "# Collision of symbol name and device name (eg. Frames, Supply ...)\n";
sprintf(h, "# Rename PREFIX of Device %s at (%.4f %.4f) - sheet %d before renaming %s at (%.4f %.4f) - sheet %d';\n",
SymNames[xx], SymX[xx] / 1000.0, SymY[xx] / 1000.0, symsh[xx],
OldNames[s], x[s] / 1000.0, y[s] / 1000.0, Sheet[s] );
Error += h;
}
}
return Error;
}
//--------------------------------------------------------------------------------
void setgridmil (void) {
sprintf(c, "GRID MIL 100 OFF;\n");
cmd += c;
// ## only display layer 94 (symbol) if placed a text
// ## at symbol origin. 15.06.2004 [email protected]
sprintf(c, "DISPLAY NONE 94 -95 -96;\n");
cmd += c;
return;
}
//--------------------------------------------------------------------------------
void visible(UL_SCHEMATIC S) {
sprintf(c, "DISP NONE ");
cmd += c;
S.layers(L) {
if (L.visible) {
sprintf(c, "%d ", L.number);
cmd += c;
}
}
cmd += ";\n";
return;
}
void menue(void) {
int Result = dlgDialog("Renumber Schematic") {
dlgLabel("<font color=red>" + Info + "</font>");
dlgHBoxLayout {
dlgGroup("Sort X") {
dlgRadioButton("&Ascending", descx);
dlgRadioButton("&Descending", descx);
}
dlgGroup("Sort Y") {
dlgRadioButton("A&scending", descy);
dlgRadioButton("D&escending", descy);
}
dlgGroup("Weighting of emphases") {
dlgRadioButton("X-direction", emphases);
dlgRadioButton("Y-direction", emphases);
}
dlgStretch(1);
}
dlgHBoxLayout {
dlgLabel("Do not renumber parts with &Prefix ");
dlgStringEdit(NoPrefixRenumber);
dlgStretch(1);
}
dlgHBoxLayout {
dlgVBoxLayout dlgSpacing(180);
dlgGroup("Sheet") {
dlgLabel("Start numbering for sheet at:");
dlgLabel(" - 0 numeration R1...Rn");
dlgLabel(" - 100 sheet 1: R101..R199, sheet 2: R201..R299, ...");
dlgLabel(" - 1000 sheet 1: R1001..R1999, sheet 2: R2001..R2999, ...");
dlgSpacing(10);
dlgHBoxLayout {
dlgLabel("&Numerical order ");
dlgIntEdit(numerical_order, 0, 10000);
dlgStretch(1);
}
dlgVBoxLayout dlgSpacing(10);
dlgCheckBox("Sort in numerical order on the ¤t sheet only", only_actual_sheet) {
if (only_actual_sheet) {
if (numerical_order) {
sprintf(sheet_info, "<font color=\"blue\">The starting number of current sheet is %d</font>", actual_sheet * numerical_order);
}
else {
sprintf(sheet_info, "<font color=\"red\">Please check the numerical order!</font>");
}
}
else {
if (!numerical_order) sprintf(sheet_info, "The starting number is 1");
else sprintf(sheet_info, "The starting number on sheet is X * %d", numerical_order);
}
}
dlgVBoxLayout dlgSpacing(10);
dlgLabel(sheet_info, 1);
dlgStretch(1);
}
}
dlgHBoxLayout {
dlgPushButton("+&OK") {
if (only_actual_sheet && !numerical_order) {
sprintf(sheet_info, "<nobr><font color=\"red\">Please check the numerical order!</font></nobr>");
dlgMessageBox(sheet_info, "OK");
}
else dlgAccept();
}
dlgSpacing(15);
dlgPushButton("-Cancel") dlgReject();
dlgSpacing(15);
dlgLabel(Version);
dlgStretch(1);
}
};
if (!Result) exit (0);
return ;
}
if (schematic) {
if (sheet) {
sheet(S) actual_sheet = S.number;
sprintf(sheet_info, "The current sheet is %d", actual_sheet);
}
schematic(S) {
menue();
int l = 1;
int chk;
// find the hightest number part of the refance name
S.sheets(SH)
{
int tmpn;
string tmpS;
SH.parts(P)
{
if( ( tmpn = GetNumberIndex(P.name)) == -1 )
continue;
else
{
if( ( tmpn = strtol( strsub( P.name, tmpn ))) > maxRefNumber )
maxRefNumber = tmpn;
}
}
} // sheets(SH)
S.sheets(SH) {
if (only_actual_sheet) {
; // do not change the actual sheet number
}
else {
actual_sheet = SH.number; // set the numer to actuel sheet number
}
if (actual_sheet == SH.number) { // 2008.01.30
SH.parts(P)
{
int n = GetNumberIndex(P.name); // index to start of Refance number in Refance
if( n == -1 ) // Only renumber parts which have no numbers !
n = strlen(P.name);
else
n = 0; // Has Refance number so invert the meaning for the next check.
if (n > 0) // do we have a refance number on the end ?
{ // Yes !
if (P.device.package) // **** only Devices with Packages
{ // **** without Supply symbol Frames ect...
// **** DO NOT RENUMBER Elements with this PREFIX
if (prefix(P.name) == NoPrefixRenumber) // returns Refance without any traling number
; // Do thing is match
else {
DevPrefix[Dnr] = prefix(P.name); // Treturns Refnace without any traling number
DevName[Dnr] = P.name; // holds Refnace/prefix with traling number
++Dnr;
P.instances(I) {
int found = -1;
for (int fn = 0; fn < nrNames; fn++) {
if (OldNames[fn] == P.name)
{
found = fn;
break;
}
}
if (found < 0) {
x[nrNames] = u2mil(I.x); // cannot use E.x/y directly because of
y[nrNames] = u2mil(I.y); // sort() problem with integers > 32767
OldNames[nrNames] = P.name; // in version 3.50
NewNames[nrNames] = strsub(P.name, 0, n);
Sheet[nrNames] = I.sheet;
Prefix[nrNames] = prefix(P.name); // Treturns Refnace without any traling number
++nrNames;
}
else
{
if (Sheet[fn] == I.sheet)
{
if ( u2mil(I.x) < x[fn] || u2mil(I.y) > y[fn] )
{
// tausche wenn x kleiner oder y groesser
x[fn] > u2mil(I.x);
y[fn] > u2mil(I.y);
}
}
}
}
}
} // if (P.device.package) { // **** only Devices with Packages // Only Symbol (Supply, Port, Frame...)
else
{ // *** check PartName on Symbols Supply, Port, Frame ... ***
SymPrefix[Snr] = prefix(P.name); // Treturns Refnace without any traling number
SymDevName[Snr] = P.name; // holds Refnace/prefix with traling number
P.instances(I) {
SymNames[Snr] = P.name; // Device-Name of Symbol
SymX[Snr] = u2mil(I.x); // cannot use E.x/y directly because of
SymY[Snr] = u2mil(I.y); // sort() problem with integers > 32767
symsh[Snr] = I.sheet;
++Snr;
break;
}
}
} // if (n > 0) // do we have a refance number on the end ?
P.instances(I) {
ckx[chk] = u2mil(I.x); // cannot use E.x/y directly because of
cky[chk] = u2mil(I.y); // sort() problem with integers > 32767
ckname[chk] = I.name;
cksh[chk] = I.sheet;
chk++;
}
}
} // 2008.01.30
}
CheckSameOrigin(chk);
SortElements();
GenerateNames(); // Add new number to Refances and put in NewNames
setgridmil ();
GenerateScript();
if (CheckNames()) { //check collision before rename ***
int select;
dlgDialog("Symbol ref Device Names") {
dlgVBoxLayout {
dlgLabel("Warnings for renumber!");
dlgTextView(Error);
}
dlgHBoxLayout {
dlgSpacing(450);
}
dlgHBoxLayout {
dlgPushButton("+&OK") dlgAccept();
dlgStretch(1);
}
};
exit (-1);
}
sprintf(c, "GRID INCH 0.1;\n");
cmd += c;
sprintf(c, "EDIT .S%d;\n", actual_sheet);
cmd += c;
visible(S);
string fname = filesetext(S.name, "~renumsch.scr");
output(fname, "wtD") printf("%s", cmd);
exit ("SCRIPT '" + fname + "';");
}
}
else {
dlgMessageBox("\n Start this ULP in a Schematic \n");
exit (0);
}