Salome HOME
Issue #2596: Application errors when smash big face from small
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanSmash.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_BooleanSmash.h"
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeList.h>
30 #include <GeomAlgoAPI_PaveFiller.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAPI_ShapeExplorer.h>
33 #include <GeomAPI_ShapeIterator.h>
34
35 //==================================================================================================
36 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
37 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
38 {
39 }
40
41 //==================================================================================================
42 void FeaturesPlugin_BooleanSmash::initAttributes()
43 {
44   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
45   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
46 }
47
48 //==================================================================================================
49 void FeaturesPlugin_BooleanSmash::execute()
50 {
51   ListOfShape anObjects, aTools;
52   std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
53
54   // Getting objects.
55   AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
56   for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
57     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
58     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
59     if(!anObject.get()) {
60       return;
61     }
62     ResultPtr aContext = anObjectAttr->context();
63     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
64     if (aResCompSolidPtr.get())
65     {
66       std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
67
68       std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
69         anIt = aCompSolidsObjects.begin();
70       for (; anIt != aCompSolidsObjects.end(); anIt++) {
71         if (anIt->first->isEqual(aContextShape)) {
72           aCompSolidsObjects[anIt->first].push_back(anObject);
73           break;
74         }
75       }
76       if (anIt == aCompSolidsObjects.end()) {
77         aCompSolidsObjects[aContextShape].push_back(anObject);
78       }
79
80     } else {
81       anObjects.push_back(anObject);
82     }
83   }
84
85   // Getting tools.
86   AttributeSelectionListPtr aToolsSelList = selectionList(TOOL_LIST_ID());
87   for(int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
88     AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
89     GeomShapePtr aTool = aToolAttr->value();
90     if(!aTool.get()) {
91       return;
92     }
93     aTools.push_back(aTool);
94   }
95
96   int aResultIndex = 0;
97
98   if((anObjects.empty() && aCompSolidsObjects.empty())
99      || aTools.empty()) {
100     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
101     setError(aFeatureError);
102     return;
103   }
104
105   // List of original shapes for naming.
106   ListOfShape anOriginalShapes;
107   anOriginalShapes.insert(anOriginalShapes.end(), anObjects.begin(), anObjects.end());
108   anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
109
110   // Collecting all shapes which will be smashed.
111   ListOfShape aShapesToSmash;
112   aShapesToSmash.insert(aShapesToSmash.end(), anObjects.begin(), anObjects.end());
113
114   // Collecting solids from compsolids which will not be modified in
115   // boolean operation and will be added to result.
116   ListOfShape aShapesToAdd;
117   for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
118        anIt = aCompSolidsObjects.begin();
119        anIt != aCompSolidsObjects.end();
120        ++anIt)
121   {
122     std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
123     ListOfShape& aUsedInOperationSolids = anIt->second;
124     anOriginalShapes.push_back(aCompSolid);
125     aShapesToSmash.insert(aShapesToSmash.end(),
126                           aUsedInOperationSolids.begin(),
127                           aUsedInOperationSolids.end());
128
129     // Collect solids from compsolid which will not be modified in boolean operation.
130     for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
131          anExp.more();
132          anExp.next())
133     {
134       std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
135       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
136       for (; anIt != aUsedInOperationSolids.end(); anIt++) {
137         if (aSolidInCompSolid->isEqual(*anIt)) {
138           break;
139         }
140       }
141       if (anIt == aUsedInOperationSolids.end()) {
142         aShapesToAdd.push_back(aSolidInCompSolid);
143       }
144     }
145   }
146
147   GeomAlgoAPI_MakeShapeList aMakeShapeList;
148   GeomAPI_DataMapOfShapeShape aMapOfShapes;
149   if (!aShapesToAdd.empty()) {
150     // Cut objects with not used solids.
151     std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
152       new GeomAlgoAPI_Boolean(aShapesToSmash,
153                               aShapesToAdd,
154                               GeomAlgoAPI_Boolean::BOOL_CUT));
155
156     if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
157       aShapesToSmash.clear();
158       aShapesToSmash.push_back(anObjectsCutAlgo->shape());
159       aMakeShapeList.appendAlgo(anObjectsCutAlgo);
160       aMapOfShapes.merge(anObjectsCutAlgo->mapOfSubShapes());
161     }
162
163     // Cut tools with not used solids.
164     std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
165       new GeomAlgoAPI_Boolean(aTools,
166                               aShapesToAdd,
167                               GeomAlgoAPI_Boolean::BOOL_CUT));
168
169     if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
170       aTools.clear();
171       aTools.push_back(aToolsCutAlgo->shape());
172       aMakeShapeList.appendAlgo(aToolsCutAlgo);
173       aMapOfShapes.merge(aToolsCutAlgo->mapOfSubShapes());
174     }
175   }
176
177   // Cut objects with tools.
178   std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
179     new GeomAlgoAPI_Boolean(aShapesToSmash,
180                             aTools,
181                             GeomAlgoAPI_Boolean::BOOL_CUT));
182
183   // Checking that the algorithm worked properly.
184   if (!aBoolAlgo->isDone()) {
185     static const std::string aFeatureError = "Error: Boolean algorithm failed.";
186     setError(aFeatureError);
187     return;
188   }
189   if (aBoolAlgo->shape()->isNull()) {
190     static const std::string aShapeError = "Error: Resulting shape is Null.";
191     setError(aShapeError);
192     return;
193   }
194   if (!aBoolAlgo->isValid()) {
195     std::string aFeatureError = "Error: Resulting shape is not valid.";
196     setError(aFeatureError);
197     return;
198   }
199   aMakeShapeList.appendAlgo(aBoolAlgo);
200   aMapOfShapes.merge(aBoolAlgo->mapOfSubShapes());
201
202   // Put all (cut result, tools and not used solids) to PaveFiller.
203   GeomShapePtr aShape = aBoolAlgo->shape();
204   GeomAPI_ShapeIterator anIt(aShape);
205   if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
206     aShapesToAdd.push_back(aShape);
207   }
208   aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
209
210   if (aShapesToAdd.size() == 1) {
211     aShape = aShapesToAdd.front();
212   }
213   else {
214     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
215       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
216     if (!aFillerAlgo->isDone()) {
217       std::string aFeatureError = "Error: PaveFiller algorithm failed.";
218       setError(aFeatureError);
219       return;
220     }
221     if (aFillerAlgo->shape()->isNull()) {
222       static const std::string aShapeError = "Error: Resulting shape is Null.";
223       setError(aShapeError);
224       return;
225     }
226     if (!aFillerAlgo->isValid()) {
227       std::string aFeatureError = "Error: Resulting shape is not valid.";
228       setError(aFeatureError);
229       return;
230     }
231
232     aShape = aFillerAlgo->shape();
233     aMakeShapeList.appendAlgo(aFillerAlgo);
234     aMapOfShapes.merge(aFillerAlgo->mapOfSubShapes());
235   }
236
237   std::shared_ptr<GeomAPI_Shape> aFrontShape = anOriginalShapes.front();
238   anOriginalShapes.pop_front();
239   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
240   loadNamingDS(aResultBody,
241                aFrontShape,
242                anOriginalShapes,
243                aShape,
244                aMakeShapeList,
245                aMapOfShapes);
246
247   setResult(aResultBody, aResultIndex);
248   aResultIndex++;
249
250   // remove the rest results if there were produced in the previous pass
251   removeResults(aResultIndex);
252 }
253
254 //==================================================================================================
255 void FeaturesPlugin_BooleanSmash::loadNamingDS(ResultBodyPtr theResultBody,
256                                              const GeomShapePtr theBaseShape,
257                                              const ListOfShape& theTools,
258                                              const GeomShapePtr theResultShape,
259                                              GeomAlgoAPI_MakeShape& theMakeShape,
260                                              GeomAPI_DataMapOfShapeShape& theMapOfShapes)
261 {
262   //load result
263   if (theBaseShape->isEqual(theResultShape)) {
264     theResultBody->store(theResultShape, false);
265   } else if (theResultShape->isEqual(theTools.front())) {
266     theResultBody->store(theResultShape, false);
267   } else {
268     const int aModifyVTag = 1;
269     const int aModifyETag = 2;
270     const int aModifyFTag = 3;
271     const int aDeletedTag = 4;
272     /// sub solids will be placed at labels 5, 6, etc. if result is compound of solids
273     const int aSubsolidsTag = 5;
274
275     theResultBody->storeModified(theBaseShape, theResultShape, aSubsolidsTag);
276
277     const std::string aModVName = "Modified_Vertex";
278     const std::string aModEName = "Modified_Edge";
279     const std::string aModFName = "Modified_Face";
280
281     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::VERTEX,
282                                                aModifyVTag, aModVName, theMapOfShapes, false,
283                                                true, true);
284     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::EDGE,
285                                                aModifyETag, aModEName, theMapOfShapes, false,
286                                                true, true);
287     theResultBody->loadAndOrientModifiedShapes(&theMakeShape, theBaseShape, GeomAPI_Shape::FACE,
288                                                aModifyFTag, aModFName, theMapOfShapes, false,
289                                                true, true);
290
291     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
292                                      GeomAPI_Shape::VERTEX, aDeletedTag);
293     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
294                                      GeomAPI_Shape::EDGE, aDeletedTag);
295     theResultBody->loadDeletedShapes(&theMakeShape, theBaseShape,
296                                      GeomAPI_Shape::FACE, aDeletedTag);
297
298     for (ListOfShape::const_iterator anIter = theTools.begin(); anIter != theTools.end(); anIter++)
299     {
300       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX,
301                                                  aModifyVTag, aModVName, theMapOfShapes, false,
302                                                  true, true);
303
304       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE,
305                                                  aModifyETag, aModEName, theMapOfShapes, false,
306                                                  true, true);
307
308       theResultBody->loadAndOrientModifiedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE,
309                                                  aModifyFTag, aModFName, theMapOfShapes, false,
310                                                  true, true);
311
312       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::VERTEX, aDeletedTag);
313       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::EDGE, aDeletedTag);
314       theResultBody->loadDeletedShapes(&theMakeShape, *anIter, GeomAPI_Shape::FACE, aDeletedTag);
315     }
316   }
317 }