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