Salome HOME
Task 3.2. Concealment into multi-level Compounds
[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   AttributePtr aVerAttr = data()->addAttribute(VERSION_ID(), ModelAPI_AttributeInteger::typeId());
58   aVerAttr->setIsArgument(false);
59   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), VERSION_ID());
60   if (!integer(VERSION_ID())->isInitialized() &&
61       !selectionList(OBJECT_LIST_ID())->isInitialized() &&
62       !selectionList(TOOL_LIST_ID())->isInitialized()) {
63     // this is a newly created feature (not read from file),
64     // so, initialize the latest version
65     integer(VERSION_ID())->setValue(THE_COMMON_VERSION_1);
66   }
67 }
68
69 //==================================================================================================
70 void FeaturesPlugin_BooleanCommon::execute()
71 {
72   ListOfShape aPlanes;
73   ObjectHierarchy anObjects, aTools;
74
75   bool isSimpleMode = false;
76
77   AttributeStringPtr aCreationMethodAttr = string(CREATION_METHOD());
78   if (aCreationMethodAttr.get()
79       && aCreationMethodAttr->value() == CREATION_METHOD_SIMPLE()) {
80     isSimpleMode = true;
81   }
82
83   // Getting objects.
84   if (!processAttribute(OBJECT_LIST_ID(), anObjects, aPlanes))
85     return;
86   // Planes are not supported as objects of COMMON operation
87   aPlanes.clear();
88
89   // Getting tools.
90   if (!isSimpleMode &&
91       !processAttribute(TOOL_LIST_ID(), aTools, aPlanes))
92     return;
93
94   if (anObjects.IsEmpty() || (!isSimpleMode && aTools.IsEmpty() && aPlanes.empty())) {
95     std::string aFeatureError = "Error: Not enough objects for boolean operation.";
96     setError(aFeatureError);
97     return;
98   }
99
100   // version of COMMON feature
101   int aCommonVersion = version();
102
103   int aResultIndex = 0;
104   GeomShapePtr aResultCompound;
105
106   std::string anError;
107   std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList());
108   std::vector<FeaturesPlugin_Tools::ResultBaseAlgo> aResultBaseAlgoList;
109   ListOfShape aResultShapesList;
110
111   if (isSimpleMode) {
112     ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
113     GeomShapePtr aShape = *anObjectsIt;
114     for (++anObjectsIt; anObjectsIt != anObjects.End(); ++anObjectsIt) {
115       std::shared_ptr<GeomAlgoAPI_Boolean> aCommonAlgo(
116         new GeomAlgoAPI_Boolean(aShape,
117                                 *anObjectsIt,
118                                 GeomAlgoAPI_Tools::BOOL_COMMON));
119
120       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCommonAlgo, getKind(), anError)) {
121         setError(anError);
122         return;
123       }
124
125       aShape = aCommonAlgo->shape();
126       aMakeShapeList->appendAlgo(aCommonAlgo);
127     }
128
129     if (aCommonVersion == THE_COMMON_VERSION_1) {
130       // merge hierarchies of compounds containing objects and tools
131       // and append the result of the FUSE operation
132       aShape = keepUnusedSubsOfCompound(aShape, anObjects, aTools, aMakeShapeList);
133     }
134
135     GeomAPI_ShapeIterator aShapeIt(aShape);
136     if (aShapeIt.more() || aShape->shapeType() == GeomAPI_Shape::VERTEX) {
137       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
138         document()->createBody(data(), aResultIndex);
139
140       ListOfShape anObjectList = anObjects.Objects();
141       ListOfShape aToolsList;
142       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
143                                                anObjectList,
144                                                aToolsList,
145                                                aMakeShapeList,
146                                                aShape);
147       GeomShapePtr aBaseShape = anObjectList.front();
148       anObjectList.pop_front();
149       setResult(aResultBody, aResultIndex);
150       aResultIndex++;
151
152       aToolsList = anObjectList;
153       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
154       aRBA.resultBody = aResultBody;
155       aRBA.baseShape = aBaseShape;
156       aRBA.makeShape = aMakeShapeList;
157       aResultBaseAlgoList.push_back(aRBA);
158       aResultShapesList.push_back(aShape);
159     }
160   } else {
161     if (aCommonVersion == THE_COMMON_VERSION_1) {
162       // merge hierarchies of compounds containing objects and tools
163       aResultCompound =
164           keepUnusedSubsOfCompound(GeomShapePtr(), anObjects, aTools, aMakeShapeList);
165     }
166
167     bool isOk = true;
168     for (ObjectHierarchy::Iterator anObjectsIt = anObjects.Begin();
169          anObjectsIt != anObjects.End() && isOk;
170          ++anObjectsIt)
171     {
172       GeomShapePtr anObject = *anObjectsIt;
173       GeomShapePtr aParent = anObjects.Parent(anObject);
174
175       if (aParent) {
176         GeomAPI_Shape::ShapeType aShapeType = aParent->shapeType();
177         if (aShapeType == GeomAPI_Shape::COMPOUND) {
178           // Compound handling
179           isOk = processCompound(GeomAlgoAPI_Tools::BOOL_COMMON,
180                                  anObjects, aParent, aTools.Objects(),
181                                  aResultIndex, aResultBaseAlgoList, aResultShapesList,
182                                  aResultCompound);
183         }
184         else if (aShapeType == GeomAPI_Shape::COMPSOLID) {
185           // Compsolid handling
186           isOk = processCompsolid(GeomAlgoAPI_Tools::BOOL_COMMON,
187                                   anObjects, aParent, aTools.Objects(), ListOfShape(),
188                                   aResultIndex, aResultBaseAlgoList, aResultShapesList,
189                                   aResultCompound);
190         }
191       } else {
192         // process object as is
193         isOk = processObject(GeomAlgoAPI_Tools::BOOL_COMMON,
194                              anObject, aTools.Objects(), aPlanes,
195                              aResultIndex, aResultBaseAlgoList, aResultShapesList,
196                              aResultCompound);
197       }
198     }
199
200     GeomAPI_ShapeIterator aShapeIt(aResultCompound);
201     if (aShapeIt.more()) {
202       std::shared_ptr<ModelAPI_ResultBody> aResultBody =
203           document()->createBody(data(), aResultIndex);
204
205       ListOfShape anObjectList = anObjects.Objects();
206       ListOfShape aToolsList = aTools.Objects();
207       FeaturesPlugin_Tools::loadModifiedShapes(aResultBody,
208                                                anObjectList,
209                                                aToolsList,
210                                                aMakeShapeList,
211                                                aResultCompound);
212       setResult(aResultBody, aResultIndex++);
213
214       // merge algorithms
215       FeaturesPlugin_Tools::ResultBaseAlgo aRBA;
216       aRBA.resultBody = aResultBody;
217       aRBA.baseShape = anObjectList.front();
218       for (std::vector<FeaturesPlugin_Tools::ResultBaseAlgo>::iterator aRBAIt = aResultBaseAlgoList.begin();
219            aRBAIt != aResultBaseAlgoList.end(); ++aRBAIt) {
220         aMakeShapeList->appendAlgo(aRBAIt->makeShape);
221       }
222       aRBA.makeShape = aMakeShapeList;
223       aResultBaseAlgoList.clear();
224       aResultBaseAlgoList.push_back(aRBA);
225     }
226   }
227
228   // Store deleted shapes after all results has been proceeded. This is to avoid issue when in one
229   // result shape has been deleted, but in another it was modified or stayed.
230   if (!aResultCompound)
231     aResultCompound = GeomAlgoAPI_CompoundBuilder::compound(aResultShapesList);
232   FeaturesPlugin_Tools::loadDeletedShapes(aResultBaseAlgoList,
233                                           aTools.Objects(),
234                                           aResultCompound);
235
236   // remove the rest results if there were produced in the previous pass
237   removeResults(aResultIndex);
238 }