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