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 AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
58 aVerAttr->setIsArgument(false);
59 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
60 if (!integer(VERSION_ID())->isInitialized() &&
61 !selectionList(OBJECT_LIST_ID())->isInitialized() &&
62 !selectionList(TOOL_LIST_ID())->isInitialized()) {
63 // this is a newly created feature (not read from file),
64 // so, initialize the latest version
65 integer(VERSION_ID())->setValue(THE_COMMON_VERSION_1);
69 //==================================================================================================
70 void FeaturesPlugin_BooleanCommon::execute()
73 ObjectHierarchy 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.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
95 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
96 setError(aFeatureError);
100 // version of COMMON feature
101 int aCommonVersion = version();
103 int aResultIndex = 0;
104 GeomShapePtr aResultCompound;
107 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
108 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
109 ListOfShape aResultShapesList;
112 ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
113 GeomShapePtr aShape = *anObjectsIt;
114 for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
115 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
116 new GeomAlgoAPI_Boolean(aShape,
118 GeomAlgoAPI_Tools::BOOL_COMMON));
120 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
125 aShape = aCommonAlgo->shape();
126 aMakeShapeList->appendAlgo(aCommonAlgo);
129 if (aCommonVersion == THE_COMMON_VERSION_1) {
130 // merge hierarchies of compounds containing objects and tools
131 // and append the result of the FUSE operation
132 aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
135 GeomAPI_ShapeIterator aShapeIt(aShape);
136 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
137 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
138 document()->createBody(data(), aResultIndex);
140 ListOfShape anObjectList = anObjects.Objects();
141 ListOfShape aToolsList;
142 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
147 GeomShapePtr aBaseShape = anObjectList.front();
148 anObjectList.pop_front();
149 setResult(aResultBody, aResultIndex);
152 aToolsList = anObjectList;
153 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
154 aRBA.resultBody = aResultBody;
155 aRBA.baseShape = aBaseShape;
156 aRBA.makeShape = aMakeShapeList;
157 aResultBaseAlgoList.push_back(aRBA);
158 aResultShapesList.push_back(aShape);
161 if (aCommonVersion == THE_COMMON_VERSION_1) {
162 // merge hierarchies of compounds containing objects and tools
164 keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
168 for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
169 anObjectsIt != anObjects.End() && isOk;
172 GeomShapePtr anObject = *anObjectsIt;
173 GeomShapePtr aParent = anObjects.Parent(anObject);
176 GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
177 if (aShapeType == GeomAPI_Shape::COMPOUND) {
179 isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
180 anObjects, aParent, aTools.Objects(),
181 aResultIndex, aResultBaseAlgoList, aResultShapesList,
184 else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
185 // Compsolid handling
186 isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
187 anObjects, aParent, aTools.Objects(), ListOfShape(),
188 aResultIndex, aResultBaseAlgoList, aResultShapesList,
192 // process object as is
193 isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
194 anObject, aTools.Objects(), aPlanes,
195 aResultIndex, aResultBaseAlgoList, aResultShapesList,
200 GeomAPI_ShapeIterator aShapeIt(aResultCompound);
201 if (aShapeIt.more()) {
202 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
203 document()->createBody(data(), aResultIndex);
205 ListOfShape anObjectList = anObjects.Objects();
206 ListOfShape aToolsList = aTools.Objects();
207 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
212 setResult(aResultBody, aResultIndex++);
215 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
216 aRBA.resultBody = aResultBody;
217 aRBA.baseShape = anObjectList.front();
218 for (std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>::iterator aRBAIt = aResultBaseAlgoList.begin();
219 aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
220 aMakeShapeList->appendAlgo(aRBAIt->makeShape);
222 aRBA.makeShape = aMakeShapeList;
223 aResultBaseAlgoList.clear();
224 aResultBaseAlgoList.push_back(aRBA);
228 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
229 // result shape has been deleted, but in another it was modified or stayed.
230 if (!aResultCompound)
231 aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
232 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
236 // remove the rest results if there were produced in the previous pass
237 removeResults(aResultIndex);