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