0025357: STL writer does not check the given shape for existing triangulation and...
[occt.git] / src / StlAPI / StlAPI_Writer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <StlAPI_Writer.ixx>
15 #include <StlTransfer.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <Bnd_Box.hxx>
18 #include <RWStl.hxx>
19 #include <BRepBndLib.hxx>
20 #include <OSD_Path.hxx>
21
22 StlAPI_Writer::StlAPI_Writer()
23 {
24   theStlMesh = new StlMesh_Mesh;
25   theASCIIMode = Standard_True;
26 }
27
28 Standard_Boolean& StlAPI_Writer::ASCIIMode() 
29 {
30   return theASCIIMode;
31 }
32
33 StlAPI_ErrorStatus StlAPI_Writer::Write(const TopoDS_Shape& theShape, const Standard_CString theFileName)
34 {
35   OSD_Path aFile(theFileName);
36   StlTransfer::RetrieveMesh(theShape, theStlMesh);
37
38   if (theStlMesh.IsNull() || theStlMesh->IsEmpty())
39     return StlAPI_MeshIsEmpty;
40
41   // Write the built mesh
42   Standard_Boolean wasFileOpened = Standard_False;
43   if (theASCIIMode) {
44     wasFileOpened = RWStl::WriteAscii(theStlMesh, aFile);
45     }  
46   else {
47     wasFileOpened = RWStl::WriteBinary(theStlMesh, aFile);
48     }
49
50   if (!wasFileOpened)
51     return StlAPI_CannotOpenFile;
52
53   return StlAPI_StatusOK;
54 }
55