Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanFuse.cpp
1 // Copyright (C) 2014-2019  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_BooleanFuse.h"
21
22 #include "FeaturesPlugin_Tools.h"
23
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeBoolean.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_AttributeSelectionList.h>
28 #include <ModelAPI_AttributeString.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Validator.h>
32
33 #include <GeomAlgoAPI_Boolean.h>
34 #include <GeomAlgoAPI_MakeShapeList.h>
35 #include <GeomAlgoAPI_PaveFiller.h>
36 #include <GeomAlgoAPI_ShapeBuilder.h>
37 #include <GeomAlgoAPI_ShapeTools.h>
38 #include <GeomAlgoAPI_Tools.h>
39 #include <GeomAlgoAPI_UnifySameDomain.h>
40
41 #include <GeomAPI_ShapeExplorer.h>
42 #include <GeomAPI_ShapeIterator.h>
43
44 //==================================================================================================
45 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
46 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
47 {
48 }
49
50 //==================================================================================================
51 void FeaturesPlugin_BooleanFuse::initAttributes()
52 {
53   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
54
55   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
56   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
57
58   data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
59
60   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
61   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
62
63   initVersion(THE_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
64 }
65
66 //==================================================================================================
67 void FeaturesPlugin_BooleanFuse::execute()
68 {
69   std::string anError;
70   ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
71   ListOfShape aPlanes;
72
73   bool isSimpleCreation = false;
74
75   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
76   if (aCreationMethodAttr.get()
77       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
78   {
79     isSimpleCreation = true;
80   }
81
82   // Getting objects.
83   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
84     return;
85
86   // Getting tools.
87   if (!isSimpleCreation &&
88       !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
89     return;
90
91   ListOfShape anObjects, aTools, anEdgesAndFaces;
92   // all objects except edges and faces
93   anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
94                                    GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
95   aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
96                                 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
97
98   if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
99     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
100     setError(aFeatureError);
101     return;
102   }
103
104   // version of FUSE feature
105   int aFuseVersion = version();
106
107   // Collecting all solids which will be fused.
108   ListOfShape aSolidsToFuse;
109   aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
110   aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
111
112   // Collecting solids from compsolids which will not be modified
113   // in boolean operation and will be added to result.
114   bool isProcessCompsolid = !isSimpleCreation || aFuseVersion >= THE_FUSE_VERSION_1;
115   ListOfShape aShapesToAdd;
116   for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
117        isProcessCompsolid && anObjectsIt != anObjectsHierarchy.End();
118        ++anObjectsIt) {
119     GeomShapePtr anObject = *anObjectsIt;
120     GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
121
122     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
123       // mark all subs of this parent as precessed to avoid handling twice
124       aParent = anObjectsHierarchy.Parent(anObject);
125
126       ListOfShape aUsed, aNotUsed;
127       anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
128       aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
129     }
130   }
131
132   ListOfShape anOriginalShapes = aSolidsToFuse;
133   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
134
135   // Cut edges and faces(if we have any) with solids.
136   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
137   GeomShapePtr aCuttedEdgesAndFaces;
138   if (!anEdgesAndFaces.empty()) {
139     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
140       anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
141     if (aCutAlgo->isDone()) {
142       aCuttedEdgesAndFaces = aCutAlgo->shape();
143       aMakeShapeList->appendAlgo(aCutAlgo);
144     }
145   }
146
147   if (aShapesToAdd.empty() || !aCuttedEdgesAndFaces) {
148     anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
149                             anEdgesAndFaces.end());
150   }
151
152   // If we have compsolids then cut with not used solids all others.
153   if (!aShapesToAdd.empty()) {
154     aSolidsToFuse.clear();
155     for (ListOfShape::iterator
156          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
157       ListOfShape aOneObjectList;
158       aOneObjectList.push_back(*anIt);
159       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
160         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
161
162       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
163         aSolidsToFuse.push_back(aCutAlgo->shape());
164         aMakeShapeList->appendAlgo(aCutAlgo);
165       }
166     }
167   }
168
169   if (!aSolidsToFuse.empty()) {
170     anObjects.clear();
171     anObjects.push_back(aSolidsToFuse.back());
172     aSolidsToFuse.pop_back();
173     aTools = aSolidsToFuse;
174   }
175
176   // Fuse all objects and all tools.
177   GeomShapePtr aShape;
178   if (anObjects.size() == 1 && aTools.empty()) {
179     aShape = anObjects.front();
180   } else if (anObjects.empty() && aTools.size() == 1) {
181     aShape = aTools.front();
182   } else if ((anObjects.size() + aTools.size()) > 1) {
183     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
184       aTools,
185       GeomAlgoAPI_Tools::BOOL_FUSE));
186
187     // Checking that the algorithm worked properly.
188     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
189       setError(anError);
190       return;
191     }
192
193     aShape = aFuseAlgo->shape();
194     aMakeShapeList->appendAlgo(aFuseAlgo);
195   }
196
197   // Combine result with not used solids from compsolid and edges and faces (if we have any).
198   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
199     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
200   } else {
201     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
202   }
203   if (!aShapesToAdd.empty()) {
204     if (aShape.get()) {
205       aShapesToAdd.push_back(aShape);
206     }
207     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
208       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
209     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
210       setError(anError);
211       return;
212     }
213
214     aShape = aFillerAlgo->shape();
215     aMakeShapeList->appendAlgo(aFillerAlgo);
216   }
217
218   bool isRemoveEdges = false;
219   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
220   if (removeEdgesAttr.get()) {
221     isRemoveEdges = removeEdgesAttr->value();
222   }
223
224   if (isRemoveEdges) {
225     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
226       new GeomAlgoAPI_UnifySameDomain(aShape));
227
228     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
229       setError(anError);
230       return;
231     }
232
233     aShape = aUnifyAlgo->shape();
234     aMakeShapeList->appendAlgo(aUnifyAlgo);
235   }
236
237   if (aFuseVersion == THE_VERSION_1) {
238     // merge hierarchies of compounds containing objects and tools
239     // and append the result of the FUSE operation
240     aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
241   }
242
243   int aResultIndex = 0;
244
245   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
246
247   ListOfShape anEmptyTools;
248   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
249                                            anOriginalShapes,
250                                            anEmptyTools,
251                                            aMakeShapeList,
252                                            aShape);
253   setResult(aResultBody, aResultIndex);
254   aResultIndex++;
255
256   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
257                                           GeomShapePtr(),
258                                           anOriginalShapes,
259                                           aMakeShapeList,
260                                           aShape);
261
262   // remove the rest results if there were produced in the previous pass
263   removeResults(aResultIndex);
264 }