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