Salome HOME
Refactoring: split base class of Boolean operation (separate methods related to versi...
[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 static const int THE_SMASH_VERSION_1 = 20190506;
39
40 //==================================================================================================
41 FeaturesPlugin_BooleanSmash::FeaturesPlugin_BooleanSmash()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_SMASH)
43 {
44 }
45
46 //==================================================================================================
47 void FeaturesPlugin_BooleanSmash::initAttributes()
48 {
49   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
50   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
51
52   initVersion(THE_SMASH_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
53 }
54
55 //==================================================================================================
56 void FeaturesPlugin_BooleanSmash::execute()
57 {
58   std::string anError;
59   ObjectHierarchy anObjectsHistory, aToolsHistory;
60   ListOfShape aPlanes;
61
62   // Getting objects and tools.
63   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHistory, aPlanes) ||
64       !processAttribute(TOOL_LIST_ID(), aToolsHistory, aPlanes))
65     return;
66
67   int aResultIndex = 0;
68
69   if (anObjectsHistory.IsEmpty() || aToolsHistory.IsEmpty()) {
70     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
71     setError(aFeatureError);
72     return;
73   }
74
75   // Collecting all shapes which will be smashed.
76   ListOfShape aShapesToSmash = anObjectsHistory.Objects();
77
78   // List of original shapes for naming.
79   ListOfShape anOriginalShapes;
80   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToSmash.begin(), aShapesToSmash.end());
81   ListOfShape aTools = aToolsHistory.Objects();
82   anOriginalShapes.insert(anOriginalShapes.end(), aTools.begin(), aTools.end());
83
84   // Collecting solids from compsolids which will not be modified in
85   // boolean operation and will be added to result.
86   ListOfShape aShapesToAdd;
87   for (ObjectHierarchy::Iterator anIt = anObjectsHistory.Begin();
88        anIt != anObjectsHistory.End();
89        ++anIt)
90   {
91     GeomShapePtr aParent = anObjectsHistory.Parent(*anIt, false);
92     if (aParent) {
93       anOriginalShapes.push_back(aParent);
94
95       ListOfShape aUsed, aNotUsed;
96       anObjectsHistory.SplitCompound(aParent, aUsed, aNotUsed);
97       aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
98
99       // add unused shapes of compounds/compsolids to the history,
100       // to avoid treating them as unused later when constructing a compound containing
101       // the result of Smash and all unused sub-shapes of multi-level compounds
102       for (ListOfShape::iterator anIt = aNotUsed.begin(); anIt != aNotUsed.end(); ++anIt)
103         anObjectsHistory.AddObject(*anIt);
104     }
105   }
106
107   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
108   if (!aShapesToAdd.empty()) {
109     // Cut objects with not used solids.
110     std::shared_ptr<GeomAlgoAPI_Boolean> anObjectsCutAlgo(
111       new GeomAlgoAPI_Boolean(aShapesToSmash,
112                               aShapesToAdd,
113                               GeomAlgoAPI_Tools::BOOL_CUT));
114
115     if (GeomAlgoAPI_ShapeTools::volume(anObjectsCutAlgo->shape()) > 1.e-27) {
116       aShapesToSmash.clear();
117       aShapesToSmash.push_back(anObjectsCutAlgo->shape());
118       aMakeShapeList->appendAlgo(anObjectsCutAlgo);
119     }
120
121     // Cut tools with not used solids.
122     std::shared_ptr<GeomAlgoAPI_Boolean> aToolsCutAlgo(
123       new GeomAlgoAPI_Boolean(aTools,
124                               aShapesToAdd,
125                               GeomAlgoAPI_Tools::BOOL_CUT));
126
127     if (GeomAlgoAPI_ShapeTools::volume(aToolsCutAlgo->shape()) > 1.e-27) {
128       aTools.clear();
129       aTools.push_back(aToolsCutAlgo->shape());
130       aMakeShapeList->appendAlgo(aToolsCutAlgo);
131     }
132   }
133
134   // Cut objects with tools.
135   std::shared_ptr<GeomAlgoAPI_Boolean> aBoolAlgo(
136     new GeomAlgoAPI_Boolean(aShapesToSmash,
137                             aTools,
138                             GeomAlgoAPI_Tools::BOOL_CUT));
139
140   // Checking that the algorithm worked properly.
141   if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
142     setError(anError);
143     return;
144   }
145
146   aMakeShapeList->appendAlgo(aBoolAlgo);
147
148   // Put all (cut result, tools and not used solids) to PaveFiller.
149   GeomShapePtr aShape = aBoolAlgo->shape();
150   GeomAPI_ShapeIterator anIt(aShape);
151   if (anIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
152     aShapesToAdd.push_back(aShape);
153   }
154   aShapesToAdd.insert(aShapesToAdd.end(), aTools.begin(), aTools.end());
155
156   if (aShapesToAdd.size() == 1) {
157     aShape = aShapesToAdd.front();
158   }
159   else {
160     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
161       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
162     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
163       setError(anError);
164       return;
165     }
166
167     aShape = aFillerAlgo->shape();
168     aMakeShapeList->appendAlgo(aFillerAlgo);
169   }
170
171   // take into account a version of SMASH feature
172   int aSmashVersion = version();
173   if (aSmashVersion == THE_SMASH_VERSION_1) {
174     // merge hierarchies of compounds containing objects and tools
175     // and append the result of the FUSE operation
176     aShape = keepUnusedSubsOfCompound(aShape, anObjectsHistory, aToolsHistory, aMakeShapeList);
177   }
178
179   std::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data(), aResultIndex);
180
181   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
182                                            anOriginalShapes,
183                                            anOriginalShapes,
184                                            aMakeShapeList,
185                                            aShape);
186
187   setResult(aResultBody, aResultIndex);
188   aResultIndex++;
189
190   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
191                                           GeomShapePtr(),
192                                           anOriginalShapes,
193                                           aMakeShapeList,
194                                           aShape);
195
196   // remove the rest results if there were produced in the previous pass
197   removeResults(aResultIndex);
198 }