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_VersionedBoolean_H_
21 #define FeaturesPlugin_VersionedBoolean_H_
23 #include "FeaturesPlugin.h"
24 #include "FeaturesPlugin_Tools.h"
26 #include <GeomAlgoAPI_Tools.h>
28 #include <ModelAPI_Feature.h>
30 class ModelAPI_Attribute;
31 class ModelAPI_Result;
32 class GeomAlgoAPI_MakeShapeList;
34 /// \class FeaturesPlugin_VersionedBoolean
36 /// \brief Feature controls a version of Boolean operations.
37 class FeaturesPlugin_VersionedBoolean : public ModelAPI_Feature
40 /// Attribute name of the version of Boolean feature
41 inline static const std::string& VERSION_ID()
43 static const std::string MY_VERSION_ID("version");
49 /// Use plugin manager for features creation.
50 FeaturesPlugin_VersionedBoolean() {}
52 /// Initialize version field of the Boolean feature.
53 /// The version is initialized for newly created features,
54 /// not read from previously stored document.
55 void initVersion(const int theVersion,
56 const std::shared_ptr<ModelAPI_Attribute> theObjectsAttr,
57 const std::shared_ptr<ModelAPI_Attribute> theToolsAttr);
59 /// Auxiliary class to store hierarchy of Boolean operation objects/tools
60 /// and their parent shapes (compounds or compsolids)
61 class ObjectHierarchy {
62 typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
63 typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
64 typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
65 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
67 ListOfShape myObjects; ///< list of objects/tools of Boolean operation
68 MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
69 /// indices of compounds/compsolids to keep the order of parent shapes
70 /// corresponding to the order of objects
71 MapShapeToIndex myParentIndices;
72 /// list of shape and its subshapes stored according to the index of parent shape
73 std::vector<ShapeAndSubshapes> mySubshapes;
75 SetOfShape myProcessedObjects;
78 /// Add object of Boolean opration
79 void AddObject(const GeomShapePtr& theObject);
81 /// Maps shape and its parent
82 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
84 /// Return parent shape for the given, or empty if it is a high-level shape.
85 /// By default, the parent and all its subshapes are marked as processed for further skip.
86 GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
88 /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
89 void SplitCompound(const GeomShapePtr& theCompShape,
91 ListOfShape& theNotUsed) const;
93 /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
94 /// The generated list will contain only shapes unused during the Boolean operation.
95 void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
97 /// Return \c true if there is no object in hierarchy
100 /// Return list of objects
101 const ListOfShape& Objects() const { return myObjects; }
102 /// Separate objects of the given range of types and all other objects
103 void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
104 const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
105 const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
108 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
109 const SetOfShape& theUsed) const;
113 friend class ObjectHierarchy;
115 ObjectHierarchy* myHierarchy;
116 ListOfShape::iterator myObject;
119 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
121 void SkipAlreadyProcessed();
124 bool operator==(const Iterator&) const;
125 bool operator!=(const Iterator&) const;
127 Iterator& operator++();
128 Iterator operator++(int);
130 GeomShapePtr operator*() const;
137 /// Process SelectionList attribute and fill the objects hierarchy.
138 bool processAttribute(const std::string& theAttributeName,
139 ObjectHierarchy& theObjects,
140 ListOfShape& thePlanesList);
142 /// Perform Boolean operation of the object with the tools.
143 /// In case of theResultCompound is not empty, the result of Boolean operation
144 /// is added to this compound, and corresponding ResultBody is not generated.
145 /// \return \c false if something went wrong
146 bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
147 const GeomShapePtr& theObject,
148 const ListOfShape& theTools,
149 const ListOfShape& thePlanes,
151 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
152 ListOfShape& theResultShapesList,
153 GeomShapePtr theResulCompound = GeomShapePtr());
155 /// Perform Boolean operation of the Compsolid with the tools
156 /// In case of theResultCompound is not empty, the result of Boolean operation
157 /// is added to this compound, and corresponding ResultBody is not generated.
158 /// \return \c false if something went wrong
159 bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
160 const ObjectHierarchy& theCompsolidHierarchy,
161 const GeomShapePtr& theCompsolid,
162 const ListOfShape& theTools,
163 const ListOfShape& thePlanes,
165 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
166 ListOfShape& theResultShapesList,
167 GeomShapePtr theResulCompound = GeomShapePtr());
169 /// Perform Boolean operation of the Compound with the tools
170 /// In case of theResultCompound is not empty, the result of Boolean operation
171 /// is added to this compound, and corresponding ResultBody is not generated.
172 /// \return \c false if something went wrong
173 bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
174 const ObjectHierarchy& theCompoundHierarchy,
175 const GeomShapePtr& theCompound,
176 const ListOfShape& theTools,
178 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
179 ListOfShape& theResultShapesList,
180 GeomShapePtr theResulCompound = GeomShapePtr());
182 /// Process unused sub-shapes of compounds.
183 /// Keep the compound hierarchy, but merge top-level compounds
184 /// into a single compound and add the result of the FUSE operation.
185 GeomShapePtr keepUnusedSubsOfCompound(
186 const GeomShapePtr& theResult,
187 const ObjectHierarchy& theObjectsHierarchy,
188 const ObjectHierarchy& theToolsHierarchy,
189 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
191 /// Return version of the feature
195 void parentForShape(const GeomShapePtr& theShape,
196 const std::shared_ptr<ModelAPI_Result>& theContext,
197 ObjectHierarchy& theShapesHierarchy);