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