Salome HOME
9911d4283860bf99dc98e2fdf8d14e3baf51a18a
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_BooleanCommon.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_BooleanCommon.h"
21
22 #include <ModelAPI_ResultBody.h>
23 #include <ModelAPI_AttributeInteger.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_Session.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAlgoAPI_Boolean.h>
31 #include <GeomAlgoAPI_MakeShapeCustom.h>
32 #include <GeomAlgoAPI_MakeShapeList.h>
33 #include <GeomAlgoAPI_ShapeTools.h>
34 #include <GeomAPI_Face.h>
35 #include <GeomAPI_ShapeIterator.h>
36 #include <GeomAPI_ShapeExplorer.h>
37 #include <GeomAlgoAPI_PaveFiller.h>
38 #include <GeomAlgoAPI_CompoundBuilder.h>
39 #include <GeomAlgoAPI_Tools.h>
40
41 static const int THE_COMMON_VERSION_1 = 20190506;
42
43 //==================================================================================================
44 FeaturesPlugin_BooleanCommon::FeaturesPlugin_BooleanCommon()
45 : FeaturesPlugin_Boolean(FeaturesPlugin_Boolean::BOOL_COMMON)
46 {
47 }
48
49 //==================================================================================================
50 void FeaturesPlugin_BooleanCommon::initAttributes()
51 {
52   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
53
54   data()->addAttribute(OBJECT_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
55   data()->addAttribute(TOOL_LIST_ID(), ModelAPI_AttributeSelectionList::typeId());
56
57   initVersion(THE_COMMON_VERSION_1);
58 }
59
60 //==================================================================================================
61 void FeaturesPlugin_BooleanCommon::execute()
62 {
63   ListOfShape aPlanes;
64   ObjectHierarchy anObjects, aTools;
65
66   bool isSimpleMode = false;
67
68   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
69   if (aCreationMethodAttr.get()
70       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
71     isSimpleMode = true;
72   }
73
74   // Getting objects.
75   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
76     return;
77   // Planes are not supported as objects of COMMON operation
78   aPlanes.clear();
79
80   // Getting tools.
81   if (!isSimpleMode &&
82       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
83     return;
84
85   if (anObjects.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
86     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
87     setError(aFeatureError);
88     return;
89   }
90
91   // version of COMMON feature
92   int aCommonVersion = version();
93
94   int aResultIndex = 0;
95   GeomShapePtr aResultCompound;
96
97   std::string anError;
98   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
99   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
100   ListOfShape aResultShapesList;
101
102   if (isSimpleMode) {
103     ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
104     GeomShapePtr aShape = *anObjectsIt;
105     for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
106       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
107         new GeomAlgoAPI_Boolean(aShape,
108                                 *anObjectsIt,
109                                 GeomAlgoAPI_Tools::BOOL_COMMON));
110
111       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
112         setError(anError);
113         return;
114       }
115
116       aShape = aCommonAlgo->shape();
117       aMakeShapeList->appendAlgo(aCommonAlgo);
118     }
119
120     if (aCommonVersion == THE_COMMON_VERSION_1) {
121       // merge hierarchies of compounds containing objects and tools
122       // and append the result of the FUSE operation
123       aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
124     }
125
126     GeomAPI_ShapeIterator aShapeIt(aShape);
127     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
128       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
129         document()->createBody(data(), aResultIndex);
130
131       ListOfShape anObjectList = anObjects.Objects();
132       ListOfShape aToolsList;
133       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
134                                                anObjectList,
135                                                aToolsList,
136                                                aMakeShapeList,
137                                                aShape);
138       GeomShapePtr aBaseShape = anObjectList.front();
139       anObjectList.pop_front();
140       setResult(aResultBody, aResultIndex);
141       aResultIndex++;
142
143       aToolsList = anObjectList;
144       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
145       aRBA.resultBody = aResultBody;
146       aRBA.baseShape = aBaseShape;
147       aRBA.makeShape = aMakeShapeList;
148       aResultBaseAlgoList.push_back(aRBA);
149       aResultShapesList.push_back(aShape);
150     }
151   } else {
152     if (aCommonVersion == THE_COMMON_VERSION_1) {
153       // merge hierarchies of compounds containing objects and tools
154       aResultCompound =
155           keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
156     }
157
158     bool isOk = true;
159     for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
160          anObjectsIt != anObjects.End() && isOk;
161          ++anObjectsIt)
162     {
163       GeomShapePtr anObject = *anObjectsIt;
164       GeomShapePtr aParent = anObjects.Parent(anObject);
165
166       if (aParent) {
167         GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
168         if (aShapeType == GeomAPI_Shape::COMPOUND) {
169           // Compound handling
170           isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
171                                  anObjects, aParent, aTools.Objects(),
172                                  aResultIndex, aResultBaseAlgoList, aResultShapesList,
173                                  aResultCompound);
174         }
175         else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
176           // Compsolid handling
177           isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
178                                   anObjects, aParent, aTools.Objects(), ListOfShape(),
179                                   aResultIndex, aResultBaseAlgoList, aResultShapesList,
180                                   aResultCompound);
181         }
182       } else {
183         // process object as is
184         isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
185                              anObject, aTools.Objects(), aPlanes,
186                              aResultIndex, aResultBaseAlgoList, aResultShapesList,
187                              aResultCompound);
188       }
189     }
190
191     storeResult(anObjects.Objects(), aTools.Objects(), aResultCompound, aResultIndex,
192                 aMakeShapeList, aResultBaseAlgoList);
193   }
194
195   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
196   // result shape has been deleted, but in another it was modified or stayed.
197   if (!aResultCompound)
198     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
199   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
200                                           aTools.Objects(),
201                                           aResultCompound);
202
203   // remove the rest results if there were produced in the previous pass
204   removeResults(aResultIndex);
205 }