]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_VersionedBoolean.h
Salome HOME
098509c3c4842a85aab0afd590119efc428896de
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_VersionedBoolean.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_VersionedBoolean_H_
21 #define FeaturesPlugin_VersionedBoolean_H_
22
23 #include "FeaturesPlugin.h"
24 #include "FeaturesPlugin_Tools.h"
25
26 #include <GeomAlgoAPI_Tools.h>
27
28 #include <ModelAPI_Feature.h>
29
30 class ModelAPI_Attribute;
31 class ModelAPI_Result;
32 class GeomAlgoAPI_MakeShapeList;
33
34 /// \class FeaturesPlugin_VersionedBoolean
35 /// \ingroup Plugins
36 /// \brief Feature controls a version of Boolean operations.
37 class FeaturesPlugin_VersionedBoolean : public ModelAPI_Feature
38 {
39 protected:
40   static const std::string& BOP_VERSION_9_4()
41   {
42     static const std::string VERSION("v9.4");
43     return VERSION;
44   }
45
46   /// Use plugin manager for features creation.
47   FeaturesPlugin_VersionedBoolean() {}
48
49   /// Initialize version field of the Boolean feature.
50   /// The version is initialized for newly created features,
51   /// not read from previously stored document.
52   void initVersion(const std::string& theVersion,
53                    const std::shared_ptr<ModelAPI_Attribute> theObjectsAttr
54                             = std::shared_ptr<ModelAPI_Attribute>(),
55                    const std::shared_ptr<ModelAPI_Attribute> theToolsAttr
56                             = std::shared_ptr<ModelAPI_Attribute>());
57
58   /// Auxiliary class to store hierarchy of Boolean operation objects/tools
59   /// and their parent shapes (compounds or compsolids)
60   class ObjectHierarchy {
61     typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
62     typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
63     typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
64     typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
65
66     ListOfShape myObjects; ///< list of objects/tools of Boolean operation
67     MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
68     /// indices of compounds/compsolids to keep the order of parent shapes
69     /// corresponding to the order of objects
70     MapShapeToIndex  myParentIndices;
71     /// list of shape and its subshapes stored according to the index of parent shape
72     std::vector<ShapeAndSubshapes> mySubshapes;
73
74     SetOfShape myProcessedObjects;
75
76   public:
77     /// Add object of Boolean opration
78     void AddObject(const GeomShapePtr& theObject);
79
80     /// Maps shape and its parent
81     void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
82
83     /// Return parent shape for the given, or empty if it is a high-level shape.
84     /// By default, the parent and all its subshapes are marked as processed for further skip.
85     GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
86
87     /// Marke the shape as already processed
88     void MarkProcessed(const GeomShapePtr& theShape);
89     /// Marke list ofshapes as already processed
90     void MarkProcessed(const ListOfShape& theShapes);
91
92     /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
93     void SplitCompound(const GeomShapePtr& theCompShape,
94                        ListOfShape& theUsed,
95                        ListOfShape& theNotUsed) const;
96
97     /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
98     /// The generated list will contain only shapes unused during the Boolean operation.
99     void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
100
101     /// Return \c true if there is no object in hierarchy
102     bool IsEmpty() const;
103
104     /// Return list of objects
105     const ListOfShape& Objects() const { return myObjects; }
106     /// Separate objects of the given range of types and all other objects
107     void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
108         const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
109         const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
110
111   private:
112     GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
113                                    const SetOfShape& theUsed) const;
114
115   public:
116     class Iterator {
117       friend class ObjectHierarchy;
118
119       ObjectHierarchy* myHierarchy;
120       ListOfShape::iterator myObject;
121
122       Iterator() {}
123       Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
124
125       void SkipAlreadyProcessed();
126
127     public:
128       bool operator==(const Iterator&) const;
129       bool operator!=(const Iterator&) const;
130
131       Iterator& operator++();
132       Iterator  operator++(int);
133
134       GeomShapePtr operator*() const;
135     };
136
137     Iterator Begin();
138     Iterator End();
139   };
140
141   /// Process SelectionList attribute and fill the objects hierarchy.
142   bool processAttribute(const std::string& theAttributeName,
143                         ObjectHierarchy& theObjects,
144                         ListOfShape& thePlanesList);
145
146   /// Perform Boolean operation of the object with the tools.
147   /// In case of theResultCompound is not empty, the result of Boolean operation
148   /// is added to this compound, and corresponding ResultBody is not generated.
149   /// \return \c false if something went wrong
150   bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
151                      const GeomShapePtr& theObject,
152                      const ListOfShape& theTools,
153                      const ListOfShape& thePlanes,
154                      int& theResultIndex,
155                      std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
156                      ListOfShape& theResultShapesList,
157                      GeomShapePtr theResulCompound = GeomShapePtr());
158
159   /// Perform Boolean operation of the Compsolid with the tools
160   /// In case of theResultCompound is not empty, the result of Boolean operation
161   /// is added to this compound, and corresponding ResultBody is not generated.
162   /// \return \c false if something went wrong
163   bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
164                         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                         GeomShapePtr theResulCompound = GeomShapePtr());
172
173   /// Perform Boolean operation of the Compound with the tools
174   /// In case of theResultCompound is not empty, the result of Boolean operation
175   /// is added to this compound, and corresponding ResultBody is not generated.
176   /// \return \c false if something went wrong
177   bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
178                        ObjectHierarchy& theCompoundHierarchy,
179                        const GeomShapePtr& theCompound,
180                        const ListOfShape& theTools,
181                        int& theResultIndex,
182                        std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
183                        ListOfShape& theResultShapesList,
184                        GeomShapePtr theResulCompound = GeomShapePtr());
185
186   /// Resize planes to fit them to the bounding box of the given lins of objects.
187   static void resizePlanes(const ListOfShape& theObjects,
188                            ListOfShape& thePlanes,
189                            std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
190
191   /// Process unused sub-shapes of compounds.
192   /// Keep the compound hierarchy, but merge top-level compounds
193   /// into a single compound and add the result of the FUSE operation.
194   GeomShapePtr keepUnusedSubsOfCompound(
195       const GeomShapePtr& theResult,
196       const ObjectHierarchy& theObjectsHierarchy,
197       const ObjectHierarchy& theToolsHierarchy,
198       std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
199
200 private:
201   void parentForShape(const GeomShapePtr& theShape,
202                       const std::shared_ptr<ModelAPI_Result>& theContext,
203                       ObjectHierarchy& theShapesHierarchy);
204 };
205
206 #endif