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