Salome HOME
f2f11b3541115a2e4453bf7ce0bf097dc4c35a2c
[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   /// Load Naming data structure of the feature to the document
82   void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
83                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
84                     const ListOfShape& theTools,
85                     const std::shared_ptr<GeomAPI_Shape> theResultShape,
86                     const GeomMakeShapePtr& theMakeShape);
87
88
89   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
90   /// and their parent shapes (compounds or compsolids)
91   class ObjectHierarchy {
92     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
93     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
94     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
95     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
96
97     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
98     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
99     /// indices of compounds/compsolids to keep the order of parent shapes
100     /// corresponding to the order of objects
101     MapShapeToIndex  myParentIndices;
102     /// list of shape and its subshapes stored according to the index of parent shape
103     std::vector<ShapeAndSubshapes> mySubshapes;
104
105     SetOfShape myProcessedObjects;
106
107   public:
108     /// Add object of Boolean opration
109     void AddObject(const GeomShapePtr& theObject);
110
111     /// Maps shape and its parent
112     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
113
114     /// Return parent shape for the given, or empty if it is a high-level shape.
115     /// By default, the parent and all its subshapes are marked as processed for further skip.
116     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
117
118     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
119     void SplitCompound(const GeomShapePtr& theCompShape,
120                        ListOfShape& theUsed,
121                        ListOfShape& theNotUsed) const;
122
123     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
124     /// The generated list will contain only shapes unused during the Boolean operation.
125     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
126
127     /// Return \c true if there is no object in hierarchy
128     bool IsEmpty() const;
129
130     /// Return list of objects
131     const ListOfShape& Objects() const { return myObjects; }
132     /// Separate objects of the given range of types and all other objects
133     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
134         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
135         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
136
137   private:
138     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
139                                    const SetOfShape& theUsed) const;
140
141   public:
142     class Iterator {
143       friend class ObjectHierarchy;
144
145       ObjectHierarchy* myHierarchy;
146       ListOfShape::iterator myObject;
147
148       Iterator() {}
149       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
150
151       void SkipAlreadyProcessed();
152
153     public:
154       bool operator==(const Iterator&) const;
155       bool operator!=(const Iterator&) const;
156
157       Iterator& operator++();
158       Iterator  operator++(int);
159
160       GeomShapePtr operator*() const;
161     };
162
163     Iterator Begin();
164     Iterator End();
165   };
166
167   /// Process SelectionList attribute and fill the objects hierarchy.
168   bool processAttribute(const std::string& theAttributeName,
169                         ObjectHierarchy& theObjects,
170                         ListOfShape& thePlanesList);
171
172   /// Perform Boolean operation of the object with the tools.
173   /// In case of theResultCompound is not empty, the result of Boolean operation
174   /// is added to this compound, and corresponding ResultBody is not generated.
175   /// \return \c false if something went wrong
176   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
177                      const GeomShapePtr& theObject,
178                      const ListOfShape& theTools,
179                      const ListOfShape& thePlanes,
180                      int& theResultIndex,
181                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
182                      ListOfShape& theResultShapesList,
183                      GeomShapePtr theResulCompound = GeomShapePtr());
184
185   /// Perform Boolean operation of the Compsolid with the tools
186   /// In case of theResultCompound is not empty, the result of Boolean operation
187   /// is added to this compound, and corresponding ResultBody is not generated.
188   /// \return \c false if something went wrong
189   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
190                         const ObjectHierarchy& theCompsolidHierarchy,
191                         const GeomShapePtr& theCompsolid,
192                         const ListOfShape& theTools,
193                         const ListOfShape& thePlanes,
194                         int& theResultIndex,
195                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
196                         ListOfShape& theResultShapesList,
197                         GeomShapePtr theResulCompound = GeomShapePtr());
198
199   /// Perform Boolean operation of the Compound with the tools
200   /// In case of theResultCompound is not empty, the result of Boolean operation
201   /// is added to this compound, and corresponding ResultBody is not generated.
202   /// \return \c false if something went wrong
203   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
204                        const ObjectHierarchy& theCompoundHierarchy,
205                        const GeomShapePtr& theCompound,
206                        const ListOfShape& theTools,
207                        int& theResultIndex,
208                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
209                        ListOfShape& theResultShapesList,
210                        GeomShapePtr theResulCompound = GeomShapePtr());
211
212   /// Process unused sub-shapes of compounds.
213   /// Keep the compound hierarchy, but merge top-level compounds
214   /// into a single compound and add the result of the FUSE operation.
215   GeomShapePtr keepUnusedSubsOfCompound(
216       const GeomShapePtr& theResult,
217       const ObjectHierarchy& theObjectsHierarchy,
218       const ObjectHierarchy& theToolsHierarchy,
219       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
220
221   /// Return version of the feature
222   int version();
223
224 private:
225   void parentForShape(const GeomShapePtr& theShape,
226                       const std::shared_ptr<ModelAPI_Result>& theContext,
227                       ObjectHierarchy& theShapesHierarchy);
228
229 private:
230   OperationType myOperationType;
231 };
232
233 #endif