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