0024002: Overall code and build procedure refactoring -- automatic
[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
15 #include <Bnd_Box.hxx>
16 #include <BRepBndLib.hxx>
17 #include <OSD_Path.hxx>
18 #include <RWStl.hxx>
19 #include <StlAPI_Writer.hxx>
20 #include <StlMesh_Mesh.hxx>
21 #include <StlTransfer.hxx>
22 #include <TopoDS_Shape.hxx>
23
24 StlAPI_Writer::StlAPI_Writer()
25 {
26   theStlMesh = new StlMesh_Mesh;
27   theASCIIMode = Standard_True;
28 }
29
30 Standard_Boolean& StlAPI_Writer::ASCIIMode() 
31 {
32   return theASCIIMode;
33 }
34
35 StlAPI_ErrorStatus StlAPI_Writer::Write(const TopoDS_Shape& theShape, const Standard_CString theFileName)
36 {
37   OSD_Path aFile(theFileName);
38   StlTransfer::RetrieveMesh(theShape, theStlMesh);
39
40   if (theStlMesh.IsNull() || theStlMesh->IsEmpty())
41     return StlAPI_MeshIsEmpty;
42
43   // Write the built mesh
44   Standard_Boolean wasFileOpened = Standard_False;
45   if (theASCIIMode) {
46     wasFileOpened = RWStl::WriteAscii(theStlMesh, aFile);
47     }  
48   else {
49     wasFileOpened = RWStl::WriteBinary(theStlMesh, aFile);
50     }
51
52   if (!wasFileOpened)
53     return StlAPI_CannotOpenFile;
54
55   return StlAPI_StatusOK;
56 }
57