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