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_AttributeDouble.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
31 #include <GeomAlgoAPI_Boolean.h>
32 #include <GeomAlgoAPI_MakeShapeCustom.h>
33 #include <GeomAlgoAPI_MakeShapeList.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35 #include <GeomAPI_Face.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAlgoAPI_PaveFiller.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_Tools.h>
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 data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
59 // Initialize the fuzzy parameter with a value below Precision::Confusion() to indicate,
60 // that the internal algorithms should use their default fuzzy value, if none was specified
62 real(FUZZY_PARAM_ID())->setValue(1.e-8);
64 initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
67 //==================================================================================================
68 void FeaturesPlugin_BooleanCommon::execute()
71 GeomAPI_ShapeHierarchy anObjects, aTools;
73 bool isSimpleMode = false;
75 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
76 if (aCreationMethodAttr.get()
77 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
82 if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
84 // Planes are not supported as objects of COMMON operation
89 !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
92 if (anObjects.empty() || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
93 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
94 setError(aFeatureError);
98 // Getting fuzzy parameter.
99 // Used as additional tolerance to eliminate tiny results.
100 double aFuzzy = real(FUZZY_PARAM_ID())->value();
102 // version of COMMON feature
103 const std::string aCommonVersion = data()->version();
105 int aResultIndex = 0;
106 GeomShapePtr aResultCompound;
109 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
110 std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
111 ListOfShape aResultShapesList;
114 GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
115 GeomShapePtr aShape = *anObjectsIt;
116 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
117 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
118 new GeomAlgoAPI_Boolean(aShape,
120 GeomAlgoAPI_Tools::BOOL_COMMON,
123 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
128 aShape = aCommonAlgo->shape();
129 aMakeShapeList->appendAlgo(aCommonAlgo);
132 if (aCommonVersion == BOP_VERSION_9_4()) {
133 // merge hierarchies of compounds containing objects and tools
134 // and append the result of the FUSE operation
135 aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
138 GeomAPI_ShapeIterator aShapeIt(aShape);
139 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
140 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
141 document()->createBody(data(), aResultIndex);
143 ListOfShape anObjectList = anObjects.objects();
144 ListOfShape aToolsList;
145 ModelAPI_Tools::loadModifiedShapes(aResultBody,
150 GeomShapePtr aBaseShape = anObjectList.front();
151 anObjectList.pop_front();
152 setResult(aResultBody, aResultIndex);
155 aToolsList = anObjectList;
156 ModelAPI_Tools::ResultBaseAlgo aRBA;
157 aRBA.resultBody = aResultBody;
158 aRBA.baseShape = aBaseShape;
159 aRBA.makeShape = aMakeShapeList;
160 aResultBaseAlgoList.push_back(aRBA);
161 aResultShapesList.push_back(aShape);
164 if (aCommonVersion == BOP_VERSION_9_4()) {
165 // merge hierarchies of compounds containing objects and tools
167 keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
171 for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
172 anObjectsIt != anObjects.end() && isOk;
175 GeomShapePtr anObject = *anObjectsIt;
176 GeomShapePtr aParent = anObjects.parent(anObject);
179 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
180 if (aShapeType == GeomAPI_Shape::COMPOUND) {
182 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
183 anObjects, aParent, aTools.objects(),
185 aResultIndex, aResultBaseAlgoList, aResultShapesList,
188 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
189 // Compsolid handling
190 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
191 anObjects, aParent, aTools.objects(), ListOfShape(),
193 aResultIndex, aResultBaseAlgoList, aResultShapesList,
197 // process object as is
198 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
199 anObject, aTools.objects(), aPlanes,
201 aResultIndex, aResultBaseAlgoList, aResultShapesList,
206 storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
207 aMakeShapeList, aResultBaseAlgoList);
210 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
211 // result shape has been deleted, but in another it was modified or stayed.
212 if (!aResultCompound)
213 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
214 ModelAPI_Tools::loadDeletedShapes(aResultBaseAlgoList,
218 // remove the rest results if there were produced in the previous pass
219 removeResults(aResultIndex);