#usage "<b>Rename NETs in a schematic with NET name suffix</b><p>"
       "<author>Author: support@cadsoft.de</author>";

// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED.

string Help =
       "Wird ein Netz umbenannt, dass aus mehreren Segmenten besteht,\n" +
       "bzw. auf mehrere Sheets verteilt ist, muss die Abfrage:\n" +
       " Change Name of:\n" +
       " This Segment\n" +
       " Every Segment on this Sheet\n" +
       " All Segments on all Sheets\n\n" +
       "immer mit 'Every Segment on this Sheet' bzw.\n" +
       "'All Segments on all Sheets' beantwortet werden.\n\n";



string Version = "Version 1.0";

                       // *********************************************
string Suffix = "";    // **** Anhang an den Netznamen,            ****
                       // **** kann nach belieben geändert werden. ****
                       // *********************************************

real Grid = 100; // in 100 Mil


int index[];
int s = 0, pg = 0;

numeric string    n[];        // P.name;
        int       is[];        // I.sheet;
        int       ix[];
        int       iy[];

string fileName;

// ### Functions ###
void setgridmil (void) {
  printf("GRID mil 100;\n");
  return;
}

void editsheet(int i) {
  if (s != is[i]) {          // Sheet wechseln
    s = is[i];
    printf("EDIT .S%d;\n",s);
    }
  return;
}

void namescript(void) {
  for (int i = 0; i < pg; i++) {
    editsheet(i);                // ** Change Sheet
    printf("NAME '%s%s' (%.2f %.2f);\n", n[i], Suffix, u2mil(ix[i]), u2mil(iy[i]));
  }
  return;
}

int found (string f)
{
  for (int i = 0; i < pg; i++) {
    if (n[i] == f) return i;
    }
  return -1;
}

// ** MAIN **
if (schematic) {
  schematic(SCH) {
    fileName = filesetext(SCH.name, "renamenet-suffix~~~.scr");

    int Result = dlgDialog("Rename NET in Schematic") {
      dlgLabel(Help);
      dlgHBoxLayout {
        dlgLabel("Net &suffix");
        dlgStringEdit(Suffix);
        dlgStretch(1);
      }
      dlgHBoxLayout {
        dlgPushButton("OK") dlgAccept();
        dlgPushButton("-Cancel") dlgReject();
        dlgStretch(1);
      }
    };
    if (!Result) exit(0);

    output(fileName, "wtD") {

      printf("# 'This file is generated by %s, exported from;\n", Version);
      printf("# '%s at %s;\n", SCH.name, t2string(time()));
      printf("# '%s;\n\n", EAGLE_SIGNATURE);
      setgridmil();
      printf("DISPLAY NONE 91;\n");

      // collect all Nets
      SCH.sheets(SH) {
        SH.nets(N) {
          N.segments(SEG) {
            SEG.wires(W) {
              n[pg]  = N.name;
              ix[pg] = W.x1;
              iy[pg] = W.y1;
              is[pg] = SH.number;
              if (found(n[pg]) == -1) pg++;
              break;
            }
            break;
          }
        }
      }
      namescript();

      printf("GRID INCH;\n");
      printf("GRID 0.1;\n");
      printf("DISPLAY ALL -93;\n");
    }
    exit ("SCRIPT '" + fileName + "';");
  }
}
else dlgMessageBox("Start this ULP in Schematic!", "OK");