]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_BooleanFuse.cpp
Salome HOME
FUSE operation refactoring (process edges and faces while creating objects hierarchy).
[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   anOriginalShapes.insert(anOriginalShapes.end(), anEdgesAndFaces.begin(),
130                           anEdgesAndFaces.end());
131
132   // If we have compsolids then cut with not used solids all others.
133   if (!aShapesToAdd.empty()) {
134     aSolidsToFuse.clear();
135     for (ListOfShape::iterator
136          anIt = anOriginalShapes.begin(); anIt != anOriginalShapes.end(); anIt++) {
137       ListOfShape aOneObjectList;
138       aOneObjectList.push_back(*anIt);
139       std::shared_ptr<GeomAlgoAPI_Boolean> aCutAlgo(
140         new GeomAlgoAPI_Boolean(aOneObjectList, aShapesToAdd, GeomAlgoAPI_Tools::BOOL_CUT));
141
142       if (GeomAlgoAPI_ShapeTools::volume(aCutAlgo->shape()) > 1.e-27) {
143         aSolidsToFuse.push_back(aCutAlgo->shape());
144         aMakeShapeList->appendAlgo(aCutAlgo);
145       }
146     }
147   }
148
149   if (!aSolidsToFuse.empty()) {
150     anObjects.clear();
151     anObjects.push_back(aSolidsToFuse.back());
152     aSolidsToFuse.pop_back();
153     aTools = aSolidsToFuse;
154   }
155
156   // Fuse all objects and all tools.
157   GeomShapePtr aShape;
158   if (anObjects.size() == 1 && aTools.empty()) {
159     aShape = anObjects.front();
160   } else if (anObjects.empty() && aTools.size() == 1) {
161     aShape = aTools.front();
162   } else if ((anObjects.size() + aTools.size()) > 1) {
163     std::shared_ptr<GeomAlgoAPI_Boolean> aFuseAlgo(new GeomAlgoAPI_Boolean(anObjects,
164       aTools,
165       GeomAlgoAPI_Tools::BOOL_FUSE));
166
167     // Checking that the algorithm worked properly.
168     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFuseAlgo, getKind(), anError)) {
169       setError(anError);
170       return;
171     }
172
173     aShape = aFuseAlgo->shape();
174     aMakeShapeList->appendAlgo(aFuseAlgo);
175   }
176
177   // Combine result with not used solids from compsolid and edges and faces (if we have any).
178   if (aCuttedEdgesAndFaces.get() && !aCuttedEdgesAndFaces->isNull()) {
179     aShapesToAdd.push_back(aCuttedEdgesAndFaces);
180   } else {
181     aShapesToAdd.insert(aShapesToAdd.end(), anEdgesAndFaces.begin(), anEdgesAndFaces.end());
182   }
183   if (!aShapesToAdd.empty()) {
184     if (aShape.get()) {
185       aShapesToAdd.push_back(aShape);
186     }
187     std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
188       new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
189     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aFillerAlgo, getKind(), anError)) {
190       setError(anError);
191       return;
192     }
193
194     aShape = aFillerAlgo->shape();
195     aMakeShapeList->appendAlgo(aFillerAlgo);
196   }
197
198   bool isRemoveEdges = false;
199   AttributeBooleanPtr removeEdgesAttr = boolean(REMOVE_INTERSECTION_EDGES_ID());
200   if (removeEdgesAttr.get()) {
201     isRemoveEdges = removeEdgesAttr->value();
202   }
203
204   if (isRemoveEdges) {
205     std::shared_ptr<GeomAlgoAPI_UnifySameDomain> aUnifyAlgo(
206       new GeomAlgoAPI_UnifySameDomain(aShape));
207
208     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aUnifyAlgo, getKind(), anError)) {
209       setError(anError);
210       return;
211     }
212
213     aShape = aUnifyAlgo->shape();
214     aMakeShapeList->appendAlgo(aUnifyAlgo);
215   }
216
217   int aResultIndex = 0;
218
219   ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
220
221   ListOfShape anEmptyTools;
222   FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
223                                            anOriginalShapes,
224                                            anEmptyTools,
225                                            aMakeShapeList,
226                                            aShape);
227   setResult(aResultBody, aResultIndex);
228   aResultIndex++;
229
230   FeaturesPlugin_Tools::loadDeletedShapes(aResultBody,
231                                           GeomShapePtr(),
232                                           anOriginalShapes,
233                                           aMakeShapeList,
234                                           aShape);
235
236   // remove the rest results if there were produced in the previous pass
237   removeResults(aResultIndex);
238 }