/*
 *  This EAGLE User Language Program prints the netlist with x,y locations of pads
 *  of a board or net list of schematic.
 *  The output files are named boardname_tst.txt and schematicname_net.txt respectively.
 *
 *  2005-07-27 by support@cadsoft.de
 *  Now the position and drill diameter of vias will be taken into consideration,too.
 */

if (board) board(B) {
   output(filesetext(B.name, "_tst.txt")) {
   printf("%s\n\n", EAGLE_SIGNATURE);
   printf("Netlist with pin locations exported from %s at %s\n\n", B.name, t2string(time()));
   printf("%-*s\n   %-*s %s %s %s\n", SIGNAL_NAME_LENGTH, "Net",
                            ELEMENT_NAME_LENGTH, "Part",
                            "Pad","    x","    y");
   B.signals(S) { 
     numeric string Part[], Pad[];
     real x[], y[];
     int cnt = 0, index[];

     S.contactrefs(C) {
       Part[cnt] = C.element.name;
       Pad[cnt] = C.contact.name;
       x[cnt] = u2mil(C.contact.x);
       y[cnt] = u2mil(C.contact.y);
       cnt++;
       }
     if (cnt) {
        sort(cnt, index, Part, Pad, x, y);
        printf("\n");
        for (int i = 0; i < cnt; i++)
            printf("%-*s\n   %-*s %s %7.2f %7.2f\n", 
                   SIGNAL_NAME_LENGTH, i ? "" : S.name, 
                   ELEMENT_NAME_LENGTH, Part[index[i]], 
                   Pad[index[i]], x[index[i]], y[index[i]]);
      printf("\n");
      S.vias(V) {
      printf("         Via       %7.2f %7.2f  %3.2f \n", 
            u2mil(V.x), u2mil(V.y), u2mil(V.drill));
      }
     }
    }
   }
 }

if (schematic) schematic(SCH) {
   output(filesetext(SCH.name, "_net.txt")) {
   printf("%s\n\n", EAGLE_SIGNATURE);
   printf("Netlist exported from %s at %s\n\n", SCH.name, t2string(time()));
   printf("%-*s\n   %-*s %s\n", NET_NAME_LENGTH, "Net",
                            PART_NAME_LENGTH, "Part",
                            "Pin");
   SCH.nets(N) {
     numeric string Part[], Pin[];
     int cnt = 0, index[];

     N.pinrefs(P) {
       Part[cnt] = P.part.name;
       Pin[cnt] = P.pin.name;
       cnt++;
       }
     if (cnt) {
        sort(cnt, index, Part, Pin);
        printf("\n");
        for (int i = 0; i < cnt; i++)
            printf("%-*s\n   %-*s %s\n", 
                   NET_NAME_LENGTH, i ? "" : N.name, 
                   PART_NAME_LENGTH, Part[index[i]], 
                   Pin[index[i]]);
        }
     }
   }
 }
