1 /* --------------------------------------------------------------------  */
2 /*                          CALCULIX                                     */
3 /*                   - GRAPHICAL INTERFACE -                             */
4 /*                                                                       */
5 /*     A 3-dimensional pre- and post-processor for finite elements       */
6 /*              Copyright (C) 1996 Klaus Wittig                          */
7 /*                                                                       */
8 /*     This program is free software; you can redistribute it and/or     */
9 /*     modify it under the terms of the GNU General Public License as    */
10 /*     published by the Free Software Foundation; version 2 of           */
11 /*     the License.                                                      */
12 /*                                                                       */
13 /*     This program is distributed in the hope that it will be useful,   */
14 /*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */
15 /*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
16 /*     GNU General Public License for more details.                      */
17 /*                                                                       */
18 /*     You should have received a copy of the GNU General Public License */
19 /*     along with this program; if not, write to the Free Software       */
20 /*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
21 /* --------------------------------------------------------------------  */
22 
23 #include <extUtil.h>
24 
25 #define TEST     0
26 
27 #define INI_FIELD_SIZE 100000
28 
29 
30 
readStl(char * datin,Summen * anz,Nodes ** nptr,Elements ** eptr,Datasets ** lptr)31 int readStl( char *datin, Summen *anz, Nodes **nptr, Elements **eptr, Datasets **lptr )
32 {
33   FILE *handle;
34   int i=0;
35 
36   char rec_str[MAX_LINE_LENGTH];
37   char buffer[MAX_LINE_LENGTH];
38   int  node_field_size, elem_field_size;
39   int  e_nmax=1, e_nmin=1;
40   int  length;
41 
42   Nodes     *node=NULL;
43   Elements  *elem=NULL;
44 
45 
46 
47   node_field_size=INI_FIELD_SIZE;
48   do
49   {
50     if ( (node = (Nodes *)realloc( (Nodes *)node, (node_field_size+1) * sizeof(Nodes))) == NULL )
51     {
52       printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", node_field_size );
53       node_field_size/=2;
54     }
55     if(node_field_size<100)
56     {
57       printf("\n\n ERROR: not enough memory in readfrd()\n\n");
58       exit(-1);
59     }
60   }while(!node);
61 
62   elem_field_size=INI_FIELD_SIZE;
63   do
64   {
65     if((elem = (Elements *)realloc( (Elements *)elem, (elem_field_size+1) * sizeof(Elements))) == NULL )
66   	{
67       printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", elem_field_size );
68       elem_field_size/=2;
69     }
70     if(elem_field_size<100)
71     {
72       printf("\n\n ERROR: not enough memory in readfrd()\n\n");
73       exit(-1);
74     }
75   }while(!elem);
76 
77   anz->n=anz->e=anz->l=-1;
78 
79   /* Open the files and check to see that it was opened correctly */
80   handle = fopen (datin, "r");
81   if ( handle== NULL )  { printf ("ERROR: The input file \"%s\" could not be opened.\n\n", datin); return(-1); }
82   else  printf (" file:%s opened\n", datin);
83 
84   printf (" reading stl format\n");
85 
86   length = frecord( handle, rec_str);
87   sscanf(rec_str, "%s", buffer);
88   for(i=0;i<strlen(buffer); i++) buffer[i]=toupper(buffer[i]);
89   if(compare(buffer, "SOLID", 5)==5)
90   {
91     strcpy(anz->model, rec_str);
92     printf (" MODEL NAME:  %s", anz->model);
93   }
94   else
95   {
96     printf ("\n\nFATAL ERROR: no proper file-format found.\n\n");
97     return (-1);
98   }
99 
100   anz->emax=0;  anz->emin=MAX_INTEGER;
101   anz->nmax=0;  anz->nmin= MAX_INTEGER;
102   while(length)
103   {
104     length = frecord( handle, rec_str);
105     if (rec_str[length] == (char)EOF) break;
106 
107     sscanf(rec_str, "%s", buffer);
108     for(i=0;i<strlen(buffer); i++) buffer[i]=toupper(buffer[i]);
109     if(compare(buffer, "FACET", 5)==5)
110     {
111       /* overread "outer loop" */
112       length = frecord( handle, rec_str);
113       for(i=0; i<3; i++)
114       {
115         anz->n++;
116         node[anz->n].nr = anz->n+1;
117         if (node[anz->n].nr>=node_field_size)
118         {
119           node_field_size=node[anz->n].nr+100;
120           if ( (node = (Nodes *)realloc((Nodes *)node, (node_field_size+1) * sizeof(Nodes))) == NULL )
121           {
122             printf("\n\n ERROR: realloc failed, nodenr:%d\n\n", node[anz->n].nr) ;
123             return(-1);
124           }
125         }
126         node[node[anz->n].nr].indx=anz->n;
127 
128         length = frecord( handle, rec_str);
129         sscanf(rec_str,"%*s %lf %lf %lf", &node[node[anz->n].nr].nx,&node[node[anz->n].nr].ny,&node[node[anz->n].nr].nz);
130         if (node[anz->n].nr >  anz->nmax)  anz->nmax=node[anz->n].nr;
131         if (node[anz->n].nr <  anz->nmin)  anz->nmin=node[anz->n].nr;
132 #if TEST
133         printf (" n=%d x=%lf y=%lf z=%lf \n",  node[anz->n].nr,
134         node[node[anz->n].nr].nx, node[node[anz->n].nr].ny,
135         node[node[anz->n].nr].nz);
136 #endif
137       }
138       anz->e++;
139       if (anz->e>=elem_field_size)
140       {
141         elem_field_size=anz->e+100;
142         if((elem=(Elements *)realloc((Elements *)elem,(elem_field_size+1)*sizeof(Elements)))==NULL)
143         {
144           printf("\n\n ERROR: realloc failed, elem-index:%d\n\n", anz->e);
145           return(-1);
146         }
147       }
148       elem[anz->e].nr    = anz->e+1;
149       elem[anz->e].type  = 7;
150       elem[anz->e].group = 1;
151       elem[anz->e].mat   = 1;
152       anz->etype[elem[anz->e].type]++;
153       if (elem[anz->e].nr >  anz->emax)  anz->emax=elem[anz->e].nr;
154       if (elem[anz->e].nr <  anz->emin)  anz->emin=elem[anz->e].nr;
155 #if TEST
156       printf (" e=%d typ=%d mat=%d \n", elem[anz->e].nr,
157       elem[anz->e].type, elem[anz->e].group, elem[anz->e].mat );
158 #endif
159       for (i=0; i<3; i++) elem[anz->e].nod[i]=node[anz->n-2+i].nr;
160     }
161   }
162   anz->n++;
163   anz->e++;
164   anz->l++;
165   fclose(handle);
166 
167   node_field_size=anz->nmax+1;
168   if((node =(Nodes *)realloc( (Nodes *)node, node_field_size*sizeof(Nodes)))==NULL)
169     printf("\n\n ERROR: realloc failed\n\n") ;
170   else
171     printf ("\n %d nodes reallocated \n",anz->nmax);
172 
173   elem_field_size=anz->e+1;
174   if ( (elem = (Elements *)realloc((Elements *)elem, elem_field_size * sizeof(Elements))) == NULL )
175     printf("\n\n ERROR: in readfrd realloc failed\n\n") ;
176   else
177     printf ("\n %d elements reallocated \n", anz->e);
178 
179   if ( e_nmax > (anz->nmax) )
180   {
181     printf ("\nWARNING: element requestes a nodename higher than allocated\n\n");
182     printf (" e_nmax=%d e_nmin=%d\n", e_nmax, e_nmin );
183   }
184   if ( e_nmin < 1 )
185   {
186     printf ("\nWARNING: element requestes a nodename lower than allocated\n\n");
187     printf (" e_nmax=%d e_nmin=%d\n", e_nmax, e_nmin );
188   }
189 
190   *nptr = node; *eptr = elem;
191   return(1);
192 }
193 
194