9037efae15400eb229b2a862528f0af1ba2baf57
[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 #define MAX2(X, Y)      (  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
23 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X,Y) , Z) )
24
25 StlAPI_Writer::StlAPI_Writer()
26 {
27   theStlMesh = new StlMesh_Mesh;
28   theASCIIMode = Standard_True;
29   theDeflection = 0.01;
30   theRelativeMode = Standard_True;
31   theCoefficient = 0.001;
32 }
33
34 void StlAPI_Writer::SetDeflection(const Standard_Real aDeflection) 
35 {
36   theDeflection = aDeflection;
37 }
38 void StlAPI_Writer::SetCoefficient(const Standard_Real aCoefficient) 
39 {
40   theCoefficient = aCoefficient;
41 }
42
43 Standard_Boolean& StlAPI_Writer::RelativeMode() 
44 {
45   return theRelativeMode;
46 }
47
48 Standard_Boolean& StlAPI_Writer::ASCIIMode() 
49 {
50   return theASCIIMode;
51 }
52
53 void StlAPI_Writer::Write(const TopoDS_Shape& theShape, const Standard_CString theFileName, const Standard_Boolean theInParallel) 
54 {
55   OSD_Path aFile(theFileName);
56   if (theRelativeMode) {
57     Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
58     Bnd_Box Total;
59     BRepBndLib::Add(theShape, Total);
60     Total.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
61     theDeflection = MAX3(aXmax-aXmin , aYmax-aYmin , aZmax-aZmin)*theCoefficient;
62   }
63   StlTransfer::BuildIncrementalMesh(theShape, theDeflection, theInParallel, theStlMesh);
64   // Write the built mesh
65   if (theASCIIMode) {
66     RWStl::WriteAscii(theStlMesh, aFile);
67     }  
68   else {
69     RWStl::WriteBinary(theStlMesh, aFile);
70     }
71 }
72