1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef FeaturesPlugin_Boolean_H_
21 #define FeaturesPlugin_Boolean_H_
23 #include "FeaturesPlugin.h"
24 #include "FeaturesPlugin_Tools.h"
26 #include <GeomAlgoAPI_MakeShape.h>
27 #include <GeomAlgoAPI_Tools.h>
29 #include <ModelAPI_Feature.h>
31 class ModelAPI_Result;
32 class GeomAlgoAPI_MakeShapeList;
34 /// \class FeaturesPlugin_Boolean
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
49 /// Attribute name of main objects.
50 inline static const std::string& OBJECT_LIST_ID()
52 static const std::string MY_OBJECT_LIST_ID("main_objects");
53 return MY_OBJECT_LIST_ID;
56 /// Attribute name of tool objects.
57 inline static const std::string& TOOL_LIST_ID()
59 static const std::string MY_TOOL_LIST_ID("tool_objects");
60 return MY_TOOL_LIST_ID;
63 /// Attribute name of the version of Boolean feature
64 inline static const std::string& VERSION_ID()
66 static const std::string MY_VERSION_ID("version");
70 /// \return boolean operation type.
71 FEATURESPLUGIN_EXPORT OperationType operationType();
73 /// Request for initialization of data model of the feature: adding all attributes.
74 FEATURESPLUGIN_EXPORT virtual void initAttributes();
78 /// Use plugin manager for features creation.
79 FeaturesPlugin_Boolean(const OperationType theOperationType);
81 /// Initialize version field of the Boolean feature.
82 /// The version is initialized for newly created features,
83 /// not read from previously stored document.
84 void initVersion(const int theVersion);
86 /// Load Naming data structure of the feature to the document
87 void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
88 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
89 const ListOfShape& theTools,
90 const std::shared_ptr<GeomAPI_Shape> theResultShape,
91 const GeomMakeShapePtr& theMakeShape);
94 /// Auxiliary class to store hierarchy of Boolean operation objects/tools
95 /// and their parent shapes (compounds or compsolids)
96 class ObjectHierarchy {
97 typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
98 typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
99 typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
100 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
102 ListOfShape myObjects; ///< list of objects/tools of Boolean operation
103 MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
104 /// indices of compounds/compsolids to keep the order of parent shapes
105 /// corresponding to the order of objects
106 MapShapeToIndex myParentIndices;
107 /// list of shape and its subshapes stored according to the index of parent shape
108 std::vector<ShapeAndSubshapes> mySubshapes;
110 SetOfShape myProcessedObjects;
113 /// Add object of Boolean opration
114 void AddObject(const GeomShapePtr& theObject);
116 /// Maps shape and its parent
117 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
119 /// Return parent shape for the given, or empty if it is a high-level shape.
120 /// By default, the parent and all its subshapes are marked as processed for further skip.
121 GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
123 /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
124 void SplitCompound(const GeomShapePtr& theCompShape,
125 ListOfShape& theUsed,
126 ListOfShape& theNotUsed) const;
128 /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
129 /// The generated list will contain only shapes unused during the Boolean operation.
130 void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
132 /// Return \c true if there is no object in hierarchy
133 bool IsEmpty() const;
135 /// Return list of objects
136 const ListOfShape& Objects() const { return myObjects; }
137 /// Separate objects of the given range of types and all other objects
138 void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
139 const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
140 const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
143 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
144 const SetOfShape& theUsed) const;
148 friend class ObjectHierarchy;
150 ObjectHierarchy* myHierarchy;
151 ListOfShape::iterator myObject;
154 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
156 void SkipAlreadyProcessed();
159 bool operator==(const Iterator&) const;
160 bool operator!=(const Iterator&) const;
162 Iterator& operator++();
163 Iterator operator++(int);
165 GeomShapePtr operator*() const;
172 /// Process SelectionList attribute and fill the objects hierarchy.
173 bool processAttribute(const std::string& theAttributeName,
174 ObjectHierarchy& theObjects,
175 ListOfShape& thePlanesList);
177 /// Perform Boolean operation of the object with the tools.
178 /// In case of theResultCompound is not empty, the result of Boolean operation
179 /// is added to this compound, and corresponding ResultBody is not generated.
180 /// \return \c false if something went wrong
181 bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
182 const GeomShapePtr& theObject,
183 const ListOfShape& theTools,
184 const ListOfShape& thePlanes,
186 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
187 ListOfShape& theResultShapesList,
188 GeomShapePtr theResulCompound = GeomShapePtr());
190 /// Perform Boolean operation of the Compsolid with the tools
191 /// In case of theResultCompound is not empty, the result of Boolean operation
192 /// is added to this compound, and corresponding ResultBody is not generated.
193 /// \return \c false if something went wrong
194 bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
195 const ObjectHierarchy& theCompsolidHierarchy,
196 const GeomShapePtr& theCompsolid,
197 const ListOfShape& theTools,
198 const ListOfShape& thePlanes,
200 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
201 ListOfShape& theResultShapesList,
202 GeomShapePtr theResulCompound = GeomShapePtr());
204 /// Perform Boolean operation of the Compound with the tools
205 /// In case of theResultCompound is not empty, the result of Boolean operation
206 /// is added to this compound, and corresponding ResultBody is not generated.
207 /// \return \c false if something went wrong
208 bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
209 const ObjectHierarchy& theCompoundHierarchy,
210 const GeomShapePtr& theCompound,
211 const ListOfShape& theTools,
213 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
214 ListOfShape& theResultShapesList,
215 GeomShapePtr theResulCompound = GeomShapePtr());
217 /// Process unused sub-shapes of compounds.
218 /// Keep the compound hierarchy, but merge top-level compounds
219 /// into a single compound and add the result of the FUSE operation.
220 GeomShapePtr keepUnusedSubsOfCompound(
221 const GeomShapePtr& theResult,
222 const ObjectHierarchy& theObjectsHierarchy,
223 const ObjectHierarchy& theToolsHierarchy,
224 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
226 /// Store result shape if it is not empty and increase results counter
227 void storeResult(const ListOfShape& theObjects,
228 const ListOfShape& theTools,
229 const GeomShapePtr theResultShape,
231 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList,
232 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList);
234 /// Return version of the feature
238 void parentForShape(const GeomShapePtr& theShape,
239 const std::shared_ptr<ModelAPI_Result>& theContext,
240 ObjectHierarchy& theShapesHierarchy);
243 OperationType myOperationType;