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;
33 /// \class FeaturesPlugin_Boolean
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
48 /// Attribute name of main objects.
49 inline static const std::string& OBJECT_LIST_ID()
51 static const std::string MY_OBJECT_LIST_ID("main_objects");
52 return MY_OBJECT_LIST_ID;
55 /// Attribute name of tool objects.
56 inline static const std::string& TOOL_LIST_ID()
58 static const std::string MY_TOOL_LIST_ID("tool_objects");
59 return MY_TOOL_LIST_ID;
62 /// Attribute name of the version of Boolean feature
63 inline static const std::string& VERSION_ID()
65 static const std::string MY_VERSION_ID("version");
69 /// \return boolean operation type.
70 FEATURESPLUGIN_EXPORT OperationType operationType();
72 /// Request for initialization of data model of the feature: adding all attributes.
73 FEATURESPLUGIN_EXPORT virtual void initAttributes();
77 /// Use plugin manager for features creation.
78 FeaturesPlugin_Boolean(const OperationType theOperationType);
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);
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;
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;
104 SetOfShape myProcessedObjects;
107 /// Add object of Boolean opration
108 void AddObject(const GeomShapePtr& theObject);
110 /// Maps shape and its parent
111 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
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);
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;
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;
126 /// Return \c true if there is no object in hierarchy
127 bool IsEmpty() const;
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;
137 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
138 const SetOfShape& theUsed) const;
142 friend class ObjectHierarchy;
144 ObjectHierarchy* myHierarchy;
145 ListOfShape::iterator myObject;
148 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
150 void SkipAlreadyProcessed();
153 bool operator==(const Iterator&) const;
154 bool operator!=(const Iterator&) const;
156 Iterator& operator++();
157 Iterator operator++(int);
159 GeomShapePtr operator*() const;
166 /// Process SelectionList attribute and fill the objects hierarchy.
167 bool processAttribute(const std::string& theAttributeName,
168 ObjectHierarchy& theObjects,
169 ListOfShape& thePlanesList);
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,
178 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
179 ListOfShape& theResultShapesList);
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,
189 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
190 ListOfShape& theResultShapesList);
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,
199 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
200 ListOfShape& theResultShapesList);
203 void parentForShape(const GeomShapePtr& theShape,
204 const std::shared_ptr<ModelAPI_Result>& theContext,
205 ObjectHierarchy& theShapesHierarchy);
208 OperationType myOperationType;