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 //==================================================================================================
42 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
43 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
47 //==================================================================================================
48 void FeaturesPlugin_BooleanCommon::initAttributes()
50 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
52 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
53 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
55 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
58 //==================================================================================================
59 void FeaturesPlugin_BooleanCommon::execute()
62 GeomAPI_ShapeHierarchy anObjects, aTools;
64 bool isSimpleMode = false;
66 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
67 if (aCreationMethodAttr.get()
68 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
73 if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
75 // Planes are not supported as objects of COMMON operation
80 !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
83 if (anObjects.empty() || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
84 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
85 setError(aFeatureError);
89 // version of COMMON feature
90 const std::string aCommonVersion = data()->version();
93 GeomShapePtr aResultCompound;
96 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
97 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
98 ListOfShape aResultShapesList;
101 GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
102 GeomShapePtr aShape = *anObjectsIt;
103 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
104 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
105 new GeomAlgoAPI_Boolean(aShape,
107 GeomAlgoAPI_Tools::BOOL_COMMON));
109 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
114 aShape = aCommonAlgo->shape();
115 aMakeShapeList->appendAlgo(aCommonAlgo);
118 if (aCommonVersion == BOP_VERSION_9_4()) {
119 // merge hierarchies of compounds containing objects and tools
120 // and append the result of the FUSE operation
121 aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
124 GeomAPI_ShapeIterator aShapeIt(aShape);
125 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
126 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
127 document()->createBody(data(), aResultIndex);
129 ListOfShape anObjectList = anObjects.objects();
130 ListOfShape aToolsList;
131 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
136 GeomShapePtr aBaseShape = anObjectList.front();
137 anObjectList.pop_front();
138 setResult(aResultBody, aResultIndex);
141 aToolsList = anObjectList;
142 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
143 aRBA.resultBody = aResultBody;
144 aRBA.baseShape = aBaseShape;
145 aRBA.makeShape = aMakeShapeList;
146 aResultBaseAlgoList.push_back(aRBA);
147 aResultShapesList.push_back(aShape);
150 if (aCommonVersion == BOP_VERSION_9_4()) {
151 // merge hierarchies of compounds containing objects and tools
153 keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
157 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
158 anObjectsIt != anObjects.end() && isOk;
161 GeomShapePtr anObject = *anObjectsIt;
162 GeomShapePtr aParent = anObjects.parent(anObject);
165 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
166 if (aShapeType == GeomAPI_Shape::COMPOUND) {
168 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
169 anObjects, aParent, aTools.objects(),
170 aResultIndex, aResultBaseAlgoList, aResultShapesList,
173 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
174 // Compsolid handling
175 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
176 anObjects, aParent, aTools.objects(), ListOfShape(),
177 aResultIndex, aResultBaseAlgoList, aResultShapesList,
181 // process object as is
182 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
183 anObject, aTools.objects(), aPlanes,
184 aResultIndex, aResultBaseAlgoList, aResultShapesList,
189 storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
190 aMakeShapeList, aResultBaseAlgoList);
193 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
194 // result shape has been deleted, but in another it was modified or stayed.
195 if (!aResultCompound)
196 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
197 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
201 // remove the rest results if there were produced in the previous pass
202 removeResults(aResultIndex);