/* cvtmount.c */
/* creates partial mount files for CSM84 from Protel pick&place file  */
#include <stdio.h>
#include <string.h>
#include <math.h>

static double X, Y, R, rot;
static double dummy1,dummy2,dummy3,dummy4;
static long I,J, Flag, Mnum,fdr_orient;
int cp,NumFeeders,head, fdrslot;
static char InFileName[256], FileName[256], CompFileName[256];
static char Designator[16],Footprint[16],PartType[16];
static char TopBot,a[256];
struct FEEDER {
  int FdrSlot;               // feeder slot number [1-84]
  char PartName[16];         // name of part
  double orient;             // orientation in feeder
  int head;                  // head to mount with [1-3]
};
struct FEEDER feeders[99];

// static bool Done;
static FILE *InFile, *OutFile, *CompFile;

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

  for (i = 1.0, j = 0; (i <= 3) || (i<= fabs(RealNum)); i *= 10.0, j++)
    {}
  //  printf("i j = %f %d, fabs(RealNum) = %f\n",i,j,fabs(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);
  //  printf("format is : %s  Number is %f\n",format,RealNum);
    fprintf(OutFile, format, (RealNum));
}


void WriteLineNo()
{
  fprintf(OutFile,"M%d  =  ",Mnum);
  Mnum += 1;
}

void compfile(char *FileName) {
  NumFeeders = 1;
  CompFile = fopen(FileName,"r");

  while (feof(CompFile) == 0) {
    fscanf(CompFile,"%d %s %lg %d\n",&feeders[NumFeeders].FdrSlot,feeders[NumFeeders].PartName,
	   &feeders[NumFeeders].orient,&feeders[NumFeeders].head);
    printf("feeder %d is %d %s %f %d\n",NumFeeders,feeders[NumFeeders].FdrSlot,&feeders[NumFeeders].PartName,
	   feeders[NumFeeders].orient,feeders[NumFeeders].head);
    NumFeeders ++;
  }
  fclose(CompFile);
  //  NumFeeders--;           // adjust count of components
}

main(int argc, char *argv[])
{
  I = 2;
  Flag = 0;
  Mnum = 0;
  OutFile = NULL;
  InFile = NULL;
  CompFile = NULL;
  if (argc < 4) {
    printf("Usage : component_file,  pick&place file, top_mount_file, bot_mount_file\n");
    exit(-1);
  }
  sscanf(argv[1],"%s", &CompFileName);
  sscanf(argv[2],"%s", &InFileName);
  sscanf(argv[3],"%s", &FileName);
  compfile(CompFileName);             // read the component file
  //    open the data file
  InFile = fopen(InFileName,"r");
  fscanf(InFile,"%s %s %s %s %s %s %s %s %s %s %s %s",a,a,a,a,a,a,a,a,a,a,a,a);  // throw away first two lines
  fscanf(InFile,"%s %s %s %s %s\n",a,a,a,a,a);  // throw away first two lines
  fscanf(InFile,"\n");  // throw away first two lines
  OutFile = fopen(FileName,"w");
  if (OutFile != NULL)
    rewind(OutFile);
  else
    OutFile = tmpfile();
  if (OutFile == NULL)
    exit(-1);
  while (feof(InFile) == 0) {
      printf("reading line %d\n",I);
      fscanf(InFile,"%s %s %lgmm %lgmm %lgmm %lgmm %lgmm %lgmm %c %lg %s\n",Designator,Footprint,&X,&Y,&dummy1,&dummy2,
	     &dummy3,&dummy4,&TopBot,&R,PartType);
      printf("%s %s %lg %lg %lg %lg %lg %lg %c %lg '%s'\n",&Designator,&Footprint,X,Y,dummy1,dummy2,
	     dummy3,dummy4,TopBot,R,&PartType);
      if (TopBot == 'B' && Flag == 0) {
	Flag = 1;               // set flag to say we are doing bottom side now
	Mnum = 0;               // reset mount line number
	fclose(OutFile);
	sscanf(argv[4],"%s", &FileName);
	OutFile = fopen(FileName,"w");
	if (OutFile != NULL)
	  rewind(OutFile);
	else
	  OutFile = tmpfile();
	if (OutFile == NULL)
	  exit(-1);
      }
      head = 0;                    // 0 means no component match
      for (J=1;J<=NumFeeders;J++) {
	if (strcmp(PartType,feeders[J].PartName) == 0) {
	  fdr_orient = feeders[J].orient;
	  head = feeders[J].head;
	  fdrslot = feeders[J].FdrSlot;
	  printf("matched part %s to component %d\n",&PartType,J);
	  break;
	}
      }
      if (head == 0) {
	printf("Failed to match part '%s' to any component.\n",&PartType);
	//	exit(-1);
      }
      else {
	WriteLineNo();
	if (Flag == 1)  X = -X;  // mirror X axis for back side of board
	outNum(X,2);
	fprintf(OutFile,"  ");
	outNum(Y,2);
	fprintf(OutFile,"  ");
	rot = R-feeders[J].orient;  // correct rotation on parts that are not at nominal rotation in feeder
	if (Flag == 1) rot = -rot;  // mirror rotation on back side of board
	if (rot == 360.00) rot = 0.0;
	if (rot == 270.00) rot = -90.0;
	if (rot == -270.00) rot = 90.0;
	if (rot > 360.00) rot -= 360.0;
	if (rot < -360.00) rot += 360.0;
	outNum(rot,2);
	fprintf(OutFile,"  ");
	fprintf(OutFile, "%d000%2.2d\n",head,fdrslot);
      }
      I++;              // increment input file line counter
  }
  fclose(OutFile);
}



/* End. */




