#usage "<b>Export data for SMD solder cream dispenser</b>\n"
       "<p>"
       "Generates data to control the solder cream dosage for the dispenser "
       "unit of 'Martin' on 'Bungard' drill/milling machines."
       "<p>"
       "The file <i>boardname.plt</i> contains the coordinates of the SMD pads "
       "in the Top layer, while <i>boardname.plb</i> contains those in the "
       "bottom layer."
       "<p>"
       "The file <i>boardname.dod</i> contains the information of the SMD pads "
       "regarding tool number Txx."
       "<p>"
       "<author>Author: support@cadsoft.de</author>"

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

/*
    Mit uval = 0 - 3 kann die Einheit ** der Daten eingestellt werden.

    **  0: 1/10 mil
    **  1: 1 mil
    **  2: 0.025 mm
    **  3: 0.01 mm

 *06.12.2001 az
*/

int uval = 3;

string Version = "1.0.4";

int smdx[], smdy[];    // size of smd
int x[], y[];          // coordinate of SMD
int tb[];              // top or bottom side
int index[];
int c = 0;  // counter

void header(void) {
  switch (uval) {
      case 0 :
         printf("1/10 mil\n%%\n");
         break;

      case 1 :
         printf("1 mil\n%%\n");
         break;

      case 2 :
         printf("0.025 mm\n%%\n");
         break;

      case 3 :
         printf("0.01 mm\n%%\n");
         break;
   }
}

real value (int v) {
  switch (uval) {
    case 0 :
      return (u2mil(v) * 10);

    case 1 :
      return (u2mil(v));

    case 2 :
      return (u2mm(v) * 40);

    case 3 :
      return (u2mm(v) * 100);
  }
}

void genfiles(string file) {
  sort(c, index, tb, smdx, smdy, x, y);  // sortiere top/bottom, groesse, coord.

  int tool = 0;
  int ox =0;
  int oy =0;

  for (int n = 0; n < c; n++) {
    if (smdx[index[n]] == ox && smdy[index[n]] ==oy) {
       ;
      }
    else {                  // *** change tool #
      ox = smdx[index[n]];
      oy = smdy[index[n]];
      tool++;

      printf("T%02d\n", tool);       // print tool change in file #1

      output(filesetext(file, ".plb"), "at") {
        printf("T%02d\n", tool);     // print tool change in file #2
        }

      output(filesetext(file, ".dod"), "at") {   // print in file #3
         printf("T%02d  X%06.0f Y%06.0f\t| %06.2f\n", tool,
                value(smdx[index[n]]), value(smdy[index[n]]),
                u2mm(smdx[index[n]]) * u2mm(smdy[index[n]]) );
        }
      }

    if (tb[index[n]]) {
      printf("X%06.0fY%06.0f\n",
               value(x[index[n]]), value(y[index[n]]) );
      }
    else {
      output(filesetext(file, ".plb"), "at") {
        printf("X%06.0fY%06.0f\n",
                value(x[index[n]]), value(y[index[n]]) );
        }
      }
    }
  }

void  trailer(void) { printf("M30\n"); }

void menue(void) {
   dlgDialog("Dose pro") {
      dlgLabel (usage);
      int align = 1;
      dlgHBoxLayout {
         dlgGroup("Data resolution") {
            dlgRadioButton("1/10 mil", uval);
            dlgRadioButton("1 mil", uval);
            dlgRadioButton("0.025 mm", uval);
            dlgRadioButton("0.01 mm", uval);
            }
         dlgStretch(1);
         }
      dlgHBoxLayout {
         dlgPushButton("+&OK") dlgAccept();
         dlgPushButton("-&Cancel") exit (0);
         dlgStretch(1);
         }
      };
   return;
   }

// main
if (board) {
  board(B) {
    menue();
    output(filesetext(B.name, ".dod"), "wt") {
      printf("This file is generated by %s %s, exported from:\n", filename(argv[0]), Version);
      printf("%s at %s;\n", B.name, t2string(time()));
      printf("%s;\n\n", EAGLE_SIGNATURE);
      }
    output(filesetext(B.name, ".plb"), "wt") {
      header();
      }
    output(filesetext(B.name, ".plt"), "wt") {
      header();
      B.elements(E) {
        E.package.contacts(C) {
          if (C.smd) {

             // collect SMD-Pads
             smdx[c] = C.smd.dx;
             smdy[c] = C.smd.dy;
             x[c] = C.smd.x;
             y[c] = C.smd.y;

             if (E.mirror) {
                tb[c] = 0;
                }
             else {
                tb[c] = 1;
                }
             c++;
          }
        }
      }

    output(filesetext(B.name, ".dod"), "at") {
    printf("%d SMD-Pads\n\n", c);
    header();
    printf("Tool  Pad-X    Pad-Y      Square mm^2\n");
      }
    genfiles(B.name);
    trailer();
    output(filesetext(B.name, ".plb"), "at") { trailer(); }
    }
  }
}

else dlgMessageBox("Start this ULP from a Board!", "OK");
