Salome HOME
Change the paradigm of versioning of Boolean Operations on the Python API level.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanFill.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_BooleanFill.h"
21 #include "FeaturesPlugin_Tools.h"
22
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_Tools.h>
26
27 #include <GeomAlgoAPI_Boolean.h>
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_MakeShapeCustom.h>
30 #include <GeomAlgoAPI_MakeShapeList.h>
31 #include <GeomAlgoAPI_Partition.h>
32 #include <GeomAlgoAPI_PaveFiller.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAlgoAPI_Tools.h>
35
36 #include <GeomAPI_Face.h>
37 #include <GeomAPI_ShapeExplorer.h>
38 #include <GeomAPI_ShapeIterator.h>
39
40 #include <algorithm>
41 #include <map>
42
43 //=================================================================================================
44 FeaturesPlugin_BooleanFill::FeaturesPlugin_BooleanFill()
45   : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_FILL)
46 {
47 }
48
49 //=================================================================================================
50 void FeaturesPlugin_BooleanFill::initAttributes()
51 {
52   FeaturesPlugin_Boolean::initAttributes();
53   initVersion(THE_VERSION_1, selectionList(OBJECT_LIST_ID()), selectionList(TOOL_LIST_ID()));
54 }
55
56 //=================================================================================================
57 void FeaturesPlugin_BooleanFill::execute()
58 {
59   std::string anError;
60   ObjectHierarchy anObjects, aTools;
61   ListOfShape aPlanes;
62
63   // Getting objects.
64   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
65     return;
66   // Planes are not supported as objects of FILL operation
67   aPlanes.clear();
68
69   // Getting tools.
70   if (!processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
71     return;
72
73   int aResultIndex = 0;
74
75   if (anObjects.IsEmpty() || (aTools.IsEmpty() && aPlanes.empty())) {
76     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
77     setError(aFeatureError);
78     return;
79   }
80
81   // version of Split feature
82   int aSplitVersion = version();
83
84   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
85   ListOfShape aResultShapesList;
86
87   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
88
89   GeomShapePtr aResultCompound;
90   if (aSplitVersion == THE_VERSION_1) {
91     // merge hierarchies of compounds containing objects and tools
92     aResultCompound =
93         keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
94   }
95
96   // For solids cut each object with all tools.
97   bool isOk = true;
98   for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
99        anObjectsIt != anObjects.End() && isOk;
100        ++anObjectsIt) {
101     GeomShapePtr anObject = *anObjectsIt;
102     GeomShapePtr aParent = anObjects.Parent(anObject, false);
103
104     if (aParent && aParent->shapeType() == GeomAPI_Shape::COMPSOLID) {
105       // get parent once again to mark it and the subs as processed
106       aParent = anObjects.Parent(anObject);
107       // compsolid handling
108       isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_PARTITION,
109                               anObjects, aParent, aTools.Objects(), aPlanes,
110                               aResultIndex, aResultBaseAlgoList, aResultShapesList,
111                               aResultCompound);
112     } else {
113       // process object as is
114       isOk = processObject(GeomAlgoAPI_Tools::BOOL_PARTITION,
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 }