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