Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 09:17:56

0001 // Copyright (c) 2022 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 _RWPly_CafWriter_HeaderFiler
0015 #define _RWPly_CafWriter_HeaderFiler
0016 
0017 #include <TColStd_IndexedDataMapOfStringString.hxx>
0018 #include <TColStd_MapOfAsciiString.hxx>
0019 #include <TDF_LabelSequence.hxx>
0020 #include <TopTools_ShapeMapHasher.hxx>
0021 #include <RWMesh_CoordinateSystemConverter.hxx>
0022 #include <XCAFPrs_Style.hxx>
0023 
0024 #include <memory>
0025 
0026 class Message_ProgressRange;
0027 class RWMesh_FaceIterator;
0028 class TDocStd_Document;
0029 
0030 class Message_LazyProgressScope;
0031 class RWPly_PlyWriterContext;
0032 
0033 //! PLY writer context from XCAF document.
0034 class RWPly_CafWriter : public Standard_Transient
0035 {
0036   DEFINE_STANDARD_RTTIEXT(RWPly_CafWriter, Standard_Transient)
0037 public:
0038   //! Main constructor.
0039   //! @param[in] theFile path to output PLY file
0040   Standard_EXPORT RWPly_CafWriter(const TCollection_AsciiString& theFile);
0041 
0042   //! Destructor.
0043   Standard_EXPORT virtual ~RWPly_CafWriter();
0044 
0045   //! Return transformation from OCCT to PLY coordinate system.
0046   const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCSTrsf; }
0047 
0048   //! Return transformation from OCCT to PLY coordinate system.
0049   RWMesh_CoordinateSystemConverter& ChangeCoordinateSystemConverter() { return myCSTrsf; }
0050 
0051   //! Set transformation from OCCT to PLY coordinate system.
0052   void SetCoordinateSystemConverter(const RWMesh_CoordinateSystemConverter& theConverter)
0053   {
0054     myCSTrsf = theConverter;
0055   }
0056 
0057   //! Return default material definition to be used for nodes with only color defined.
0058   const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
0059 
0060   //! Set default material definition to be used for nodes with only color defined.
0061   void SetDefaultStyle(const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
0062 
0063 public:
0064   //! Return TRUE if vertex position should be stored with double floating point precision; FALSE by
0065   //! default.
0066   bool IsDoublePrecision() const { return myIsDoublePrec; }
0067 
0068   //! Set if vertex position should be stored with double floating point precision.
0069   void SetDoublePrecision(bool theDoublePrec) { myIsDoublePrec = theDoublePrec; }
0070 
0071   //! Return TRUE if normals should be written; TRUE by default.
0072   bool HasNormals() const { return myHasNormals; }
0073 
0074   //! Set if normals are defined.
0075   void SetNormals(const bool theHasNormals) { myHasNormals = theHasNormals; }
0076 
0077   //! Return TRUE if UV / texture coordinates should be written; FALSE by default.
0078   bool HasTexCoords() const { return myHasTexCoords; }
0079 
0080   //! Set if UV / texture coordinates should be written.
0081   void SetTexCoords(const bool theHasTexCoords) { myHasTexCoords = theHasTexCoords; }
0082 
0083   //! Return TRUE if point colors should be written; TRUE by default.
0084   bool HasColors() const { return myHasColors; }
0085 
0086   //! Set if point colors should be written.
0087   void SetColors(bool theToWrite) { myHasColors = theToWrite; }
0088 
0089   //! Return TRUE if part Id should be written as element attribute; TRUE by default.
0090   bool HasPartId() const { return myHasPartId; }
0091 
0092   //! Set if part Id should be written as element attribute; FALSE by default.
0093   //! Cannot be combined with HasFaceId().
0094   void SetPartId(bool theSurfId)
0095   {
0096     myHasPartId = theSurfId;
0097     myHasFaceId = myHasFaceId && !myHasPartId;
0098   }
0099 
0100   //! Return TRUE if face Id should be written as element attribute; FALSE by default.
0101   bool HasFaceId() const { return myHasFaceId; }
0102 
0103   //! Set if face Id should be written as element attribute; FALSE by default.
0104   //! Cannot be combined with HasPartId().
0105   void SetFaceId(bool theSurfId)
0106   {
0107     myHasFaceId = theSurfId;
0108     myHasPartId = myHasPartId && !myHasFaceId;
0109   }
0110 
0111 public:
0112   //! Write PLY file and associated MTL material file.
0113   //! Triangulation data should be precomputed within shapes!
0114   //! @param[in] theDocument    input document
0115   //! @param[in] theRootLabels  list of root shapes to export
0116   //! @param[in] theLabelFilter optional filter with document nodes to export,
0117   //!                           with keys defined by XCAFPrs_DocumentExplorer::DefineChildId() and
0118   //!                           filled recursively (leaves and parent assembly nodes at all levels);
0119   //!                           when not NULL, all nodes not included into the map will be ignored
0120   //! @param[in] theFileInfo    map with file metadata to put into PLY header section
0121   //! @param[in] theProgress    optional progress indicator
0122   //! @return FALSE on file writing failure
0123   Standard_EXPORT virtual bool Perform(const Handle(TDocStd_Document)&             theDocument,
0124                                        const TDF_LabelSequence&                    theRootLabels,
0125                                        const TColStd_MapOfAsciiString*             theLabelFilter,
0126                                        const TColStd_IndexedDataMapOfStringString& theFileInfo,
0127                                        const Message_ProgressRange&                theProgress);
0128 
0129   //! Write PLY file and associated MTL material file.
0130   //! Triangulation data should be precomputed within shapes!
0131   //! @param[in] theDocument input document
0132   //! @param[in] theFileInfo map with file metadata to put into PLY header section
0133   //! @param[in] theProgress optional progress indicator
0134   //! @return FALSE on file writing failure
0135   Standard_EXPORT virtual bool Perform(const Handle(TDocStd_Document)&             theDocument,
0136                                        const TColStd_IndexedDataMapOfStringString& theFileInfo,
0137                                        const Message_ProgressRange&                theProgress);
0138 
0139 protected:
0140   //! Return TRUE if face mesh should be skipped (e.g. because it is invalid or empty).
0141   Standard_EXPORT virtual Standard_Boolean toSkipFaceMesh(const RWMesh_FaceIterator& theFaceIter);
0142 
0143   //! Collect face triangulation info.
0144   //! @param[in] theFace face to process
0145   //! @param[in,out] theNbNodes overall number of triangulation nodes (should be appended)
0146   //! @param[in,out] theNbElems overall number of triangulation elements (should be appended)
0147   Standard_EXPORT virtual void addFaceInfo(const RWMesh_FaceIterator& theFace,
0148                                            Standard_Integer&          theNbNodes,
0149                                            Standard_Integer&          theNbElems);
0150 
0151   //! Write the shape.
0152   //! @param[in] theWriter      PLY writer context
0153   //! @param[in] thePSentry     progress sentry
0154   //! @param[in] theWriteStep   export step, 0 for vertex attributes, 1 for elements
0155   //! @param[in] theLabel       document label to process
0156   //! @param[in] theParentTrsf  parent node transformation
0157   //! @param[in] theParentStyle parent node style
0158   Standard_EXPORT virtual bool writeShape(RWPly_PlyWriterContext&    theWriter,
0159                                           Message_LazyProgressScope& thePSentry,
0160                                           const Standard_Integer     theWriteStep,
0161                                           const TDF_Label&           theLabel,
0162                                           const TopLoc_Location&     theParentTrsf,
0163                                           const XCAFPrs_Style&       theParentStyle);
0164 
0165   //! Write face triangle vertices and attributes.
0166   //! @param[in] theWriter  PLY writer context
0167   //! @param[in] thePSentry progress sentry
0168   //! @param[in] theFace    current face
0169   //! @return FALSE on writing file error
0170   Standard_EXPORT virtual bool writeNodes(RWPly_PlyWriterContext&    theWriter,
0171                                           Message_LazyProgressScope& thePSentry,
0172                                           const RWMesh_FaceIterator& theFace);
0173 
0174   //! Write face triangles indices.
0175   //! @param[in] theWriter  PLY writer context
0176   //! @param[in] thePSentry progress sentry
0177   //! @param[in] theFace    current face
0178   //! @return FALSE on writing file error
0179   Standard_EXPORT virtual bool writeIndices(RWPly_PlyWriterContext&    theWriter,
0180                                             Message_LazyProgressScope& thePSentry,
0181                                             const RWMesh_FaceIterator& theFace);
0182 
0183 protected:
0184   TCollection_AsciiString myFile; //!< output PLY file
0185                                   // clang-format off
0186   RWMesh_CoordinateSystemConverter myCSTrsf;       //!< transformation from OCCT to PLY coordinate system
0187   XCAFPrs_Style                    myDefaultStyle; //!< default material definition to be used for nodes with only color defined
0188                                   // clang-format on
0189   Standard_Boolean myIsDoublePrec;
0190   Standard_Boolean myHasNormals;
0191   Standard_Boolean myHasColors;
0192   Standard_Boolean myHasTexCoords;
0193   Standard_Boolean myHasPartId;
0194   Standard_Boolean myHasFaceId;
0195 };
0196 
0197 #endif // _RWPly_CafWriter_HeaderFiler