Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:18:53

0001 // Copyright (c) 2017-2019 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _RWMesh_MaterialMap_HeaderFile
0015 #define _RWMesh_MaterialMap_HeaderFile
0016 
0017 #include <NCollection_DoubleMap.hxx>
0018 #include <NCollection_Map.hxx>
0019 #include <XCAFPrs_Style.hxx>
0020 
0021 //! Material manager.
0022 //! Provides an interface for collecting all materials within the document before writing it into
0023 //! file, and for copying associated image files (textures) into sub-folder near by exported model.
0024 class RWMesh_MaterialMap : public Standard_Transient
0025 {
0026   DEFINE_STANDARD_RTTIEXT(RWMesh_MaterialMap, Standard_Transient)
0027 public:
0028   //! Main constructor.
0029   Standard_EXPORT RWMesh_MaterialMap(const TCollection_AsciiString& theFile);
0030 
0031   //! Destructor.
0032   Standard_EXPORT ~RWMesh_MaterialMap() override;
0033 
0034   //! Return default material definition to be used for nodes with only color defined.
0035   const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
0036 
0037   //! Set default material definition to be used for nodes with only color defined.
0038   void SetDefaultStyle(const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
0039 
0040   //! Find already registered material
0041   TCollection_AsciiString FindMaterial(const XCAFPrs_Style& theStyle) const
0042   {
0043     if (myStyles.IsBound1(theStyle))
0044     {
0045       return myStyles.Find1(theStyle);
0046     }
0047     return TCollection_AsciiString();
0048   }
0049 
0050   //! Register material and return its name identifier.
0051   Standard_EXPORT virtual TCollection_AsciiString AddMaterial(const XCAFPrs_Style& theStyle);
0052 
0053   //! Create texture folder "modelName/textures"; for example:
0054   //! MODEL:  Path/ModelName.gltf
0055   //! IMAGES: Path/ModelName/textures/
0056   //! Warning! Output folder is NOT cleared.
0057   Standard_EXPORT virtual bool CreateTextureFolder();
0058 
0059   //! Copy and rename texture file to the new location.
0060   //! @param[out] theResTexture  result texture file path (relative to the model)
0061   //! @param[in] theTexture  original texture
0062   //! @param[in] theKey  material key
0063   Standard_EXPORT virtual bool CopyTexture(TCollection_AsciiString&          theResTexture,
0064                                            const occ::handle<Image_Texture>& theTexture,
0065                                            const TCollection_AsciiString&    theKey);
0066 
0067   //! Virtual method actually defining the material (e.g. export to the file).
0068   virtual void DefineMaterial(const XCAFPrs_Style&           theStyle,
0069                               const TCollection_AsciiString& theKey,
0070                               const TCollection_AsciiString& theName) = 0;
0071 
0072   //! Return failed flag.
0073   bool IsFailed() const { return myIsFailed; }
0074 
0075 protected:
0076   //! Copy file to another place.
0077   Standard_EXPORT static bool copyFileTo(const TCollection_AsciiString& theFileSrc,
0078                                          const TCollection_AsciiString& theFileDst);
0079 
0080 protected:
0081   TCollection_AsciiString myFolder;            //!< output folder for glTF file
0082   TCollection_AsciiString myTexFolder;         //!< output folder for images (full  path)
0083   TCollection_AsciiString myTexFolderShort;    //!< output folder for images (short path)
0084   TCollection_AsciiString myFileName;          //!< output glTF file path
0085   TCollection_AsciiString myShortFileNameBase; //!< output glTF file name without extension
0086   TCollection_AsciiString myKeyPrefix;         //!< prefix for generated keys
0087   NCollection_DoubleMap<XCAFPrs_Style, TCollection_AsciiString>
0088                                               myStyles;       //!< map of processed styles
0089   NCollection_Map<occ::handle<Image_Texture>> myImageFailMap; //!< map of images failed to be copied
0090                                                               // clang-format off
0091   XCAFPrs_Style           myDefaultStyle;      //!< default material definition to be used for nodes with only color defined
0092                                                               // clang-format on
0093   int  myNbMaterials;                                         //!< number of registered materials
0094   bool myIsFailed;                                            //!< flag indicating failure
0095   bool myMatNameAsKey; //!< flag indicating usage of material name as key
0096 };
0097 
0098 #endif // _RWMesh_MaterialMap_HeaderFile