Salome HOME
Task 3.2. Concealment into multi-level Compounds
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Boolean.h
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef FeaturesPlugin_Boolean_H_
21 #define FeaturesPlugin_Boolean_H_
22
23 #include "FeaturesPlugin.h"
24 #include "FeaturesPlugin_Tools.h"
25
26 #include <GeomAlgoAPI_MakeShape.h>
27 #include <GeomAlgoAPI_Tools.h>
28
29 #include <ModelAPI_Feature.h>
30
31 class ModelAPI_Result;
32 class GeomAlgoAPI_MakeShapeList;
33
34 /// \class FeaturesPlugin_Boolean
35 /// \ingroup Plugins
36 /// \brief Feature for applying of Boolean operations on Solids.
37 /// Supports four kinds of Boolean operations: Cut, Fuse, Common and Smash.
38 class FeaturesPlugin_Boolean : public ModelAPI_Feature
39 {
40 public:
41   enum OperationType {
42     BOOL_CUT,
43     BOOL_FUSE,
44     BOOL_COMMON,
45     BOOL_FILL,
46     BOOL_SMASH
47   };
48
49   /// Attribute name of main objects.
50   inline static const std::string& OBJECT_LIST_ID()
51   {
52     static const std::string MY_OBJECT_LIST_ID("main_objects");
53     return MY_OBJECT_LIST_ID;
54   }
55
56   /// Attribute name of tool objects.
57   inline static const std::string& TOOL_LIST_ID()
58   {
59     static const std::string MY_TOOL_LIST_ID("tool_objects");
60     return MY_TOOL_LIST_ID;
61   }
62
63   /// Attribute name of the version of Boolean feature
64   inline static const std::string& VERSION_ID()
65   {
66     static const std::string MY_VERSION_ID("version");
67     return MY_VERSION_ID;
68   }
69
70   /// \return boolean operation type.
71   FEATURESPLUGIN_EXPORT OperationType operationType();
72
73   /// Request for initialization of data model of the feature: adding all attributes.
74   FEATURESPLUGIN_EXPORT virtual void initAttributes();
75
76 protected:
77
78   /// Use plugin manager for features creation.
79   FeaturesPlugin_Boolean(const OperationType theOperationType);
80
81   /// Initialize version field of the Boolean feature.
82   /// The version is initialized for newly created features,
83   /// not read from previously stored document.
84   void initVersion(const int theVersion);
85
86   /// Load Naming data structure of the feature to the document
87   void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
88                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
89                     const ListOfShape& theTools,
90                     const std::shared_ptr<GeomAPI_Shape> theResultShape,
91                     const GeomMakeShapePtr& theMakeShape);
92
93
94   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
95   /// and their parent shapes (compounds or compsolids)
96   class ObjectHierarchy {
97     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
98     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
99     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
100     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
101
102     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
103     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
104     /// indices of compounds/compsolids to keep the order of parent shapes
105     /// corresponding to the order of objects
106     MapShapeToIndex  myParentIndices;
107     /// list of shape and its subshapes stored according to the index of parent shape
108     std::vector<ShapeAndSubshapes> mySubshapes;
109
110     SetOfShape myProcessedObjects;
111
112   public:
113     /// Add object of Boolean opration
114     void AddObject(const GeomShapePtr& theObject);
115
116     /// Maps shape and its parent
117     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
118
119     /// Return parent shape for the given, or empty if it is a high-level shape.
120     /// By default, the parent and all its subshapes are marked as processed for further skip.
121     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
122
123     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
124     void SplitCompound(const GeomShapePtr& theCompShape,
125                        ListOfShape& theUsed,
126                        ListOfShape& theNotUsed) const;
127
128     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
129     /// The generated list will contain only shapes unused during the Boolean operation.
130     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
131
132     /// Return \c true if there is no object in hierarchy
133     bool IsEmpty() const;
134
135     /// Return list of objects
136     const ListOfShape& Objects() const { return myObjects; }
137     /// Separate objects of the given range of types and all other objects
138     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
139         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
140         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
141
142   private:
143     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
144                                    const SetOfShape& theUsed) const;
145
146   public:
147     class Iterator {
148       friend class ObjectHierarchy;
149
150       ObjectHierarchy* myHierarchy;
151       ListOfShape::iterator myObject;
152
153       Iterator() {}
154       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
155
156       void SkipAlreadyProcessed();
157
158     public:
159       bool operator==(const Iterator&) const;
160       bool operator!=(const Iterator&) const;
161
162       Iterator& operator++();
163       Iterator  operator++(int);
164
165       GeomShapePtr operator*() const;
166     };
167
168     Iterator Begin();
169     Iterator End();
170   };
171
172   /// Process SelectionList attribute and fill the objects hierarchy.
173   bool processAttribute(const std::string& theAttributeName,
174                         ObjectHierarchy& theObjects,
175                         ListOfShape& thePlanesList);
176
177   /// Perform Boolean operation of the object with the tools.
178   /// In case of theResultCompound is not empty, the result of Boolean operation
179   /// is added to this compound, and corresponding ResultBody is not generated.
180   /// \return \c false if something went wrong
181   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
182                      const GeomShapePtr& theObject,
183                      const ListOfShape& theTools,
184                      const ListOfShape& thePlanes,
185                      int& theResultIndex,
186                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
187                      ListOfShape& theResultShapesList,
188                      GeomShapePtr theResulCompound = GeomShapePtr());
189
190   /// Perform Boolean operation of the Compsolid with the tools
191   /// In case of theResultCompound is not empty, the result of Boolean operation
192   /// is added to this compound, and corresponding ResultBody is not generated.
193   /// \return \c false if something went wrong
194   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
195                         const ObjectHierarchy& theCompsolidHierarchy,
196                         const GeomShapePtr& theCompsolid,
197                         const ListOfShape& theTools,
198                         const ListOfShape& thePlanes,
199                         int& theResultIndex,
200                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
201                         ListOfShape& theResultShapesList,
202                         GeomShapePtr theResulCompound = GeomShapePtr());
203
204   /// Perform Boolean operation of the Compound with the tools
205   /// In case of theResultCompound is not empty, the result of Boolean operation
206   /// is added to this compound, and corresponding ResultBody is not generated.
207   /// \return \c false if something went wrong
208   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
209                        const ObjectHierarchy& theCompoundHierarchy,
210                        const GeomShapePtr& theCompound,
211                        const ListOfShape& theTools,
212                        int& theResultIndex,
213                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
214                        ListOfShape& theResultShapesList,
215                        GeomShapePtr theResulCompound = GeomShapePtr());
216
217   /// Process unused sub-shapes of compounds.
218   /// Keep the compound hierarchy, but merge top-level compounds
219   /// into a single compound and add the result of the FUSE operation.
220   GeomShapePtr keepUnusedSubsOfCompound(
221       const GeomShapePtr& theResult,
222       const ObjectHierarchy& theObjectsHierarchy,
223       const ObjectHierarchy& theToolsHierarchy,
224       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
225
226   /// Return version of the feature
227   int version();
228
229 private:
230   void parentForShape(const GeomShapePtr& theShape,
231                       const std::shared_ptr<ModelAPI_Result>& theContext,
232                       ObjectHierarchy& theShapesHierarchy);
233
234 private:
235   OperationType myOperationType;
236 };
237
238 #endif