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