#include <cgx.h>


extern double gtol;
extern Scale     scale[1];
extern Summen    anz[1];

extern Alias     *alias;
extern Sets      *set;
extern Materials *material; 
extern Psets     *pset;
extern Points    *point;
extern Lines     *line;
extern Lcmb      *lcmb;
extern Gsur      *surf;
extern Gbod      *body;
extern Nurbl     *nurbl;
extern Nurbs     *nurbs;
extern Materials *material; 
extern Amplitudes *amplitude; 
extern SumGeo    anzGeo[1];
extern SumAsci   sumAsci[1];
extern Nodes     *node;

extern int       num_onod;          /* nr-of-original-nodes (nodes from input-file) */
extern int       num_olc;
extern SpecialSet specialset[1];

void readstep( unsigned char *datin, int addFlag )
{
  FILE      *handle1;
  int       result=0, length, i, gtolFlag=0;
  unsigned char string[MAX_LINE_LENGTH];
  unsigned char command[MAX_LINE_LENGTH];
  unsigned char buffer[MAX_LINE_LENGTH];
  unsigned char entityName[MAX_LINE_LENGTH];
  unsigned char setName[MAX_LINE_LENGTH];
  unsigned char P1[MAX_LINE_LENGTH],P2[MAX_LINE_LENGTH];
  double max[3], min[3];
  double px,py,pz;

  handle1 = fopen (datin, "r");
  if (handle1==NULL)
  {
    printf (" ERROR: The input file \"%s\" could not be opened.\n\n", datin);
    return;
  }
  else  printf ("\n%s opened",datin);

  printf ("\n reading file\n");
  while (result > -1)
  {
    length=frecord( handle1, string);
    if (string[length] == (unsigned char)EOF) break;

    /* make all uppercase */
    i=0; while(string[i]!=(unsigned char)NULL) { string[i]=toupper(string[i]); i++; }

    /* extract commandNr command parameters */
    i=0; while(string[i]!=(unsigned char)NULL)
    {
      if(string[i]=='=') string[i]=' ';
      else if(string[i]=='(') string[i]=' ';
      else if(string[i]==')') string[i]=' ';
      else if(string[i]==',') string[i]=' ';
      else if(string[i]==';') string[i]=' ';
      i++;
    }
    command[0]=(unsigned char)NULL;
    sscanf(string,"%*s %s",command);
    // printf("%s\n", string);

    if (compareStrings(command, "CARTESIAN_POINT")>0)
    {
      /* point coordinates */
      sscanf(string,"%s %*s %s %lf %lf %lf",entityName,setName,&px,&py,&pz);
      setName[0]='S'; setName[strlen(setName)-1]=(unsigned char)NULL;

      sprintf( buffer, "%s %lf %lf %lf",entityName,px,py,pz);
      pre_pnt( buffer, 0 );
      pre_seta( setName, "p", entityName);
    }
    else if (compareStrings(command, "LINE")>0)
    {
      /* straight line */
      sscanf(string,"%s %*s %s %s %s",entityName,setName,P1,P2);
      setName[0]='S'; setName[strlen(setName)-1]=(unsigned char)NULL;

      sprintf( buffer, "%s %s %s",entityName,P1,P2);
      pre_line( buffer, 0 );
      pre_seta( setName, "l", entityName);
    }
  }
  fclose( handle1);

  if(!gtolFlag)
  {
    /* calculate the geometric tolerance based on all line-end-points */
    for(i=0; i<3; i++)
    {
      max[i]=-MAXVALUE;
      min[i]= MAXVALUE;
    }
    for(i=0; i<anzGeo->p; i++) if(point[i].name!=(unsigned char *)NULL)
    {
      if(point[i].px > max[0]) max[0]=point[i].px;
      if(point[i].py > max[1]) max[1]=point[i].py;
      if(point[i].pz > max[2]) max[2]=point[i].pz;
      if(point[i].px < min[0]) min[0]=point[i].px;
      if(point[i].py < min[1]) min[1]=point[i].py;
      if(point[i].pz < min[2]) min[2]=point[i].pz;
    }
    /*
    for(i=0; i<anzGeo->l; i++) if(line[i].name!=(unsigned char *)NULL)
    {
      if(point[line[i].p1].px > max[0]) max[0]=point[line[i].p1].px;
      if(point[line[i].p2].px > max[0]) max[0]=point[line[i].p2].px;
      if(point[line[i].p1].py > max[1]) max[1]=point[line[i].p1].py;
      if(point[line[i].p2].py > max[1]) max[1]=point[line[i].p2].py;
      if(point[line[i].p1].pz > max[2]) max[2]=point[line[i].p1].pz;
      if(point[line[i].p2].pz > max[2]) max[2]=point[line[i].p2].pz;
  
      if(point[line[i].p1].px < min[0]) min[0]=point[line[i].p1].px;
      if(point[line[i].p2].px < min[0]) min[0]=point[line[i].p2].px;
      if(point[line[i].p1].py < min[1]) min[1]=point[line[i].p1].py;
      if(point[line[i].p2].py < min[1]) min[1]=point[line[i].p2].py;
      if(point[line[i].p1].pz < min[2]) min[2]=point[line[i].p1].pz;
      if(point[line[i].p2].pz < min[2]) min[2]=point[line[i].p2].pz;
    }
    */
    gtol=0.;
    for(i=0; i<3; i++)
    {
      max[i]-=min[i];
      if(max[i]>gtol) gtol=max[i];
    }
    gtol=GTOL*gtol*scale->w;
    printf("\ngtol set to:%e\n\n", gtol);
  }

  /* delete the entities in specialSet->zap */
  zap(specialset->zap);

  descalNodes ( anz->n, node, scale );
  descalPoints( anzGeo->p, point, scale);
  descalSurfs( anzGeo->s, surf, scale);
  getScaleValues( anz->n, anzGeo->p, point, node, scale);
  scalNodes ( anz->n, node, scale );
  scalPoints ( anzGeo->p, point, scale );
  scalSurfs( anzGeo->s, surf, scale);

  /* recalculate the line-shapes */
  for (i=0; i<anzGeo->l; i++) rep(i);
  /* recalculate the nurbl-controll-points */
  for (i=0; i<anzGeo->nurl; i++) repNurl(i);
  /* recalculate the nurbs-controll-points */
  for (i=0; i<anzGeo->nurs; i++) repNurs(i);
  /* correct the orientation of all entities */
  orientSet( "all" );

  /* das neue netz muss noch zur beleuchteten ansicht aufbereitet werden  */
  num_olc       = anz->l;
  makeSurfaces();
  allocUtilityData();

  printf (" done \n\n");
}

