Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-01 08:33:21

0001 // Created: 2016-05-01
0002 // Author: Andrey Betenev
0003 // Copyright: Open CASCADE 2016
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _RWStl_Reader_HeaderFile
0017 #define _RWStl_Reader_HeaderFile
0018 
0019 #include <gp_XYZ.hxx>
0020 #include <Standard_ReadLineBuffer.hxx>
0021 #include <Standard_IStream.hxx>
0022 
0023 class Message_ProgressRange;
0024 
0025 //! An abstract class implementing procedure to read STL file.
0026 //!
0027 //! This class is not bound to particular data structure and can be used to read the file directly
0028 //! into arbitrary data model. To use it, create descendant class and implement methods addNode()
0029 //! and addTriangle().
0030 //!
0031 //! Call method Read() to read the file. In the process of reading, the tool will call methods
0032 //! addNode() and addTriangle() to fill the mesh data structure.
0033 //!
0034 //! The nodes with equal coordinates are merged automatically  on the fly.
0035 class RWStl_Reader : public Standard_Transient
0036 {
0037   DEFINE_STANDARD_RTTIEXT(RWStl_Reader, Standard_Transient)
0038 public:
0039   //! Default constructor.
0040   Standard_EXPORT RWStl_Reader();
0041 
0042   //! Reads data from STL file (either binary or Ascii).
0043   //! This function supports reading multi-domain STL files formed by concatenation
0044   //! of several "plain" files.
0045   //! The mesh nodes are not merged between domains.
0046   //! Unicode paths can be given in UTF-8 encoding.
0047   //! Format is recognized automatically by analysis of the file header.
0048   //! Returns true if success, false on error or user break.
0049   Standard_EXPORT Standard_Boolean Read(const char*                  theFile,
0050                                         const Message_ProgressRange& theProgress);
0051 
0052   //! Guess whether the stream is an Ascii STL file, by analysis of the first bytes (~200).
0053   //! If the stream does not support seekg() then the parameter isSeekgAvailable should
0054   //! be passed as 'false', in this case the function attempts to put back the read symbols
0055   //! to the stream which thus must support ungetc().
0056   //! Returns true if the stream seems to contain Ascii STL.
0057   Standard_EXPORT Standard_Boolean IsAscii(Standard_IStream& theStream,
0058                                            const bool        isSeekgAvailable);
0059 
0060   //! Reads STL data from binary stream.
0061   //! The stream must be opened in binary mode.
0062   //! Stops after reading the number of triangles recorded in the file header.
0063   //! Returns true if success, false on error or user break.
0064   Standard_EXPORT Standard_Boolean ReadBinary(Standard_IStream&            theStream,
0065                                               const Message_ProgressRange& theProgress);
0066 
0067   //! Reads data from the stream assumed to contain Ascii STL data.
0068   //! The stream can be opened either in binary or in Ascii mode.
0069   //! Reading stops at the position specified by theUntilPos,
0070   //! or end of file is reached, or when keyword "endsolid" is found.
0071   //! Empty lines are not supported and will read to reading failure.
0072   //! If theUntilPos is non-zero, reads not more than until that position.
0073   //! Returns true if success, false on error or user break.
0074   Standard_EXPORT Standard_Boolean ReadAscii(Standard_IStream&            theStream,
0075                                              Standard_ReadLineBuffer&     theBuffer,
0076                                              const std::streampos         theUntilPos,
0077                                              const Message_ProgressRange& theProgress);
0078 
0079 public:
0080   //! Callback function to be implemented in descendant.
0081   //! Should create new node with specified coordinates in the target model, and return its ID as
0082   //! integer.
0083   virtual Standard_Integer AddNode(const gp_XYZ& thePnt) = 0;
0084 
0085   //! Callback function to be implemented in descendant.
0086   //! Should create new triangle built on specified nodes in the target model.
0087   virtual void AddTriangle(Standard_Integer theN1,
0088                            Standard_Integer theN2,
0089                            Standard_Integer theN3) = 0;
0090 
0091   //! Callback function to be implemented in descendant.
0092   //! Should create a new triangulation for a solid in multi-domain case.
0093   virtual void AddSolid() {}
0094 
0095 public:
0096   //! Return merge tolerance; M_PI/2 by default - all nodes are merged regardless angle between
0097   //! triangles.
0098   Standard_Real MergeAngle() const { return myMergeAngle; }
0099 
0100   //! Set merge angle in radians.
0101   //! Specify something like M_PI/4 (45 degrees) to avoid merge nodes between triangles at sharp
0102   //! corners.
0103   void SetMergeAngle(Standard_Real theAngleRad) { myMergeAngle = theAngleRad; }
0104 
0105   //! Return linear merge tolerance; 0.0 by default (only 3D points with exactly matching
0106   //! coordinates are merged).
0107   double MergeTolerance() const { return myMergeTolearance; }
0108 
0109   //! Set linear merge tolerance.
0110   void SetMergeTolerance(double theTolerance) { myMergeTolearance = theTolerance; }
0111 
0112 protected:
0113   Standard_Real myMergeAngle;
0114   Standard_Real myMergeTolearance;
0115 };
0116 
0117 #endif