1 // Created on: 1994-10-13
2 // Created by: Marc LEGAY
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <BRepBuilderAPI_CellFilter.hxx>
19 #include <BRepBuilderAPI_VertexInspector.hxx>
23 #include <Message.hxx>
24 #include <Message_Messenger.hxx>
25 #include <Message_ProgressIndicator.hxx>
26 #include <Message_ProgressSentry.hxx>
28 #include <OSD_File.hxx>
29 #include <OSD_Host.hxx>
30 #include <OSD_OpenFile.hxx>
31 #include <OSD_Path.hxx>
32 #include <OSD_Protection.hxx>
33 #include <Precision.hxx>
35 #include <Standard_NoMoreObject.hxx>
36 #include <Standard_TypeMismatch.hxx>
37 #include <StlMesh_Mesh.hxx>
38 #include <StlMesh_MeshExplorer.hxx>
39 #include <TCollection_AsciiString.hxx>
42 // A static method adding nodes to a mesh and keeping coincident (sharing) nodes.
43 static Standard_Integer AddVertex(Handle(StlMesh_Mesh)& mesh,
44 BRepBuilderAPI_CellFilter& filter,
45 BRepBuilderAPI_VertexInspector& inspector,
48 Standard_Integer index;
49 inspector.SetCurrent(p);
50 gp_XYZ minp = inspector.Shift(p, -Precision::Confusion());
51 gp_XYZ maxp = inspector.Shift(p, +Precision::Confusion());
52 filter.Inspect(minp, maxp, inspector);
53 const TColStd_ListOfInteger& indices = inspector.ResInd();
54 if (indices.IsEmpty() == Standard_False)
56 index = indices.First(); // it should be only one
57 inspector.ClearResList();
61 index = mesh->AddVertex(p.X(), p.Y(), p.Z());
69 static const size_t HEADER_SIZE = 84;
70 static const size_t SIZEOF_STL_FACET = 50;
71 static const size_t ASCII_LINES_PER_FACET = 7;
73 static const int IND_THRESHOLD = 1000; // increment the indicator every 1k triangles
75 //=======================================================================
76 //function : WriteInteger
77 //purpose : writing a Little Endian 32 bits integer
78 //=======================================================================
80 inline static void WriteInteger(OSD_File& ofile,const Standard_Integer value)
83 Standard_Integer i;// don't be afraid, this is just an unsigned int
89 Standard_Integer entier;
91 entier = bidargum.c[0] & 0xFF;
92 entier |= (bidargum.c[1] & 0xFF) << 0x08;
93 entier |= (bidargum.c[2] & 0xFF) << 0x10;
94 entier |= (bidargum.c[3] & 0xFF) << 0x18;
96 ofile.Write((char *)&entier,sizeof(bidargum.c));
99 //=======================================================================
100 //function : WriteDouble2Float
101 //purpose : writing a Little Endian 32 bits float
102 //=======================================================================
104 inline static void WriteDouble2Float(OSD_File& ofile,Standard_Real value)
107 Standard_ShortReal f;
111 bidargum.f = (Standard_ShortReal)value;
113 Standard_Integer entier;
115 entier = bidargum.c[0] & 0xFF;
116 entier |= (bidargum.c[1] & 0xFF) << 0x08;
117 entier |= (bidargum.c[2] & 0xFF) << 0x10;
118 entier |= (bidargum.c[3] & 0xFF) << 0x18;
120 ofile.Write((char *)&entier,sizeof(bidargum.c));
124 //=======================================================================
125 //function : readFloat2Double
126 //purpose : reading a Little Endian 32 bits float
127 //=======================================================================
129 inline static Standard_Real ReadFloat2Double(OSD_File &aFile)
137 Standard_Address adr;
138 adr = (Standard_Address)c;
139 Standard_Integer lread;
140 aFile.Read(adr,4,lread);
141 bidargum.i = c[0] & 0xFF;
142 bidargum.i |= (c[1] & 0xFF) << 0x08;
143 bidargum.i |= (c[2] & 0xFF) << 0x10;
144 bidargum.i |= (c[3] & 0xFF) << 0x18;
146 return (Standard_Real)(bidargum.f);
151 //=======================================================================
152 //function : WriteBinary
153 //purpose : write a binary STL file in Little Endian format
154 //=======================================================================
156 Standard_Boolean RWStl::WriteBinary (const Handle(StlMesh_Mesh)& theMesh,
157 const OSD_Path& thePath,
158 const Handle(Message_ProgressIndicator)& theProgInd)
160 OSD_File aFile (thePath);
161 aFile.Build (OSD_WriteOnly, OSD_Protection());
163 Standard_Real x1, y1, z1;
164 Standard_Real x2, y2, z2;
165 Standard_Real x3, y3, z3;
167 // writing 80 bytes of the trash?
169 aFile.Write ((Standard_Address)sval,80);
170 WriteInteger (aFile, theMesh->NbTriangles());
173 StlMesh_MeshExplorer aMexp (theMesh);
175 // create progress sentry for domains
176 Standard_Integer aNbDomains = theMesh->NbDomains();
177 Message_ProgressSentry aDPS (theProgInd, "Mesh domains", 0, aNbDomains, 1);
178 for (Standard_Integer nbd = 1; nbd <= aNbDomains && aDPS.More(); nbd++, aDPS.Next())
180 // create progress sentry for triangles in domain
181 Message_ProgressSentry aTPS (theProgInd, "Triangles", 0,
182 theMesh->NbTriangles (nbd), IND_THRESHOLD);
183 Standard_Integer aTriangleInd = 0;
184 for (aMexp.InitTriangle (nbd); aMexp.MoreTriangle(); aMexp.NextTriangle())
186 aMexp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
187 //pgo aMexp.TriangleOrientation (x,y,z);
188 gp_XYZ Vect12 ((x2-x1), (y2-y1), (z2-z1));
189 gp_XYZ Vect13 ((x3-x1), (y3-y1), (z3-z1));
190 gp_XYZ Vnorm = Vect12 ^ Vect13;
191 Standard_Real Vmodul = Vnorm.Modulus ();
192 if (Vmodul > gp::Resolution())
194 Vnorm.Divide(Vmodul);
198 // si Vnorm est quasi-nul, on le charge a 0 explicitement
199 Vnorm.SetCoord (0., 0., 0.);
202 WriteDouble2Float (aFile, Vnorm.X());
203 WriteDouble2Float (aFile, Vnorm.Y());
204 WriteDouble2Float (aFile, Vnorm.Z());
206 WriteDouble2Float (aFile, x1);
207 WriteDouble2Float (aFile, y1);
208 WriteDouble2Float (aFile, z1);
210 WriteDouble2Float (aFile, x2);
211 WriteDouble2Float (aFile, y2);
212 WriteDouble2Float (aFile, z2);
214 WriteDouble2Float (aFile, x3);
215 WriteDouble2Float (aFile, y3);
216 WriteDouble2Float (aFile, z3);
218 aFile.Write (&dum, 2);
220 // update progress only per 1k triangles
221 if (++aTriangleInd % IND_THRESHOLD == 0)
230 Standard_Boolean isInterrupted = !aDPS.More();
231 return !isInterrupted;
233 //=======================================================================
234 //function : WriteAscii
235 //purpose : write an ASCII STL file
236 //=======================================================================
238 Standard_Boolean RWStl::WriteAscii (const Handle(StlMesh_Mesh)& theMesh,
239 const OSD_Path& thePath,
240 const Handle(Message_ProgressIndicator)& theProgInd)
242 OSD_File theFile (thePath);
243 theFile.Build(OSD_WriteOnly,OSD_Protection());
244 TCollection_AsciiString buf ("solid\n");
245 theFile.Write (buf,buf.Length());buf.Clear();
247 Standard_Real x1, y1, z1;
248 Standard_Real x2, y2, z2;
249 Standard_Real x3, y3, z3;
252 // create progress sentry for domains
253 Standard_Integer aNbDomains = theMesh->NbDomains();
254 Message_ProgressSentry aDPS (theProgInd, "Mesh domains", 0, aNbDomains, 1);
255 StlMesh_MeshExplorer aMexp (theMesh);
256 for (Standard_Integer nbd = 1; nbd <= aNbDomains && aDPS.More(); nbd++, aDPS.Next())
258 // create progress sentry for triangles in domain
259 Message_ProgressSentry aTPS (theProgInd, "Triangles", 0,
260 theMesh->NbTriangles (nbd), IND_THRESHOLD);
261 Standard_Integer aTriangleInd = 0;
262 for (aMexp.InitTriangle (nbd); aMexp.MoreTriangle(); aMexp.NextTriangle())
264 aMexp.TriangleVertices (x1,y1,z1,x2,y2,z2,x3,y3,z3);
266 // Standard_Real x, y, z;
267 // aMexp.TriangleOrientation (x,y,z);
269 gp_XYZ Vect12 ((x2-x1), (y2-y1), (z2-z1));
270 gp_XYZ Vect23 ((x3-x2), (y3-y2), (z3-z2));
271 gp_XYZ Vnorm = Vect12 ^ Vect23;
272 Standard_Real Vmodul = Vnorm.Modulus ();
273 if (Vmodul > gp::Resolution())
275 Vnorm.Divide (Vmodul);
279 // si Vnorm est quasi-nul, on le charge a 0 explicitement
280 Vnorm.SetCoord (0., 0., 0.);
283 " facet normal % 12e % 12e % 12e\n"
285 " vertex % 12e % 12e % 12e\n"
286 " vertex % 12e % 12e % 12e\n"
287 " vertex % 12e % 12e % 12e\n"
290 Vnorm.X(), Vnorm.Y(), Vnorm.Z(),
295 theFile.Write (buf, buf.Length()); buf.Clear();
297 // update progress only per 1k triangles
298 if (++aTriangleInd % IND_THRESHOLD == 0)
308 theFile.Write (buf, buf.Length()); buf.Clear();
310 Standard_Boolean isInterrupted = !aDPS.More();
311 return !isInterrupted;
313 //=======================================================================
314 //function : ReadFile
317 //=======================================================================
319 Handle(StlMesh_Mesh) RWStl::ReadFile (const OSD_Path& thePath,
320 const Handle(Message_ProgressIndicator)& theProgInd)
322 OSD_File file (thePath);
323 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
324 Standard_Boolean IsAscii;
325 unsigned char str[128];
326 Standard_Integer lread,i;
327 Standard_Address ach;
328 ach = (Standard_Address)str;
330 // we skip the header which is in Ascii for both modes
331 file.Read(ach,HEADER_SIZE,lread);
333 // we read 128 characters to detect if we have a non-ascii char
334 file.Read(ach,sizeof(str),lread);
336 IsAscii = Standard_True;
337 for (i = 0; i< lread && IsAscii; ++i) {
339 IsAscii = Standard_False;
343 cout << (IsAscii ? "ascii\n" : "binary\n");
347 return IsAscii ? RWStl::ReadAscii (thePath, theProgInd)
348 : RWStl::ReadBinary (thePath, theProgInd);
351 //=======================================================================
352 //function : ReadBinary
355 //=======================================================================
357 Handle(StlMesh_Mesh) RWStl::ReadBinary (const OSD_Path& thePath,
358 const Handle(Message_ProgressIndicator)& /*theProgInd*/)
360 Standard_Integer ifacet;
361 Standard_Real fx,fy,fz,fx1,fy1,fz1,fx2,fy2,fz2,fx3,fy3,fz3;
362 Standard_Integer i1,i2,i3,lread;
364 Standard_Address adr;
365 adr = (Standard_Address)buftest;
368 OSD_File theFile (thePath);
369 theFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
371 // the size of the file (minus the header size)
372 // must be a multiple of SIZEOF_STL_FACET
375 Standard_Size filesize = theFile.Size();
377 // don't trust the number of triangles which is coded in the file sometimes it is wrong
378 Standard_Integer NBFACET = (Standard_Integer)((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
381 throw Standard_NoMoreObject("RWStl::ReadBinary (wrong file size)");
384 theFile.Seek (80, OSD_FromBeginning);
385 theFile.Read (adr, 4, lread);
386 Standard_Integer aNbTrisInHeader = (((char* )buftest)[3] << 24) | (((Standard_Byte* )buftest)[2] << 16)
387 | (((Standard_Byte* )buftest)[1] << 8 ) | (((Standard_Byte* )buftest)[0] << 0 );
388 if (NBFACET < aNbTrisInHeader)
390 Message::DefaultMessenger()->Send (TCollection_AsciiString ("RWStl - Binary STL file defines more triangles (") + aNbTrisInHeader
391 + ") that can be read (" + NBFACET + ") - probably corrupted file",
394 else if (NBFACET > aNbTrisInHeader)
396 Message::DefaultMessenger()->Send (TCollection_AsciiString ("RWStl - Binary STL file defines less triangles (") + aNbTrisInHeader
397 + ") that can be read (" + NBFACET + ") - probably corrupted file",
400 else if ((filesize - HEADER_SIZE) % SIZEOF_STL_FACET != 0)
402 Message::DefaultMessenger()->Send (TCollection_AsciiString ("RWStl - Binary STL file has unidentified tail"),
407 theFile.Seek(HEADER_SIZE,OSD_FromBeginning);
409 // create the StlMesh_Mesh object
410 Handle(StlMesh_Mesh) ReadMesh = new StlMesh_Mesh ();
411 ReadMesh->AddDomain ();
413 // Filter unique vertices to share the nodes of the mesh.
414 BRepBuilderAPI_CellFilter uniqueVertices(Precision::Confusion());
415 BRepBuilderAPI_VertexInspector inspector(Precision::Confusion());
417 for (ifacet=1; ifacet<=NBFACET; ++ifacet) {
418 // read normal coordinates
419 fx = ReadFloat2Double(theFile);
420 fy = ReadFloat2Double(theFile);
421 fz = ReadFloat2Double(theFile);
424 fx1 = ReadFloat2Double(theFile);
425 fy1 = ReadFloat2Double(theFile);
426 fz1 = ReadFloat2Double(theFile);
429 fx2 = ReadFloat2Double(theFile);
430 fy2 = ReadFloat2Double(theFile);
431 fz2 = ReadFloat2Double(theFile);
434 fx3 = ReadFloat2Double(theFile);
435 fy3 = ReadFloat2Double(theFile);
436 fz3 = ReadFloat2Double(theFile);
439 i1 = AddVertex(ReadMesh, uniqueVertices, inspector, gp_XYZ(fx1, fy1, fz1));
440 i2 = AddVertex(ReadMesh, uniqueVertices, inspector, gp_XYZ(fx2, fy2, fz2));
441 i3 = AddVertex(ReadMesh, uniqueVertices, inspector, gp_XYZ(fx3, fy3, fz3));
444 ReadMesh->AddTriangle (i1,i2,i3,fx,fy,fz);
447 theFile.Read(adr,2,lread);
454 //=======================================================================
455 //function : ReadAscii
458 //=======================================================================
460 Handle(StlMesh_Mesh) RWStl::ReadAscii (const OSD_Path& thePath,
461 const Handle(Message_ProgressIndicator)& theProgInd)
463 TCollection_AsciiString filename;
465 Standard_Integer nbLines = 0;
466 Standard_Integer nbTris = 0;
467 Standard_Integer iTri;
468 Standard_Integer i1,i2,i3;
469 Handle(StlMesh_Mesh) ReadMesh;
471 thePath.SystemName (filename);
474 FILE* file = OSD_OpenFile(filename.ToCString(),"r");
476 fseek(file,0L,SEEK_END);
478 long filesize = ftell(file);
482 // count the number of lines
483 for (ipos = 0; ipos < filesize; ++ipos) {
484 if (getc(file) == '\n')
488 // compute number of triangles
489 nbTris = (nbLines / ASCII_LINES_PER_FACET);
491 // go back to the beginning of the file
495 while (getc(file) != '\n');
497 cout << "start mesh\n";
499 ReadMesh = new StlMesh_Mesh();
500 ReadMesh->AddDomain();
502 // Filter unique vertices to share the nodes of the mesh.
503 BRepBuilderAPI_CellFilter uniqueVertices(Precision::Confusion());
504 BRepBuilderAPI_VertexInspector inspector(Precision::Confusion());
507 Message_ProgressSentry aPS (theProgInd, "Triangles", 0, (nbTris - 1) * 1.0 / IND_THRESHOLD, 1);
508 for (iTri = 0; iTri < nbTris && aPS.More();)
510 char x[256]="", y[256]="", z[256]="";
512 // reading the facet normal
513 if (3 != fscanf(file,"%*s %*s %80s %80s %80s\n", x, y, z))
514 break; // error should be properly reported
515 gp_XYZ aN (Atof(x), Atof(y), Atof(z));
517 // skip the keywords "outer loop"
518 if (fscanf(file,"%*s %*s") < 0)
522 if (3 != fscanf(file,"%*s %80s %80s %80s\n", x, y, z))
523 break; // error should be properly reported
524 gp_XYZ aV1 (Atof(x), Atof(y), Atof(z));
525 if (3 != fscanf(file,"%*s %80s %80s %80s\n", x, y, z))
526 break; // error should be properly reported
527 gp_XYZ aV2 (Atof(x), Atof(y), Atof(z));
528 if (3 != fscanf(file,"%*s %80s %80s %80s\n", x, y, z))
529 break; // error should be properly reported
530 gp_XYZ aV3 (Atof(x), Atof(y), Atof(z));
532 // here the facet must be built and put in the mesh datastructure
534 i1 = AddVertex(ReadMesh, uniqueVertices, inspector, aV1);
535 i2 = AddVertex(ReadMesh, uniqueVertices, inspector, aV2);
536 i3 = AddVertex(ReadMesh, uniqueVertices, inspector, aV3);
537 ReadMesh->AddTriangle (i1, i2, i3, aN.X(), aN.Y(), aN.Z());
539 // skip the keywords "endloop"
540 if (fscanf(file,"%*s") < 0)
543 // skip the keywords "endfacet"
544 if (fscanf(file,"%*s") < 0)
547 // update progress only per 1k triangles
548 if (++iTri % IND_THRESHOLD == 0)
552 cout << "end mesh\n";