Salome HOME
Get rid of compilation warnings. Part I.
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Tools.cpp
1 // Copyright (C) 2014-2020  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_Tools.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_ResultPart.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_CompoundBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30
31 #include <GeomAPI_PlanarEdges.h>
32 #include <GeomAPI_ShapeIterator.h>
33
34 //==================================================================================================
35 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
36                                               const ListOfShape& theBaseShapes,
37                                               const ListOfShape& theTools,
38                                               const GeomMakeShapePtr& theMakeShape,
39                                               const GeomShapePtr theResultShape,
40                                               const std::string& theNamePrefix)
41 {
42   theResultBody->storeModified(theBaseShapes, theResultShape, theMakeShape);
43
44   ListOfShape aShapes = theBaseShapes;
45   ListOfShape::const_iterator aToolIter = theTools.cbegin();
46   for(; aToolIter != theTools.cend(); aToolIter++)
47     aShapes.push_back(*aToolIter);
48
49   for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); ++anIter)
50   {
51     theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::VERTEX, theNamePrefix);
52     theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::EDGE, theNamePrefix);
53     theResultBody->loadModifiedShapes(theMakeShape, *anIter, GeomAPI_Shape::FACE, theNamePrefix);
54   }
55 }
56
57 //==================================================================================================
58 void FeaturesPlugin_Tools::loadModifiedShapes(ResultBodyPtr theResultBody,
59                                               const GeomShapePtr& theBaseShape,
60                                               const GeomMakeShapePtr& theMakeShape,
61                                               const std::string theName)
62 {
63   switch(theBaseShape->shapeType()) {
64     case GeomAPI_Shape::COMPOUND: {
65       for(GeomAPI_ShapeIterator anIt(theBaseShape); anIt.more(); anIt.next())
66       {
67         loadModifiedShapes(theResultBody,
68                            anIt.current(),
69                            theMakeShape,
70                            theName);
71       }
72       break;
73     }
74     case GeomAPI_Shape::COMPSOLID:
75     case GeomAPI_Shape::SOLID:
76     case GeomAPI_Shape::SHELL: {
77       theResultBody->loadModifiedShapes(theMakeShape,
78                                         theBaseShape,
79                                         GeomAPI_Shape::FACE,
80                                         theName);
81     }
82     case GeomAPI_Shape::FACE:
83     case GeomAPI_Shape::WIRE: {
84       theResultBody->loadModifiedShapes(theMakeShape,
85                                         theBaseShape,
86                                         GeomAPI_Shape::EDGE,
87                                         theName);
88     }
89     case GeomAPI_Shape::EDGE: {
90       theResultBody->loadModifiedShapes(theMakeShape,
91                                         theBaseShape,
92                                         GeomAPI_Shape::VERTEX,
93                                         theName);
94     }
95     default: // [to avoid compilation warning]
96       break;
97   }
98 }
99
100 //==================================================================================================
101 void FeaturesPlugin_Tools::loadDeletedShapes(ResultBodyPtr theResultBody,
102   const GeomShapePtr theBaseShape,
103   const ListOfShape& theTools,
104   const GeomMakeShapePtr& theMakeShape,
105   const GeomShapePtr theResultShapesCompound)
106 {
107   ListOfShape aShapes = theTools;
108   if (theBaseShape.get())
109     aShapes.push_front(theBaseShape);
110
111   for (ListOfShape::const_iterator anIter = aShapes.begin(); anIter != aShapes.end(); anIter++)
112   {
113     theResultBody->loadDeletedShapes(theMakeShape,
114                                      *anIter,
115                                      GeomAPI_Shape::VERTEX,
116                                      theResultShapesCompound);
117     theResultBody->loadDeletedShapes(theMakeShape,
118                                      *anIter,
119                                      GeomAPI_Shape::EDGE,
120                                      theResultShapesCompound);
121     theResultBody->loadDeletedShapes(theMakeShape,
122                                      *anIter,
123                                      GeomAPI_Shape::FACE,
124                                      theResultShapesCompound);
125     // store information about deleted solids because of unittest TestBooleanCommon_SolidsHistory
126     // on OCCT 7.4.0 : common produces modified compsolid, so, move to the end for removed solids
127     // starts to produce whole compsolid
128     theResultBody->loadDeletedShapes(theMakeShape,
129                                      *anIter,
130                                      GeomAPI_Shape::SOLID,
131                                      theResultShapesCompound);
132   }
133 }
134
135 //==================================================================================================
136 void FeaturesPlugin_Tools::loadDeletedShapes(
137   std::vector<ResultBaseAlgo>& theResultBaseAlgoList,
138   const ListOfShape& theTools,
139   const GeomShapePtr theResultShapesCompound)
140 {
141   for (std::vector<ResultBaseAlgo>::iterator anIt = theResultBaseAlgoList.begin();
142        anIt != theResultBaseAlgoList.end();
143        ++anIt)
144   {
145     ResultBaseAlgo& aRCA = *anIt;
146     loadDeletedShapes(aRCA.resultBody,
147                       aRCA.baseShape,
148                       theTools,
149                       aRCA.makeShape,
150                       theResultShapesCompound);
151   }
152 }
153
154 //==================================================================================================
155 bool FeaturesPlugin_Tools::getShape(const AttributeSelectionListPtr theSelectionList,
156                                     const bool theShareTopology,
157                                     ListOfShape& theShapesList,
158                                     std::string& theError)
159 {
160   theShapesList.clear();
161
162   ListOfShape aBaseFacesList;
163   std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
164   if(!theSelectionList.get()) {
165     theError = "Error: Could not get base objects selection list.";
166     return false;
167   }
168   if(theSelectionList->size() == 0) {
169     theError = "Error: Base objects list is empty.";
170     return false;
171   }
172   for(int anIndex = 0; anIndex < theSelectionList->size(); anIndex++) {
173     AttributeSelectionPtr aBaseObjectSelection = theSelectionList->value(anIndex);
174     if(!aBaseObjectSelection.get()) {
175       theError = "Error: Selected base object is empty.";
176       return false;
177     }
178     GeomShapePtr aBaseShape = aBaseObjectSelection->value();
179     if(aBaseShape.get() && !aBaseShape->isNull()) {
180       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
181       if(aST == GeomAPI_Shape::SOLID || aST == GeomAPI_Shape::COMPSOLID) {
182         theError = "Error: Selected shapes has unsupported type.";
183         return false;
184       }
185       ResultConstructionPtr aConstruction =
186         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
187       if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
188           aST == GeomAPI_Shape::WIRE) {
189         // It is a wire on the sketch, store it to make face later.
190         aSketchWiresMap[aConstruction].push_back(aBaseShape);
191         continue;
192       } else {
193       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
194                                    theShapesList.push_back(aBaseShape);
195       }
196     } else {
197       // This may be the whole sketch result selected, check and get faces.
198       ResultConstructionPtr aConstruction =
199         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
200       if(!aConstruction.get()) {
201         theError = "Error: Selected sketches does not have results.";
202         return false;
203       }
204       int aFacesNum = aConstruction->facesNum();
205       if(aFacesNum == 0) {
206         // Probably it can be construction.
207         aBaseShape = aConstruction->shape();
208         if(aBaseShape.get() && !aBaseShape->isNull()) {
209           GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
210           if(aST != GeomAPI_Shape::VERTEX && aST != GeomAPI_Shape::EDGE &&
211              aST != GeomAPI_Shape::WIRE &&
212              aST != GeomAPI_Shape::FACE && aST != GeomAPI_Shape::SHELL) {
213             theError = "Error: Selected shapes has unsupported type.";
214             return false;
215           }
216           aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
217                                        theShapesList.push_back(aBaseShape);
218         }
219       } else {
220         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
221           GeomShapePtr aBaseFace = aConstruction->face(aFaceIndex);
222           if(!aBaseFace.get() || aBaseFace->isNull()) {
223             theError = "Error: One of the faces on selected sketch is null.";
224             return false;
225           }
226           aBaseFacesList.push_back(aBaseFace);
227         }
228       }
229     }
230   }
231
232   // Make faces from sketch wires.
233   for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
234       anIt != aSketchWiresMap.cend(); ++anIt) {
235     const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
236         std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
237     const ListOfShape& aWiresList = (*anIt).second;
238     ListOfShape aFaces;
239     GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
240                                                aSketchPlanarEdges->norm(),
241                                                aWiresList,
242                                                aFaces);
243     aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
244   }
245
246   // Searching faces with common edges.
247   if(theShareTopology && aBaseFacesList.size() > 1) {
248     GeomShapePtr aFacesCompound = GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
249     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL, theShapesList);
250   } else {
251     theShapesList.insert(theShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
252   }
253   return true;
254 }
255
256 //==================================================================================================
257 bool FeaturesPlugin_Tools::shapesFromSelectionList(
258     const std::shared_ptr<ModelAPI_AttributeSelectionList> theSelectionList,
259     const bool theStoreFullHierarchy,
260     GeomAPI_ShapeHierarchy& theHierarchy,
261     std::list<ResultPtr>& theParts)
262 {
263   int aSize = theSelectionList->size();
264   for (int anObjectsIndex = 0; anObjectsIndex < aSize; anObjectsIndex++) {
265     AttributeSelectionPtr anObjectAttr = theSelectionList->value(anObjectsIndex);
266     std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
267     if (!anObject.get()) { // may be for not-activated parts
268       return false;
269     }
270     ResultPtr aContext = anObjectAttr->context();
271     if (aContext && aContext->groupName() == ModelAPI_ResultPart::group())
272       theParts.push_back(aContext);
273     else {
274       // store full shape hierarchy for the corresponding version only
275       theHierarchy.addObject(anObject);
276       if (theStoreFullHierarchy)
277         ModelAPI_Tools::fillShapeHierarchy(anObject, aContext, theHierarchy);
278     }
279   }
280   return true;
281 }