]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.h
Salome HOME
Refactor Boolean operations in FeaturesPlugin.
[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
116   public:
117     class Iterator {
118       friend class ObjectHierarchy;
119
120       ObjectHierarchy* myHierarchy;
121       ListOfShape::iterator myObject;
122
123       Iterator() {}
124       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
125
126       void SkipAlreadyProcessed();
127
128     public:
129       bool operator==(const Iterator&) const;
130       bool operator!=(const Iterator&) const;
131
132       Iterator& operator++();
133       Iterator  operator++(int);
134
135       GeomShapePtr operator*() const;
136     };
137
138     Iterator Begin();
139     Iterator End();
140   };
141
142   /// Process SelectionList attribute and fill the objects hierarchy.
143   bool processAttribute(const std::string& theAttributeName,
144                         ObjectHierarchy& theObjects,
145                         ListOfShape& thePlanesList);
146
147   /// Perform Boolean operation of the object with the tools
148   /// \return \c false if something went wrong
149   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
150                      const GeomShapePtr& theObject,
151                      const ListOfShape& theTools,
152                      const ListOfShape& thePlanes,
153                      int& theResultIndex,
154                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
155                      ListOfShape& theResultShapesList);
156
157   /// Perform Boolean operation of the Compsolid with the tools
158   /// \return \c false if something went wrong
159   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
160                         const ObjectHierarchy& theCompsolidHierarchy,
161                         const GeomShapePtr& theCompsolid,
162                         const ListOfShape& theTools,
163                         const ListOfShape& thePlanes,
164                         int& theResultIndex,
165                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
166                         ListOfShape& theResultShapesList);
167
168   /// Perform Boolean operation of the Compound with the tools
169   /// \return \c false if something went wrong
170   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
171                        const ObjectHierarchy& theCompoundHierarchy,
172                        const GeomShapePtr& theCompound,
173                        const ListOfShape& theTools,
174                        int& theResultIndex,
175                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
176                        ListOfShape& theResultShapesList);
177
178 private:
179   void parentForShape(const GeomShapePtr& theShape,
180                       const std::shared_ptr<ModelAPI_Result>& theContext,
181                       ObjectHierarchy& theShapesHierarchy);
182
183 private:
184   OperationType myOperationType;
185 };
186
187 #endif