1 // Copyright (c) 2015-2021 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 #ifndef _RWObj_ObjWriterContext_HeaderFiler
15 #define _RWObj_ObjWriterContext_HeaderFiler
16 
17 #include <Graphic3d_Vec.hxx>
18 #include <TCollection_AsciiString.hxx>
19 #include <TColStd_IndexedDataMapOfStringString.hxx>
20 
21 //! Auxiliary low-level tool writing OBJ file.
22 class RWObj_ObjWriterContext
23 {
24 public:
25 
26   //! Main constructor.
27   Standard_EXPORT RWObj_ObjWriterContext (const TCollection_AsciiString& theName);
28 
29   //! Destructor, will emit error message if file was not closed.
30   Standard_EXPORT ~RWObj_ObjWriterContext();
31 
32   //! Return true if file has been opened.
IsOpened() const33   bool IsOpened() const { return myFile != NULL; }
34 
35   //! Correctly close the file.
36   Standard_EXPORT bool Close();
37 
38   //! Return true if normals are defined.
HasNormals() const39   bool HasNormals() const { return myHasNormals; }
40 
41   //! Set if normals are defined.
SetNormals(const bool theHasNormals)42   void SetNormals (const bool theHasNormals) { myHasNormals = theHasNormals; }
43 
44   //! Return true if normals are defined.
HasTexCoords() const45   bool HasTexCoords() const { return myHasTexCoords; }
46 
47   //! Set if normals are defined.
SetTexCoords(const bool theHasTexCoords)48   void SetTexCoords (const bool theHasTexCoords) { myHasTexCoords = theHasTexCoords; }
49 
50   //! Write the header.
51   Standard_EXPORT bool WriteHeader (const Standard_Integer theNbNodes,
52                                     const Standard_Integer theNbElems,
53                                     const TCollection_AsciiString& theMatLib,
54                                     const TColStd_IndexedDataMapOfStringString& theFileInfo);
55 
56   //! Return active material or empty string if not set.
ActiveMaterial() const57   const TCollection_AsciiString& ActiveMaterial() const { return myActiveMaterial; }
58 
59   //! Set active material.
60   Standard_EXPORT bool WriteActiveMaterial (const TCollection_AsciiString& theMaterial);
61 
62   //! Writing a triangle
63   Standard_EXPORT bool WriteTriangle (const Graphic3d_Vec3i& theTri);
64 
65   //! Writing a quad
66   Standard_EXPORT bool WriteQuad (const Graphic3d_Vec4i& theQuad);
67 
68   //! Writing a vector
69   Standard_EXPORT bool WriteVertex (const Graphic3d_Vec3& theValue);
70 
71   //! Writing a vector
72   Standard_EXPORT bool WriteNormal (const Graphic3d_Vec3& theValue);
73 
74   //! Writing a vector
75   Standard_EXPORT bool WriteTexCoord (const Graphic3d_Vec2& theValue);
76 
77   //! Writing a group name
78   Standard_EXPORT bool WriteGroup (const TCollection_AsciiString& theValue);
79 
80   //! Increment indices shift.
81   Standard_EXPORT void FlushFace (Standard_Integer theNbNodes);
82 
83 public:
84 
85   Standard_Integer NbFaces;
86 
87 private:
88 
89   FILE* myFile;
90   TCollection_AsciiString myName;
91   TCollection_AsciiString myActiveMaterial;
92   Graphic3d_Vec4i myElemPosFirst;
93   Graphic3d_Vec4i myElemNormFirst;
94   Graphic3d_Vec4i myElemUVFirst;
95   bool myHasNormals;
96   bool myHasTexCoords;
97 
98 };
99 
100 #endif // _RWObj_ObjWriterContext_HeaderFiler
101