Salome HOME
support fuzzy parameter in all boolean operations
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.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_BooleanCommon.h"
21
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeDouble.h>
24 #include <ModelAPI_AttributeInteger.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_Validator.h>
30
31 #include <GeomAlgoAPI_Boolean.h>
32 #include <GeomAlgoAPI_MakeShapeCustom.h>
33 #include <GeomAlgoAPI_MakeShapeList.h>
34 #include <GeomAlgoAPI_ShapeTools.h>
35 #include <GeomAPI_Face.h>
36 #include <GeomAPI_ShapeIterator.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAlgoAPI_PaveFiller.h>
39 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <GeomAlgoAPI_Tools.h>
41
42
43 //==================================================================================================
44 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
45 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
46 {
47 }
48
49 //==================================================================================================
50 void FeaturesPlugin_BooleanCommon::initAttributes()
51 {
52   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
53
54   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
55   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
56
57   data()->addAttribute(FUZZY_PARAM_ID(), ModelAPI_AttributeDouble::typeId());
58   
59   // Initialize the fuzzy parameter with a value below Precision::Confusion() to indicate,
60   // that the internal algorithms should use their default fuzzy value, if none was specified
61   // by the user.
62   real(FUZZY_PARAM_ID())->setValue(1.e-8);
63
64   initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
65 }
66
67 //==================================================================================================
68 void FeaturesPlugin_BooleanCommon::execute()
69 {
70   ListOfShape aPlanes;
71   GeomAPI_ShapeHierarchy anObjects, aTools;
72
73   bool isSimpleMode = false;
74
75   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
76   if (aCreationMethodAttr.get()
77       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
78     isSimpleMode = true;
79   }
80
81   // Getting objects.
82   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
83     return;
84   // Planes are not supported as objects of COMMON operation
85   aPlanes.clear();
86
87   // Getting tools.
88   if (!isSimpleMode &&
89       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
90     return;
91
92   if (anObjects.empty() || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
93     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
94     setError(aFeatureError);
95     return;
96   }
97
98   // Getting fuzzy parameter.
99   // Used as additional tolerance to eliminate tiny results.
100   double aFuzzy = real(FUZZY_PARAM_ID())->value();
101
102   // version of COMMON feature
103   const std::string aCommonVersion = data()->version();
104
105   int aResultIndex = 0;
106   GeomShapePtr aResultCompound;
107
108   std::string anError;
109   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
110   std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
111   ListOfShape aResultShapesList;
112
113   if (isSimpleMode) {
114     GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
115     GeomShapePtr aShape = *anObjectsIt;
116     for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
117       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
118         new GeomAlgoAPI_Boolean(aShape,
119                                 *anObjectsIt,
120                                 GeomAlgoAPI_Tools::BOOL_COMMON,
121                                 aFuzzy));
122
123       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
124         setError(anError);
125         return;
126       }
127
128       aShape = aCommonAlgo->shape();
129       aMakeShapeList->appendAlgo(aCommonAlgo);
130     }
131
132     if (aCommonVersion == BOP_VERSION_9_4()) {
133       // merge hierarchies of compounds containing objects and tools
134       // and append the result of the FUSE operation
135       aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
136     }
137
138     GeomAPI_ShapeIterator aShapeIt(aShape);
139     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
140       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
141         document()->createBody(data(), aResultIndex);
142
143       ListOfShape anObjectList = anObjects.objects();
144       ListOfShape aToolsList;
145       ModelAPI_Tools::loadModifiedShapes(aResultBody,
146                                          anObjectList,
147                                          aToolsList,
148                                          aMakeShapeList,
149                                          aShape);
150       GeomShapePtr aBaseShape = anObjectList.front();
151       anObjectList.pop_front();
152       setResult(aResultBody, aResultIndex);
153       aResultIndex++;
154
155       aToolsList = anObjectList;
156       ModelAPI_Tools::ResultBaseAlgo aRBA;
157       aRBA.resultBody = aResultBody;
158       aRBA.baseShape = aBaseShape;
159       aRBA.makeShape = aMakeShapeList;
160       aResultBaseAlgoList.push_back(aRBA);
161       aResultShapesList.push_back(aShape);
162     }
163   } else {
164     if (aCommonVersion == BOP_VERSION_9_4()) {
165       // merge hierarchies of compounds containing objects and tools
166       aResultCompound =
167           keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
168     }
169
170     bool isOk = true;
171     for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
172          anObjectsIt != anObjects.end() && isOk;
173          ++anObjectsIt)
174     {
175       GeomShapePtr anObject = *anObjectsIt;
176       GeomShapePtr aParent = anObjects.parent(anObject);
177
178       if (aParent) {
179         GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
180         if (aShapeType == GeomAPI_Shape::COMPOUND) {
181           // Compound handling
182           isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
183                                  anObjects, aParent, aTools.objects(),
184                                  aFuzzy,
185                                  aResultIndex, aResultBaseAlgoList, aResultShapesList,
186                                  aResultCompound);
187         }
188         else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
189           // Compsolid handling
190           isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
191                                   anObjects, aParent, aTools.objects(), ListOfShape(),
192                                   aFuzzy,
193                                   aResultIndex, aResultBaseAlgoList, aResultShapesList,
194                                   aResultCompound);
195         }
196       } else {
197         // process object as is
198         isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
199                              anObject, aTools.objects(), aPlanes,
200                              aFuzzy,
201                              aResultIndex, aResultBaseAlgoList, aResultShapesList,
202                              aResultCompound);
203       }
204     }
205
206     storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
207                 aMakeShapeList, aResultBaseAlgoList);
208   }
209
210   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
211   // result shape has been deleted, but in another it was modified or stayed.
212   if (!aResultCompound)
213     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
214   ModelAPI_Tools::loadDeletedShapes(aResultBaseAlgoList,
215                                     aTools.objects(),
216                                     aResultCompound);
217
218   // remove the rest results if there were produced in the previous pass
219   removeResults(aResultIndex);
220 }