Salome HOME
Refactor Boolean operations in FeaturesPlugin.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanSmash.cpp
1 // Copyright (C) 2014-2019  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_BooleanSmash.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_BooleanSmash::FeaturesPlugin_BooleanSmash()
40 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
41 {
42 }
43
44 //==================================================================================================
45 void FeaturesPlugin_BooleanSmash::initAttributes()
46 {
47   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
48   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
49 }
50
51 //==================================================================================================
52 void FeaturesPlugin_BooleanSmash::execute()
53 {
54   std::string anError;
55   ListOfShape anObjects, aTools;
56   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
57
58   // Getting objects.
59   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
60   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
61     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
62     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
63     if(!anObject.get()) {
64       return;
65     }
66     ResultPtr aContext = anObjectAttr->context();
67     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
68     if (aResCompSolidPtr.get())
69     {
70       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
71
72       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
73         anIt = aCompSolidsObjects.begin();
74       for (; anIt != aCompSolidsObjects.end(); anIt++) {
75         if (anIt->first->isEqual(aContextShape)) {
76           aCompSolidsObjects[anIt->first].push_back(anObject);
77           break;
78         }
79       }
80       if (anIt == aCompSolidsObjects.end()) {
81         aCompSolidsObjects[aContextShape].push_back(anObject);
82       }
83
84     } else {
85       anObjects.push_back(anObject);
86     }
87   }
88
89   // Getting tools.
90   AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
91   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
92     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
93     GeomShapePtr aTool = aToolAttr->value();
94     if(!aTool.get()) {
95       return;
96     }
97     aTools.push_back(aTool);
98   }
99
100   int aResultIndex = 0;
101
102   if((anObjects.empty() && aCompSolidsObjects.empty())
103      || aTools.empty()) {
104     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
105     setError(aFeatureError);
106     return;
107   }
108
109   // List of original shapes for naming.
110   ListOfShape anOriginalShapes;
111   anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
112   anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
113
114   // Collecting all shapes which will be smashed.
115   ListOfShape aShapesToSmash;
116   aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
117
118   // Collecting solids from compsolids which will not be modified in
119   // boolean operation and will be added to result.
120   ListOfShape aShapesToAdd;
121   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
122        anIt = aCompSolidsObjects.begin();
123        anIt != aCompSolidsObjects.end();
124        ++anIt)
125   {
126     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
127     ListOfShape& aUsedInOperationSolids = anIt->second;
128     anOriginalShapes.push_back(aCompSolid);
129     aShapesToSmash.insert(aShapesToSmash.end(),
130                           aUsedInOperationSolids.begin(),
131                           aUsedInOperationSolids.end());
132
133     // Collect solids from compsolid which will not be modified in boolean operation.
134     for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
135          anExp.more();
136          anExp.next())
137     {
138       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
139       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
140       for (; anIt != aUsedInOperationSolids.end(); anIt++) {
141         if (aSolidInCompSolid->isEqual(*anIt)) {
142           break;
143         }
144       }
145       if (anIt == aUsedInOperationSolids.end()) {
146         aShapesToAdd.push_back(aSolidInCompSolid);
147       }
148     }
149   }
150
151   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
152   if (!aShapesToAdd.empty()) {
153     // Cut objects with not used solids.
154     std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
155       new GeomAlgoAPI_Boolean(aShapesToSmash,
156                               aShapesToAdd,
157                               GeomAlgoAPI_Tools::BOOL_CUT));
158
159     if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
160       aShapesToSmash.clear();
161       aShapesToSmash.push_back(anObjectsCutAlgo->shape());
162       aMakeShapeList->appendAlgo(anObjectsCutAlgo);
163     }
164
165     // Cut tools with not used solids.
166     std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
167       new GeomAlgoAPI_Boolean(aTools,
168                               aShapesToAdd,
169                               GeomAlgoAPI_Tools::BOOL_CUT));
170
171     if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
172       aTools.clear();
173       aTools.push_back(aToolsCutAlgo->shape());
174       aMakeShapeList->appendAlgo(aToolsCutAlgo);
175     }
176   }
177
178   // Cut objects with tools.
179   std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
180     new GeomAlgoAPI_Boolean(aShapesToSmash,
181                             aTools,
182                             GeomAlgoAPI_Tools::BOOL_CUT));
183
184   // Checking that the algorithm worked properly.
185   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
186     setError(anError);
187     return;
188   }
189
190   aMakeShapeList->appendAlgo(aBoolAlgo);
191
192   // Put all (cut result, tools and not used solids) to PaveFiller.
193   GeomShapePtr aShape = aBoolAlgo->shape();
194   GeomAPI_ShapeIterator anIt(aShape);
195   if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
196     aShapesToAdd.push_back(aShape);
197   }
198   aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
199
200   if (aShapesToAdd.size() == 1) {
201     aShape = aShapesToAdd.front();
202   }
203   else {
204     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
205       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
206     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
207       setError(anError);
208       return;
209     }
210
211     aShape = aFillerAlgo->shape();
212     aMakeShapeList->appendAlgo(aFillerAlgo);
213   }
214
215   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
216
217   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
218                                            anOriginalShapes,
219                                            anOriginalShapes,
220                                            aMakeShapeList,
221                                            aShape);
222
223   setResult(aResultBody, aResultIndex);
224   aResultIndex++;
225
226   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
227                                           GeomShapePtr(),
228                                           anOriginalShapes,
229                                           aMakeShapeList,
230                                           aShape);
231
232   // remove the rest results if there were produced in the previous pass
233   removeResults(aResultIndex);
234 }