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