I am having a situation her
I am trying to read an stl file using C so that i can use it in another software called Dymola. Since dymola only takes C, i cannot use C++ to write the code btw.
So heres the thing, i made a code for reading one triangle in the stl file and it works pretty good. The task is to save the values in the stl file into two arrays. One arry is for the vertices of the triangle (meshing) and the other array is for the normal matrix.The program i wrote works except when the stl file has exponent values like "1.000000e+003". When such values are encountered, the program crashes.
I will give you guys a brief idea of how my program works. It reads an stl file using fgetc() so it is character by character. The values are stored in a variable called ch and then saved into a string. The strings are later converted into a double by atof() and then saved in the array. When i tried debugging the program, i found that the program crashes just after the + sign of the value like "1.000000e+003" is encountered. The ch takes a value [ 0 '\000' ] instead of [ 48 '0' ] which i have no idea why it happens. I will give you guys the code here and also an example stl. Just save the stl with some name like hello.stl and then you can run it as argument.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp = fopen(argv[1], "r");
char ch, k, chprev = 'a'; //a is a random character
int z = 0, i1 = 0, i2 = 0, i3 = 0, k2 = 0;
int loopx, loopy, loopn = 3;
int endtri = 0; // makes sure that at end of one triangle chprev is 'a'
char tempstring[25];
double vectarr[3][3], normarr[1][3];
if (fp == NULL) {
printf("Cannot open file.\n");
exit(1);
}
while ((ch = fgetc(fp)) != EOF) {
if (ch == 'm') { //indicates beginning of a triangle
z++;
k = ch;
} //Face Normal Matrix
if (k == 'm' && ch == ' ' && chprev != ' ') { //indicates completion of a string
if (z != 0) {
vectarr[loopx][loopy] = atof(tempstring);
if (loopn != 3)
normarr[0][loopn] = atof(tempstring);
}
z++;
}
if (k == 'm' && z == 2 && ch != ' ') {
tempstring[i1] = ch;
i1++;
tempstring[i1] = '\0';
loopn = 0;
} else if (k == 'm' && z == 3 && ch != ' ') {
tempstring[i2] = ch;
i2++;
tempstring[i2] = '\0';
loopn = 1;
} else if (k == 'm' && z == 4 && ch != ' ') {
tempstring[i3] = ch;
i3++;
tempstring[i3] = '\0';
loopn = 2;
}
if (k2 == 0) {
if (ch == 'x') {
z = 1;
k = ch;
i1 = 0;
i2 = 0;
i3 = 0;
} //Vertex1 Matrix
if (k == 'x' && ch == ' ' && chprev != ' ')
z++;
if (k == 'x' && z == 2 && ch != ' ') {
tempstring[i1] = ch;
i1++;
tempstring[i1] = '\0';
loopx = 0;
loopy = 0;
} else if (k == 'x' && z == 3 && ch != ' ') {
tempstring[i2] = ch;
i2++;
tempstring[i2] = '\0';
loopx = 0;
loopy = 1;
} else if (k == 'x' && z == 4 && ch != ' ') {
tempstring[i3] = ch;
i3++;
tempstring[i3] = '\0';
loopx = 0;
loopy = 2;
} else if (k == 'x' && z == 5)
k2 = 1;
}
if (k2 == 1) {
if (ch == 'x') {
z = 1;
k = ch;
i1 = 0;
i2 = 0;
i3 = 0;
} //Vertex2 Matrix
if (k == 'x' && ch == ' ' && chprev != ' ')
z++;
if (k == 'x' && z == 2 && ch != ' ') {
tempstring[i1] = ch;
i1++;
tempstring[i1] = '\0';
loopx = 1;
loopy = 0;
} else if (k == 'x' && z == 3 && ch != ' ') {
tempstring[i2] = ch;
i2++;
tempstring[i2] = '\0';
loopx = 1;
loopy = 1;
} else if (k == 'x' && z == 4 && ch != ' ') {
tempstring[i3] = ch;
i3++;
tempstring[i3] = '\0';
loopx = 1;
loopy = 2;
} else if (k == 'x' && z == 5)
k2 = 2;
}
if (k2 == 2) {
if (ch == 'x') {
z = 1;
k = ch;
i1 = 0;
i2 = 0;
i3 = 0;
} //Vertex3 Matrix
if (k == 'x' && ch == ' ' && chprev != ' ')
z++;
if (k == 'x' && z == 2 && ch != ' ') {
tempstring[i1] = ch;
i1++;
tempstring[i1] = '\0';
loopx = 2;
loopy = 0;
} else if (k == 'x' && z == 3 && ch != ' ') {
tempstring[i2] = ch;
i2++;
tempstring[i2] = '\0';
loopx = 2;
loopy = 1;
} else if (k == 'x' && z == 4 && ch != ' ') {
tempstring[i3] = ch;
i3++;
tempstring[i3] = '\0';
loopx = 2;
loopy = 2;
} else if (k == 'x' && z == 5) { // Time to reinitialize
z = 0, i1 = 0, i2 = 0, i3 = 0, k2 = 0, chprev = 'a';
endtri = 1;
}
}
if (endtri != 1)
chprev = ch;
}
for (loopx = 0; loopx < 3; loopx++) {
for (loopy = 0; loopy < 3; loopy++)
printf("%f", vectarr[loopx][loopy]);
printf("\n");
}
for (loopx = 0; loopx < 3; loopx++)
printf("%f", normarr[0][loopx]);
if (fp != NULL)
fclose(fp);
return 0;
}
Heres the stl i am using:
facet normal 0.000000e+000 0.000000e+000 1.000000e+000
outer loop
vertex 0.000000e+000 0.000000e+000 0.000000e+000
vertex -9.094947e-013 1.000000e+003 0.000000e+000
vertex 2.000000e+003 0.000000e+000 0.000000e+000
endloop
Thank you.
Btw since i am a beginner programmer, i dont have too much experience with programming. My buddy was saying its going to be hard to use C for stl files and instead use python stl parsors. I dont know what the hell is that, but if you guys can tell me more alternative and simple fast method to do the same thing i am doing now, that will be of great help.
With Regards
Adam

New Topic/Question
Reply




MultiQuote






|