1 // Copyright (c) 2017-2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #ifndef _RWMesh_MaterialMap_HeaderFile
15 #define _RWMesh_MaterialMap_HeaderFile
17 #include <NCollection_DoubleMap.hxx>
18 #include <NCollection_Map.hxx>
19 #include <XCAFPrs_Style.hxx>
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
26 DEFINE_STANDARD_RTTIEXT(RWMesh_MaterialMap, Standard_Transient)
30 Standard_EXPORT RWMesh_MaterialMap (const TCollection_AsciiString& theFile);
33 Standard_EXPORT virtual ~RWMesh_MaterialMap();
35 //! Return default material definition to be used for nodes with only color defined.
36 const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
38 //! Set default material definition to be used for nodes with only color defined.
39 void SetDefaultStyle (const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
41 //! Find already registered material
42 TCollection_AsciiString FindMaterial (const XCAFPrs_Style& theStyle) const
44 if (myStyles.IsBound1 (theStyle))
46 return myStyles.Find1 (theStyle);
48 return TCollection_AsciiString();
51 //! Register material and return its name identifier.
52 Standard_EXPORT virtual TCollection_AsciiString AddMaterial (const XCAFPrs_Style& theStyle);
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();
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);
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;
73 //! Return failed flag.
74 bool IsFailed() const { return myIsFailed; }
78 //! Copy file to another place.
79 Standard_EXPORT static bool copyFileTo (const TCollection_AsciiString& theFileSrc,
80 const TCollection_AsciiString& theFileDst);
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
102 #endif // _RWMesh_MaterialMap_HeaderFile