1 // Copyright (C) 2014-2022 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_AttributeBoolean.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeInteger.h>
26 #include <ModelAPI_AttributeSelectionList.h>
27 #include <ModelAPI_AttributeString.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Validator.h>
32 #include <GeomAlgoAPI_Boolean.h>
33 #include <GeomAlgoAPI_MakeShapeCustom.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAPI_Face.h>
37 #include <GeomAPI_ShapeIterator.h>
38 #include <GeomAPI_ShapeExplorer.h>
39 #include <GeomAlgoAPI_PaveFiller.h>
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41 #include <GeomAlgoAPI_Tools.h>
44 static const double DEFAULT_FUZZY = 1.e-5;
47 //==================================================================================================
48 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
49 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
53 //==================================================================================================
54 void FeaturesPlugin_BooleanCommon::initAttributes()
56 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
58 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
59 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
61 data()->addAttribute(USE_FUZZY_ID(), ModelAPI_AttributeBoolean::typeId());
62 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
63 boolean(USE_FUZZY_ID())->setValue(false); // Do NOT use the fuzzy parameter by default.
64 real(FUZZY_PARAM_ID())->setValue(DEFAULT_FUZZY);
66 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
69 //==================================================================================================
70 void FeaturesPlugin_BooleanCommon::execute()
73 GeomAPI_ShapeHierarchy anObjects, aTools;
75 bool isSimpleMode = false;
77 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
78 if (aCreationMethodAttr.get()
79 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
84 if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
86 // Planes are not supported as objects of COMMON operation
91 !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
94 if (anObjects.empty() || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
95 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
96 setError(aFeatureError);
100 // Getting fuzzy parameter.
101 // Used as additional tolerance to eliminate tiny results.
102 // Using -1 as fuzzy value in the GeomAlgoAPI means to ignore it during the boolean operation!
103 bool aUseFuzzy = boolean(USE_FUZZY_ID())->value();
104 double aFuzzy = (aUseFuzzy ? real(FUZZY_PARAM_ID())->value() : -1);
106 // version of COMMON feature
107 const std::string aCommonVersion = data()->version();
109 int aResultIndex = 0;
110 GeomShapePtr aResultCompound;
113 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
114 std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
115 ListOfShape aResultShapesList;
118 GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
119 GeomShapePtr aShape = *anObjectsIt;
120 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
121 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
122 new GeomAlgoAPI_Boolean(aShape,
124 GeomAlgoAPI_Tools::BOOL_COMMON,
127 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
132 aShape = aCommonAlgo->shape();
133 aMakeShapeList->appendAlgo(aCommonAlgo);
136 if (aCommonVersion == BOP_VERSION_9_4()) {
137 // merge hierarchies of compounds containing objects and tools
138 // and append the result of the FUSE operation
139 aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
142 GeomAPI_ShapeIterator aShapeIt(aShape);
143 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
144 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
145 document()->createBody(data(), aResultIndex);
147 ListOfShape anObjectList = anObjects.objects();
148 ListOfShape aToolsList;
149 ModelAPI_Tools::loadModifiedShapes(aResultBody,
154 GeomShapePtr aBaseShape = anObjectList.front();
155 anObjectList.pop_front();
156 setResult(aResultBody, aResultIndex);
159 aToolsList = anObjectList;
160 ModelAPI_Tools::ResultBaseAlgo aRBA;
161 aRBA.resultBody = aResultBody;
162 aRBA.baseShape = aBaseShape;
163 aRBA.makeShape = aMakeShapeList;
164 aResultBaseAlgoList.push_back(aRBA);
165 aResultShapesList.push_back(aShape);
168 if (aCommonVersion == BOP_VERSION_9_4()) {
169 // merge hierarchies of compounds containing objects and tools
171 keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
175 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
176 anObjectsIt != anObjects.end() && isOk;
179 GeomShapePtr anObject = *anObjectsIt;
180 GeomShapePtr aParent = anObjects.parent(anObject);
183 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
184 if (aShapeType == GeomAPI_Shape::COMPOUND) {
186 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
187 anObjects, aParent, aTools.objects(),
189 aResultIndex, aResultBaseAlgoList, aResultShapesList,
192 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
193 // Compsolid handling
194 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
195 anObjects, aParent, aTools.objects(), ListOfShape(),
197 aResultIndex, aResultBaseAlgoList, aResultShapesList,
201 // process object as is
202 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
203 anObject, aTools.objects(), aPlanes,
205 aResultIndex, aResultBaseAlgoList, aResultShapesList,
210 storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
211 aMakeShapeList, aResultBaseAlgoList);
214 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
215 // result shape has been deleted, but in another it was modified or stayed.
216 if (!aResultCompound)
217 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
218 ModelAPI_Tools::loadDeletedShapes(aResultBaseAlgoList,
222 // remove the rest results if there were produced in the previous pass
223 removeResults(aResultIndex);