/* spotdrilltap.c */
/* creates 3 files to spot, drill and tap a sequence of
   holes */
#include <stdio.h>
#include <string.h>

static double HoleDepth, FeedRate, X, Y;
static double SpotDepth, SpotFeed, DrillDepth, DrillFeed, HoleFeed, TapDepth,TapFeed;
static double RPM, TPI;
static double LineNo;
static long I,J, NumX,NumY,NumCoords;
int cp;
static char InFileName[256], FileName[256];
// static bool Done;
static FILE *InFile, *OutFile;
static double X_List[1000],Y_List[1000];

void outNum(RealNum, SigFrac)
double RealNum;
long SigFrac;
{
  double i;
  int j;
  char format[8];

  for (i = 1.0, j = 0; i <= 6 || i<= abs(RealNum); i *= 10.0, j++)
    {}
  if (SigFrac > 0) 
    j += SigFrac+1;  /* J <= # int digits + 1 for decimal pt + #frac digits */
  if (RealNum < 0.0 )
    j += 1;          /* add a position for the minus sign */
  sprintf(format,"%%%d.%df",j,SigFrac);
    fprintf(OutFile, format, (RealNum));
}


void WriteLineNo()
{
  putc('N', OutFile);
  outNum(LineNo, 0L);
  LineNo += 10.0;
  putc(' ', OutFile);
}

void runfile(char *FileName, double Depth, double Feed,
		      double RPM, double TPI) {
 OutFile = fopen(FileName,"w");
  if (OutFile != NULL)
    rewind(OutFile);
  else
    OutFile = tmpfile();
  if (OutFile == NULL)
    exit(-1);
  if (RPM == 0.0)
    FeedRate = Feed;
  else
    FeedRate = RPM / TPI;
  LineNo = 10.0;
  for (I=1; I <= NumCoords; I++)
  {
    WriteLineNo();
    fprintf(OutFile, "G01 F45 ");   /* Linear Interpolation */
    putc('X', OutFile);
    outNum(X_List[I], 4L);
    fprintf(OutFile, " Y");
    outNum(Y_List[I], 4L);
    putc('\n', OutFile);
    WriteLineNo();
    fprintf(OutFile, " Z");
    outNum(0.1,4L);
    putc('\n', OutFile);
    WriteLineNo();
    fprintf(OutFile, "F");
    outNum(FeedRate,2L);
    fprintf(OutFile, " Z");
    outNum(-Depth,4L);
    putc('\n', OutFile);
    WriteLineNo();
    fprintf(OutFile, "F");
    if (RPM == 0.0)
      outNum(45.0,2L);
    else
      outNum(FeedRate*2.0,2L);
    fprintf(OutFile, " Z");
    outNum(0.1,4L);
    putc('\n', OutFile);
    WriteLineNo();
    fprintf(OutFile,"F30 Z");
    outNum(0.5,4L);
    putc('\n', OutFile);
  }
  WriteLineNo();
  fprintf(OutFile, "M02\n");   /*End of Program */
  if (OutFile != NULL)
    fclose(OutFile);
  OutFile = NULL;
  if (OutFile != NULL)
    fclose(OutFile);
}

main(argc, argv)
int argc;
char *argv[];
{
  OutFile = NULL;
  printf("Enter name of file that lists hole X Y coordinates:");
  scanf("%s", &InFileName);
  printf("\n");
  getchar();
  //    read in the data file
  InFile = fopen(InFileName,"r");
  for (I=1; I <= 1000; I++)
    if (feof(InFile) == 0) {
      printf("reading line %d\n",I);
      fscanf(InFile,"%lg%lg\n",&X,&Y);
      X_List[I] = X;
      Y_List[I] = Y;
    }
    else {
      NumCoords = I-1;
      fclose(InFile);
      I=1001;
   }
  printf("%d coordinates read from file.\n",NumCoords);

  printf("\nEnter  depth to spot, and feed rate (real):");
  scanf("%lg%lg%*[^\n]", &SpotDepth,&SpotFeed);
  getchar();
  printf("\nEnter  depth to drill, and feed rate (real):");
  scanf("%lg%lg%*[^\n]", &DrillDepth,&DrillFeed);
  getchar();
  printf("\nEnter  depth to tap (real):");
  scanf("%lg%*[^\n]", &TapDepth);
  getchar();
  printf("\nEnter tapping spindle RPM, # threads/inch (real):");
  scanf("%lg%lg%*[^\n]", &RPM, &TPI);
  getchar();
  //  printf("\nEnter File Name for CAM output:");
  //scanf("%s",&FileName);
  //putchar('\n');
  cp = strstr(InFileName,".") - InFileName;
  strncpy(FileName,InFileName,cp);
  strcat(FileName,"spot.cam");
  runfile(FileName,SpotDepth,SpotFeed,0.0,0.0);
  FileName[cp]='\0';
  strcat(FileName,"drill.cam");
  runfile(FileName,DrillDepth,DrillFeed,0.0,0.0);
  FileName[cp]='\0';
  strcat(FileName,"tap.cam");
  runfile(FileName,TapDepth,TapFeed,RPM,TPI);
}



/* End. */



