JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
STL.cpp
Go to the documentation of this file.
1
10#include "STL.h"
11
12
13using namespace jbxl;
14
15
23DllExport STLData* jbxl::readSTLFile(char* fname, long int* fno)
24{
25 FILE* fp;
26 struct stat stbuf;
27 STLData* stldata;
28 unsigned int temp;
29
30 fp = fopen(fname, "rb");
31 if (fp==NULL) {
32 PRINT_MESG("JBXL::readSTLFile: ERROR: File open Error! (%s)\n", fname);
33 return NULL;
34 }
35
36 fseek(fp, 80, SEEK_SET);
37 fread(&temp, 4, 1, fp);
38 fclose(fp);
39 *fno = (long int)temp;
40
41 stat(fname, &stbuf);
42 if (stbuf.st_size == (*fno)*50+84) { // ファイルサイズのチェック
43 stldata = readSTLFileB(fname, fno); // バイナリファイル
44 }
45 else {
46 stldata = readSTLFileA(fname, fno); // アスキーファイル
47 }
48
49 if (stldata==NULL) {
50 if (*fno!=0) {
51 DEBUG_MODE PRINT_MESG("readSTLFile: メモリの確保に失敗.\n");
52 }
53 else {
54 DEBUG_MODE PRINT_MESG("readSTLFile: データの読み込みに失敗.\n");
55 }
56 }
57 else {
58 DEBUG_MODE PRINT_MESG("readSTLFile: file name = %s\n", fname);
59 DEBUG_MODE PRINT_MESG("readSTLFile: read facet No. = %d\n", *fno);
60 }
61
62 return stldata;
63}
64
65
74DllExport int jbxl::writeSTLFile (char* fname, BREP_SOLID* solid, bool ascii)
75{
76 int nn;
77
78 if (ascii) nn = writeSTLFileA(fname, solid);
79 else nn = writeSTLFileB(fname, solid);
80
81 if (nn<0) DEBUG_MODE PRINT_MESG("writeSTLFile: ファイルオープンエラー.\n");
82 return nn;
83}
84
85
94DllExport int jbxl::writeSTLFile (char* fname, BREP_SOLID_LIST solid_list, bool ascii)
95{
96 int nn;
97
98 if (ascii) nn = writeSTLFileA(fname, solid_list);
99 else nn = writeSTLFileB(fname, solid_list);
100
101 if (nn<0) DEBUG_MODE PRINT_MESG("writeSTLFile: ファイルオープンエラー.\n");
102 return nn;
103}
104
105
112DllExport STLData* jbxl::readSTLFileA(char* fname, long int* fno)
113{
114 FILE* fp;
115 STLData* stldata = NULL;
116 float vect[3];
117 char buffer[LBUF], dummy[LBUF], *pbuf;
118 int vno=0;
119
120 // ファイルをオープンしてデータ数を数える.
121 *fno = 0;
122 fp = fopen(fname, "r");
123 if (fp==NULL) {
124 PRINT_MESG("JBXL::readSTLFileA: ERROR: File open Error! (%s)\n", fname);
125 return NULL;
126 }
127
128 fgets(buffer, LBUF, fp);
129 while (!feof(fp)) {
130 pbuf = buffer;
131 while(*pbuf==' '||*pbuf==CHAR_TAB) pbuf++;
132
133 if (!strncasecmp(pbuf, "facet ", 6)) (*fno)++;
134 if (!strncasecmp(pbuf, "vertex ", 7)) vno++;
135 fgets(buffer, LBUF, fp);
136 }
137 fclose(fp);
138
139 // メモリの確保
140 if (vno!=0 && vno==(*fno)*3) {
141 stldata = (STLData*)malloc(sizeof(STLData)*(*fno));
142 if (stldata!=NULL) memset(stldata, 0, sizeof(STLData)*(*fno));
143 }
144 if (stldata==NULL) {
145 *fno = 0;
146 return NULL;
147 }
148
149 // もう一度ファイルをオープンして,データを読み込む.
150 int i = 0, j = 0;
151 fp = fopen(fname, "r");
152 if (fp==NULL) {
153 PRINT_MESG("JBXL::readSTLFileA: ERROR: File re-open Error! (%s)\n", fname);
154 return NULL;
155 }
156
157 fgets(buffer, LBUF, fp);
158 while (!feof(fp)) {
159 pbuf = buffer;
160 while(*pbuf==' '||*pbuf==CHAR_TAB) pbuf++;
161
162 if (!strncasecmp(pbuf, "facet ", 6)) { // 法線ベクトル
163 sscanf(buffer, "%s %s %f %f %f", dummy, dummy, &vect[0], &vect[1], &vect[2]);
164 for (int k=0; k<3; k++) stldata[i].vect[k] = vect[k];
165 j = 3;
166 }
167 else if (!strncasecmp(pbuf, "vertex ", 7) && j>=3 && j<12) {// 座標
168 sscanf(buffer, "%s %f %f %f", dummy, &vect[0], &vect[1], &vect[2]);
169 for (int k=0; k<3; k++) stldata[i].vect[j+k] = vect[k];
170 j += 3;
171 if (j==12) i++;
172 }
173 fgets(buffer, LBUF, fp);
174 }
175 fclose(fp);
176
177 return stldata;
178}
179
180
186DllExport STLData* jbxl::readSTLFileB(char* fname, long int* fno)
187{
188 FILE* fp;
189 char message[81]; // STLのファイルメッセージ
190 tmpSTLData* tmp_stldata;
191 STLData* stldata;
192 STLData* exdata;
193
194 fp = fopen(fname, "rb");
195 if (fp==NULL) {
196 PRINT_MESG("JBXL::readSTLFileB: ERROR: File open Error! (%s)\n", fname);
197 return NULL;
198 }
199 fread(message, 80, 1, fp);
200 message[80] = '\0'; // STLのファイルメッセージ(message[])80Byteは未使用
201
202 fread(fno, 4, 1, fp);
203 tmp_stldata = (tmpSTLData*)malloc(sizeof(tmpSTLData)*(*fno));
204 if (tmp_stldata==NULL) return NULL;
205 memset(tmp_stldata, 0, sizeof(tmpSTLData)*(*fno));
206 fread(tmp_stldata, sizeof(tmpSTLData), *fno, fp);
207 fclose(fp);
208
209 stldata = (STLData*)malloc(sizeof(STLData)*(*fno));
210 if (stldata==NULL) {
211 free(tmp_stldata);
212 return NULL;
213 }
214 memset(stldata, 0, sizeof(STLData)*(*fno));
215
216 for (int i=0; i<(*fno); i++) {
217 exdata = (STLData*)(&tmp_stldata[i]); // アドレスの読み替え(50Byteの構造体を作れないため)
218 for (int j=0; j<12; j++) {
219 stldata[i].vect[j] = exdata->vect[j]; // データの移動
220 }
221 }
222
223 free(tmp_stldata);
224 return stldata;
225}
226
227
234{
235 FILE* fp;
236 int nn = 0;
237
238 fp = fopen(fname, "w");
239 if (fp==NULL) {
240 PRINT_MESG("JBXL::writeSTLFileA: ERROR: Solid file open Error! (%s)\n", fname);
241 return -1;
242 }
243
244 fprintf(fp, "solid %s\n", fname);
245
246 BREP_CONTOUR_LIST::iterator icon;
247 for (icon=solid->contours.begin(); icon!=solid->contours.end(); icon++){
248 fprintf(fp, "facet normal %g %g %g\n", (*icon)->normal.x, (*icon)->normal.y, (*icon)->normal.z);
249 fprintf(fp, " outer loop\n");
250
251 BREP_WING* wing = (*icon)->wing;
252 for (int i=0; i<3; i++) {
253 Vector<double> vect = wing->vertex->point;
254 fprintf(fp, " vertex %g %g %g\n", vect.x, vect.y, vect.z);
255 wing = wing->next;
256 }
257 fprintf(fp," endloop\n");
258 fprintf(fp,"endfacet\n");
259 nn++;
260 }
261 fprintf(fp, "endsolid %s\n", fname);
262 fclose(fp);
263
264 return nn;
265}
266
267
274{
275 FILE* fp;
276 int fno;
277 char message[80]="STL Binary Data Program by Fumi.Iseki";
278 STLData stldata;
279
280 fp = fopen(fname, "wb");
281 if (fp==NULL) {
282 PRINT_MESG("JBXL::writeSTLFileB: ERROR: Solid file open Error! (%s)\n", fname);
283 return -1;
284 }
285
286 fno = (int)solid->contours.size();
287 fwrite(message, 80, 1, fp);
288 fwrite(&fno, 4, 1, fp);
289
290 BREP_CONTOUR_LIST::iterator icon;
291 for (icon=solid->contours.begin(); icon!=solid->contours.end(); icon++){
292 stldata.vect[0] = (float)(*icon)->normal.x;
293 stldata.vect[1] = (float)(*icon)->normal.y;
294 stldata.vect[2] = (float)(*icon)->normal.z;
295
296 BREP_WING* wing = (*icon)->wing;
297 for (int i=0; i<3; i++) {
298 Vector<double> vect = wing->vertex->point;
299 stldata.vect[3*i+3] = (float)vect.x;
300 stldata.vect[3*i+4] = (float)vect.y;
301 stldata.vect[3*i+5] = (float)vect.z;
302 wing = wing->next;
303 }
304 fwrite(&stldata, 50, 1, fp);
305 }
306 fclose(fp);
307
308 return fno;
309}
310
311
318{
319 FILE* fp;
320 int nn = 0;
321
322 fp = fopen(fname, "wa");
323 if (fp==NULL) {
324 PRINT_MESG("JBXL::writeSTLFileA: ERROR: Solid list file open Error! (%s)\n", fname);
325 return -1;
326 }
327
328 fprintf(fp, "solid %s\n", fname);
329
330 BREP_SOLID_LIST::iterator isolid;
331 for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++){
332 //
333 BREP_CONTOUR_LIST::iterator icon;
334 for (icon=(*isolid)->contours.begin(); icon!=(*isolid)->contours.end(); icon++){
335 fprintf(fp, "facet normal %g %g %g\n", (*icon)->normal.x, (*icon)->normal.y, (*icon)->normal.z);
336 fprintf(fp, " outer loop\n");
337
338 BREP_WING* wing = (*icon)->wing;
339 for (int i=0; i<3; i++) {
340 Vector<double> vect = wing->vertex->point;
341 fprintf(fp, " vertex %g %g %g\n", vect.x, vect.y, vect.z);
342 wing = wing->next;
343 }
344 fprintf(fp," endloop\n");
345 fprintf(fp,"endfacet\n");
346 nn++;
347 }
348 }
349 fprintf(fp, "endsolid %s\n", fname);
350 fclose(fp);
351
352 return nn;
353}
354
355
362{
363 FILE* fp;
364 int fno = 0;
365 char message[80]="STL Binary Data Program by Fumi.Iseki";
366 STLData stldata;
367
368 fp = fopen(fname, "wb");
369 if (fp==NULL) {
370 PRINT_MESG("JBXL::writeSTLFileB: ERROR: Solid list file open Error! (%s)\n", fname);
371 return -1;
372 }
373
374 BREP_SOLID_LIST::iterator isolid;
375 for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++){
376 fno += (int)((*isolid)->contours.size());
377 }
378 fwrite(message, 80, 1, fp);
379 fwrite(&fno, 4, 1, fp);
380
381 //
382 for (isolid=solid_list.begin(); isolid!=solid_list.end(); isolid++){
383 //
384 BREP_CONTOUR_LIST::iterator icon;
385 for (icon=(*isolid)->contours.begin(); icon!=(*isolid)->contours.end(); icon++){
386 stldata.vect[0] = (float)(*icon)->normal.x;
387 stldata.vect[1] = (float)(*icon)->normal.y;
388 stldata.vect[2] = (float)(*icon)->normal.z;
389
390 BREP_WING* wing = (*icon)->wing;
391 for (int i=0; i<3; i++) {
392 Vector<double> vect = wing->vertex->point;
393 stldata.vect[3*i+3] = (float)vect.x;
394 stldata.vect[3*i+4] = (float)vect.y;
395 stldata.vect[3*i+5] = (float)vect.z;
396 wing = wing->next;
397 }
398 fwrite(&stldata, 50, 1, fp);
399 }
400 }
401 fclose(fp);
402
403 return fno;
404}
405
406
414{
415 BREP_WING* wing = contour->wing;
416
417 PRINT_MESG("facet normal %g %g %g\n", contour->normal.x, contour->normal.y, contour->normal.z);
418 PRINT_MESG("outer loop\n");
419 for (int i=0; i<3; i++) {
420 Vector<double> vect = wing->vertex->point;
421 PRINT_MESG("vertex %g %g %g\n", vect.x, vect.y, vect.z);
422 wing = wing->next;
423 }
424 PRINT_MESG("endloop\n");
425 PRINT_MESG("endfacet\n");
426}
427
428
430{
431 if (stldata!=NULL) free(stldata);
432}
STL ファイル入出力用ライブラリ ヘッダ
BREP_WING * wing
Definition Brep.h:171
Vector< double > normal
Definition Brep.h:174
BREP_CONTOUR_LIST contours
Contours リスト
Definition Brep.h:83
Vector< double > point
頂点の座標.
Definition Brep.h:260
BREP_WING * next
Definition Brep.h:212
BREP_VERTEX * vertex
Start of Vertex.
Definition Brep.h:210
#define LBUF
Definition common.h:146
#define strncasecmp
Definition common.h:59
#define DllExport
Definition common.h:105
Definition Brep.h:29
DllExport void freeSTL(STLData *stldata)
Definition STL.cpp:429
DllExport void println_FacetAsciiSTL(BREP_CONTOUR *contour)
Definition STL.cpp:413
DllExport int writeSTLFileA(char *fname, BREP_SOLID *solid)
Definition STL.cpp:233
DllExport STLData * readSTLFileB(char *fname, long int *fno)
Definition STL.cpp:186
DllExport STLData * readSTLFileA(char *fname, long int *fno)
Definition STL.cpp:112
DllExport STLData * readSTLFile(char *fname, long int *fno)
Definition STL.cpp:23
DllExport int writeSTLFile(char *fname, BREP_SOLID *solid, bool ascii=false)
Definition STL.cpp:74
std::list< BREP_SOLID * > BREP_SOLID_LIST
Definition Brep.h:41
DllExport int writeSTLFileB(char *fname, BREP_SOLID *solid)
Definition STL.cpp:273
float vect[12]
Definition STL.h:32
データ読み込み用一時データ
Definition STL.h:20
#define CHAR_TAB
タブ
Definition tools.h:80
#define PRINT_MESG(...)
環境依存用の出力関数.MS Windows用は未実装
Definition tools.h:469
#define DEBUG_MODE
Definition tools.h:502