/*
 *  This EAGLE User Language Program produces a script file (the "rotate.scr")
 *  that can be used to rotate around the orign point
 *  a package of the current library to a given angle.
 */

// Rotate all (unique!) pads, holes, polygons wires and circles (not recomented) but doesn't rotate arcs, rectangles, smds and texts

real New_Angle = 30;                          // Give the desired New_Angle
                                              // to rotate the part in degrees
string SCR_Path = "c:\\work\\eagle35\\scr";   // Give the path for the script files (SCR) with
                                              // double "\" ; the output script is "rotate.scr"
New_Angle *= 10;
New_Angle = round (New_Angle);
New_Angle /= 10;
while (New_Angle < 0) New_Angle += 360;
while (New_Angle > 359.9) New_Angle -= 360;
New_Angle /= 180;
New_Angle *= PI;

int n, X[], Y[], L[], used_layers[];

int is_new (int x, int y, int l)
{
  int a = 1;
  for (int i = 0; i < n; i++)
  {
    if (X[i] == x && Y[i] == y && L[i] == l) a = 0;
  }
  X[n] = x;
  Y[n] = y;
  L[n] = l;
  n++;
  return (a);
}

void move (int x_cord, int y_cord, int layer)
{
  real x_pos, y_pos;
  x_pos = + u2mil(x_cord) * cos(New_Angle) + u2mil(y_cord) * sin(New_Angle);
  y_pos = - u2mil(x_cord) * sin(New_Angle) + u2mil(y_cord) * cos(New_Angle);
  // Go through the used layers (this avoids problems with wires on different layers
  // that are selected at the same coordinates), but remember the used layers for display:
  used_layers[layer] = 1;
  printf("DISPLAY NONE %d;\n", layer);
  printf ("MOVE (%f %f) (%f %f);\n", u2mil(x_cord), u2mil(y_cord), x_pos, y_pos);
}
  
if (package) package (P)
  {
    output (SCR_Path + "\\rotate.scr")
    {
      printf ("GRID MIL FINEST;\n");

      P.contacts (C)
      {
        // Since cut rotate pad shapes make them round (if they are not)
        if (C.pad.shape != PAD_SHAPE_ROUND) printf ("CHANGE SHAPE ROUND (%f %f);\n", u2mil(C.pad.x), u2mil(C.pad.y));
        move (C.x, C.y, LAYER_PADS);
      }

      P.holes (H) 
      {
        move (H.x, H.y, LAYER_HOLES);
      }

      P.polygons (P)
      {
        n = 0;
        P.wires (W)
        {
          if (is_new (W.x1, W.y1, W.layer)) move (W.x1, W.y1, W.layer);
          if (is_new (W.x2, W.y2, W.layer)) move (W.x2, W.y2, W.layer);
        }
      }

      n = 0;
      P.wires (W)
      {
        if (is_new (W.x1, W.y1, W.layer)) move (W.x1, W.y1, W.layer);
        if (is_new (W.x2, W.y2, W.layer)) move (W.x2, W.y2, W.layer);
      }

      P.circles (C)
      {
        move (C.x, C.y, C.layer);
      }

      // Reactivate the active layers:
      printf("DISPLAY");
      for (int i = 1; i < 256; i++) if (used_layers[i]) printf(" %d", i);
      printf(";\n");

      printf("GRID LAST;\n");
    }
  }
