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