// stl_btoa.c read binary .stl write ascii .stl // solid cube1.stl // facet normal 0.000000 0.000000 -1.000000 // outer loop // vertex -0.500000 0.500000 0.500000 // vertex 0.500000 0.500000 0.500000 // vertex 0.500000 -0.500000 0.500000 // endloop // endfacet // ... // endsolid // input binary .stl // 80 char header, b is a byte, 8-bit character // b b b b 32 bit integer, number of triangles (groups) // // three b b b b 32 bit float nx, ny, nz normals // three b b b b 32 but floats x1, y1, z1 point // three b b b b 32 but floats x2, y2, z2 point // three b b b b 32 but floats x3, y3, z3 point // b b zero bytes // group repeated ntri times #include #include int main(int argc, char * argv[]) { union {int i; float f;} data; // data.i = 10; // printf( "data.i : %d\n", data.i); // data.f = 220.5; // printf( "data.f : %f\n", data.f); char junk; int nvert, nv0, nv1, nv2, nv3 ; float nx, ny, nz; // normal int nxi, nyi, nzi; // bytes float x1, y1, z1; // first vertex float x2, y2, z2; // second vertex float x3, y3, z3; // third vertex int i, ntri; FILE * fp; FILE * fpout; printf("stl_btoa.c running \n"); printf("reading, writing %s \n", argv[1], argv[2]); fp = fopen(argv[1], "rb"); if(fp == NULL) {printf("can not open \n"); return 0;} printf("file opened reading 80 characters %s\n", argv[1]); fpout = fopen(argv[2], "w"); if(fpout == NULL) {printf("can not open %s\n", argv[2]); return 0;} printf("file opened for writing %s\n", argv[2]); fprintf(fpout, "solid %s \n", argv[2]); for(i=0; i<80; i++) junk = fgetc(fp); nv0 = fgetc(fp); nv1 = fgetc(fp); nv2 = fgetc(fp); nv3 = fgetc(fp); printf("triangles to read %d %d %d %d \n", nv0, nv1, nv2, nv3); ntri = nv3*256*256*256 + nv2*256*256 + nv1*256 +nv0; printf("ntri = %d \n", ntri); for(i=0; i