1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "FeaturesPlugin_BooleanCommon.h"
22 #include "FeaturesPlugin_Tools.h"
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeSelectionList.h>
26 #include <ModelAPI_AttributeString.h>
27 #include <ModelAPI_Tools.h>
29 #include <GeomAlgoAPI_Boolean.h>
30 #include <GeomAlgoAPI_MakeShapeCustom.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAPI_Face.h>
34 #include <GeomAPI_ShapeIterator.h>
35 #include <GeomAPI_ShapeExplorer.h>
36 #include <GeomAlgoAPI_PaveFiller.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
38 #include <GeomAlgoAPI_Tools.h>
41 //==================================================================================================
42 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
43 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
47 //==================================================================================================
48 void FeaturesPlugin_BooleanCommon::initAttributes()
50 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
52 data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
53 data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
56 //==================================================================================================
57 void FeaturesPlugin_BooleanCommon::execute()
59 ListOfShape anObjects, aTools, aPlanes;
60 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompSolidsObjects;
61 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape> aCompoundObjects;
63 bool isSimpleMode = false;
65 AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
66 if (aCreationMethodAttr.get()
67 && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
72 AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECT_LIST_ID());
73 for (int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
74 AttributeSelectionPtr anObjectAttr = anObjectsSelList->value(anObjectsIndex);
75 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
76 if (!anObject.get()) {
79 ResultPtr aContext = anObjectAttr->context();
80 ResultBodyPtr aResCompSolidPtr = ModelAPI_Tools::bodyOwner(aContext);
82 && aResCompSolidPtr.get())
84 std::shared_ptr<GeomAPI_Shape> aContextShape = aResCompSolidPtr->shape();
85 GeomAPI_Shape::ShapeType aShapeType = aResCompSolidPtr->shape()->shapeType();
86 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>& aMap =
87 aShapeType == GeomAPI_Shape::COMPSOLID ? aCompSolidsObjects : aCompoundObjects;
89 std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
91 for (; anIt != aMap.end(); anIt++) {
92 if (anIt->first->isEqual(aContextShape)) {
93 aMap[anIt->first].push_back(anObject);
97 if (anIt == aMap.end()) {
98 aMap[aContextShape].push_back(anObject);
103 anObjects.push_back(anObject);
109 AttributeSelectionListPtr aToolsSelList = selectionList(FeaturesPlugin_Boolean::TOOL_LIST_ID());
110 for (int aToolsIndex = 0; aToolsIndex < aToolsSelList->size(); aToolsIndex++) {
111 AttributeSelectionPtr aToolAttr = aToolsSelList->value(aToolsIndex);
112 GeomShapePtr aTool = aToolAttr->value();
114 // It could be a construction plane.
115 ResultPtr aContext = aToolAttr->context();
116 aPlanes.push_back(aToolAttr->context()->shape());
118 aTools.push_back(aTool);
123 if ((anObjects.empty() && aCompSolidsObjects.empty() && aCompoundObjects.empty())
124 || (!isSimpleMode && aTools.empty() && aPlanes.empty())) {
125 std::string aFeatureError = "Error: Not enough objects for boolean operation.";
126 setError(aFeatureError);
130 int aResultIndex = 0;
132 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
133 std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
134 ListOfShape aResultShapesList;
138 ListOfShape::iterator anObjectsIt = anObjects.begin();
139 GeomShapePtr aShape = *anObjectsIt;
140 for (++anObjectsIt; anObjectsIt != anObjects.end(); ++anObjectsIt) {
141 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
142 new GeomAlgoAPI_Boolean(aShape,
144 GeomAlgoAPI_Boolean::BOOL_COMMON));
146 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
151 aShape = aCommonAlgo->shape();
152 aMakeShapeList->appendAlgo(aCommonAlgo);
155 GeomAPI_ShapeIterator aShapeIt(aShape);
156 if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
157 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
158 document()->createBody(data(), aResultIndex);
160 ListOfShape anEmptyTools;
161 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
166 GeomShapePtr aBaseShape = anObjects.front();
167 anObjects.pop_front();
168 setResult(aResultBody, aResultIndex);
172 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
173 aRBA.resultBody = aResultBody;
174 aRBA.baseShape = aBaseShape;
175 aRBA.makeShape = aMakeShapeList;
176 aResultBaseAlgoList.push_back(aRBA);
177 aResultShapesList.push_back(aShape);
180 for (ListOfShape::iterator anObjectsIt = anObjects.begin();
181 anObjectsIt != anObjects.end();
184 std::shared_ptr<GeomAPI_Shape> anObject = *anObjectsIt;
185 ListOfShape aListWithObject;
186 aListWithObject.push_back(anObject);
187 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
188 std::shared_ptr<GeomAlgoAPI_MakeShape> aBoolAlgo;
189 GeomShapePtr aResShape;
191 std::list<std::shared_ptr<GeomAPI_Pnt> > aBoundingPoints =
192 GeomAlgoAPI_ShapeTools::getBoundingBox(aListWithObject, 1.0);
195 ListOfShape aToolsWithPlanes = aTools;
196 for (ListOfShape::const_iterator anIt = aPlanes.cbegin();
197 anIt != aPlanes.cend();
199 GeomShapePtr aPlane = *anIt;
200 GeomShapePtr aTool = GeomAlgoAPI_ShapeTools::fitPlaneToBox(aPlane, aBoundingPoints);
201 std::shared_ptr<GeomAlgoAPI_MakeShapeCustom> aMkShCustom(
202 new GeomAlgoAPI_MakeShapeCustom);
203 aMkShCustom->addModified(aPlane, aTool);
204 aMakeShapeList->appendAlgo(aMkShCustom);
205 aToolsWithPlanes.push_back(aTool);
208 aBoolAlgo.reset(new GeomAlgoAPI_Boolean(aListWithObject,
210 GeomAlgoAPI_Boolean::BOOL_COMMON));
211 aResShape = aBoolAlgo->shape();
213 // Checking that the algorithm worked properly.
214 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aBoolAlgo, getKind(), anError)) {
219 aMakeShapeList->appendAlgo(aBoolAlgo);
221 GeomAPI_ShapeIterator aShapeIt(aResShape);
222 if (aShapeIt.more() || aResShape->shapeType() == GeomAPI_Shape::VERTEX) {
223 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
224 document()->createBody(data(), aResultIndex);
225 ListOfShape anObjectList;
226 anObjectList.push_back(anObject);
227 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
232 setResult(aResultBody, aResultIndex);
235 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
236 aRBA.resultBody = aResultBody;
237 aRBA.baseShape = anObject;
238 aRBA.makeShape = aMakeShapeList;
239 aResultBaseAlgoList.push_back(aRBA);
240 aResultShapesList.push_back(aResShape);
244 // Compsolids handling
245 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
246 anIt = aCompSolidsObjects.begin();
247 anIt != aCompSolidsObjects.end();
250 std::shared_ptr<GeomAPI_Shape> aCompSolid = anIt->first;
251 ListOfShape& aUsedInOperationSolids = anIt->second;
253 // Collecting solids from compsolids which will not be modified in boolean operation.
254 ListOfShape aNotUsedSolids;
255 for (GeomAPI_ShapeExplorer anExp(aCompSolid, GeomAPI_Shape::SOLID);
259 std::shared_ptr<GeomAPI_Shape> aSolidInCompSolid = anExp.current();
260 ListOfShape::iterator aUsedIt = aUsedInOperationSolids.begin();
261 for (; aUsedIt != aUsedInOperationSolids.end(); aUsedIt++) {
262 if (aSolidInCompSolid->isEqual(*aUsedIt)) {
266 if (aUsedIt == aUsedInOperationSolids.end()) {
267 aNotUsedSolids.push_back(aSolidInCompSolid);
271 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
272 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
273 new GeomAlgoAPI_Boolean(aUsedInOperationSolids,
275 GeomAlgoAPI_Boolean::BOOL_COMMON));
277 // Checking that the algorithm worked properly.
278 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
283 aMakeShapeList->appendAlgo(aCommonAlgo);
284 GeomShapePtr aResultShape = aCommonAlgo->shape();
286 // Add result to not used solids from compsolid.
287 if (!aNotUsedSolids.empty()) {
288 ListOfShape aShapesToAdd = aNotUsedSolids;
289 aShapesToAdd.push_back(aCommonAlgo->shape());
290 std::shared_ptr<GeomAlgoAPI_PaveFiller> aFillerAlgo(
291 new GeomAlgoAPI_PaveFiller(aShapesToAdd, true));
292 if (!aFillerAlgo->isDone()) {
293 std::string aFeatureError = "Error: PaveFiller algorithm failed.";
294 setError(aFeatureError);
298 aMakeShapeList->appendAlgo(aFillerAlgo);
299 aResultShape = aFillerAlgo->shape();
302 GeomAPI_ShapeIterator aShapeIt(aResultShape);
303 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX)
305 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
306 document()->createBody(data(), aResultIndex);
308 ListOfShape aCompSolidList;
309 aCompSolidList.push_back(aCompSolid);
310 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
315 setResult(aResultBody, aResultIndex);
318 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
319 aRBA.resultBody = aResultBody;
320 aRBA.baseShape = aCompSolid;
321 aRBA.makeShape = aMakeShapeList;
322 aResultBaseAlgoList.push_back(aRBA);
323 aResultShapesList.push_back(aResultShape);
327 // Compounds handling
328 for (std::map<std::shared_ptr<GeomAPI_Shape>, ListOfShape>::iterator
329 anIt = aCompoundObjects.begin();
330 anIt != aCompoundObjects.end();
333 std::shared_ptr<GeomAPI_Shape> aCompound = anIt->first;
334 ListOfShape& aUsedInOperationShapes = anIt->second;
336 // Collecting shapes from compound which will not be modified in boolean operation.
337 ListOfShape aNotUsedShapes;
338 for (GeomAPI_ShapeIterator aCompIt(aCompound);
342 std::shared_ptr<GeomAPI_Shape> aShapeInCompound = aCompIt.current();
343 ListOfShape::iterator aUsedIt = aUsedInOperationShapes.begin();
344 for (; aUsedIt != aUsedInOperationShapes.end(); aUsedIt++) {
345 if (aShapeInCompound->isEqual(*aUsedIt)) {
349 if (aUsedIt == aUsedInOperationShapes.end()) {
350 aNotUsedShapes.push_back(aShapeInCompound);
354 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
355 std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
356 new GeomAlgoAPI_Boolean(aUsedInOperationShapes,
358 GeomAlgoAPI_Boolean::BOOL_COMMON));
360 // Checking that the algorithm worked properly.
361 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
366 aMakeShapeList->appendAlgo(aCommonAlgo);
367 GeomShapePtr aResultShape = aCommonAlgo->shape();
369 // Add result to not used shape from compound.
370 if (!aNotUsedShapes.empty()) {
371 ListOfShape aShapesForResult = aNotUsedShapes;
372 if (aResultShape->shapeType() == GeomAPI_Shape::COMPOUND) {
373 for (GeomAPI_ShapeIterator aResultIt(aResultShape); aResultIt.more(); aResultIt.next()) {
374 aShapesForResult.push_back(aResultIt.current());
378 aShapesForResult.push_back(aResultShape);
381 if (aShapesForResult.size() == 1) {
382 aResultShape = aShapesForResult.front();
385 aResultShape = GeomAlgoAPI_CompoundBuilder::compound(aShapesForResult);
389 GeomAPI_ShapeIterator aShapeIt(aResultShape);
390 if (aShapeIt.more() || aResultShape->shapeType() == GeomAPI_Shape::VERTEX) {
391 std::shared_ptr<ModelAPI_ResultBody> aResultBody =
392 document()->createBody(data(), aResultIndex);
394 ListOfShape aCompoundList;
395 aCompoundList.push_back(aCompound);
396 FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
401 setResult(aResultBody, aResultIndex);
404 FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
405 aRBA.resultBody = aResultBody;
406 aRBA.baseShape = aCompound;
407 aRBA.makeShape = aMakeShapeList;
408 aResultBaseAlgoList.push_back(aRBA);
409 aResultShapesList.push_back(aResultShape);
415 // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
416 // result shape has been deleted, but in another it was modified or stayed.
417 GeomShapePtr aResultShapesCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
418 FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList, aTools, aResultShapesCompound);
420 // remove the rest results if there were produced in the previous pass
421 removeResults(aResultIndex);