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_AttributeSelectionList.h>
24 #include <ModelAPI_AttributeString.h>
25 #include <ModelAPI_Tools.h>
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_MakeShapeCustom.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAPI_Face.h>
32 #include <GeomAPI_ShapeIterator.h>
33 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_CompoundBuilder.h>
36 #include <GeomAlgoAPI_Tools.h>
39 //==================================================================================================
40 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
41 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
45 //==================================================================================================
46 void FeaturesPlugin_BooleanCommon::initAttributes()
48 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
50 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
51 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54 //==================================================================================================
55 void FeaturesPlugin_BooleanCommon::execute()
58 ObjectHierarchy anObjects, aTools;
60 bool isSimpleMode = false;
62 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
63 if (aCreationMethodAttr.get()
64 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
69 if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
71 // Planes are not supported as objects of COMMON operation
76 !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
79 if (anObjects.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
80 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
81 setError(aFeatureError);
87 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
88 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
89 ListOfShape aResultShapesList;
92 ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
93 GeomShapePtr aShape = *anObjectsIt;
94 for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
95 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
96 new GeomAlgoAPI_Boolean(aShape,
98 GeomAlgoAPI_Tools::BOOL_COMMON));
100 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
105 aShape = aCommonAlgo->shape();
106 aMakeShapeList->appendAlgo(aCommonAlgo);
109 GeomAPI_ShapeIterator aShapeIt(aShape);
110 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
111 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
112 document()->createBody(data(), aResultIndex);
114 ListOfShape anObjectList = anObjects.Objects();
115 ListOfShape aToolsList;
116 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
121 GeomShapePtr aBaseShape = anObjectList.front();
122 anObjectList.pop_front();
123 setResult(aResultBody, aResultIndex);
126 aToolsList = anObjectList;
127 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
128 aRBA.resultBody = aResultBody;
129 aRBA.baseShape = aBaseShape;
130 aRBA.makeShape = aMakeShapeList;
131 aResultBaseAlgoList.push_back(aRBA);
132 aResultShapesList.push_back(aShape);
136 for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
137 anObjectsIt != anObjects.End() && isOk;
140 GeomShapePtr anObject = *anObjectsIt;
141 GeomShapePtr aParent = anObjects.Parent(anObject);
144 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
145 if (aShapeType == GeomAPI_Shape::COMPOUND) {
147 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
148 anObjects, aParent, aTools.Objects(),
149 aResultIndex, aResultBaseAlgoList, aResultShapesList);
151 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
152 // Compsolid handling
153 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
154 anObjects, aParent, aTools.Objects(), ListOfShape(),
155 aResultIndex, aResultBaseAlgoList, aResultShapesList);
158 // process object as is
159 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
160 anObject, aTools.Objects(), aPlanes,
161 aResultIndex, aResultBaseAlgoList, aResultShapesList);
166 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
167 // result shape has been deleted, but in another it was modified or stayed.
168 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
169 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
171 aResultShapesCompound);
173 // remove the rest results if there were produced in the previous pass
174 removeResults(aResultIndex);