Salome HOME
60b3581b37a6cb6096f35a0e29c70faaa2756680
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.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_BooleanCommon.h"
21
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>
29
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>
40
41 //==================================================================================================
42 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
43 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
44 {
45 }
46
47 //==================================================================================================
48 void FeaturesPlugin_BooleanCommon::initAttributes()
49 {
50   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
51
52   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
53   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
54
55   initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
56 }
57
58 //==================================================================================================
59 void FeaturesPlugin_BooleanCommon::execute()
60 {
61   ListOfShape aPlanes;
62   GeomAPI_ShapeHierarchy anObjects, aTools;
63
64   bool isSimpleMode = false;
65
66   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
67   if (aCreationMethodAttr.get()
68       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
69     isSimpleMode = true;
70   }
71
72   // Getting objects.
73   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
74     return;
75   // Planes are not supported as objects of COMMON operation
76   aPlanes.clear();
77
78   // Getting tools.
79   if (!isSimpleMode &&
80       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
81     return;
82
83   if (anObjects.empty() || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
84     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
85     setError(aFeatureError);
86     return;
87   }
88
89   // version of COMMON feature
90   const std::string aCommonVersion = data()->version();
91
92   int aResultIndex = 0;
93   GeomShapePtr aResultCompound;
94
95   std::string anError;
96   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
97   std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
98   ListOfShape aResultShapesList;
99
100   if (isSimpleMode) {
101     GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
102     GeomShapePtr aShape = *anObjectsIt;
103     for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
104       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
105         new GeomAlgoAPI_Boolean(aShape,
106                                 *anObjectsIt,
107                                 GeomAlgoAPI_Tools::BOOL_COMMON));
108
109       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
110         setError(anError);
111         return;
112       }
113
114       aShape = aCommonAlgo->shape();
115       aMakeShapeList->appendAlgo(aCommonAlgo);
116     }
117
118     if (aCommonVersion == BOP_VERSION_9_4()) {
119       // merge hierarchies of compounds containing objects and tools
120       // and append the result of the FUSE operation
121       aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
122     }
123
124     GeomAPI_ShapeIterator aShapeIt(aShape);
125     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
126       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
127         document()->createBody(data(), aResultIndex);
128
129       ListOfShape anObjectList = anObjects.objects();
130       ListOfShape aToolsList;
131       ModelAPI_Tools::loadModifiedShapes(aResultBody,
132                                                anObjectList,
133                                                aToolsList,
134                                                aMakeShapeList,
135                                                aShape);
136       GeomShapePtr aBaseShape = anObjectList.front();
137       anObjectList.pop_front();
138       setResult(aResultBody, aResultIndex);
139       aResultIndex++;
140
141       aToolsList = anObjectList;
142       ModelAPI_Tools::ResultBaseAlgo aRBA;
143       aRBA.resultBody = aResultBody;
144       aRBA.baseShape = aBaseShape;
145       aRBA.makeShape = aMakeShapeList;
146       aResultBaseAlgoList.push_back(aRBA);
147       aResultShapesList.push_back(aShape);
148     }
149   } else {
150     if (aCommonVersion == BOP_VERSION_9_4()) {
151       // merge hierarchies of compounds containing objects and tools
152       aResultCompound =
153           keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
154     }
155
156     bool isOk = true;
157     for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
158          anObjectsIt != anObjects.end() && isOk;
159          ++anObjectsIt)
160     {
161       GeomShapePtr anObject = *anObjectsIt;
162       GeomShapePtr aParent = anObjects.parent(anObject);
163
164       if (aParent) {
165         GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
166         if (aShapeType == GeomAPI_Shape::COMPOUND) {
167           // Compound handling
168           isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
169                                  anObjects, aParent, aTools.objects(),
170                                  aResultIndex, aResultBaseAlgoList, aResultShapesList,
171                                  aResultCompound);
172         }
173         else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
174           // Compsolid handling
175           isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
176                                   anObjects, aParent, aTools.objects(), ListOfShape(),
177                                   aResultIndex, aResultBaseAlgoList, aResultShapesList,
178                                   aResultCompound);
179         }
180       } else {
181         // process object as is
182         isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
183                              anObject, aTools.objects(), aPlanes,
184                              aResultIndex, aResultBaseAlgoList, aResultShapesList,
185                              aResultCompound);
186       }
187     }
188
189     storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
190                 aMakeShapeList, aResultBaseAlgoList);
191   }
192
193   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
194   // result shape has been deleted, but in another it was modified or stayed.
195   if (!aResultCompound)
196     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
197   ModelAPI_Tools::loadDeletedShapes(aResultBaseAlgoList,
198                                           aTools.objects(),
199                                           aResultCompound);
200
201   // remove the rest results if there were produced in the previous pass
202   removeResults(aResultIndex);
203 }