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