Salome HOME
f24d6f78bb7d322edfbe041f6256b3b4806b23ff
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCut.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "FeaturesPlugin_BooleanCut.h"
21
22 #include "FeaturesPlugin_Tools.h"
23
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_Boolean.h>
29 #include <GeomAlgoAPI_CompoundBuilder.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_PaveFiller.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
34
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAPI_ShapeIterator.h>
37
38 //==================================================================================================
39 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
40 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
41 {
42 }
43
44 //==================================================================================================
45 void FeaturesPlugin_BooleanCut::initAttributes()
46 {
47   FeaturesPlugin_Boolean::initAttributes();
48   initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
49 }
50
51 //==================================================================================================
52 void FeaturesPlugin_BooleanCut::execute()
53 {
54   GeomAPI_ShapeHierarchy anObjects, aTools;
55   ListOfShape aPlanes;
56
57   // Getting objects and tools
58   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes) ||
59       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
60     return;
61
62   int aResultIndex = 0;
63
64   if(anObjects.empty() || aTools.empty()) {
65     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
66     setError(aFeatureError);
67     return;
68   }
69
70   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
71   ListOfShape aResultShapesList;
72   std::string anError;
73
74   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
75
76   GeomShapePtr aResultCompound;
77   if (data()->version() == BOP_VERSION_9_4()) {
78     // merge hierarchies of compounds containing objects and tools
79     aResultCompound =
80         keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
81   }
82
83   // For solids cut each object with all tools.
84   bool isOk = true;
85   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
86        anObjectsIt != anObjects.end() && isOk;
87        ++anObjectsIt) {
88     GeomShapePtr anObject = *anObjectsIt;
89     GeomShapePtr aParent = anObjects.parent(anObject);
90
91     if (aParent) {
92       GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
93       if (aShapeType == GeomAPI_Shape::COMPOUND) {
94         // Compound handling
95         isOk = processCompound(GeomAlgoAPI_Tools::BOOL_CUT,
96                                anObjects, aParent, aTools.objects(),
97                                aResultIndex, aResultBaseAlgoList, aResultShapesList,
98                                aResultCompound);
99       }
100       else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
101         // Compsolid handling
102         isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_CUT,
103                                 anObjects, aParent, aTools.objects(), ListOfShape(),
104                                 aResultIndex, aResultBaseAlgoList, aResultShapesList,
105                                 aResultCompound);
106       }
107     } else {
108       // process object as is
109       isOk = processObject(GeomAlgoAPI_Tools::BOOL_CUT,
110                            anObject, aTools.objects(), aPlanes,
111                            aResultIndex, aResultBaseAlgoList, aResultShapesList,
112                            aResultCompound);
113     }
114   }
115
116   storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
117               aMakeShapeList, aResultBaseAlgoList);
118
119   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
120   // result shape has been deleted, but in another it was modified or stayed.
121   if (!aResultCompound)
122     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
123   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
124                                           aTools.objects(),
125                                           aResultCompound);
126
127   // remove the rest results if there were produced in the previous pass
128   removeResults(aResultIndex);
129 }