Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[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_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_MakeShapeList.h>
34 #include <GeomAlgoAPI_PaveFiller.h>
35 #include <GeomAlgoAPI_ShapeTools.h>
36 #include <GeomAlgoAPI_Tools.h>
37 #include <GeomAlgoAPI_UnifySameDomain.h>
38 #include <GeomAPI_ShapeExplorer.h>
39
40 //==================================================================================================
41 FeaturesPlugin_BooleanFuse::FeaturesPlugin_BooleanFuse()
42 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FUSE)
43 {
44 }
45
46 //==================================================================================================
47 void FeaturesPlugin_BooleanFuse::initAttributes()
48 {
49   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
50
51   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
52   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
53
54   data()->addAttribute(REMOVE_INTERSECTION_EDGES_ID(), ModelAPI_AttributeBoolean::typeId());
55
56   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), OBJECT_LIST_ID());
57   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), TOOL_LIST_ID());
58 }
59
60 //==================================================================================================
61 void FeaturesPlugin_BooleanFuse::execute()
62 {
63   std::string anError;
64   ListOfShape anObjects, aTools, anEdgesAndFaces;
65   std::map<GeomShapePtr, ListOfShape> aCompSolidsObjects;
66
67   bool isSimpleCreation = false;
68
69   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
70   if (aCreationMethodAttr.get()
71       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
72   {
73     isSimpleCreation = true;
74   }
75
76   // Getting objects.
77   AttributeSelectionListPtr anObjectsSelList =
78     selectionList(FeaturesPlugin_Boolean::OBJECT_LIST_ID());
79   for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
80     AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
81     GeomShapePtr anObject = anObjectAttr->value();
82     if (!anObject.get()) {
83       return;
84     }
85     ResultPtr aContext = anObjectAttr->context();
86     ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
87     if (!isSimpleCreation
88         && aResCompSolidPtr.get()
89         && aResCompSolidPtr->shape()->shapeType() == GeomAPI_Shape::COMPSOLID)
90     {
91       GeomShapePtr aContextShape = aResCompSolidPtr->shape();
92       std::map<GeomShapePtr, ListOfShape>::iterator
93         anIt = aCompSolidsObjects.begin();
94       for (; anIt != aCompSolidsObjects.end(); anIt++) {
95         if (anIt->first->isEqual(aContextShape)) {
96           aCompSolidsObjects[anIt->first].push_back(anObject);
97           break;
98         }
99       }
100       if (anIt == aCompSolidsObjects.end()) {
101         aCompSolidsObjects[aContextShape].push_back(anObject);
102       }
103     } else {
104       if (anObject->shapeType() == GeomAPI_Shape::EDGE
105           || anObject->shapeType() == GeomAPI_Shape::FACE) {
106         anEdgesAndFaces.push_back(anObject);
107       } else {
108         anObjects.push_back(anObject);
109       }
110     }
111   }
112
113   // Getting tools.
114   if (!isSimpleCreation) {
115     AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
116     for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
117       AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
118       GeomShapePtr aTool = aToolAttr->value();
119       if (aTool->shapeType() == GeomAPI_Shape::EDGE
120           || aTool->shapeType() == GeomAPI_Shape::FACE)
121       {
122         anEdgesAndFaces.push_back(aTool);
123       } else {
124         aTools.push_back(aTool);
125       }
126     }
127   }
128
129   if ((anObjects.size() + aTools.size() +
130     aCompSolidsObjects.size() + anEdgesAndFaces.size()) < 2) {
131     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
132     setError(aFeatureError);
133     return;
134   }
135
136   // Collecting all solids which will be fused.
137   ListOfShape aSolidsToFuse;
138   aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
139   aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
140
141   // Collecting solids from compsolids which will not be modified
142   // in boolean operation and will be added to result.
143   ListOfShape aShapesToAdd;
144   for (std::map<GeomShapePtr, ListOfShape>::iterator anIt = aCompSolidsObjects.begin();
145        anIt != aCompSolidsObjects.end();
146        ++anIt)
147   {
148     GeomShapePtr aCompSolid = anIt->first;
149     ListOfShape& aUsedInOperationSolids = anIt->second;
150     aSolidsToFuse.insert(aSolidsToFuse.end(), aUsedInOperationSolids.begin(),
151                          aUsedInOperationSolids.end());
152
153     // Collect solids from compsolid which will not be modified in boolean operation.
154     for (GeomAPI_ShapeExplorer
155          anExp(aCompSolid, GeomAPI_Shape::SOLID); anExp.more(); anExp.next()) {
156       GeomShapePtr aSolidInCompSolid = anExp.current();
157       ListOfShape::iterator anIt = aUsedInOperationSolids.begin();
158       for (; anIt != aUsedInOperationSolids.end(); anIt++) {
159         if (aSolidInCompSolid->isEqual(*anIt)) {
160           break;
161         }
162       }
163       if (anIt == aUsedInOperationSolids.end()) {
164         aShapesToAdd.push_back(aSolidInCompSolid);
165       }
166     }
167   }
168
169   ListOfShape anOriginalShapes = aSolidsToFuse;
170   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
171
172   // Cut edges and faces(if we have any) with solids.
173   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
174   GeomShapePtr aCuttedEdgesAndFaces;
175   if (!anEdgesAndFaces.empty()) {
176     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
177       anOriginalShapes, GeomAlgoAPI_Boolean::BOOL_CUT));
178     if (aCutAlgo->isDone()) {
179       aCuttedEdgesAndFaces = aCutAlgo->shape();
180       aMakeShapeList->appendAlgo(aCutAlgo);
181     }
182   }
183   anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
184                           anEdgesAndFaces.end());
185
186   // If we have compsolids then cut with not used solids all others.
187   if (!aShapesToAdd.empty()) {
188     aSolidsToFuse.clear();
189     for (ListOfShape::iterator
190          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
191       ListOfShape aOneObjectList;
192       aOneObjectList.push_back(*anIt);
193       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
194         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Boolean::BOOL_CUT));
195
196       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
197         aSolidsToFuse.push_back(aCutAlgo->shape());
198         aMakeShapeList->appendAlgo(aCutAlgo);
199       }
200     }
201   }
202
203   if (!aSolidsToFuse.empty()) {
204     anObjects.clear();
205     anObjects.push_back(aSolidsToFuse.back());
206     aSolidsToFuse.pop_back();
207     aTools = aSolidsToFuse;
208   }
209
210   // Fuse all objects and all tools.
211   GeomShapePtr aShape;
212   if (anObjects.size() == 1 && aTools.empty()) {
213     aShape = anObjects.front();
214   } else if (anObjects.empty() && aTools.size() == 1) {
215     aShape = aTools.front();
216   } else if ((anObjects.size() + aTools.size()) > 1) {
217     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
218       aTools,
219       GeomAlgoAPI_Boolean::BOOL_FUSE));
220
221     // Checking that the algorithm worked properly.
222     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
223       setError(anError);
224       return;
225     }
226
227     aShape = aFuseAlgo->shape();
228     aMakeShapeList->appendAlgo(aFuseAlgo);
229   }
230
231   // Combine result with not used solids from compsolid and edges and faces (if we have any).
232   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
233     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
234   } else {
235     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
236   }
237   if (!aShapesToAdd.empty()) {
238     if (aShape.get()) {
239       aShapesToAdd.push_back(aShape);
240     }
241     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
242       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
243     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
244       setError(anError);
245       return;
246     }
247
248     aShape = aFillerAlgo->shape();
249     aMakeShapeList->appendAlgo(aFillerAlgo);
250   }
251
252   bool isRemoveEdges = false;
253   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
254   if (removeEdgesAttr.get()) {
255     isRemoveEdges = removeEdgesAttr->value();
256   }
257
258   if (isRemoveEdges) {
259     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
260       new GeomAlgoAPI_UnifySameDomain(aShape));
261
262     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
263       setError(anError);
264       return;
265     }
266
267     aShape = aUnifyAlgo->shape();
268     aMakeShapeList->appendAlgo(aUnifyAlgo);
269   }
270
271   int aResultIndex = 0;
272
273   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
274
275   ListOfShape anEmptyTools;
276   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
277                                            anOriginalShapes,
278                                            anEmptyTools,
279                                            aMakeShapeList,
280                                            aShape);
281   setResult(aResultBody, aResultIndex);
282   aResultIndex++;
283
284   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
285                                           GeomShapePtr(),
286                                           anOriginalShapes,
287                                           aMakeShapeList,
288                                           aShape);
289
290   // remove the rest results if there were produced in the previous pass
291   removeResults(aResultIndex);
292 }