/* --------------------------------------------------------------------  */
/*                          CALCULIX                                     */
/*                   - GRAPHICAL INTERFACE -                             */
/*                                                                       */
/*     A 3-dimensional pre- and post-processor for finite elements       */
/*              Copyright (C) 1996 Klaus Wittig                          */
/*                                                                       */
/*     This program is free software; you can redistribute it and/or     */
/*     modify it under the terms of the GNU General Public License as    */
/*     published by the Free Software Foundation; version 2 of           */
/*     the License.                                                      */
/*                                                                       */
/*     This program is distributed in the hope that it will be useful,   */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */ 
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
/*     GNU General Public License for more details.                      */
/*                                                                       */
/*     You should have received a copy of the GNU General Public License */
/*     along with this program; if not, write to the Free Software       */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
/* --------------------------------------------------------------------  */

#include <cgx.h>

#define MAX_GSUR_PARAMERTER 7

int writefbd(unsigned char *setname, SumGeo *anz, Points *pnt, Lines *line, Lcmb *lcmb, Gsur *surf, Gbod *body, Nurbs *nurbs, Sets *set )
{
  FILE *handle1;
  int  i, j, k, n, l, nr;
  unsigned char string[MAX_LINE_LENGTH], datout[MAX_LINE_LENGTH], buffer[MAX_LINE_LENGTH];
  int length, params, setNr;
  int bias_fbd;
  unsigned char attr;

  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return (-1);
  }

  length=sword( setname, string);
  string[length]='.';
  string[length+1]='f';
  string[length+2]='b';
  string[length+3]='d';
  string[length+4]=(int)NULL;
  strcpy( datout, string);
  /* Open the files and check to see that it was opened correctly */
  handle1 = fopen (datout, "w+b");
  if (handle1==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n", datout); return(-1);}
  else  printf ("\n%s opened\n",datout);

  printf ("\n write fbd  \n");

  if (anz->p)
  {
    if( compare( setname, "all", 3)==3)
      for (i=0; i<anz->p; i++)
      {
        if( pnt[i].name[0] != (unsigned char)NULL )
          fprintf (handle1, " PNT %-5s %10.5f %14.5f %14.5f \n", pnt[i].name, pnt[i].px,pnt[i].py,pnt[i].pz);
      }
    else
    {
      for (i=0; i<set[setNr].anz_p; i++)
      {
        j=set[setNr].pnt[i];
        fprintf (handle1, " PNT %-5s %10.5f %14.5f %14.5f \n", pnt[j].name, pnt[j].px,pnt[j].py,pnt[j].pz);
      }
    }
  }

  if (anz->sets)
  {
    if( compare( setname, "all", 3)==3)
    {
      for (i=1; i<anz->sets; i++)
      {
        if(set[i].type) /* SEQ */
        {
          if ( ((compare( set[i].name, setname, length) == length)
          && (length == strlen(setname) ))
          || (( compare( setname, "all", 3) == 3) 
	      &&(set[i].name[0] != (unsigned char)NULL )) )
          {
            params=MAX_PARAM_PER_RECORD/2;
            if(set[i].anz_p)
            {
              strcpy(string,"   ");
              fprintf (handle1, " SEQA %s %s pnt ", set[i].name, string );
              for (j=0; j<set[i].anz_p; j++)
              {
                if(j==params)
        	   {
        	     params+=MAX_PARAM_PER_RECORD/2;
                  strcpy(string,"END");
                  fprintf (handle1, "\n SEQA %s %s pnt ", set[i].name, string );
                }
                fprintf (handle1, " %s", pnt[set[i].pnt[j]].name);
              }
              fprintf (handle1, "\n");
            }
          }
        }
      }
    }
    else
    {
      for (l=0; l<set[setNr].anz_se; l++)
      {
        i=set[setNr].set[l];
        if(set[i].type) /* SEQ */
        {
          if ( set[i].name[0] != (unsigned char)NULL )
          {
            params=MAX_PARAM_PER_RECORD/2;
            if(set[i].anz_p)
            {
              strcpy(string,"   ");
              fprintf (handle1, " SEQA %s %s pnt ", set[i].name, string );
              for (j=0; j<set[i].anz_p; j++)
              {
                if(j==params)
        	   {
        	     params+=MAX_PARAM_PER_RECORD/2;
                  strcpy(string,"END");
                  fprintf (handle1, "\n SEQA %s %s pnt ", set[i].name, string );
                }
                fprintf (handle1, " %s", pnt[set[i].pnt[j]].name);
              }
              fprintf (handle1, "\n");
            }
          }
        }
      }
    }
  }

  if (anz->l)
  {
    if( compare( setname, "all", 3)==3)
    {
      for (i=0; i<anz->l; i++)
      {
        /* load bias and division in buffer */
        bias_fbd=getBias_fbd(i,line);
        if(line[i].div<10) sprintf(buffer, "%d0%d", bias_fbd, line[i].div);
        else if(line[i].div<100) sprintf(buffer, "%d%d", bias_fbd, line[i].div);

        if (line[i].typ=='a') /* its an arc-line */
        {
          if( line[i].name[0] != (unsigned char)NULL )
            fprintf (handle1, " LINE %s %s %s %s %s\n",
            line[i].name, pnt[line[i].p1].name,
            pnt[line[i].p2].name, pnt[line[i].trk].name, buffer );
        }
        else if (line[i].typ=='s') /* its a seq-line */
        {
          if( line[i].name[0] != (unsigned char)NULL )
            fprintf (handle1, " LINE %s %s %s %s %s\n",
            line[i].name, pnt[line[i].p1].name,
            pnt[line[i].p2].name, set[line[i].trk].name, buffer );
        }
        else
        {
          if( line[i].name[0] != (unsigned char)NULL )
            fprintf (handle1, " LINE %s %s %s %s\n",
            line[i].name, pnt[line[i].p1].name, pnt[line[i].p2].name, buffer );
        }
      }
    }
    else
    {
      for (i=0; i<set[setNr].anz_l; i++)
      {
        j=set[setNr].line[i];

        /* load bias and division in buffer */
        bias_fbd=getBias_fbd(j,line);
        if(line[j].div<10) sprintf(buffer, "%d0%d", bias_fbd, line[j].div);
        else if(line[j].div<100) sprintf(buffer, "%d%d", bias_fbd, line[j].div);

        if (line[j].typ=='a') /* its an arc-line */
        {
          fprintf (handle1, " LINE %s %s %s %s %s\n",
            line[j].name, pnt[line[j].p1].name,
            pnt[line[j].p2].name, pnt[line[j].trk].name, buffer );
        }
        else if (line[j].typ=='s') /* its an seq-line */
        {
          fprintf (handle1, " LINE %s %s %s %s %s\n",
            line[j].name, pnt[line[j].p1].name, 
            pnt[line[j].p2].name, set[line[j].trk].name, buffer );
        }
        else
        {
          fprintf (handle1, " LINE %s %s %s %s\n",
            line[j].name, pnt[line[j].p1].name, pnt[line[j].p2].name, buffer );
        }
      }
    }
  }

  if (anz->c)
  {
    if( compare( setname, "all", 3)==3)
      for (i=0; i<anz->c; i++)
      {
        if( lcmb[i].name[0] != (unsigned char)NULL )
        {
          fprintf (handle1, " LCMB %s ", lcmb[i].name );
          k=0;
          for (j=0; j<lcmb[i].nl; j++)
	  {
            if (k<8) 
	    {
              fprintf (handle1, " %1c %s", lcmb[i].o[j], line[lcmb[i].l[j]].name );
              k++;
            }
            else
	    {
              k=0; fprintf (handle1, "\n LCMB %s ADD ", lcmb[i].name );
              fprintf (handle1, " %1c %s", lcmb[i].o[j], line[lcmb[i].l[j]].name );
              k++;
            }
          }
          fprintf (handle1, " \n");
        }
      }
    else
    {
      for (i=0; i<set[setNr].anz_c; i++)
      {
          n=set[setNr].lcmb[i];
          fprintf (handle1, " LCMB %s ", lcmb[n].name );
          k=0;
          for (j=0; j<lcmb[n].nl; j++)
	  {
            if (k<8) 
	    {
              fprintf (handle1, " %1c %s", lcmb[n].o[j], line[lcmb[n].l[j]].name );
              k++;
            }
            else
	    {
              k=0; fprintf (handle1, "\n LCMB %s ADD ", lcmb[n].name );
              fprintf (handle1, " %1c %s", lcmb[n].o[j], line[lcmb[n].l[j]].name );
              k++;
            }
          }
          fprintf (handle1, " \n");
      }
    }
  }


  if (anz->nurs)
  {
    if( compare( setname, "all", 3)==3)
      for (nr=0; nr<anz->nurs; nr++)
      {
        if( nurbs[nr].name[0] != (unsigned char)NULL )
        {
          fprintf(handle1," NURS %s DEFINE %4d %4d %4d %4d %4d %4d\n", nurbs[nr].name, nurbs[nr].u_exp, nurbs[nr].v_exp, nurbs[nr].u_npnt, nurbs[nr].v_npnt, nurbs[nr].u_nknt, nurbs[nr].v_nknt);

          for (j=0; j<nurbs[nr].u_npnt; j++)
            for (k=0; k<nurbs[nr].v_npnt; k++)
              fprintf (handle1, " NURS %s CONTROL %4d %4d %s %f\n", nurbs[nr].name, j+1, k+1, pnt[nurbs[nr].ctlpnt[j][k]].name, nurbs[nr].weight[j][k] );

          for (j=0; j<nurbs[nr].u_nknt; j++)
            fprintf(handle1, " NURS %s KNOT  U %4d %f\n", nurbs[nr].name, j+1, nurbs[nr].uknt[j]);
          for (j=0; j<nurbs[nr].v_nknt; j++)
            fprintf(handle1, " NURS %s KNOT  V %4d %f\n", nurbs[nr].name, j+1, nurbs[nr].vknt[j]);

          fprintf(handle1, " NURS %s END\n", nurbs[nr].name);
        }
      }
    else
    {
      for (i=0; i<set[setNr].anz_nurs; i++)
      {
        nr=set[setNr].nurs[i];

          fprintf(handle1," NURS %s DEFINE %4d %4d %4d %4d %4d %4d\n", nurbs[nr].name, nurbs[nr].u_exp, nurbs[nr].v_exp, nurbs[nr].u_npnt, nurbs[nr].v_npnt, nurbs[nr].u_nknt, nurbs[nr].v_nknt);

          for (j=0; j<nurbs[nr].u_npnt; j++)
            for (k=0; k<nurbs[nr].v_npnt; k++)
              fprintf (handle1, " NURS %s CONTROL %4d %4d %s %f\n", nurbs[nr].name, j+1, k+1, pnt[nurbs[nr].ctlpnt[j][k]].name, nurbs[nr].weight[j][k]);

          for (j=0; j<nurbs[nr].u_nknt; j++)
            fprintf(handle1, " NURS %s KNOT  U %4d %f\n", nurbs[nr].name, j+1, nurbs[nr].uknt[j]);
          for (j=0; j<nurbs[nr].v_nknt; j++)
            fprintf(handle1, " NURS %s KNOT  V %4d %f\n", nurbs[nr].name, j+1, nurbs[nr].vknt[j]);

          fprintf(handle1, " NURS %s END\n", nurbs[nr].name);

      }
    }
  }

  /* must be after nurs */
  if (anz->s)
  {
    if( compare( setname, "all", 3)==3)
    {
      for (i=0; i<anz->s; i++)
      {
        if( surf[i].name[0] != (unsigned char)NULL )
        {
          if(surf[i].sh<0) fprintf (handle1, " GSUR %s %1c BLEND", surf[i].name, surf[i].ori );
          else fprintf (handle1, " GSUR %s %1c %s", surf[i].name, surf[i].ori, nurbs[surf[i].sh].name );
          params=0; 
          for (j=0; j<surf[i].nl; j++)
          {
            if (params==MAX_GSUR_PARAMERTER)
            { params=0; fprintf (handle1, "\n GSUR %s ADD", surf[i].name); }
            else  params++; 
            if (surf[i].typ[j]=='l')
            {
              fprintf (handle1, " %1c %s", surf[i].o[j], line[surf[i].l[j]].name );
            }
            else
            {
              fprintf (handle1, " %1c %s", surf[i].o[j], lcmb[surf[i].l[j]].name );
            }
          }
          fprintf (handle1, " \n");
        }
      }
    }
    else
    {
      for (i=0; i<set[setNr].anz_s; i++)
      {
          n=set[setNr].surf[i];
          if(surf[n].sh<0) fprintf (handle1, " GSUR %s %1c BLEND", surf[n].name, surf[n].ori );
          else fprintf (handle1, " GSUR %s %1c %s", surf[n].name, surf[n].ori, nurbs[surf[n].sh].name );
          params=0; 
          for (j=0; j<surf[n].nl; j++)
          {
            if (params==MAX_GSUR_PARAMERTER)
            { params=0; fprintf (handle1, "\n GSUR %s ADD", surf[n].name); }
            else  params++; 
            if (surf[n].typ[j]=='l')
            {
              fprintf (handle1, " %1c %s", surf[n].o[j], line[surf[n].l[j]].name );
            }
            else
            {
              fprintf (handle1, " %1c %s", surf[n].o[j], lcmb[surf[n].l[j]].name );
            }
        }
        fprintf (handle1, " \n");
      }
    }
  }

  if (anz->b)
  {
    if( compare( setname, "all", 3)==3)
      for (i=0; i<anz->b; i++)
      {
        if( body[i].name[0] != (unsigned char)NULL )
        {
          fprintf (handle1, " GBOD %s NORM", body[i].name );
          for (j=0; j<body[i].ns; j++)
            fprintf (handle1, " %1c %s", body[i].o[j], surf[body[i].s[j]].name );
          fprintf (handle1, " \n");
        }
      }
    else
    {
      for (i=0; i<set[setNr].anz_b; i++)
      {
        n=set[setNr].body[i];
        fprintf (handle1, " GBOD %s NORM", body[n].name );
        for (j=0; j<body[n].ns; j++)
          fprintf (handle1, " %1c %s", body[n].o[j], surf[body[n].s[j]].name );
        fprintf (handle1, " \n");
      }
    }
  }

  for (i=1; i<anz->sets; i++)
  {
   if((!set[i].type)&&( set[i].name[0]!='_')) /* ordinary SET */
     if ( ((compare( set[i].name, setname, length) == length)&&(length == strlen(setname) ))
	  || ((compare( setname, "all", 3) == 3) &&( set[i].name[0] != (unsigned char)NULL )) )
     {
       /*  FAM vertraegt nur einen wert hinter der spec.  */
       for (j=0; j<set[i].anz_p; j++)
       {
         fprintf (handle1, " SETA %s p %s \n", set[i].name, pnt[set[i].pnt[j]].name);
       }
       for (j=0; j<set[i].anz_l; j++)
       {
         fprintf (handle1, " SETA %s l %s \n", set[i].name, line[set[i].line[j]].name);
       }
       for (j=0; j<set[i].anz_c; j++)
       {
         fprintf (handle1, " SETA %s c %s \n", set[i].name, lcmb[set[i].lcmb[j]].name);
       }
       for (j=0; j<set[i].anz_s; j++)
       {
         fprintf (handle1, " SETA %s s %s \n", set[i].name, surf[set[i].surf[j]].name);
       }
       for (j=0; j<set[i].anz_b; j++)
       {
         fprintf (handle1, " SETA %s b %s \n", set[i].name, body[set[i].body[j]].name);
       }
       /* this is not fam compatible */
       /*
       for (j=0; j<set[i].anz_se; j++)
       {
         fprintf (handle1, " SETA %s se %s \n", set[i].name, set[set[i].set[j]].name);
       }
       */
     }
  }
  if( compare( setname, "all", 3)==3)
  {
    for (i=0; i<anz->sets; i++)
    {
     if(set[i].name[0]!=(unsigned char)NULL)
     {
       if(set[i].eattr==1)  attr='R';
       else if(set[i].eattr==2)  attr='I';
       else if(set[i].eattr==-1)  attr='U';
       else if(set[i].eattr==-2)  attr='G';
       else attr=' ';
      if(set[i].etyp==1) fprintf (handle1, " ELTY %s HE8%c \n", set[i].name, attr);
      if(set[i].etyp==4) fprintf (handle1, " ELTY %s HE20%c \n", set[i].name, attr);
      if(set[i].etyp==6) fprintf (handle1, " ELTY %s TE10 \n", set[i].name);
      if(set[i].etyp==7) fprintf (handle1, " ELTY %s TR3%c \n", set[i].name, attr);
      if(set[i].etyp==8) fprintf (handle1, " ELTY %s TR6 \n", set[i].name);
      if(set[i].etyp==9) fprintf (handle1, " ELTY %s QU4 \n", set[i].name);
      if(set[i].etyp==10) fprintf (handle1, " ELTY %s QU8 \n", set[i].name);
      if(set[i].etyp==11) fprintf (handle1, " ELTY %s BE2 \n", set[i].name);
      if(set[i].etyp==12) fprintf (handle1, " ELTY %s BE3 \n", set[i].name);
     }
    }
  }
  else
  { 
     if(set[setNr].eattr==1)  attr='R';
     else if(set[setNr].eattr==2)  attr='I';
     else if(set[setNr].eattr==-1)  attr='U';
     else if(set[setNr].eattr==-2)  attr='G';
     else attr=' ';
      if(set[setNr].etyp==1) fprintf (handle1, " ELTY %s HE8%c \n", set[setNr].name, attr);
      if(set[setNr].etyp==4) fprintf (handle1, " ELTY %s HE20%c \n", set[setNr].name, attr);
      if(set[setNr].etyp==6) fprintf (handle1, " ELTY %s TE10 \n", set[setNr].name);
      if(set[setNr].etyp==7) fprintf (handle1, " ELTY %s TR3%c \n", set[setNr].name, attr);
      if(set[setNr].etyp==8) fprintf (handle1, " ELTY %s TR6 \n", set[setNr].name);
      if(set[setNr].etyp==9) fprintf (handle1, " ELTY %s QU4 \n", set[setNr].name);
      if(set[setNr].etyp==10) fprintf (handle1, " ELTY %s QU8 \n", set[setNr].name);
      if(set[setNr].etyp==11) fprintf (handle1, " ELTY %s BE2 \n", set[setNr].name);
      if(set[setNr].etyp==12) fprintf (handle1, " ELTY %s BE3 \n", set[setNr].name);
   
  }
  fprintf (handle1, "\n");
  fclose(handle1);
  return (1);
}





