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