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 #include "FeaturesPlugin_BooleanCommon.h"
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Validator.h>
30 #include <GeomAlgoAPI_Boolean.h>
31 #include <GeomAlgoAPI_MakeShapeCustom.h>
32 #include <GeomAlgoAPI_MakeShapeList.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAPI_Face.h>
35 #include <GeomAPI_ShapeIterator.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAlgoAPI_PaveFiller.h>
38 #include <GeomAlgoAPI_CompoundBuilder.h>
39 #include <GeomAlgoAPI_Tools.h>
41 static const int THE_COMMON_VERSION_1 = 20190506;
43 //==================================================================================================
44 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
45 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
49 //==================================================================================================
50 void FeaturesPlugin_BooleanCommon::initAttributes()
52 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
54 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
55 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
57 initVersion(THE_COMMON_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
60 //==================================================================================================
61 void FeaturesPlugin_BooleanCommon::execute()
64 ObjectHierarchy anObjects, aTools;
66 bool isSimpleMode = false;
68 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
69 if (aCreationMethodAttr.get()
70 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
75 if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
77 // Planes are not supported as objects of COMMON operation
82 !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
85 if (anObjects.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
86 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
87 setError(aFeatureError);
91 // version of COMMON feature
92 int aCommonVersion = version();
95 GeomShapePtr aResultCompound;
98 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
99 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
100 ListOfShape aResultShapesList;
103 ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
104 GeomShapePtr aShape = *anObjectsIt;
105 for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
106 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
107 new GeomAlgoAPI_Boolean(aShape,
109 GeomAlgoAPI_Tools::BOOL_COMMON));
111 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
116 aShape = aCommonAlgo->shape();
117 aMakeShapeList->appendAlgo(aCommonAlgo);
120 if (aCommonVersion == THE_COMMON_VERSION_1) {
121 // merge hierarchies of compounds containing objects and tools
122 // and append the result of the FUSE operation
123 aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
126 GeomAPI_ShapeIterator aShapeIt(aShape);
127 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
128 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
129 document()->createBody(data(), aResultIndex);
131 ListOfShape anObjectList = anObjects.Objects();
132 ListOfShape aToolsList;
133 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
138 GeomShapePtr aBaseShape = anObjectList.front();
139 anObjectList.pop_front();
140 setResult(aResultBody, aResultIndex);
143 aToolsList = anObjectList;
144 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
145 aRBA.resultBody = aResultBody;
146 aRBA.baseShape = aBaseShape;
147 aRBA.makeShape = aMakeShapeList;
148 aResultBaseAlgoList.push_back(aRBA);
149 aResultShapesList.push_back(aShape);
152 if (aCommonVersion == THE_COMMON_VERSION_1) {
153 // merge hierarchies of compounds containing objects and tools
155 keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
159 for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
160 anObjectsIt != anObjects.End() && isOk;
163 GeomShapePtr anObject = *anObjectsIt;
164 GeomShapePtr aParent = anObjects.Parent(anObject);
167 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
168 if (aShapeType == GeomAPI_Shape::COMPOUND) {
170 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
171 anObjects, aParent, aTools.Objects(),
172 aResultIndex, aResultBaseAlgoList, aResultShapesList,
175 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
176 // Compsolid handling
177 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
178 anObjects, aParent, aTools.Objects(), ListOfShape(),
179 aResultIndex, aResultBaseAlgoList, aResultShapesList,
183 // process object as is
184 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
185 anObject, aTools.Objects(), aPlanes,
186 aResultIndex, aResultBaseAlgoList, aResultShapesList,
191 storeResult(anObjects.Objects(), aTools.Objects(), aResultCompound, aResultIndex,
192 aMakeShapeList, aResultBaseAlgoList);
195 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
196 // result shape has been deleted, but in another it was modified or stayed.
197 if (!aResultCompound)
198 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
199 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
203 // remove the rest results if there were produced in the previous pass
204 removeResults(aResultIndex);