]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Boolean.h
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
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   /// Attribute name of the version of Boolean feature
63   inline static const std::string& VERSION_ID()
64   {
65     static const std::string MY_VERSION_ID("version");
66     return MY_VERSION_ID;
67   }
68
69   /// \return boolean operation type.
70   FEATURESPLUGIN_EXPORT OperationType operationType();
71
72   /// Request for initialization of data model of the feature: adding all attributes.
73   FEATURESPLUGIN_EXPORT virtual void initAttributes();
74
75 protected:
76
77   /// Use plugin manager for features creation.
78   FeaturesPlugin_Boolean(const OperationType theOperationType);
79
80   /// Load Naming data structure of the feature to the document
81   void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
82                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
83                     const ListOfShape& theTools,
84                     const std::shared_ptr<GeomAPI_Shape> theResultShape,
85                     const GeomMakeShapePtr& theMakeShape);
86
87
88   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
89   /// and their parent shapes (compounds or compsolids)
90   class ObjectHierarchy {
91     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
92     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
93     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
94     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
95
96     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
97     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
98     /// indices of compounds/compsolids to keep the order of parent shapes
99     /// corresponding to the order of objects
100     MapShapeToIndex  myParentIndices;
101     /// list of shape and its subshapes stored according to the index of parent shape
102     std::vector<ShapeAndSubshapes> mySubshapes;
103
104     SetOfShape myProcessedObjects;
105
106   public:
107     /// Add object of Boolean opration
108     void AddObject(const GeomShapePtr& theObject);
109
110     /// Maps shape and its parent
111     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
112
113     /// Return parent shape for the given, or empty if it is a high-level shape.
114     /// By default, the parent and all its subshapes are marked as processed for further skip.
115     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
116
117     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
118     void SplitCompound(const GeomShapePtr& theCompShape,
119                        ListOfShape& theUsed,
120                        ListOfShape& theNotUsed) const;
121
122     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
123     /// The generated list will contain only shapes unused during the Boolean operation.
124     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
125
126     /// Return \c true if there is no object in hierarchy
127     bool IsEmpty() const;
128
129     /// Return list of objects
130     const ListOfShape& Objects() const { return myObjects; }
131     /// Separate objects of the given range of types and all other objects
132     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
133         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
134         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
135
136   private:
137     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
138                                    const SetOfShape& theUsed) const;
139
140   public:
141     class Iterator {
142       friend class ObjectHierarchy;
143
144       ObjectHierarchy* myHierarchy;
145       ListOfShape::iterator myObject;
146
147       Iterator() {}
148       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
149
150       void SkipAlreadyProcessed();
151
152     public:
153       bool operator==(const Iterator&) const;
154       bool operator!=(const Iterator&) const;
155
156       Iterator& operator++();
157       Iterator  operator++(int);
158
159       GeomShapePtr operator*() const;
160     };
161
162     Iterator Begin();
163     Iterator End();
164   };
165
166   /// Process SelectionList attribute and fill the objects hierarchy.
167   bool processAttribute(const std::string& theAttributeName,
168                         ObjectHierarchy& theObjects,
169                         ListOfShape& thePlanesList);
170
171   /// Perform Boolean operation of the object with the tools
172   /// \return \c false if something went wrong
173   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
174                      const GeomShapePtr& theObject,
175                      const ListOfShape& theTools,
176                      const ListOfShape& thePlanes,
177                      int& theResultIndex,
178                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
179                      ListOfShape& theResultShapesList);
180
181   /// Perform Boolean operation of the Compsolid with the tools
182   /// \return \c false if something went wrong
183   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
184                         const ObjectHierarchy& theCompsolidHierarchy,
185                         const GeomShapePtr& theCompsolid,
186                         const ListOfShape& theTools,
187                         const ListOfShape& thePlanes,
188                         int& theResultIndex,
189                         std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
190                         ListOfShape& theResultShapesList);
191
192   /// Perform Boolean operation of the Compound with the tools
193   /// \return \c false if something went wrong
194   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
195                        const ObjectHierarchy& theCompoundHierarchy,
196                        const GeomShapePtr& theCompound,
197                        const ListOfShape& theTools,
198                        int& theResultIndex,
199                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
200                        ListOfShape& theResultShapesList);
201
202 private:
203   void parentForShape(const GeomShapePtr& theShape,
204                       const std::shared_ptr<ModelAPI_Result>& theContext,
205                       ObjectHierarchy& theShapesHierarchy);
206
207 private:
208   OperationType myOperationType;
209 };
210
211 #endif