2 // Created: Thu Oct 13 13:41:29 1994
6 // Copyright: Matra Datavision
9 #include <OSD_Protection.hxx>
10 #include <OSD_File.hxx>
11 #include <Message_ProgressSentry.hxx>
12 #include <TCollection_AsciiString.hxx>
13 #include <Standard_NoMoreObject.hxx>
14 #include <Standard_TypeMismatch.hxx>
15 #include <Precision.hxx>
16 #include <StlMesh_MeshExplorer.hxx>
18 #include <OSD_Host.hxx>
26 static const int HEADER_SIZE = 84;
27 static const int SIZEOF_STL_FACET = 50;
28 static const int STL_MIN_FILE_SIZE = 284;
29 static const int ASCII_LINES_PER_FACET = 7;
30 static const int IND_THRESHOLD = 1000; // increment the indicator every 1k triangles
32 //=======================================================================
33 //function : WriteInteger
34 //purpose : writing a Little Endian 32 bits integer
35 //=======================================================================
37 inline static void WriteInteger(OSD_File& ofile,const Standard_Integer value)
40 Standard_Integer i;// don't be afraid, this is just an unsigned int
46 Standard_Integer entier;
48 entier = bidargum.c[0] & 0xFF;
49 entier |= (bidargum.c[1] & 0xFF) << 0x08;
50 entier |= (bidargum.c[2] & 0xFF) << 0x10;
51 entier |= (bidargum.c[3] & 0xFF) << 0x18;
53 ofile.Write((char *)&entier,sizeof(bidargum.c));
56 //=======================================================================
57 //function : WriteDouble2Float
58 //purpose : writing a Little Endian 32 bits float
59 //=======================================================================
61 inline static void WriteDouble2Float(OSD_File& ofile,Standard_Real value)
68 bidargum.f = (Standard_ShortReal)value;
70 Standard_Integer entier;
72 entier = bidargum.c[0] & 0xFF;
73 entier |= (bidargum.c[1] & 0xFF) << 0x08;
74 entier |= (bidargum.c[2] & 0xFF) << 0x10;
75 entier |= (bidargum.c[3] & 0xFF) << 0x18;
77 ofile.Write((char *)&entier,sizeof(bidargum.c));
81 //=======================================================================
82 //function : readFloat2Double
83 //purpose : reading a Little Endian 32 bits float
84 //=======================================================================
86 inline static Standard_Real ReadFloat2Double(OSD_File &aFile)
89 Standard_Boolean i; // don't be afraid, this is just an unsigned int
95 adr = (Standard_Address)c;
96 Standard_Integer lread;
97 aFile.Read(adr,4,lread);
98 bidargum.i = c[0] & 0xFF;
99 bidargum.i |= (c[1] & 0xFF) << 0x08;
100 bidargum.i |= (c[2] & 0xFF) << 0x10;
101 bidargum.i |= (c[3] & 0xFF) << 0x18;
103 return (Standard_Real)(bidargum.f);
108 //=======================================================================
109 //function : WriteBinary
110 //purpose : write a binary STL file in Little Endian format
111 //=======================================================================
113 Standard_Boolean RWStl::WriteBinary (const Handle(StlMesh_Mesh)& theMesh,
114 const OSD_Path& thePath,
115 const Handle(Message_ProgressIndicator)& theProgInd)
117 OSD_File aFile (thePath);
118 aFile.Build (OSD_WriteOnly, OSD_Protection());
120 Standard_Real x1, y1, z1;
121 Standard_Real x2, y2, z2;
122 Standard_Real x3, y3, z3;
124 // writing 80 bytes of the trash?
126 aFile.Write ((Standard_Address)sval,80);
127 WriteInteger (aFile, theMesh->NbTriangles());
130 StlMesh_MeshExplorer aMexp (theMesh);
132 // create progress sentry for domains
133 Standard_Integer aNbDomains = theMesh->NbDomains();
134 Message_ProgressSentry aDPS (theProgInd, "Mesh domains", 0, aNbDomains, 1);
135 for (Standard_Integer nbd = 1; nbd <= aNbDomains && aDPS.More(); nbd++, aDPS.Next())
137 // create progress sentry for triangles in domain
138 Message_ProgressSentry aTPS (theProgInd, "Triangles", 0,
139 theMesh->NbTriangles (nbd), IND_THRESHOLD);
140 Standard_Integer aTriangleInd = 0;
141 for (aMexp.InitTriangle (nbd); aMexp.MoreTriangle(); aMexp.NextTriangle())
143 aMexp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
144 //pgo aMexp.TriangleOrientation (x,y,z);
145 gp_XYZ Vect12 ((x2-x1), (y2-y1), (z2-z1));
146 gp_XYZ Vect13 ((x3-x1), (y3-y1), (z3-z1));
147 gp_XYZ Vnorm = Vect12 ^ Vect13;
148 Standard_Real Vmodul = Vnorm.Modulus ();
149 if (Vmodul > gp::Resolution())
151 Vnorm.Divide(Vmodul);
155 // si Vnorm est quasi-nul, on le charge a 0 explicitement
156 Vnorm.SetCoord (0., 0., 0.);
159 WriteDouble2Float (aFile, Vnorm.X());
160 WriteDouble2Float (aFile, Vnorm.Y());
161 WriteDouble2Float (aFile, Vnorm.Z());
163 WriteDouble2Float (aFile, x1);
164 WriteDouble2Float (aFile, y1);
165 WriteDouble2Float (aFile, z1);
167 WriteDouble2Float (aFile, x2);
168 WriteDouble2Float (aFile, y2);
169 WriteDouble2Float (aFile, z2);
171 WriteDouble2Float (aFile, x3);
172 WriteDouble2Float (aFile, y3);
173 WriteDouble2Float (aFile, z3);
175 aFile.Write (&dum, 2);
177 // update progress only per 1k triangles
178 if (++aTriangleInd % IND_THRESHOLD == 0)
187 Standard_Boolean isInterrupted = !aDPS.More();
188 return !isInterrupted;
190 //=======================================================================
191 //function : WriteAscii
192 //purpose : write an ASCII STL file
193 //=======================================================================
195 Standard_Boolean RWStl::WriteAscii (const Handle(StlMesh_Mesh)& theMesh,
196 const OSD_Path& thePath,
197 const Handle(Message_ProgressIndicator)& theProgInd)
199 OSD_File theFile (thePath);
200 theFile.Build(OSD_WriteOnly,OSD_Protection());
201 TCollection_AsciiString buf ("solid\n");
202 theFile.Write (buf,buf.Length());buf.Clear();
204 Standard_Real x1, y1, z1;
205 Standard_Real x2, y2, z2;
206 Standard_Real x3, y3, z3;
209 // create progress sentry for domains
210 Standard_Integer aNbDomains = theMesh->NbDomains();
211 Message_ProgressSentry aDPS (theProgInd, "Mesh domains", 0, aNbDomains, 1);
212 StlMesh_MeshExplorer aMexp (theMesh);
213 for (Standard_Integer nbd = 1; nbd <= aNbDomains && aDPS.More(); nbd++, aDPS.Next())
215 // create progress sentry for triangles in domain
216 Message_ProgressSentry aTPS (theProgInd, "Triangles", 0,
217 theMesh->NbTriangles (nbd), IND_THRESHOLD);
218 Standard_Integer aTriangleInd = 0;
219 for (aMexp.InitTriangle (nbd); aMexp.MoreTriangle(); aMexp.NextTriangle())
221 aMexp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
223 // Standard_Real x, y, z;
224 // aMexp.TriangleOrientation (x,y,z);
226 gp_XYZ Vect12 ((x2-x1), (y2-y1), (z2-z1));
227 gp_XYZ Vect23 ((x3-x2), (y3-y2), (z3-z2));
228 gp_XYZ Vnorm = Vect12 ^ Vect23;
229 Standard_Real Vmodul = Vnorm.Modulus ();
230 if (Vmodul > gp::Resolution())
232 Vnorm.Divide (Vmodul);
236 // si Vnorm est quasi-nul, on le charge a 0 explicitement
237 Vnorm.SetCoord (0., 0., 0.);
240 " facet normal % 12e % 12e % 12e\n"
242 " vertex % 12e % 12e % 12e\n"
243 " vertex % 12e % 12e % 12e\n"
244 " vertex % 12e % 12e % 12e\n"
247 Vnorm.X(), Vnorm.Y(), Vnorm.Z(),
252 theFile.Write (buf, buf.Length()); buf.Clear();
254 // update progress only per 1k triangles
255 if (++aTriangleInd % IND_THRESHOLD == 0)
265 theFile.Write (buf, buf.Length()); buf.Clear();
267 Standard_Boolean isInterrupted = !aDPS.More();
268 return !isInterrupted;
270 //=======================================================================
271 //function : ReadFile
274 //=======================================================================
276 Handle_StlMesh_Mesh RWStl::ReadFile (const OSD_Path& thePath,
277 const Handle(Message_ProgressIndicator)& theProgInd)
279 OSD_File file (thePath);
280 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
281 Standard_Boolean IsAscii;
282 unsigned char str[128];
283 Standard_Integer lread,i;
284 Standard_Address ach;
285 ach = (Standard_Address)str;
287 // we skip the header which is in Ascii for both modes
288 file.Read(ach,HEADER_SIZE,lread);
290 // we read 128 characters to detect if we have a non-ascii char
291 file.Read(ach,sizeof(str),lread);
293 IsAscii = Standard_True;
294 for (i = 0; i< lread && IsAscii; ++i) {
296 IsAscii = Standard_False;
300 cout << (IsAscii ? "ascii\n" : "binary\n");
304 return IsAscii ? RWStl::ReadAscii (thePath, theProgInd)
305 : RWStl::ReadBinary (thePath, theProgInd);
308 //=======================================================================
309 //function : ReadBinary
312 //=======================================================================
314 Handle_StlMesh_Mesh RWStl::ReadBinary (const OSD_Path& thePath,
315 const Handle(Message_ProgressIndicator)& /*theProgInd*/)
317 Standard_Integer NBFACET;
318 Standard_Integer ifacet;
319 Standard_Real fx,fy,fz,fx1,fy1,fz1,fx2,fy2,fz2,fx3,fy3,fz3;
320 Standard_Integer i1,i2,i3,lread;
322 Standard_Address adr;
323 adr = (Standard_Address)buftest;
326 OSD_File theFile (thePath);
327 theFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
329 // the size of the file (minus the header size)
330 // must be a multiple of SIZEOF_STL_FACET
333 Standard_Integer filesize = theFile.Size();
335 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0
336 || (filesize < STL_MIN_FILE_SIZE)) {
337 Standard_NoMoreObject::Raise("RWStl::ReadBinary (wrong file size)");
340 // don't trust the number of triangles which is coded in the file
341 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
342 NBFACET = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
345 theFile.Seek(HEADER_SIZE,OSD_FromBeginning);
347 // create the StlMesh_Mesh object
348 Handle(StlMesh_Mesh) ReadMesh = new StlMesh_Mesh ();
349 ReadMesh->AddDomain ();
351 for (ifacet=1; ifacet<=NBFACET; ++ifacet) {
352 // read normal coordinates
353 fx = ReadFloat2Double(theFile);
354 fy = ReadFloat2Double(theFile);
355 fz = ReadFloat2Double(theFile);
358 fx1 = ReadFloat2Double(theFile);
359 fy1 = ReadFloat2Double(theFile);
360 fz1 = ReadFloat2Double(theFile);
363 fx2 = ReadFloat2Double(theFile);
364 fy2 = ReadFloat2Double(theFile);
365 fz2 = ReadFloat2Double(theFile);
368 fx3 = ReadFloat2Double(theFile);
369 fy3 = ReadFloat2Double(theFile);
370 fz3 = ReadFloat2Double(theFile);
372 i1 = ReadMesh->AddOnlyNewVertex (fx1,fy1,fz1);
373 i2 = ReadMesh->AddOnlyNewVertex (fx2,fy2,fz2);
374 i3 = ReadMesh->AddOnlyNewVertex (fx3,fy3,fz3);
375 ReadMesh->AddTriangle (i1,i2,i3,fx,fy,fz);
378 theFile.Read(adr,2,lread);
385 //=======================================================================
386 //function : ReadAscii
389 //=======================================================================
391 Handle_StlMesh_Mesh RWStl::ReadAscii (const OSD_Path& thePath,
392 const Handle(Message_ProgressIndicator)& theProgInd)
394 TCollection_AsciiString filename;
396 Standard_Integer nbLines = 0;
397 Standard_Integer nbTris = 0;
398 Standard_Integer iTri;
399 Standard_ShortReal x[4],y[4],z[4];
400 Standard_Integer i1,i2,i3;
401 Handle(StlMesh_Mesh) ReadMesh;
403 thePath.SystemName (filename);
406 FILE* file = fopen(filename.ToCString(),"r");
408 fseek(file,0L,SEEK_END);
410 long filesize = ftell(file);
413 file = fopen(filename.ToCString(),"r");
417 // count the number of lines
418 for (ipos = 0; ipos < filesize; ++ipos) {
419 if (getc(file) == '\n')
423 // compute number of triangles
424 nbTris = (nbLines / ASCII_LINES_PER_FACET);
426 // go back to the beginning of the file
428 // file = fopen(filename.ToCString(),"r");
432 while (getc(file) != '\n');
434 cout << "start mesh\n";
436 ReadMesh = new StlMesh_Mesh();
437 ReadMesh->AddDomain();
440 Message_ProgressSentry aPS (theProgInd, "Triangles", 0, (nbTris - 1) * 1.0 / IND_THRESHOLD, 1);
441 for (iTri = 0; iTri < nbTris && aPS.More();)
443 // reading the facet normal
444 fscanf(file,"%*s %*s %f %f %f\n",&x[0],&y[0],&z[0]);
446 // skip the keywords "outer loop"
447 fscanf(file,"%*s %*s");
450 fscanf(file,"%*s %f %f %f\n",&x[1],&y[1],&z[1]);
451 fscanf(file,"%*s %f %f %f\n",&x[2],&y[2],&z[2]);
452 fscanf(file,"%*s %f %f %f\n",&x[3],&y[3],&z[3]);
454 // here the facet must be built and put in the mesh datastructure
456 i1 = ReadMesh->AddOnlyNewVertex ((Standard_Real)x[1],(Standard_Real)y[1],(Standard_Real)z[1]);
457 i2 = ReadMesh->AddOnlyNewVertex ((Standard_Real)x[2],(Standard_Real)y[2],(Standard_Real)z[2]);
458 i3 = ReadMesh->AddOnlyNewVertex ((Standard_Real)x[3],(Standard_Real)y[3],(Standard_Real)z[3]);
459 ReadMesh->AddTriangle (i1,i2,i3,(Standard_Real)x[0],(Standard_Real)y[0],(Standard_Real)z[0]);
461 // skip the keywords "endloop"
464 // skip the keywords "endfacet"
467 // update progress only per 1k triangles
468 if (++iTri % IND_THRESHOLD == 0)
472 cout << "end mesh\n";