Salome HOME
Fix regressions related to treating edges and faces in Boolean operations.
[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
33 /// \class FeaturesPlugin_Boolean
34 /// \ingroup Plugins
35 /// \brief Feature for applying of Boolean operations on Solids.
36 /// Supports four kinds of Boolean operations: Cut, Fuse, Common and Smash.
37 class FeaturesPlugin_Boolean : public ModelAPI_Feature
38 {
39 public:
40   enum OperationType {
41     BOOL_CUT,
42     BOOL_FUSE,
43     BOOL_COMMON,
44     BOOL_FILL,
45     BOOL_SMASH
46   };
47
48   /// Attribute name of main objects.
49   inline static const std::string& OBJECT_LIST_ID()
50   {
51     static const std::string MY_OBJECT_LIST_ID("main_objects");
52     return MY_OBJECT_LIST_ID;
53   }
54
55   /// Attribute name of tool objects.
56   inline static const std::string& TOOL_LIST_ID()
57   {
58     static const std::string MY_TOOL_LIST_ID("tool_objects");
59     return MY_TOOL_LIST_ID;
60   }
61
62   /// \return boolean operation type.
63   FEATURESPLUGIN_EXPORT OperationType operationType();
64
65   /// Request for initialization of data model of the feature: adding all attributes.
66   FEATURESPLUGIN_EXPORT virtual void initAttributes();
67
68 protected:
69
70   /// Use plugin manager for features creation.
71   FeaturesPlugin_Boolean(const OperationType theOperationType);
72
73   /// Load Naming data structure of the feature to the document
74   void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
75                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
76                     const ListOfShape& theTools,
77                     const std::shared_ptr<GeomAPI_Shape> theResultShape,
78                     const GeomMakeShapePtr& theMakeShape);
79
80
81   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
82   /// and their parent shapes (compounds or compsolids)
83   class ObjectHierarchy {
84     typedef std::map<GeomShapePtr, ListOfShape, GeomAPI_Shape::Comparator>  MapShapeToSubshapes;
85     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
86     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
87
88     ListOfShape myObjects;
89     MapShapeToParent myParent;
90     MapShapeToSubshapes mySubshapes;
91
92     SetOfShape myProcessedObjects;
93
94   public:
95     /// Add object of Boolean opration
96     void AddObject(const GeomShapePtr& theObject);
97
98     /// Maps shape and its parent
99     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
100
101     /// Return parent shape for the given, or empty if it is a high-level shape.
102     /// By default, the parent and all its subshapes are marked as processed for further skip.
103     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
104
105     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
106     void SplitCompound(const GeomShapePtr& theCompShape,
107                        ListOfShape& theUsed,
108                        ListOfShape& theNotUsed) const;
109
110     /// Return \c true if there is no object in hierarchy
111     bool IsEmpty() const;
112
113     /// Return list of objects
114     const ListOfShape& Objects() const { return myObjects; }
115     /// Separate objects of the given range of types and all other objects
116     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
117         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
118         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
119
120   public:
121     class Iterator {
122       friend class ObjectHierarchy;
123
124       ObjectHierarchy* myHierarchy;
125       ListOfShape::iterator myObject;
126
127       Iterator() {}
128       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
129
130       void SkipAlreadyProcessed();
131
132     public:
133       bool operator==(const Iterator&) const;
134       bool operator!=(const Iterator&) const;
135
136       Iterator& operator++();
137       Iterator  operator++(int);
138
139       GeomShapePtr operator*() const;
140     };
141
142     Iterator Begin();
143     Iterator End();
144   };
145
146   /// Process SelectionList attribute and fill the objects hierarchy.
147   bool processAttribute(const std::string& theAttributeName,
148                         ObjectHierarchy& theObjects,
149                         ListOfShape& thePlanesList);
150
151   /// Perform Boolean operation of the object with the tools
152   /// \return \c false if something went wrong
153   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
154                      const GeomShapePtr& theObject,
155                      const ListOfShape& theTools,
156                      const ListOfShape& thePlanes,
157                      int& theResultIndex,
158                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
159                      ListOfShape& theResultShapesList);
160
161   /// Perform Boolean operation of the Compsolid with the tools
162   /// \return \c false if something went wrong
163   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
164                         const ObjectHierarchy& theCompsolidHierarchy,
165                         const GeomShapePtr& theCompsolid,
166                         const ListOfShape& theTools,
167                         const ListOfShape& thePlanes,
168                         int& theResultIndex,
169                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
170                         ListOfShape& theResultShapesList);
171
172   /// Perform Boolean operation of the Compound with the tools
173   /// \return \c false if something went wrong
174   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
175                        const ObjectHierarchy& theCompoundHierarchy,
176                        const GeomShapePtr& theCompound,
177                        const ListOfShape& theTools,
178                        int& theResultIndex,
179                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
180                        ListOfShape& theResultShapesList);
181
182 private:
183   void parentForShape(const GeomShapePtr& theShape,
184                       const std::shared_ptr<ModelAPI_Result>& theContext,
185                       ObjectHierarchy& theShapesHierarchy);
186
187 private:
188   OperationType myOperationType;
189 };
190
191 #endif