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 /// Load Naming data structure of the feature to the document
82 void loadNamingDS(std::shared_ptr<ModelAPI_ResultBody> theResultBody,
83 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
84 const ListOfShape& theTools,
85 const std::shared_ptr<GeomAPI_Shape> theResultShape,
86 const GeomMakeShapePtr& theMakeShape);
89 /// Auxiliary class to store hierarchy of Boolean operation objects/tools
90 /// and their parent shapes (compounds or compsolids)
91 class ObjectHierarchy {
92 typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
93 typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
94 typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
95 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
97 ListOfShape myObjects; ///< list of objects/tools of Boolean operation
98 MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
99 /// indices of compounds/compsolids to keep the order of parent shapes
100 /// corresponding to the order of objects
101 MapShapeToIndex myParentIndices;
102 /// list of shape and its subshapes stored according to the index of parent shape
103 std::vector<ShapeAndSubshapes> mySubshapes;
105 SetOfShape myProcessedObjects;
108 /// Add object of Boolean opration
109 void AddObject(const GeomShapePtr& theObject);
111 /// Maps shape and its parent
112 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
114 /// Return parent shape for the given, or empty if it is a high-level shape.
115 /// By default, the parent and all its subshapes are marked as processed for further skip.
116 GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
118 /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
119 void SplitCompound(const GeomShapePtr& theCompShape,
120 ListOfShape& theUsed,
121 ListOfShape& theNotUsed) const;
123 /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
124 /// The generated list will contain only shapes unused during the Boolean operation.
125 void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
127 /// Return \c true if there is no object in hierarchy
128 bool IsEmpty() const;
130 /// Return list of objects
131 const ListOfShape& Objects() const { return myObjects; }
132 /// Separate objects of the given range of types and all other objects
133 void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
134 const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
135 const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
138 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
139 const SetOfShape& theUsed) const;
143 friend class ObjectHierarchy;
145 ObjectHierarchy* myHierarchy;
146 ListOfShape::iterator myObject;
149 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
151 void SkipAlreadyProcessed();
154 bool operator==(const Iterator&) const;
155 bool operator!=(const Iterator&) const;
157 Iterator& operator++();
158 Iterator operator++(int);
160 GeomShapePtr operator*() const;
167 /// Process SelectionList attribute and fill the objects hierarchy.
168 bool processAttribute(const std::string& theAttributeName,
169 ObjectHierarchy& theObjects,
170 ListOfShape& thePlanesList);
172 /// Perform Boolean operation of the object with the tools.
173 /// In case of theResultCompound is not empty, the result of Boolean operation
174 /// is added to this compound, and corresponding ResultBody is not generated.
175 /// \return \c false if something went wrong
176 bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
177 const GeomShapePtr& theObject,
178 const ListOfShape& theTools,
179 const ListOfShape& thePlanes,
181 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
182 ListOfShape& theResultShapesList,
183 GeomShapePtr theResulCompound = GeomShapePtr());
185 /// Perform Boolean operation of the Compsolid with the tools
186 /// In case of theResultCompound is not empty, the result of Boolean operation
187 /// is added to this compound, and corresponding ResultBody is not generated.
188 /// \return \c false if something went wrong
189 bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
190 const ObjectHierarchy& theCompsolidHierarchy,
191 const GeomShapePtr& theCompsolid,
192 const ListOfShape& theTools,
193 const ListOfShape& thePlanes,
195 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
196 ListOfShape& theResultShapesList,
197 GeomShapePtr theResulCompound = GeomShapePtr());
199 /// Perform Boolean operation of the Compound with the tools
200 /// In case of theResultCompound is not empty, the result of Boolean operation
201 /// is added to this compound, and corresponding ResultBody is not generated.
202 /// \return \c false if something went wrong
203 bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
204 const ObjectHierarchy& theCompoundHierarchy,
205 const GeomShapePtr& theCompound,
206 const ListOfShape& theTools,
208 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
209 ListOfShape& theResultShapesList,
210 GeomShapePtr theResulCompound = GeomShapePtr());
212 /// Process unused sub-shapes of compounds.
213 /// Keep the compound hierarchy, but merge top-level compounds
214 /// into a single compound and add the result of the FUSE operation.
215 GeomShapePtr keepUnusedSubsOfCompound(
216 const GeomShapePtr& theResult,
217 const ObjectHierarchy& theObjectsHierarchy,
218 const ObjectHierarchy& theToolsHierarchy,
219 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
221 /// Return version of the feature
225 void parentForShape(const GeomShapePtr& theShape,
226 const std::shared_ptr<ModelAPI_Result>& theContext,
227 ObjectHierarchy& theShapesHierarchy);
230 OperationType myOperationType;