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 static const std::string& BOP_VERSION_9_4()
42 static const std::string VERSION("v9.4");
46 /// Use plugin manager for features creation.
47 FeaturesPlugin_VersionedBoolean() {}
49 /// Initialize version field of the Boolean feature.
50 /// The version is initialized for newly created features,
51 /// not read from previously stored document.
52 void initVersion(const std::string& theVersion,
53 const std::shared_ptr<ModelAPI_Attribute> theObjectsAttr
54 = std::shared_ptr<ModelAPI_Attribute>(),
55 const std::shared_ptr<ModelAPI_Attribute> theToolsAttr
56 = std::shared_ptr<ModelAPI_Attribute>());
58 /// Auxiliary class to store hierarchy of Boolean operation objects/tools
59 /// and their parent shapes (compounds or compsolids)
60 class ObjectHierarchy {
61 typedef std::pair<GeomShapePtr, ListOfShape> ShapeAndSubshapes;
62 typedef std::map<GeomShapePtr, GeomShapePtr, GeomAPI_Shape::Comparator> MapShapeToParent;
63 typedef std::map<GeomShapePtr, size_t, GeomAPI_Shape::Comparator> MapShapeToIndex;
64 typedef std::set<GeomShapePtr, GeomAPI_Shape::Comparator> SetOfShape;
66 ListOfShape myObjects; ///< list of objects/tools of Boolean operation
67 MapShapeToParent myParent; ///< refer a shape to compound/compsolid containing it
68 /// indices of compounds/compsolids to keep the order of parent shapes
69 /// corresponding to the order of objects
70 MapShapeToIndex myParentIndices;
71 /// list of shape and its subshapes stored according to the index of parent shape
72 std::vector<ShapeAndSubshapes> mySubshapes;
74 SetOfShape myProcessedObjects;
77 /// Add object of Boolean opration
78 void AddObject(const GeomShapePtr& theObject);
80 /// Maps shape and its parent
81 void AddParent(const GeomShapePtr& theShape, const GeomShapePtr& theParent);
83 /// Return parent shape for the given, or empty if it is a high-level shape.
84 /// By default, the parent and all its subshapes are marked as processed for further skip.
85 GeomShapePtr Parent(const GeomShapePtr& theShape, bool theMarkProcessed = true);
87 /// Marke the shape as already processed
88 void MarkProcessed(const GeomShapePtr& theShape);
89 /// Marke list ofshapes as already processed
90 void MarkProcessed(const ListOfShape& theShapes);
92 /// Split compound/compsolid shape for subshapes selected for Boolean operation and the other.
93 void SplitCompound(const GeomShapePtr& theCompShape,
95 ListOfShape& theNotUsed) const;
97 /// Generates the list of top-level compounds, which contain the objects of Boolean operation.
98 /// The generated list will contain only shapes unused during the Boolean operation.
99 void CompoundsOfUnusedObjects(ListOfShape& theDestination) const;
101 /// Return \c true if there is no object in hierarchy
102 bool IsEmpty() const;
104 /// Return list of objects
105 const ListOfShape& Objects() const { return myObjects; }
106 /// Separate objects of the given range of types and all other objects
107 void ObjectsByType(ListOfShape& theShapesByType, ListOfShape& theOtherShapes,
108 const GeomAPI_Shape::ShapeType theMinType = GeomAPI_Shape::COMPOUND,
109 const GeomAPI_Shape::ShapeType theMaxType = GeomAPI_Shape::SHAPE) const;
112 GeomShapePtr collectUnusedSubs(const GeomShapePtr theTopLevelCompound,
113 const SetOfShape& theUsed) const;
117 friend class ObjectHierarchy;
119 ObjectHierarchy* myHierarchy;
120 ListOfShape::iterator myObject;
123 Iterator(ObjectHierarchy* theHierarchy, bool isBegin = true);
125 void SkipAlreadyProcessed();
128 bool operator==(const Iterator&) const;
129 bool operator!=(const Iterator&) const;
131 Iterator& operator++();
132 Iterator operator++(int);
134 GeomShapePtr operator*() const;
141 /// Process SelectionList attribute and fill the objects hierarchy.
142 bool processAttribute(const std::string& theAttributeName,
143 ObjectHierarchy& theObjects,
144 ListOfShape& thePlanesList);
146 /// Perform Boolean operation of the object with the tools.
147 /// In case of theResultCompound is not empty, the result of Boolean operation
148 /// is added to this compound, and corresponding ResultBody is not generated.
149 /// \return \c false if something went wrong
150 bool processObject(const GeomAlgoAPI_Tools::BOPType theBooleanType,
151 const GeomShapePtr& theObject,
152 const ListOfShape& theTools,
153 const ListOfShape& thePlanes,
155 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
156 ListOfShape& theResultShapesList,
157 GeomShapePtr theResulCompound = GeomShapePtr());
159 /// Perform Boolean operation of the Compsolid with the tools
160 /// In case of theResultCompound is not empty, the result of Boolean operation
161 /// is added to this compound, and corresponding ResultBody is not generated.
162 /// \return \c false if something went wrong
163 bool processCompsolid(const GeomAlgoAPI_Tools::BOPType theBooleanType,
164 ObjectHierarchy& theCompsolidHierarchy,
165 const GeomShapePtr& theCompsolid,
166 const ListOfShape& theTools,
167 const ListOfShape& thePlanes,
169 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
170 ListOfShape& theResultShapesList,
171 GeomShapePtr theResulCompound = GeomShapePtr());
173 /// Perform Boolean operation of the Compound with the tools
174 /// In case of theResultCompound is not empty, the result of Boolean operation
175 /// is added to this compound, and corresponding ResultBody is not generated.
176 /// \return \c false if something went wrong
177 bool processCompound(const GeomAlgoAPI_Tools::BOPType theBooleanType,
178 ObjectHierarchy& theCompoundHierarchy,
179 const GeomShapePtr& theCompound,
180 const ListOfShape& theTools,
182 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>& theResultBaseAlgoList,
183 ListOfShape& theResultShapesList,
184 GeomShapePtr theResulCompound = GeomShapePtr());
186 /// Resize planes to fit them to the bounding box of the given lins of objects.
187 static void resizePlanes(const ListOfShape& theObjects,
188 ListOfShape& thePlanes,
189 std::shared_ptr<GeomAlgoAPI_MakeShapeList>& theMakeShapeList);
191 /// Process unused sub-shapes of compounds.
192 /// Keep the compound hierarchy, but merge top-level compounds
193 /// into a single compound and add the result of the FUSE operation.
194 GeomShapePtr keepUnusedSubsOfCompound(
195 const GeomShapePtr& theResult,
196 const ObjectHierarchy& theObjectsHierarchy,
197 const ObjectHierarchy& theToolsHierarchy,
198 std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList);
201 void parentForShape(const GeomShapePtr& theShape,
202 const std::shared_ptr<ModelAPI_Result>& theContext,
203 ObjectHierarchy& theShapesHierarchy);