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 = std::shared_ptr<ModelAPI_Attribute>(),
58 const std::shared_ptr<ModelAPI_Attribute> theToolsAttr
59 = std::shared_ptr<ModelAPI_Attribute>());
61 /// Auxiliary class to store hierarchy of Boolean operation objects/tools
62 /// and their parent shapes (compounds or compsolids)
63 class ObjectHierarchy {
64 typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
65 typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
66 typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
67 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
69 ListOfShape myObjects; ///< list of objects/tools of Boolean operation
70 MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
71 /// indices of compounds/compsolids to keep the order of parent shapes
72 /// corresponding to the order of objects
73 MapShapeToIndex myParentIndices;
74 /// list of shape and its subshapes stored according to the index of parent shape
75 std::vector<ShapeAndSubshapes> mySubshapes;
77 SetOfShape myProcessedObjects;
80 /// Add object of Boolean opration
81 void AddObject(const GeomShapePtr& theObject);
83 /// Maps shape and its parent
84 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
86 /// Return parent shape for the given, or empty if it is a high-level shape.
87 /// By default, the parent and all its subshapes are marked as processed for further skip.
88 GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
90 /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
91 void SplitCompound(const GeomShapePtr& theCompShape,
93 ListOfShape& theNotUsed) const;
95 /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
96 /// The generated list will contain only shapes unused during the Boolean operation.
97 void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
99 /// Return \c true if there is no object in hierarchy
100 bool IsEmpty() const;
102 /// Return list of objects
103 const ListOfShape& Objects() const { return myObjects; }
104 /// Separate objects of the given range of types and all other objects
105 void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
106 const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
107 const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
110 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
111 const SetOfShape& theUsed) const;
115 friend class ObjectHierarchy;
117 ObjectHierarchy* myHierarchy;
118 ListOfShape::iterator myObject;
121 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
123 void SkipAlreadyProcessed();
126 bool operator==(const Iterator&) const;
127 bool operator!=(const Iterator&) const;
129 Iterator& operator++();
130 Iterator operator++(int);
132 GeomShapePtr operator*() const;
139 /// Process SelectionList attribute and fill the objects hierarchy.
140 bool processAttribute(const std::string& theAttributeName,
141 ObjectHierarchy& theObjects,
142 ListOfShape& thePlanesList);
144 /// Perform Boolean operation of the object with the tools.
145 /// In case of theResultCompound is not empty, the result of Boolean operation
146 /// is added to this compound, and corresponding ResultBody is not generated.
147 /// \return \c false if something went wrong
148 bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
149 const GeomShapePtr& theObject,
150 const ListOfShape& theTools,
151 const ListOfShape& thePlanes,
153 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
154 ListOfShape& theResultShapesList,
155 GeomShapePtr theResulCompound = GeomShapePtr());
157 /// Perform Boolean operation of the Compsolid with the tools
158 /// In case of theResultCompound is not empty, the result of Boolean operation
159 /// is added to this compound, and corresponding ResultBody is not generated.
160 /// \return \c false if something went wrong
161 bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
162 const ObjectHierarchy& theCompsolidHierarchy,
163 const GeomShapePtr& theCompsolid,
164 const ListOfShape& theTools,
165 const ListOfShape& thePlanes,
167 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
168 ListOfShape& theResultShapesList,
169 GeomShapePtr theResulCompound = GeomShapePtr());
171 /// Perform Boolean operation of the Compound with the tools
172 /// In case of theResultCompound is not empty, the result of Boolean operation
173 /// is added to this compound, and corresponding ResultBody is not generated.
174 /// \return \c false if something went wrong
175 bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
176 const ObjectHierarchy& theCompoundHierarchy,
177 const GeomShapePtr& theCompound,
178 const ListOfShape& theTools,
180 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
181 ListOfShape& theResultShapesList,
182 GeomShapePtr theResulCompound = GeomShapePtr());
184 /// Resize planes to fit them to the bounding box of the given lins of objects.
185 static void resizePlanes(const ListOfShape& theObjects,
186 ListOfShape& thePlanes,
187 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
189 /// Process unused sub-shapes of compounds.
190 /// Keep the compound hierarchy, but merge top-level compounds
191 /// into a single compound and add the result of the FUSE operation.
192 GeomShapePtr keepUnusedSubsOfCompound(
193 const GeomShapePtr& theResult,
194 const ObjectHierarchy& theObjectsHierarchy,
195 const ObjectHierarchy& theToolsHierarchy,
196 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
198 /// Return version of the feature
202 void parentForShape(const GeomShapePtr& theShape,
203 const std::shared_ptr<ModelAPI_Result>& theContext,
204 ObjectHierarchy& theShapesHierarchy);