0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / RWStl / RWStl_Reader.hxx
1 // Created: 2016-05-01
2 // Author: Andrey Betenev
3 // Copyright: Open CASCADE 2016
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _RWStl_Reader_HeaderFile
17 #define _RWStl_Reader_HeaderFile
18
19 #include <gp_XYZ.hxx>
20 #include <Standard_ReadLineBuffer.hxx>
21 #include <Standard_IStream.hxx>
22
23 class Message_ProgressRange;
24
25 //! An abstract class implementing procedure to read STL file.
26 //!
27 //! This class is not bound to particular data structure and can be used to read the file directly into arbitrary data model.
28 //! To use it, create descendant class and implement methods addNode() and addTriangle().
29 //!
30 //! Call method Read() to read the file. In the process of reading, the tool will call methods addNode() and addTriangle() to fill the mesh data structure.
31 //!
32 //! The nodes with equal coordinates are merged automatically  on the fly.
33 class RWStl_Reader : public Standard_Transient
34 {
35   DEFINE_STANDARD_RTTIEXT(RWStl_Reader, Standard_Transient)
36 public:
37
38   //! Default constructor.
39   Standard_EXPORT RWStl_Reader();
40
41   //! Reads data from STL file (either binary or Ascii).
42   //! This function supports reading multi-domain STL files formed by concatenation 
43   //! of several "plain" files. 
44   //! The mesh nodes are not merged between domains.
45   //! Unicode paths can be given in UTF-8 encoding.
46   //! Format is recognized automatically by analysis of the file header.
47   //! Returns true if success, false on error or user break.
48   Standard_EXPORT Standard_Boolean Read (const char* theFile,
49                                          const Message_ProgressRange& theProgress);
50
51   //! Guess whether the stream is an Ascii STL file, by analysis of the first bytes (~200).
52   //! If the stream does not support seekg() then the parameter isSeekgAvailable should
53   //! be passed as 'false', in this case the function attempts to put back the read symbols
54   //! to the stream which thus must support ungetc().
55   //! Returns true if the stream seems to contain Ascii STL.
56   Standard_EXPORT Standard_Boolean IsAscii (Standard_IStream& theStream,
57                                             const bool isSeekgAvailable);
58
59   //! Reads STL data from binary stream.
60   //! The stream must be opened in binary mode.
61   //! Stops after reading the number of triangles recorded in the file header.
62   //! Returns true if success, false on error or user break.
63   Standard_EXPORT Standard_Boolean ReadBinary (Standard_IStream& theStream,
64                                                const Message_ProgressRange& theProgress);
65
66   //! Reads data from the stream assumed to contain Ascii STL data.
67   //! The stream can be opened either in binary or in Ascii mode.
68   //! Reading stops at the position specified by theUntilPos,
69   //! or end of file is reached, or when keyword "endsolid" is found.
70   //! Empty lines are not supported and will read to reading failure.
71   //! If theUntilPos is non-zero, reads not more than until that position.
72   //! Returns true if success, false on error or user break.
73   Standard_EXPORT Standard_Boolean ReadAscii (Standard_IStream& theStream,
74                                               Standard_ReadLineBuffer& theBuffer,
75                                               const std::streampos theUntilPos,
76                                               const Message_ProgressRange& theProgress);
77
78 public:
79
80   //! Callback function to be implemented in descendant.
81   //! Should create new node with specified coordinates in the target model, and return its ID as integer.
82   virtual Standard_Integer AddNode (const gp_XYZ& thePnt) = 0;
83
84   //! Callback function to be implemented in descendant.
85   //! Should create new triangle built on specified nodes in the target model.
86   virtual void AddTriangle (Standard_Integer theN1, Standard_Integer theN2, Standard_Integer theN3) = 0;
87
88   //! Callback function to be implemented in descendant.
89   //! Should create a new triangulation for a solid in multi-domain case.
90   virtual void AddSolid() {}
91 public:
92
93   //! Return merge tolerance; M_PI/2 by default - all nodes are merged regardless angle between triangles.
94   Standard_Real MergeAngle() const { return myMergeAngle; }
95
96   //! Set merge angle in radians.
97   //! Specify something like M_PI/4 (45 degrees) to avoid merge nodes between triangles at sharp corners.
98   void SetMergeAngle (Standard_Real theAngleRad) { myMergeAngle = theAngleRad; }
99
100   //! Return linear merge tolerance; 0.0 by default (only 3D points with exactly matching coordinates are merged).
101   double MergeTolerance() const { return myMergeTolearance; }
102
103   //! Set linear merge tolerance.
104   void SetMergeTolerance (double theTolerance) { myMergeTolearance = theTolerance; }
105
106 protected:
107
108   Standard_Real myMergeAngle;
109   Standard_Real myMergeTolearance;
110
111 };
112
113 #endif