0024665: A sample for advanced function mechanism
[occt.git] / src / StlAPI / StlAPI_Writer.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
d5f74e42 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
973c2be1 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.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
42cf5bc1 14
7fd59977 15#include <Bnd_Box.hxx>
7fd59977 16#include <BRepBndLib.hxx>
17#include <OSD_Path.hxx>
42cf5bc1 18#include <RWStl.hxx>
19#include <StlAPI_Writer.hxx>
20#include <StlMesh_Mesh.hxx>
21#include <StlTransfer.hxx>
22#include <TopoDS_Shape.hxx>
7fd59977 23
7fd59977 24StlAPI_Writer::StlAPI_Writer()
25{
26 theStlMesh = new StlMesh_Mesh;
27 theASCIIMode = Standard_True;
7fd59977 28}
29
30Standard_Boolean& StlAPI_Writer::ASCIIMode()
31{
32 return theASCIIMode;
33}
34
b9c1e440 35StlAPI_ErrorStatus StlAPI_Writer::Write(const TopoDS_Shape& theShape, const Standard_CString theFileName)
7fd59977 36{
416d4426 37 OSD_Path aFile(theFileName);
b9c1e440 38 StlTransfer::RetrieveMesh(theShape, theStlMesh);
39
40 if (theStlMesh.IsNull() || theStlMesh->IsEmpty())
41 return StlAPI_MeshIsEmpty;
42
7fd59977 43 // Write the built mesh
b9c1e440 44 Standard_Boolean wasFileOpened = Standard_False;
7fd59977 45 if (theASCIIMode) {
b9c1e440 46 wasFileOpened = RWStl::WriteAscii(theStlMesh, aFile);
7fd59977 47 }
48 else {
b9c1e440 49 wasFileOpened = RWStl::WriteBinary(theStlMesh, aFile);
7fd59977 50 }
b9c1e440 51
52 if (!wasFileOpened)
53 return StlAPI_CannotOpenFile;
54
55 return StlAPI_StatusOK;
7fd59977 56}
57