1 // Copyright (c) 2017-2019 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 _RWMesh_MaterialMap_HeaderFile
15 #define _RWMesh_MaterialMap_HeaderFile
16 
17 #include <NCollection_DoubleMap.hxx>
18 #include <NCollection_Map.hxx>
19 #include <XCAFPrs_Style.hxx>
20 
21 //! Material manager.
22 //! Provides an interface for collecting all materials within the document before writing it into file,
23 //! and for copying associated image files (textures) into sub-folder near by exported model.
24 class RWMesh_MaterialMap : public Standard_Transient
25 {
26   DEFINE_STANDARD_RTTIEXT(RWMesh_MaterialMap, Standard_Transient)
27 public:
28 
29   //! Main constructor.
30   Standard_EXPORT RWMesh_MaterialMap (const TCollection_AsciiString& theFile);
31 
32   //! Destructor.
33   Standard_EXPORT virtual ~RWMesh_MaterialMap();
34 
35   //! Return default material definition to be used for nodes with only color defined.
DefaultStyle() const36   const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
37 
38   //! Set default material definition to be used for nodes with only color defined.
SetDefaultStyle(const XCAFPrs_Style & theStyle)39   void SetDefaultStyle (const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
40 
41   //! Find already registered material
FindMaterial(const XCAFPrs_Style & theStyle) const42   TCollection_AsciiString FindMaterial (const XCAFPrs_Style& theStyle) const
43   {
44     if (myStyles.IsBound1 (theStyle))
45     {
46       return myStyles.Find1 (theStyle);
47     }
48     return TCollection_AsciiString();
49   }
50 
51   //! Register material and return its name identifier.
52   Standard_EXPORT virtual TCollection_AsciiString AddMaterial (const XCAFPrs_Style& theStyle);
53 
54   //! Create texture folder "modelName/textures"; for example:
55   //! MODEL:  Path/ModelName.gltf
56   //! IMAGES: Path/ModelName/textures/
57   //! Warning! Output folder is NOT cleared.
58   Standard_EXPORT virtual bool CreateTextureFolder();
59 
60   //! Copy and rename texture file to the new location.
61   //! @param theResTexture [out] result texture file path (relative to the model)
62   //! @param theTexture [in] original texture
63   //! @param theKey [in] material key
64   Standard_EXPORT virtual bool CopyTexture (TCollection_AsciiString& theResTexture,
65                                             const Handle(Image_Texture)& theTexture,
66                                             const TCollection_AsciiString& theKey);
67 
68   //! Virtual method actually defining the material (e.g. export to the file).
69   virtual void DefineMaterial (const XCAFPrs_Style& theStyle,
70                                const TCollection_AsciiString& theKey,
71                                const TCollection_AsciiString& theName) = 0;
72 
73   //! Return failed flag.
IsFailed() const74   bool IsFailed() const { return myIsFailed; }
75 
76 protected:
77 
78   //! Copy file to another place.
79   Standard_EXPORT static bool copyFileTo (const TCollection_AsciiString& theFileSrc,
80                                           const TCollection_AsciiString& theFileDst);
81 
82 protected:
83 
84   TCollection_AsciiString myFolder;            //!< output folder for glTF file
85   TCollection_AsciiString myTexFolder;         //!< output folder for images (full  path)
86   TCollection_AsciiString myTexFolderShort;    //!< output folder for images (short path)
87   TCollection_AsciiString myFileName;          //!< output glTF file path
88   TCollection_AsciiString myShortFileNameBase; //!< output glTF file name without extension
89   TCollection_AsciiString myKeyPrefix;         //!< prefix for generated keys
90   NCollection_DoubleMap<XCAFPrs_Style, TCollection_AsciiString,
91                         XCAFPrs_Style, TCollection_AsciiString>
92                           myStyles;            //!< map of processed styles
93   NCollection_Map<Handle(Image_Texture), Image_Texture>
94                           myImageFailMap;      //!< map of images failed to be copied
95   XCAFPrs_Style           myDefaultStyle;      //!< default material definition to be used for nodes with only color defined
96   Standard_Integer        myNbMaterials;       //!< number of registered materials
97   Standard_Boolean        myIsFailed;          //!< flag indicating failure
98   Standard_Boolean        myMatNameAsKey;      //!< flag indicating usage of material name as key
99 
100 };
101 
102 #endif // _RWMesh_MaterialMap_HeaderFile
103