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