Salome HOME
Task 3.2. Concealment into multi-level Compounds
[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   AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
66   aVerAttr->setIsArgument(false);
67   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
68   if (!integer(VERSION_ID())->isInitialized() &&
69       !selectionList(OBJECT_LIST_ID())->isInitialized() &&
70       !selectionList(TOOL_LIST_ID())->isInitialized()) {
71     // this is a newly created feature (not read from file),
72     // so, initialize the latest version
73     integer(VERSION_ID())->setValue(THE_FUSE_VERSION_1);
74   }
75 }
76
77 //==================================================================================================
78 void FeaturesPlugin_BooleanFuse::execute()
79 {
80   std::string anError;
81   ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
82   ListOfShape aPlanes;
83
84   bool isSimpleCreation = false;
85
86   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
87   if (aCreationMethodAttr.get()
88       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE())
89   {
90     isSimpleCreation = true;
91   }
92
93   // Getting objects.
94   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
95     return;
96
97   // Getting tools.
98   if (!isSimpleCreation &&
99       !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
100     return;
101
102   ListOfShape anObjects, aTools, anEdgesAndFaces;
103   // all objects except edges and faces
104   anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
105                                    GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
106   aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
107                                 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
108
109   if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
110     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
111     setError(aFeatureError);
112     return;
113   }
114
115   // version of FUSE feature
116   AttributeIntegerPtr aVersionAttr = integer(VERSION_ID());
117   int aFuseVersion = 0;
118   if (aVersionAttr && aVersionAttr->isInitialized())
119     aFuseVersion = aVersionAttr->value();
120
121 ////  isSimpleCreation = isSimpleCreation && aFuseVersion < THE_FUSE_VERSION_1;
122
123   // Collecting all solids which will be fused.
124   ListOfShape aSolidsToFuse;
125   aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
126   aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
127
128   // Collecting solids from compsolids which will not be modified
129   // in boolean operation and will be added to result.
130   ListOfShape aShapesToAdd;
131   for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
132        !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
133        ++anObjectsIt) {
134     GeomShapePtr anObject = *anObjectsIt;
135     GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
136
137     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
138       // mark all subs of this parent as precessed to avoid handling twice
139       aParent = anObjectsHierarchy.Parent(anObject);
140
141       ListOfShape aUsed, aNotUsed;
142       anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
143       aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
144     }
145   }
146
147   ListOfShape anOriginalShapes = aSolidsToFuse;
148   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
149
150   // Cut edges and faces(if we have any) with solids.
151   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
152   GeomShapePtr aCuttedEdgesAndFaces;
153   if (!anEdgesAndFaces.empty()) {
154     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
155       anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
156     if (aCutAlgo->isDone()) {
157       aCuttedEdgesAndFaces = aCutAlgo->shape();
158       aMakeShapeList->appendAlgo(aCutAlgo);
159     }
160   }
161   anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
162                           anEdgesAndFaces.end());
163
164   // If we have compsolids then cut with not used solids all others.
165   if (!aShapesToAdd.empty()) {
166     aSolidsToFuse.clear();
167     for (ListOfShape::iterator
168          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
169       ListOfShape aOneObjectList;
170       aOneObjectList.push_back(*anIt);
171       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
172         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
173
174       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
175         aSolidsToFuse.push_back(aCutAlgo->shape());
176         aMakeShapeList->appendAlgo(aCutAlgo);
177       }
178     }
179   }
180
181   if (!aSolidsToFuse.empty()) {
182     anObjects.clear();
183     anObjects.push_back(aSolidsToFuse.back());
184     aSolidsToFuse.pop_back();
185     aTools = aSolidsToFuse;
186   }
187
188   // Fuse all objects and all tools.
189   GeomShapePtr aShape;
190   if (anObjects.size() == 1 && aTools.empty()) {
191     aShape = anObjects.front();
192   } else if (anObjects.empty() && aTools.size() == 1) {
193     aShape = aTools.front();
194   } else if ((anObjects.size() + aTools.size()) > 1) {
195     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
196       aTools,
197       GeomAlgoAPI_Tools::BOOL_FUSE));
198
199     // Checking that the algorithm worked properly.
200     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
201       setError(anError);
202       return;
203     }
204
205     aShape = aFuseAlgo->shape();
206     aMakeShapeList->appendAlgo(aFuseAlgo);
207   }
208
209   // Combine result with not used solids from compsolid and edges and faces (if we have any).
210   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
211     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
212   } else {
213     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
214   }
215   if (!aShapesToAdd.empty()) {
216     if (aShape.get()) {
217       aShapesToAdd.push_back(aShape);
218     }
219     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
220       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
221     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
222       setError(anError);
223       return;
224     }
225
226     aShape = aFillerAlgo->shape();
227     aMakeShapeList->appendAlgo(aFillerAlgo);
228   }
229
230   bool isRemoveEdges = false;
231   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
232   if (removeEdgesAttr.get()) {
233     isRemoveEdges = removeEdgesAttr->value();
234   }
235
236   if (isRemoveEdges) {
237     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
238       new GeomAlgoAPI_UnifySameDomain(aShape));
239
240     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
241       setError(anError);
242       return;
243     }
244
245     aShape = aUnifyAlgo->shape();
246     aMakeShapeList->appendAlgo(aUnifyAlgo);
247   }
248
249   if (aFuseVersion == THE_FUSE_VERSION_1) {
250     // merge hierarchies of compounds containing objects and tools
251     // and append the result of the FUSE operation
252     aShape = keepUnusedSubsOfCompound(aShape, anObjectsHierarchy, aToolsHierarchy, aMakeShapeList);
253   }
254
255   int aResultIndex = 0;
256
257   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
258
259   ListOfShape anEmptyTools;
260   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
261                                            anOriginalShapes,
262                                            anEmptyTools,
263                                            aMakeShapeList,
264                                            aShape);
265   setResult(aResultBody, aResultIndex);
266   aResultIndex++;
267
268   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
269                                           GeomShapePtr(),
270                                           anOriginalShapes,
271                                           aMakeShapeList,
272                                           aShape);
273
274   // remove the rest results if there were produced in the previous pass
275   removeResults(aResultIndex);
276 }
277
278 //==================================================================================================
279 GeomShapePtr FeaturesPlugin_BooleanFuse::keepUnusedSubsOfCompound(
280     const GeomShapePtr& theFuseResult,
281     const ObjectHierarchy& theObjectsHierarchy,
282     const ObjectHierarchy& theToolsHierarchy,
283     std::shared_ptr<GeomAlgoAPI_MakeShapeList> theMakeShapeList)
284 {
285   ListOfShape aCompounds;
286   theObjectsHierarchy.CompoundsOfUnusedObjects(aCompounds);
287   theToolsHierarchy.CompoundsOfUnusedObjects(aCompounds);
288
289   GeomShapePtr aResultShape = theFuseResult;
290   if (!aCompounds.empty()) {
291     aResultShape = aCompounds.front();
292     aCompounds.pop_front();
293
294     std::shared_ptr<GeomAlgoAPI_ShapeBuilder> aBuilder(new GeomAlgoAPI_ShapeBuilder);
295     for (ListOfShape::iterator anIt = aCompounds.begin(); anIt != aCompounds.end(); ++anIt) {
296       for (GeomAPI_ShapeIterator aSub(*anIt); aSub.more(); aSub.next())
297         aBuilder->add(aResultShape, aSub.current());
298     }
299
300     aBuilder->add(aResultShape, theFuseResult);
301
302     theMakeShapeList->appendAlgo(aBuilder);
303   }
304   return aResultShape;
305 }