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_CafWriter_HeaderFiler
15 #define _RWObj_CafWriter_HeaderFiler
16 
17 #include <TColStd_IndexedDataMapOfStringString.hxx>
18 #include <TColStd_MapOfAsciiString.hxx>
19 #include <TDF_LabelSequence.hxx>
20 #include <TopTools_ShapeMapHasher.hxx>
21 #include <RWMesh_CoordinateSystemConverter.hxx>
22 #include <XCAFPrs_Style.hxx>
23 
24 #include <memory>
25 
26 class Message_ProgressRange;
27 class RWMesh_FaceIterator;
28 class TDocStd_Document;
29 
30 class Message_LazyProgressScope;
31 class RWObj_ObjWriterContext;
32 class RWObj_ObjMaterialMap;
33 
34 //! OBJ writer context from XCAF document.
35 class RWObj_CafWriter : public Standard_Transient
36 {
37   DEFINE_STANDARD_RTTIEXT(RWObj_CafWriter, Standard_Transient)
38 public:
39 
40   //! Main constructor.
41   //! @param theFile [in] path to output OBJ file
42   Standard_EXPORT RWObj_CafWriter (const TCollection_AsciiString& theFile);
43 
44   //! Destructor.
45   Standard_EXPORT virtual ~RWObj_CafWriter();
46 
47   //! Return transformation from OCCT to OBJ coordinate system.
CoordinateSystemConverter() const48   const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCSTrsf; }
49 
50   //! Return transformation from OCCT to OBJ coordinate system.
ChangeCoordinateSystemConverter()51   RWMesh_CoordinateSystemConverter& ChangeCoordinateSystemConverter() { return myCSTrsf; }
52 
53   //! Set transformation from OCCT to OBJ coordinate system.
SetCoordinateSystemConverter(const RWMesh_CoordinateSystemConverter & theConverter)54   void SetCoordinateSystemConverter (const RWMesh_CoordinateSystemConverter& theConverter) { myCSTrsf = theConverter; }
55 
56   //! Return default material definition to be used for nodes with only color defined.
DefaultStyle() const57   const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
58 
59   //! Set default material definition to be used for nodes with only color defined.
SetDefaultStyle(const XCAFPrs_Style & theStyle)60   void SetDefaultStyle (const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
61 
62   //! Write OBJ file and associated MTL material file.
63   //! Triangulation data should be precomputed within shapes!
64   //! @param theDocument    [in] input document
65   //! @param theRootLabels  [in] list of root shapes to export
66   //! @param theLabelFilter [in] optional filter with document nodes to export,
67   //!                            with keys defined by XCAFPrs_DocumentExplorer::DefineChildId() and filled recursively
68   //!                            (leaves and parent assembly nodes at all levels);
69   //!                            when not NULL, all nodes not included into the map will be ignored
70   //! @param theFileInfo    [in] map with file metadata to put into OBJ header section
71   //! @param theProgress    [in] optional progress indicator
72   //! @return FALSE on file writing failure
73   Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
74                                         const TDF_LabelSequence& theRootLabels,
75                                         const TColStd_MapOfAsciiString* theLabelFilter,
76                                         const TColStd_IndexedDataMapOfStringString& theFileInfo,
77                                         const Message_ProgressRange& theProgress);
78 
79   //! Write OBJ file and associated MTL material file.
80   //! Triangulation data should be precomputed within shapes!
81   //! @param theDocument    [in] input document
82   //! @param theFileInfo    [in] map with file metadata to put into glTF header section
83   //! @param theProgress    [in] optional progress indicator
84   //! @return FALSE on file writing failure
85   Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
86                                         const TColStd_IndexedDataMapOfStringString& theFileInfo,
87                                         const Message_ProgressRange& theProgress);
88 
89 protected:
90 
91   //! Return TRUE if face mesh should be skipped (e.g. because it is invalid or empty).
92   Standard_EXPORT virtual Standard_Boolean toSkipFaceMesh (const RWMesh_FaceIterator& theFaceIter);
93 
94   //! Collect face triangulation info.
95   //! @param theFace [in] face to process
96   //! @param theNbNodes [in] [out] overall number of triangulation nodes (should be appended)
97   //! @param theNbElems [in] [out] overall number of triangulation elements (should be appended)
98   //! @param theNbProgressSteps [in] [out] overall number of progress steps (should be appended)
99   //! @param theToCreateMatFile [in] [out] flag to create material file or not (should be appended)
100   Standard_EXPORT virtual void addFaceInfo (const RWMesh_FaceIterator& theFace,
101                                             Standard_Integer& theNbNodes,
102                                             Standard_Integer& theNbElems,
103                                             Standard_Real& theNbProgressSteps,
104                                             Standard_Boolean& theToCreateMatFile);
105 
106   //! Write the shape.
107   //! @param theWriter  [in] OBJ writer context
108   //! @param theMatMgr  [in] OBJ material map
109   //! @param thePSentry [in] progress sentry
110   //! @param theLabel   [in] document label to process
111   //! @param theParentTrsf  [in] parent node transformation
112   //! @param theParentStyle [in] parent node style
113   //! @param theName    [in] node name
114   Standard_EXPORT virtual bool writeShape (RWObj_ObjWriterContext&        theWriter,
115                                            RWObj_ObjMaterialMap&          theMatMgr,
116                                            Message_LazyProgressScope&     thePSentry,
117                                            const TDF_Label&               theLabel,
118                                            const TopLoc_Location&         theParentTrsf,
119                                            const XCAFPrs_Style&           theParentStyle,
120                                            const TCollection_AsciiString& theName);
121 
122   //! Write face triangle vertex positions.
123   //! @param theWriter  [in] OBJ writer context
124   //! @param thePSentry [in] progress sentry
125   //! @param theFace    [in] current face
126   //! @return FALSE on writing file error
127   Standard_EXPORT virtual bool writePositions (RWObj_ObjWriterContext&    theWriter,
128                                                Message_LazyProgressScope& thePSentry,
129                                                const RWMesh_FaceIterator& theFace);
130 
131   //! Write face triangle vertex normals.
132   //! @param theWriter  [in] OBJ writer context
133   //! @param thePSentry [in] progress sentry
134   //! @param theFace    [in] current face
135   //! @return FALSE on writing file error
136   Standard_EXPORT virtual bool writeNormals (RWObj_ObjWriterContext&    theWriter,
137                                              Message_LazyProgressScope& thePSentry,
138                                              const RWMesh_FaceIterator& theFace);
139 
140   //! Write face triangle vertex texture coordinates.
141   //! @param theWriter  [in] OBJ writer context
142   //! @param thePSentry [in] progress sentry
143   //! @param theFace    [in] current face
144   //! @return FALSE on writing file error
145   Standard_EXPORT virtual bool writeTextCoords (RWObj_ObjWriterContext&    theWriter,
146                                                 Message_LazyProgressScope& thePSentry,
147                                                 const RWMesh_FaceIterator& theFace);
148 
149   //! Write face triangles indices.
150   //! @param theWriter  [in] OBJ writer context
151   //! @param thePSentry [in] progress sentry
152   //! @param theFace    [in] current face
153   //! @return FALSE on writing file error
154   Standard_EXPORT virtual bool writeIndices (RWObj_ObjWriterContext&    theWriter,
155                                              Message_LazyProgressScope& thePSentry,
156                                              const RWMesh_FaceIterator& theFace);
157 
158 
159 protected:
160 
161   TCollection_AsciiString          myFile;         //!< output OBJ file
162   RWMesh_CoordinateSystemConverter myCSTrsf;       //!< transformation from OCCT to OBJ coordinate system
163   XCAFPrs_Style                    myDefaultStyle; //!< default material definition to be used for nodes with only color defined
164 
165 };
166 
167 #endif // _RWObj_CafWriter_HeaderFiler
168