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 "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_MakeShapeCustom.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAlgoAPI_PaveFiller.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
38 #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());
56 //==================================================================================================
57 void FeaturesPlugin_BooleanCommon::execute()
59 ListOfShape anObjects, aTools, aPlanes;
60 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
61 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
63 bool isSimpleMode = false;
65 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
66 if (aCreationMethodAttr.get()
67 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
72 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
73 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
74 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
75 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
76 if (!anObject.get()) {
79 ResultPtr aContext = anObjectAttr->context();
80 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
82 && aResCompSolidPtr.get())
84 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
85 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
86 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
87 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
89 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
91 for (; anIt != aMap.end(); anIt++) {
92 if (anIt->first->isEqual(aContextShape)) {
93 aMap[anIt->first].push_back(anObject);
97 if (anIt == aMap.end()) {
98 aMap[aContextShape].push_back(anObject);
103 anObjects.push_back(anObject);
109 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
110 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
111 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
112 GeomShapePtr aTool = aToolAttr->value();
114 // It could be a construction plane.
115 ResultPtr aContext = aToolAttr->context();
116 aPlanes.push_back(aToolAttr->context()->shape());
118 aTools.push_back(aTool);
123 if ((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
124 || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
125 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
126 setError(aFeatureError);
130 int aResultIndex = 0;
132 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
133 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
134 ListOfShape aResultShapesList;
138 ListOfShape::iterator anObjectsIt = anObjects.begin();
139 GeomShapePtr aShape = *anObjectsIt;
140 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
141 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
142 new GeomAlgoAPI_Boolean(aShape,
144 GeomAlgoAPI_Boolean::BOOL_COMMON));
146 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
151 aShape = aCommonAlgo->shape();
152 aMakeShapeList->appendAlgo(aCommonAlgo);
155 GeomAPI_ShapeIterator aShapeIt(aShape);
156 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
157 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
158 document()->createBody(data(), aResultIndex);
160 GeomShapePtr aBaseShape = anObjects.front();
161 anObjects.pop_front();
162 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
167 setResult(aResultBody, aResultIndex);
171 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
172 aRBA.resultBody = aResultBody;
173 aRBA.baseShape = aBaseShape;
174 aRBA.makeShape = aMakeShapeList;
175 aResultBaseAlgoList.push_back(aRBA);
176 aResultShapesList.push_back(aShape);
179 for (ListOfShape::iterator anObjectsIt = anObjects.begin();
180 anObjectsIt != anObjects.end();
183 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
184 ListOfShape aListWithObject;
185 aListWithObject.push_back(anObject);
186 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
187 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
188 GeomShapePtr aResShape;
190 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
191 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
194 ListOfShape aToolsWithPlanes = aTools;
195 for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
196 anIt != aPlanes.cend();
198 GeomShapePtr aPlane = *anIt;
199 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
200 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
201 new GeomAlgoAPI_MakeShapeCustom);
202 aMkShCustom->addModified(aPlane, aTool);
203 aMakeShapeList->appendAlgo(aMkShCustom);
204 aToolsWithPlanes.push_back(aTool);
207 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
209 GeomAlgoAPI_Boolean::BOOL_COMMON));
210 aResShape = aBoolAlgo->shape();
212 // Checking that the algorithm worked properly.
213 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
218 aMakeShapeList->appendAlgo(aBoolAlgo);
220 GeomAPI_ShapeIterator aShapeIt(aResShape);
221 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
222 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
223 document()->createBody(data(), aResultIndex);
225 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
230 setResult(aResultBody, aResultIndex);
233 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
234 aRBA.resultBody = aResultBody;
235 aRBA.baseShape = anObject;
236 aRBA.makeShape = aMakeShapeList;
237 aResultBaseAlgoList.push_back(aRBA);
238 aResultShapesList.push_back(aResShape);
242 // Compsolids handling
243 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
244 anIt = aCompSolidsObjects.begin();
245 anIt != aCompSolidsObjects.end();
248 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
249 ListOfShape& aUsedInOperationSolids = anIt->second;
251 // Collecting solids from compsolids which will not be modified in boolean operation.
252 ListOfShape aNotUsedSolids;
253 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
257 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
258 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
259 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
260 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
264 if (aUsedIt == aUsedInOperationSolids.end()) {
265 aNotUsedSolids.push_back(aSolidInCompSolid);
269 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
270 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
271 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
273 GeomAlgoAPI_Boolean::BOOL_COMMON));
275 // Checking that the algorithm worked properly.
276 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
281 aMakeShapeList->appendAlgo(aCommonAlgo);
282 GeomShapePtr aResultShape = aCommonAlgo->shape();
284 // Add result to not used solids from compsolid.
285 if (!aNotUsedSolids.empty()) {
286 ListOfShape aShapesToAdd = aNotUsedSolids;
287 aShapesToAdd.push_back(aCommonAlgo->shape());
288 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
289 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
290 if (!aFillerAlgo->isDone()) {
291 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
292 setError(aFeatureError);
296 aMakeShapeList->appendAlgo(aFillerAlgo);
297 aResultShape = aFillerAlgo->shape();
300 GeomAPI_ShapeIterator aShapeIt(aResultShape);
301 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
303 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
304 document()->createBody(data(), aResultIndex);
306 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
311 setResult(aResultBody, aResultIndex);
314 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
315 aRBA.resultBody = aResultBody;
316 aRBA.baseShape = aCompSolid;
317 aRBA.makeShape = aMakeShapeList;
318 aResultBaseAlgoList.push_back(aRBA);
319 aResultShapesList.push_back(aResultShape);
323 // Compounds handling
324 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
325 anIt = aCompoundObjects.begin();
326 anIt != aCompoundObjects.end();
329 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
330 ListOfShape& aUsedInOperationShapes = anIt->second;
332 // Collecting shapes from compound which will not be modified in boolean operation.
333 ListOfShape aNotUsedShapes;
334 for (GeomAPI_ShapeIterator aCompIt(aCompound);
338 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
339 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
340 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
341 if (aShapeInCompound->isEqual(*aUsedIt)) {
345 if (aUsedIt == aUsedInOperationShapes.end()) {
346 aNotUsedShapes.push_back(aShapeInCompound);
350 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
351 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
352 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
354 GeomAlgoAPI_Boolean::BOOL_COMMON));
356 // Checking that the algorithm worked properly.
357 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
362 aMakeShapeList->appendAlgo(aCommonAlgo);
363 GeomShapePtr aResultShape = aCommonAlgo->shape();
365 // Add result to not used shape from compound.
366 if (!aNotUsedShapes.empty()) {
367 ListOfShape aShapesForResult = aNotUsedShapes;
368 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
369 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
370 aShapesForResult.push_back(aResultIt.current());
374 aShapesForResult.push_back(aResultShape);
377 if (aShapesForResult.size() == 1) {
378 aResultShape = aShapesForResult.front();
381 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
385 GeomAPI_ShapeIterator aShapeIt(aResultShape);
386 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
387 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
388 document()->createBody(data(), aResultIndex);
390 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
395 setResult(aResultBody, aResultIndex);
398 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
399 aRBA.resultBody = aResultBody;
400 aRBA.baseShape = aCompound;
401 aRBA.makeShape = aMakeShapeList;
402 aResultBaseAlgoList.push_back(aRBA);
403 aResultShapesList.push_back(aResultShape);
409 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
410 // result shape has been deleted, but in another it was modified or stayed.
411 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
412 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
414 // remove the rest results if there were produced in the previous pass
415 removeResults(aResultIndex);