/* spotdrilltap.c */
/* creates 3 files to spot, drill and rigid tap a sequence of
   holes, specified as a file of XY coords */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static double HoleDepth, FeedRate, X, Y;
static double SpotDepth, SpotFeed, DrillDepth, DrillFeed, HoleFeed, TapDepth,TapFeed;
static double SpotRPM, DrillRPM, TapRPM, 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];

  j=0;
  if (RealNum > 1.0 || RealNum < -1.0) {
    for (i = 1.0, j = 0; (i <= 6 || i<= abs(RealNum)); i *= 10.0, j++)
      {}
    /*      {printf("i=%f    j=%d    RealNum=%f \n",i,j,RealNum);}  */
  }
  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;
  WriteLineNo();
  fprintf(OutFile, "G61.1 M03 S");   /* exact stop mode */
  outNum(RPM, 0L);
  putc('\n', OutFile);
  WriteLineNo();
  fprintf(OutFile, "G01 Z0.5 F45\n");   /* exact stop mode */

  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();
    if (TPI < 0.01) {
      fprintf(OutFile, " Z");
      outNum(0.1, 4L);
      putc('\n', OutFile);
      WriteLineNo();
      fprintf(OutFile, " Z");
      outNum(-Depth,4L);
      fprintf(OutFile, " F");
      outNum(Feed,2L);
      putc('\n', OutFile);
      WriteLineNo();
      fprintf(OutFile, "Z");
      outNum(0.1,4L);
      fprintf(OutFile, " F");
      outNum(45.0,2L);
      putc('\n', OutFile);
    } else {
      fprintf(OutFile, " G33.1 Z");
      outNum(-Depth, 4L);
      fprintf(OutFile, " K");
      outNum((1.0/TPI), 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, feed rate, and RPM(real):");
  scanf("%lg%lg%lg%*[^\n]", &SpotDepth,&SpotFeed,&SpotRPM);
  getchar();
  printf("\nEnter  depth to drill, feed rate, and RPM (real):");
  scanf("%lg%lg%lg%*[^\n]", &DrillDepth,&DrillFeed,&DrillRPM);
  getchar();
  printf("\nEnter  depth to tap (real):");
  scanf("%lg%*[^\n]", &TapDepth);
  getchar();
  printf("\nEnter tapping spindle RPM, # threads/inch (real):");
  scanf("%lg%lg%*[^\n]", &TapRPM, &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.ngc");
  runfile(FileName,SpotDepth,SpotFeed,SpotRPM,0.0);
  FileName[cp]='\0';
  strcat(FileName,"drill.ngc");
  runfile(FileName,DrillDepth,DrillFeed,DrillRPM,0.0);
  FileName[cp]='\0';
  strcat(FileName,"tap.ngc");
  runfile(FileName,TapDepth,TapFeed,TapRPM,TPI);
}

/* End. */



