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