]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp
Salome HOME
497d529e43ad221c3941eac83a2529ac3fce4750
[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   ObjectHierarchy anObjectsHierarchy, aToolsHierarchy;
65   ListOfShape aPlanes;
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   if (!processAttribute(OBJECT_LIST_ID(), anObjectsHierarchy, aPlanes))
78     return;
79
80   // Getting tools.
81   if (!isSimpleCreation &&
82       !processAttribute(TOOL_LIST_ID(), aToolsHierarchy, aPlanes))
83     return;
84
85   ListOfShape anObjects, aTools, anEdgesAndFaces;
86   // all objects except edges and faces
87   anObjectsHierarchy.ObjectsByType(anEdgesAndFaces, anObjects,
88                                    GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
89   aToolsHierarchy.ObjectsByType(anEdgesAndFaces, aTools,
90                                 GeomAPI_Shape::FACE, GeomAPI_Shape::EDGE);
91
92   if ((anObjects.size() + aTools.size() + anEdgesAndFaces.size()) < 2) {
93     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
94     setError(aFeatureError);
95     return;
96   }
97
98   // Collecting all solids which will be fused.
99   ListOfShape aSolidsToFuse;
100   aSolidsToFuse.insert(aSolidsToFuse.end(), anObjects.begin(), anObjects.end());
101   aSolidsToFuse.insert(aSolidsToFuse.end(), aTools.begin(), aTools.end());
102
103   // Collecting solids from compsolids which will not be modified
104   // in boolean operation and will be added to result.
105   ListOfShape aShapesToAdd;
106   for (ObjectHierarchy::Iterator anObjectsIt = anObjectsHierarchy.Begin();
107        !isSimpleCreation && anObjectsIt != anObjectsHierarchy.End();
108        ++anObjectsIt) {
109     GeomShapePtr anObject = *anObjectsIt;
110     GeomShapePtr aParent = anObjectsHierarchy.Parent(anObject, false);
111
112     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
113       // mark all subs of this parent as precessed to avoid handling twice
114       aParent = anObjectsHierarchy.Parent(anObject);
115
116       ListOfShape aUsed, aNotUsed;
117       anObjectsHierarchy.SplitCompound(aParent, aUsed, aNotUsed);
118       aShapesToAdd.insert(aShapesToAdd.end(), aNotUsed.begin(), aNotUsed.end());
119     }
120   }
121
122   ListOfShape anOriginalShapes = aSolidsToFuse;
123   anOriginalShapes.insert(anOriginalShapes.end(), aShapesToAdd.begin(), aShapesToAdd.end());
124
125   // Cut edges and faces(if we have any) with solids.
126   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
127   GeomShapePtr aCuttedEdgesAndFaces;
128   if (!anEdgesAndFaces.empty()) {
129     std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(new GeomAlgoAPI_Boolean(anEdgesAndFaces,
130       anOriginalShapes, GeomAlgoAPI_Tools::BOOL_CUT));
131     if (aCutAlgo->isDone()) {
132       aCuttedEdgesAndFaces = aCutAlgo->shape();
133       aMakeShapeList->appendAlgo(aCutAlgo);
134     }
135   }
136   anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
137                           anEdgesAndFaces.end());
138
139   // If we have compsolids then cut with not used solids all others.
140   if (!aShapesToAdd.empty()) {
141     aSolidsToFuse.clear();
142     for (ListOfShape::iterator
143          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
144       ListOfShape aOneObjectList;
145       aOneObjectList.push_back(*anIt);
146       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
147         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
148
149       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
150         aSolidsToFuse.push_back(aCutAlgo->shape());
151         aMakeShapeList->appendAlgo(aCutAlgo);
152       }
153     }
154   }
155
156   if (!aSolidsToFuse.empty()) {
157     anObjects.clear();
158     anObjects.push_back(aSolidsToFuse.back());
159     aSolidsToFuse.pop_back();
160     aTools = aSolidsToFuse;
161   }
162
163   // Fuse all objects and all tools.
164   GeomShapePtr aShape;
165   if (anObjects.size() == 1 && aTools.empty()) {
166     aShape = anObjects.front();
167   } else if (anObjects.empty() && aTools.size() == 1) {
168     aShape = aTools.front();
169   } else if ((anObjects.size() + aTools.size()) > 1) {
170     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
171       aTools,
172       GeomAlgoAPI_Tools::BOOL_FUSE));
173
174     // Checking that the algorithm worked properly.
175     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
176       setError(anError);
177       return;
178     }
179
180     aShape = aFuseAlgo->shape();
181     aMakeShapeList->appendAlgo(aFuseAlgo);
182   }
183
184   // Combine result with not used solids from compsolid and edges and faces (if we have any).
185   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
186     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
187   } else {
188     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
189   }
190   if (!aShapesToAdd.empty()) {
191     if (aShape.get()) {
192       aShapesToAdd.push_back(aShape);
193     }
194     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
195       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
196     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
197       setError(anError);
198       return;
199     }
200
201     aShape = aFillerAlgo->shape();
202     aMakeShapeList->appendAlgo(aFillerAlgo);
203   }
204
205   bool isRemoveEdges = false;
206   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
207   if (removeEdgesAttr.get()) {
208     isRemoveEdges = removeEdgesAttr->value();
209   }
210
211   if (isRemoveEdges) {
212     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
213       new GeomAlgoAPI_UnifySameDomain(aShape));
214
215     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
216       setError(anError);
217       return;
218     }
219
220     aShape = aUnifyAlgo->shape();
221     aMakeShapeList->appendAlgo(aUnifyAlgo);
222   }
223
224   int aResultIndex = 0;
225
226   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
227
228   ListOfShape anEmptyTools;
229   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
230                                            anOriginalShapes,
231                                            anEmptyTools,
232                                            aMakeShapeList,
233                                            aShape);
234   setResult(aResultBody, aResultIndex);
235   aResultIndex++;
236
237   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
238                                           GeomShapePtr(),
239                                           anOriginalShapes,
240                                           aMakeShapeList,
241                                           aShape);
242
243   // remove the rest results if there were produced in the previous pass
244   removeResults(aResultIndex);
245 }