Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:42

0001 // Author: Kirill Gavrilov
0002 // Copyright (c) 2016-2019 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _RWMesh_CafReader_HeaderFile
0016 #define _RWMesh_CafReader_HeaderFile
0017 
0018 #include <Message_ProgressRange.hxx>
0019 #include <OSD_OpenFile.hxx>
0020 #include <RWMesh_CoordinateSystemConverter.hxx>
0021 #include <RWMesh_NodeAttributes.hxx>
0022 #include <TColStd_IndexedDataMapOfStringString.hxx>
0023 #include <TDF_Label.hxx>
0024 #include <TopTools_SequenceOfShape.hxx>
0025 
0026 class TDocStd_Document;
0027 class XCAFDoc_ShapeTool;
0028 class XCAFDoc_ColorTool;
0029 class XCAFDoc_VisMaterialTool;
0030 
0031 //! Extended status bits.
0032 enum RWMesh_CafReaderStatusEx
0033 {
0034   RWMesh_CafReaderStatusEx_NONE    = 0,    //!< empty status
0035   RWMesh_CafReaderStatusEx_Partial = 0x01, //!< partial read (due to unexpected EOF, syntax error, memory limit)
0036 };
0037 
0038 //! The general interface for importing mesh data into XDE document.
0039 //!
0040 //! The tool implements auxiliary structures for creating an XDE document in two steps:
0041 //! 1) Creating TopoDS_Shape hierarchy (myRootShapes)
0042 //!    and Shape attributes (myAttribMap) separately within performMesh().
0043 //!    Attributes include names and styles.
0044 //! 2) Filling XDE document from these auxiliary structures.
0045 //!    Named elements are expanded within document structure, while Compounds having no named children will remain collapsed.
0046 //!    In addition, unnamed nodes can be filled with generated names like "Face", "Compound" via generateNames() method,
0047 //!    and the very root unnamed node can be filled from file name like "MyModel.obj".
0048 class RWMesh_CafReader : public Standard_Transient
0049 {
0050   DEFINE_STANDARD_RTTIEXT(RWMesh_CafReader, Standard_Transient)
0051 public:
0052 
0053   //! Structure holding tools for filling the document.
0054   struct CafDocumentTools
0055   {
0056     Handle(XCAFDoc_ShapeTool)       ShapeTool;
0057     Handle(XCAFDoc_ColorTool)       ColorTool;
0058     Handle(XCAFDoc_VisMaterialTool) VisMaterialTool;
0059     NCollection_DataMap<TopoDS_Shape, TDF_Label, TopTools_ShapeMapHasher> ComponentMap;
0060   };
0061 
0062 public:
0063 
0064   //! Empty constructor.
0065   Standard_EXPORT RWMesh_CafReader();
0066 
0067   //! Destructor.
0068   Standard_EXPORT virtual ~RWMesh_CafReader();
0069 
0070   //! Return target document.
0071   const Handle(TDocStd_Document)& Document() const { return myXdeDoc; }
0072 
0073   //! Set target document.
0074   //! Set system length unit according to the units of the document
0075   Standard_EXPORT void SetDocument(const Handle(TDocStd_Document)& theDoc);
0076 
0077   //! Return prefix for generating root labels names.
0078   const TCollection_AsciiString& RootPrefix() const { return myRootPrefix; }
0079 
0080   //! Set prefix for generating root labels names
0081   void SetRootPrefix (const TCollection_AsciiString& theRootPrefix) { myRootPrefix = theRootPrefix; }
0082 
0083   //! Flag indicating if partially read file content should be put into the XDE document, TRUE by default.
0084   //!
0085   //! Partial read means unexpected end of file, critical parsing syntax errors in the middle of file, or reached memory limit
0086   //! indicated by performMesh() returning FALSE.
0087   //! Partial read allows importing a model even in case of formal reading failure,
0088   //! so that it will be up to user to decide if processed data has any value.
0089   //!
0090   //! In case of partial read (performMesh() returns FALSE, but there are some data that could be put into document),
0091   //! Perform() will return TRUE and result flag will have failure bit set.
0092   //! @sa MemoryLimitMiB(), ExtraStatus().
0093   Standard_Boolean ToFillIncompleteDocument() const { return myToFillIncomplete; }
0094 
0095   //! Set flag allowing partially read file content to be put into the XDE document.
0096   void SetFillIncompleteDocument (Standard_Boolean theToFillIncomplete) { myToFillIncomplete = theToFillIncomplete; }
0097 
0098   //! Return memory usage limit in MiB, -1 by default which means no limit.
0099   Standard_Integer MemoryLimitMiB() const { return myMemoryLimitMiB; }
0100 
0101   //! Set memory usage limit in MiB; can be ignored by reader implementation!
0102   void SetMemoryLimitMiB (Standard_Integer theLimitMiB) { myMemoryLimitMiB = theLimitMiB; }
0103 
0104 public:
0105 
0106   //! Return coordinate system converter.
0107   const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCoordSysConverter; }
0108 
0109   //! Set coordinate system converter.
0110   void SetCoordinateSystemConverter (const RWMesh_CoordinateSystemConverter& theConverter) { myCoordSysConverter = theConverter; }
0111 
0112   //! Return the length unit to convert into while reading the file, defined as scale factor for m (meters);
0113   //! -1.0 by default, which means that NO conversion will be applied.
0114   Standard_Real SystemLengthUnit() const { return myCoordSysConverter.OutputLengthUnit(); }
0115 
0116   //! Set system length units to convert into while reading the file, defined as scale factor for m (meters).
0117   void SetSystemLengthUnit (Standard_Real theUnits) { myCoordSysConverter.SetOutputLengthUnit (theUnits); }
0118 
0119   //! Return TRUE if system coordinate system has been defined; FALSE by default.
0120   Standard_Boolean HasSystemCoordinateSystem() const { return myCoordSysConverter.HasOutputCoordinateSystem(); }
0121 
0122   //! Return system coordinate system; UNDEFINED by default, which means that no conversion will be done.
0123   const gp_Ax3& SystemCoordinateSystem() const { return myCoordSysConverter.OutputCoordinateSystem(); }
0124 
0125   //! Set system origin coordinate system to perform conversion into during read.
0126   void SetSystemCoordinateSystem (const gp_Ax3& theCS) { myCoordSysConverter.SetOutputCoordinateSystem (theCS); }
0127 
0128   //! Set system origin coordinate system to perform conversion into during read.
0129   void SetSystemCoordinateSystem (RWMesh_CoordinateSystem theCS) { myCoordSysConverter.SetOutputCoordinateSystem (theCS); }
0130 
0131   //! Return the length unit to convert from while reading the file, defined as scale factor for m (meters).
0132   //! Can be undefined (-1.0) if file format is unitless.
0133   Standard_Real FileLengthUnit() const { return myCoordSysConverter.InputLengthUnit(); }
0134 
0135   //! Set (override) file length units to convert from while reading the file, defined as scale factor for m (meters).
0136   void SetFileLengthUnit (Standard_Real theUnits) { myCoordSysConverter.SetInputLengthUnit (theUnits); }
0137 
0138   //! Return TRUE if file origin coordinate system has been defined.
0139   Standard_Boolean HasFileCoordinateSystem() const { return myCoordSysConverter.HasInputCoordinateSystem(); }
0140 
0141   //! Return file origin coordinate system; can be UNDEFINED, which means no conversion will be done.
0142   const gp_Ax3& FileCoordinateSystem() const { return myCoordSysConverter.InputCoordinateSystem(); }
0143 
0144   //! Set (override) file origin coordinate system to perform conversion during read.
0145   void SetFileCoordinateSystem (const gp_Ax3& theCS) { myCoordSysConverter.SetInputCoordinateSystem (theCS); }
0146 
0147   //! Set (override) file origin coordinate system to perform conversion during read.
0148   void SetFileCoordinateSystem (RWMesh_CoordinateSystem theCS) { myCoordSysConverter.SetInputCoordinateSystem (theCS); }
0149 
0150 public:
0151 
0152   //! Open stream and pass it to Perform method.
0153   //! The Document instance should be set beforehand.
0154   bool Perform (const TCollection_AsciiString& theFile,
0155                 const Message_ProgressRange& theProgress)
0156   {
0157     std::ifstream aStream;
0158     OSD_OpenStream(aStream, theFile, std::ios_base::in | std::ios_base::binary);
0159     return Perform(aStream, theProgress, theFile);
0160   }
0161 
0162   //! Read the data from specified file.
0163   bool Perform (std::istream& theStream,
0164                 const Message_ProgressRange& theProgress,
0165                 const TCollection_AsciiString& theFile = "")
0166   {
0167     return perform(theStream, theFile, theProgress, Standard_False);
0168   }
0169 
0170   //! Return extended status flags.
0171   //! @sa RWMesh_CafReaderStatusEx enumeration.
0172   Standard_Integer ExtraStatus() const { return myExtraStatus; }
0173 
0174 public:
0175 
0176   //! Return result as a single shape.
0177   Standard_EXPORT TopoDS_Shape SingleShape() const;
0178 
0179   //! Return the list of complementary files - external references (textures, data, etc.).
0180   const NCollection_IndexedMap<TCollection_AsciiString>& ExternalFiles() const { return myExternalFiles; }
0181 
0182   //! Return metadata map.
0183   const TColStd_IndexedDataMapOfStringString& Metadata() const { return myMetadata; }
0184 
0185   //! Open stream and pass it to ProbeHeader method.
0186   Standard_Boolean ProbeHeader (const TCollection_AsciiString& theFile,
0187                                 const Message_ProgressRange& theProgress = Message_ProgressRange())
0188   {
0189     std::ifstream aStream;
0190     OSD_OpenStream(aStream, theFile, std::ios_base::in | std::ios_base::binary);
0191     return ProbeHeader (aStream, theFile, theProgress);
0192   }
0193 
0194   //! Read the header data from specified file without reading entire model.
0195   //! The main purpose is collecting metadata and external references - for copying model into a new location, for example.
0196   //! Can be NOT implemented (unsupported by format / reader).
0197   Standard_Boolean ProbeHeader (std::istream& theStream,
0198                                 const TCollection_AsciiString& theFile = "",
0199                                 const Message_ProgressRange& theProgress = Message_ProgressRange())
0200   {
0201     return perform(theStream, theFile, theProgress, Standard_True);
0202   }
0203 
0204 protected:
0205 
0206   //! Open stream and pass it to Perform method.
0207   //! @param theFile    file to read
0208   //! @param optional   progress indicator
0209   //! @param theToProbe flag indicating that mesh data should be skipped and only basing information to be read
0210   Standard_EXPORT virtual Standard_Boolean perform (const TCollection_AsciiString& theFile,
0211                                                     const Message_ProgressRange& theProgress,
0212                                                     const Standard_Boolean theToProbe);
0213 
0214   //! Read the data from specified file.
0215   //! Default implementation calls performMesh() and fills XDE document from collected shapes.
0216   //! @param theStream  input stream
0217   //! @param theFile    path of additional files
0218   //! @param optional   progress indicator
0219   //! @param theToProbe flag indicating that mesh data should be skipped and only basing information to be read
0220   Standard_EXPORT virtual Standard_Boolean perform (std::istream& theStream,
0221                                                     const TCollection_AsciiString& theFile,
0222                                                     const Message_ProgressRange& theProgress,
0223                                                     const Standard_Boolean theToProbe);
0224 
0225   //! Read the mesh from specified file
0226   Standard_EXPORT virtual Standard_Boolean performMesh (const TCollection_AsciiString& theFile,
0227                                                         const Message_ProgressRange& theProgress,
0228                                                         const Standard_Boolean theToProbe)
0229   {
0230     std::ifstream aStream;
0231     OSD_OpenStream(aStream, theFile, std::ios_base::in | std::ios_base::binary);
0232     return performMesh(aStream, theFile, theProgress, theToProbe);
0233   }
0234 
0235   //! Read the mesh from specified file - interface to be implemented by sub-classes.
0236   Standard_EXPORT virtual Standard_Boolean performMesh (std::istream& theStream,
0237                                                         const TCollection_AsciiString& theFile,
0238                                                         const Message_ProgressRange& theProgress,
0239                                                         const Standard_Boolean theToProbe) = 0;
0240 
0241 //! @name tools for filling XDE document
0242 protected:
0243 
0244   //! Fill document with new root shapes.
0245   Standard_EXPORT virtual void fillDocument();
0246 
0247   //! Append new shape into the document (recursively).
0248   Standard_EXPORT Standard_Boolean addShapeIntoDoc (CafDocumentTools& theTools,
0249                                                     const TopoDS_Shape& theShape,
0250                                                     const TDF_Label& theLabel,
0251                                                     const TCollection_AsciiString& theParentName);
0252 
0253   //! Append new sub-shape into the document (recursively).
0254   Standard_EXPORT Standard_Boolean addSubShapeIntoDoc (CafDocumentTools& theTools,
0255                                                        const TopoDS_Shape& theShape,
0256                                                        const TDF_Label& theParentLabel);
0257 
0258   //! Put name attribute onto the label.
0259   Standard_EXPORT void setShapeName (const TDF_Label& theLabel,
0260                                      const TopAbs_ShapeEnum theShapeType,
0261                                      const TCollection_AsciiString& theName,
0262                                      const TDF_Label& theParentLabel,
0263                                      const TCollection_AsciiString& theParentName);
0264 
0265   //! Put color and material attributes onto the label.
0266   Standard_EXPORT void setShapeStyle (const CafDocumentTools& theTools,
0267                                       const TDF_Label& theLabel,
0268                                       const XCAFPrs_Style& theStyle);
0269 
0270   //! Put name data (metadata) attribute onto the label.
0271   Standard_EXPORT void setShapeNamedData (const CafDocumentTools& theTools,
0272                                           const TDF_Label& theLabel,
0273                                           const Handle(TDataStd_NamedData)& theNameData);
0274 
0275   //! Generate names for root labels starting from specified index.
0276   Standard_EXPORT void generateNames (const TCollection_AsciiString& theFile,
0277                                       const Standard_Integer theRootLower,
0278                                       const Standard_Boolean theWithSubLabels);
0279 
0280   //! Return shape type as string.
0281   //! @sa TopAbs::ShapeTypeToString()
0282   static TCollection_AsciiString shapeTypeToString (TopAbs_ShapeEnum theType)
0283   {
0284     TCollection_AsciiString aString = TopAbs::ShapeTypeToString (theType);
0285     aString.Capitalize();
0286     return aString;
0287   }
0288 
0289 protected:
0290 
0291   Handle(TDocStd_Document)  myXdeDoc;            //!< target document
0292 
0293   TColStd_IndexedDataMapOfStringString
0294                             myMetadata;          //!< metadata map
0295   NCollection_IndexedMap<TCollection_AsciiString>
0296                             myExternalFiles;     //!< the list of complementary files - external references (textures, data, etc.)
0297   TCollection_AsciiString   myRootPrefix;        //!< root folder for generating root labels names
0298   TopTools_SequenceOfShape  myRootShapes;        //!< sequence of result root shapes
0299   RWMesh_NodeAttributeMap   myAttribMap;         //!< map of per-shape attributes
0300 
0301   RWMesh_CoordinateSystemConverter
0302                             myCoordSysConverter; //!< coordinate system converter
0303   Standard_Boolean          myToFillDoc;         //!< fill document from shape sequence
0304   Standard_Boolean          myToFillIncomplete;  //!< fill the document with partially retrieved data even if reader has failed with error
0305   Standard_Integer          myMemoryLimitMiB;    //!< memory usage limit
0306   Standard_Integer          myExtraStatus;       //!< extra status bitmask
0307 
0308 };
0309 
0310 #endif // _RWMesh_CafReader_HeaderFile