Salome HOME
Naming functions were from FeaturesPlugin_Tools to ModelAPI_Tools
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCut.cpp
1 // Copyright (C) 2014-2021  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_BooleanCut.h"
21
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_Tools.h>
25
26 #include <GeomAlgoAPI_Boolean.h>
27 #include <GeomAlgoAPI_CompoundBuilder.h>
28 #include <GeomAlgoAPI_MakeShapeList.h>
29 #include <GeomAlgoAPI_PaveFiller.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomAlgoAPI_Tools.h>
32
33 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAPI_ShapeIterator.h>
35
36 //==================================================================================================
37 FeaturesPlugin_BooleanCut::FeaturesPlugin_BooleanCut()
38 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_CUT)
39 {
40 }
41
42 //==================================================================================================
43 void FeaturesPlugin_BooleanCut::initAttributes()
44 {
45   FeaturesPlugin_Boolean::initAttributes();
46   initVersion(BOP_VERSION_9_4(), selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
47 }
48
49 //==================================================================================================
50 void FeaturesPlugin_BooleanCut::execute()
51 {
52   GeomAPI_ShapeHierarchy anObjects, aTools;
53   ListOfShape aPlanes;
54
55   // Getting objects and tools
56   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes) ||
57       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
58     return;
59
60   int aResultIndex = 0;
61
62   if(anObjects.empty() || aTools.empty()) {
63     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
64     setError(aFeatureError);
65     return;
66   }
67
68   std::vector<ModelAPI_Tools::ResultBaseAlgo> aResultBaseAlgoList;
69   ListOfShape aResultShapesList;
70   std::string anError;
71
72   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
73
74   GeomShapePtr aResultCompound;
75   if (data()->version() == BOP_VERSION_9_4()) {
76     // merge hierarchies of compounds containing objects and tools
77     aResultCompound =
78         keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
79   }
80
81   // For solids cut each object with all tools.
82   bool isOk = true;
83   for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin();
84        anObjectsIt != anObjects.end() && isOk;
85        ++anObjectsIt) {
86     GeomShapePtr anObject = *anObjectsIt;
87     GeomShapePtr aParent = anObjects.parent(anObject);
88
89     if (aParent) {
90       GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
91       if (aShapeType == GeomAPI_Shape::COMPOUND) {
92         // Compound handling
93         isOk = processCompound(GeomAlgoAPI_Tools::BOOL_CUT,
94                                anObjects, aParent, aTools.objects(),
95                                aResultIndex, aResultBaseAlgoList, aResultShapesList,
96                                aResultCompound);
97       }
98       else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
99         // Compsolid handling
100         isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_CUT,
101                                 anObjects, aParent, aTools.objects(), ListOfShape(),
102                                 aResultIndex, aResultBaseAlgoList, aResultShapesList,
103                                 aResultCompound);
104       }
105     } else {
106       // process object as is
107       isOk = processObject(GeomAlgoAPI_Tools::BOOL_CUT,
108                            anObject, aTools.objects(), aPlanes,
109                            aResultIndex, aResultBaseAlgoList, aResultShapesList,
110                            aResultCompound);
111     }
112   }
113
114   storeResult(anObjects.objects(), aTools.objects(), aResultCompound, aResultIndex,
115               aMakeShapeList, aResultBaseAlgoList);
116
117   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
118   // result shape has been deleted, but in another it was modified or stayed.
119   if (!aResultCompound)
120     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
121   ModelAPI_Tools::loadDeletedShapes(aResultBaseAlgoList,
122                                           aTools.objects(),
123                                           aResultCompound);
124
125   // remove the rest results if there were produced in the previous pass
126   removeResults(aResultIndex);
127 }