Salome HOME
[Code coverage GeomAlgoAPI]: Refactoring of ShapeAPI and additional unit tests
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_MakeShape.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef GeomAlgoAPI_MakeShape_H_
22 #define GeomAlgoAPI_MakeShape_H_
23
24 #include <GeomAlgoAPI.h>
25 #include <GeomAPI_DataMapOfShapeShape.h>
26
27 #include <list>
28 #include <memory>
29 #include <map>
30 #include <string>
31
32 /// \class GeomAlgoAPI_MakeShape
33 /// \ingroup DataAlgo
34 /// \brief Interface to the root class of all topological shapes constructions
35 class GeomAlgoAPI_MakeShape : public GeomAPI_Interface
36 {
37 public:
38   /// Builder type enum
39   enum BuilderType {
40     Unknown,
41     OCCT_BRepBuilderAPI_MakeShape,
42     OCCT_BOPAlgo_Builder
43   };
44
45 public:
46   /// \brief Empty constructor.
47   GEOMALGOAPI_EXPORT GeomAlgoAPI_MakeShape();
48
49   /// Destructor for remove myHist
50   GEOMALGOAPI_EXPORT ~GeomAlgoAPI_MakeShape();
51
52   /// \brief Constructor by builder and builder type.
53   /// \param[in] theBuilder pointer to the builder.
54   /// \param[in] theBuilderType builder type.
55   template<class T> explicit GeomAlgoAPI_MakeShape(
56     T* theBuilder,
57     const BuilderType theBuilderType = OCCT_BRepBuilderAPI_MakeShape)
58   : GeomAPI_Interface(theBuilder),
59     myBuilderType(theBuilderType)
60   {
61     initialize();
62   }
63
64   /// \brief Initializes internals.
65   /// \param[in] theBuilder pointer to the builder.
66   /// \param[in] theBuilderType builder type.
67   template<class T> void initialize(
68     T* theBuilder,
69     const BuilderType theBuilderType = OCCT_BRepBuilderAPI_MakeShape)
70   {
71     setImpl(theBuilder);
72     myBuilderType = theBuilderType;
73     initialize();
74   }
75
76   /// Execute the algorithm.
77   GEOMALGOAPI_EXPORT virtual void build() {}
78
79   /// \return status of builder.
80   GEOMALGOAPI_EXPORT bool isDone() const;
81
82   /// \return a shape built by the shape construction algorithm.
83   GEOMALGOAPI_EXPORT virtual const GeomShapePtr shape() const;
84
85   /// \return true if resulting shape is valid.
86   GEOMALGOAPI_EXPORT bool isValid() const;
87
88   /// \return true if resulting shape has volume.
89   GEOMALGOAPI_EXPORT bool hasVolume() const;
90
91   /// \return map of sub-shapes of the result. To be used for History keeping.
92   GEOMALGOAPI_EXPORT std::shared_ptr<GeomAPI_DataMapOfShapeShape> mapOfSubShapes() const;
93
94   /// \return the list of shapes generated from the shape \a theShape.
95   /// \param[in] theOldShape base shape.
96   /// \param[out] theNewShapes shapes generated from \a theShape. Does not cleared!
97   GEOMALGOAPI_EXPORT virtual void generated(const GeomShapePtr theOldShape,
98                                             ListOfShape& theNewShapes);
99
100   /// \return the list of shapes modified from the shape \a theShape.
101   /// \param[in] theOldShape base shape.
102   /// \param[out] theNewShapes shapes modified from \a theShape. Does not cleared!
103   GEOMALGOAPI_EXPORT virtual void modified(const GeomShapePtr theOldShape,
104                                            ListOfShape& theNewShapes);
105
106   /// \return true if theShape was deleted.
107   /// \param[in] theOldShape base shape.
108   GEOMALGOAPI_EXPORT virtual bool isDeleted(const GeomShapePtr theOldShape);
109
110   /// \return true if the data were correct.
111   GEOMALGOAPI_EXPORT virtual bool check() { return true; };
112
113   /// \brief Prepare the naming of faces.
114   GEOMALGOAPI_EXPORT virtual void prepareNamingFaces();
115
116   ///  \return the list of created faces.
117   GEOMALGOAPI_EXPORT std::map< std::string, GeomShapePtr > getCreatedFaces() {
118     return myCreatedFaces;
119   }
120
121   /// \return the error.
122   GEOMALGOAPI_EXPORT std::string getError() { return myError; }
123
124   /// \brief Check the validity of the produced shape.
125   GEOMALGOAPI_EXPORT bool checkValid(std::string theMessage);
126
127   /// Optimization of access the new shapes by old shapes for the limited set of needed new shapes.
128   /// \param theWholeOld the whole old shape
129   /// \param theShapeType type of the sub-shapes that is used for optimization
130   /// \returns true if optimization containers are already filled
131   GEOMALGOAPI_EXPORT bool isNewShapesCollected(GeomShapePtr theWholeOld,
132                                                const int theShapeType);
133
134   /// Optimization of access the new shapes by old shapes for the limited set of needed new shapes.
135   /// \param theWholeOld the whole old shape
136   /// \param theShapeType type of the sub-shapes that is used for optimization
137   /// \returns true if optimization containers are already filled
138   GEOMALGOAPI_EXPORT void collectNewShapes(GeomShapePtr theWholeOld,
139                                            const int theShapeType);
140
141   /// Optimization of access the new shapes by old shapes for the limited set of needed new shapes.
142   /// \param theWholeOld the whole old shape
143   /// \param theNewShape the whole new shape
144   /// \param theShapeType type of the old sub-shapes
145   /// \returns compound of all old shapes that were used for creation of the given new
146   GEOMALGOAPI_EXPORT GeomShapePtr oldShapesForNew(GeomShapePtr theWholeOld,
147                                                   GeomShapePtr theNewShape,
148                                                   const int theShapeType);
149
150   /// Replaces \a theShape with shape from \a myMap.
151   GEOMALGOAPI_EXPORT void fixOrientation(GeomShapePtr& theShape);
152
153 protected:
154   /// \brief Sets builder type.
155   /// \param[in] theBuilderType new builder type.
156   void setBuilderType(const BuilderType theBuilderType);
157
158   /// \brief Sets status of builder.
159   /// \param[in] theFlag new status.
160   void setDone(const bool theFlag);
161
162   /// \brief Sets result shape.
163   /// \param[in] theShape new shape.
164   void setShape(const GeomShapePtr theShape);
165
166   /// \return true if passed shape is valid for history.
167   bool isValidForHistory(const GeomShapePtr theShape);
168
169 protected:
170    /// Data map to keep correct orientation of sub-shapes.
171   std::shared_ptr<GeomAPI_DataMapOfShapeShape> myMap;
172   /// Error occurred during the execution of an algorithm.
173   std::string myError;
174   /// Map of created faces with their name for naming.
175   std::map< std::string, GeomShapePtr > myCreatedFaces;
176
177 private:
178   /// \brief Initializes internals.
179   void initialize();
180
181 private:
182   GeomAlgoAPI_MakeShape::BuilderType myBuilderType; ///< Type of make shape builder.
183   bool myDone; ///< Builder status.
184   GeomShapePtr myShape; ///< Resulting shape.
185
186   /// map that is used to keep the optimization structure for access to the history
187   /// kind of sub-shapes -> whole old shape -> new shape -> list of old shapes that create this new
188   void* myHist;
189 };
190
191 typedef std::shared_ptr<GeomAlgoAPI_MakeShape> GeomMakeShapePtr;
192 typedef std::list<GeomMakeShapePtr> ListOfMakeShape;
193
194 #endif