]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Pipe.cpp
Salome HOME
[Code coverage]: Move checking the algorithm's result into separate function
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Pipe.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "FeaturesPlugin_Pipe.h"
22
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_AttributeSelectionList.h>
25 #include <ModelAPI_AttributeString.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31 #include <GeomAlgoAPI_Pipe.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_Tools.h>
34
35 #include <GeomAPI_PlanarEdges.h>
36 #include <GeomAPI_ShapeExplorer.h>
37
38 #include <map>
39 #include <sstream>
40
41 static void storeSubShape(ResultBodyPtr theResultBody,
42                           const GeomShapePtr theShape,
43                           const GeomAPI_Shape::ShapeType theType,
44                           const std::string theName,
45                           int& theShapeIndex);
46
47 //==================================================================================================
48 FeaturesPlugin_Pipe::FeaturesPlugin_Pipe()
49 {
50 }
51
52 //==================================================================================================
53 void FeaturesPlugin_Pipe::initAttributes()
54 {
55   data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
56
57   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
58   data()->addAttribute(PATH_OBJECT_ID(), ModelAPI_AttributeSelection::typeId());
59
60   data()->addAttribute(BINORMAL_ID(), ModelAPI_AttributeSelection::typeId());
61
62   data()->addAttribute(LOCATIONS_ID(), ModelAPI_AttributeSelectionList::typeId());
63   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATIONS_ID());
64 }
65
66 //==================================================================================================
67 void FeaturesPlugin_Pipe::execute()
68 {
69   // Getting creation method.
70   std::string aCreationMethod = string(CREATION_METHOD())->value();
71
72   // Getting base objects.
73   ListOfShape aBaseShapesList, aBaseFacesList;
74   std::map<ResultConstructionPtr, ListOfShape> aSketchWiresMap;
75   AttributeSelectionListPtr aBaseObjectsSelectionList = selectionList(BASE_OBJECTS_ID());
76   if(!aBaseObjectsSelectionList.get()) {
77     setError("Error: Could not get base objects selection list.");
78     return;
79   }
80   if(aBaseObjectsSelectionList->size() == 0) {
81     setError("Error: Base objects list is empty.");
82     return;
83   }
84   for(int anIndex = 0; anIndex < aBaseObjectsSelectionList->size(); anIndex++) {
85     AttributeSelectionPtr aBaseObjectSelection = aBaseObjectsSelectionList->value(anIndex);
86     if(!aBaseObjectSelection.get()) {
87       setError("Error: One of the selected base objects is empty.");
88       return;
89     }
90     std::shared_ptr<GeomAPI_Shape> aBaseShape = aBaseObjectSelection->value();
91     if(aBaseShape.get() && !aBaseShape->isNull()) {
92       GeomAPI_Shape::ShapeType aST = aBaseShape->shapeType();
93       ResultConstructionPtr aConstruction =
94         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
95       if(aConstruction.get() && !aBaseShape->isEqual(aConstruction->shape()) &&
96           aST == GeomAPI_Shape::WIRE) {
97         // It is a wire on the sketch, store it to make face later.
98         aSketchWiresMap[aConstruction].push_back(aBaseShape);
99         continue;
100       } else {
101       aST == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
102                                    aBaseShapesList.push_back(aBaseShape);
103       }
104     } else {
105       // This may be the whole sketch result selected, check and get faces.
106       ResultConstructionPtr aConstruction =
107         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aBaseObjectSelection->context());
108       if(!aConstruction.get()) {
109         setError("Error: One of selected sketches does not have results.");
110         return;
111       }
112       int aFacesNum = aConstruction->facesNum();
113       if(aFacesNum == 0) {
114         // Probably it can be construction.
115         aBaseShape = aConstruction->shape();
116         if(aBaseShape.get() && !aBaseShape->isNull()) {
117           aBaseShape->shapeType() == GeomAPI_Shape::FACE ? aBaseFacesList.push_back(aBaseShape) :
118                                                            aBaseShapesList.push_back(aBaseShape);
119         }
120       } else {
121         for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
122           std::shared_ptr<GeomAPI_Shape> aBaseFace =
123             std::dynamic_pointer_cast<GeomAPI_Shape>(aConstruction->face(aFaceIndex));
124           if(!aBaseFace.get() || aBaseFace->isNull()) {
125             setError("Error: One of the faces on selected sketch is Null.");
126             return;
127           }
128           aBaseFacesList.push_back(aBaseFace);
129         }
130       }
131     }
132   }
133
134   // Make faces from sketch wires.
135   for(std::map<ResultConstructionPtr, ListOfShape>::const_iterator anIt = aSketchWiresMap.cbegin();
136       anIt != aSketchWiresMap.cend(); ++anIt) {
137     const std::shared_ptr<GeomAPI_PlanarEdges> aSketchPlanarEdges =
138       std::dynamic_pointer_cast<GeomAPI_PlanarEdges>((*anIt).first->shape());
139     const ListOfShape& aWiresList = (*anIt).second;
140     ListOfShape aFaces;
141     GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aSketchPlanarEdges->origin(),
142                                                aSketchPlanarEdges->norm(),
143                                                aWiresList,
144                                                aFaces);
145     aBaseFacesList.insert(aBaseFacesList.end(), aFaces.begin(), aFaces.end());
146   }
147
148   // Searching faces with common edges.
149   if(aCreationMethod == CREATION_METHOD_SIMPLE()) {
150     ListOfShape aShells;
151     ListOfShape aFreeFaces;
152     std::shared_ptr<GeomAPI_Shape> aFacesCompound =
153       GeomAlgoAPI_CompoundBuilder::compound(aBaseFacesList);
154     GeomAlgoAPI_ShapeTools::combineShapes(aFacesCompound, GeomAPI_Shape::SHELL,
155                                           aShells, aFreeFaces);
156     aBaseShapesList.insert(aBaseShapesList.end(), aFreeFaces.begin(), aFreeFaces.end());
157     aBaseShapesList.insert(aBaseShapesList.end(), aShells.begin(), aShells.end());
158   } else {
159     aBaseShapesList.insert(aBaseShapesList.end(), aBaseFacesList.begin(), aBaseFacesList.end());
160   }
161
162   // Getting path.
163   AttributeSelectionPtr aPathSelection = selection(PATH_OBJECT_ID());
164   if(!aPathSelection.get()) {
165     setError("Error: Path selection is empty.");
166     return;
167   }
168   std::shared_ptr<GeomAPI_Shape> aPathShape =
169     std::dynamic_pointer_cast<GeomAPI_Shape>(aPathSelection->value());
170   if(!aPathShape.get() && aPathSelection->context().get()) {
171     // Probaply it is a construction.
172     aPathShape = aPathSelection->context()->shape();
173   }
174   if(!aPathShape.get() || aPathShape->isNull()) {
175     setError("Error: Path shape is null.");
176     return;
177   }
178
179   // Getting Bi-Normal
180   std::shared_ptr<GeomAPI_Shape> aBiNormal;
181   if(aCreationMethod == CREATION_METHOD_BINORMAL()) {
182     AttributeSelectionPtr aBiNormalSelection = selection(BINORMAL_ID());
183     if(!aBiNormalSelection.get()) {
184       setError("Error: Bi-Normal selection is empty.");
185       return;
186     }
187     aBiNormal = std::dynamic_pointer_cast<GeomAPI_Shape>(aBiNormalSelection->value());
188     if(!aBiNormal.get() && aBiNormalSelection->context().get()) {
189       // Probably it is a construction.
190       aBiNormal = aBiNormalSelection->context()->shape();
191     }
192     if(!aBiNormal.get() || aBiNormal->isNull()) {
193       setError("Error: Bi-Normal shape is null.");
194       return;
195     }
196   }
197
198   // Getting locations.
199   ListOfShape aLocations;
200   if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
201     AttributeSelectionListPtr aLocationsSelectionList = selectionList(LOCATIONS_ID());
202     if(!aLocationsSelectionList.get()) {
203       setError("Error: Could not get locations selection list.");
204       return;
205     }
206     for(int anIndex = 0; anIndex < aLocationsSelectionList->size(); anIndex++) {
207       AttributeSelectionPtr aLocationSelection = aLocationsSelectionList->value(anIndex);
208       if(!aLocationSelection.get()) {
209         setError("Error: One of the selected location is empty.");
210         return;
211       }
212       std::shared_ptr<GeomAPI_Shape> aLocationShape = aLocationSelection->value();
213       if(!aLocationShape.get() && aLocationSelection->context().get()) {
214         // Probably it is a construction.
215         aLocationShape = aLocationSelection->context()->shape();
216       }
217       if(!aLocationShape.get() || aLocationShape->isNull()) {
218         setError("Error: One of the selected location shape is null.");
219         return;
220       }
221       aLocations.push_back(aLocationShape);
222     }
223   }
224
225   // Generating result for each object.
226   int aResultIndex = 0;
227   std::string anError;
228   if(aCreationMethod == CREATION_METHOD_SIMPLE() ||
229       aCreationMethod == CREATION_METHOD_BINORMAL()) {
230     for(ListOfShape::const_iterator
231         anIter = aBaseShapesList.cbegin(); anIter != aBaseShapesList.cend(); anIter++) {
232       std::shared_ptr<GeomAPI_Shape> aBaseShape = *anIter;
233
234       std::shared_ptr<GeomAlgoAPI_Pipe> aPipeAlgo(
235         aCreationMethod == CREATION_METHOD_SIMPLE() ? new GeomAlgoAPI_Pipe(aBaseShape,
236                                                                            aPathShape)
237                                                     : new GeomAlgoAPI_Pipe(aBaseShape,
238                                                                            aPathShape,
239                                                                            aBiNormal));
240
241       if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPipeAlgo, getKind(), anError)) {
242         setError(anError);
243         aResultIndex = 0;
244         break;
245       }
246
247       storeResult(aBaseShape, aPipeAlgo, aResultIndex++);
248     }
249   } else if(aCreationMethod == CREATION_METHOD_LOCATIONS()) {
250     std::shared_ptr<GeomAlgoAPI_Pipe> aPipeAlgo(new GeomAlgoAPI_Pipe(aBaseShapesList,
251                                                                      aLocations,
252                                                                      aPathShape));
253
254     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aPipeAlgo, getKind(), anError)) {
255       setError(anError);
256       removeResults(0);
257       return;
258     }
259
260     storeResult(aBaseShapesList, aPipeAlgo, aResultIndex++);
261   } else {
262     setError("Error: Wrong creation method.");
263     return;
264   }
265
266   removeResults(aResultIndex);
267 }
268
269 //==================================================================================================
270 void FeaturesPlugin_Pipe::storeResult(const std::shared_ptr<GeomAPI_Shape> theBaseShape,
271                                       const std::shared_ptr<GeomAlgoAPI_Pipe> thePipeAlgo,
272                                       const int theResultIndex)
273 {
274   // Create result body.
275   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
276
277   // Store generated shape.
278   aResultBody->storeGenerated(theBaseShape, thePipeAlgo->shape());
279
280   // Store generated edges/faces.
281   GeomAPI_Shape::ShapeType aBaseShapeType = theBaseShape->shapeType();
282   GeomAPI_Shape::ShapeType aShapeTypeToExplode;
283
284   switch(aBaseShapeType) {
285     case GeomAPI_Shape::VERTEX: {
286       aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
287       break;
288     }
289     case GeomAPI_Shape::EDGE:
290     case GeomAPI_Shape::WIRE: {
291       std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
292       GeomAlgoAPI_ShapeTools::findBounds(theBaseShape, aV1, aV2);
293       ListOfShape aV1History, aV2History;
294       thePipeAlgo->generated(aV1, aV1History);
295       thePipeAlgo->generated(aV2, aV2History);
296       if(!aV1History.empty()) {
297         aResultBody->generated(aV1, aV1History.front());
298       }
299       if(!aV2History.empty()) {
300         aResultBody->generated(aV2, aV2History.front());
301       }
302     }
303     case GeomAPI_Shape::FACE:
304     case GeomAPI_Shape::SHELL: {
305       aShapeTypeToExplode = GeomAPI_Shape::EDGE;
306       break;
307     }
308     case GeomAPI_Shape::COMPOUND: {
309       aShapeTypeToExplode = GeomAPI_Shape::COMPOUND;
310     }
311   }
312
313   if(aShapeTypeToExplode == GeomAPI_Shape::VERTEX ||
314       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
315     aResultBody->loadGeneratedShapes(thePipeAlgo, theBaseShape, GeomAPI_Shape::VERTEX);
316   }
317   if(aShapeTypeToExplode == GeomAPI_Shape::EDGE ||
318       aShapeTypeToExplode == GeomAPI_Shape::COMPOUND) {
319     aResultBody->loadGeneratedShapes(thePipeAlgo, theBaseShape, GeomAPI_Shape::EDGE);
320   }
321
322   // Store from shapes.
323   storeShapes(aResultBody, aBaseShapeType, thePipeAlgo->fromShapes(), "From_");
324
325   // Store to shapes.
326   storeShapes(aResultBody, aBaseShapeType, thePipeAlgo->toShapes(), "To_");
327
328   setResult(aResultBody, theResultIndex);
329 }
330
331 //==================================================================================================
332 void FeaturesPlugin_Pipe::storeResult(const ListOfShape& theBaseShapes,
333                                       const std::shared_ptr<GeomAlgoAPI_Pipe> thePipeAlgo,
334                                       const int theResultIndex)
335 {
336   // Create result body.
337   ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex);
338
339   // Store generated shape.
340   aResultBody->storeGenerated(theBaseShapes.front(), thePipeAlgo->shape());
341
342   // Store generated edges/faces.
343   for(ListOfShape::const_iterator anIter = theBaseShapes.cbegin();
344       anIter != theBaseShapes.cend();
345       ++anIter)
346   {
347     GeomShapePtr aBaseShape = *anIter;
348     GeomAPI_Shape::ShapeType aBaseShapeType = aBaseShape->shapeType();
349     GeomAPI_Shape::ShapeType aShapeTypeToExplode;
350     switch(aBaseShapeType) {
351       case GeomAPI_Shape::VERTEX: {
352         aShapeTypeToExplode = GeomAPI_Shape::VERTEX;
353         break;
354       }
355       case GeomAPI_Shape::EDGE:
356       case GeomAPI_Shape::WIRE: {
357         std::shared_ptr<GeomAPI_Vertex> aV1, aV2;
358         GeomAlgoAPI_ShapeTools::findBounds(aBaseShape, aV1, aV2);
359         ListOfShape aV1History, aV2History;
360         thePipeAlgo->generated(aV1, aV1History);
361         thePipeAlgo->generated(aV2, aV2History);
362         aResultBody->generated(aV1, aV1History.front());
363         aResultBody->generated(aV2, aV2History.front());
364       }
365       case GeomAPI_Shape::FACE:
366       case GeomAPI_Shape::SHELL: {
367         aShapeTypeToExplode = GeomAPI_Shape::EDGE;
368         break;
369       }
370     }
371     aResultBody->loadGeneratedShapes(thePipeAlgo, aBaseShape, aShapeTypeToExplode);
372   }
373
374   // Store from shapes.
375   storeShapes(aResultBody, theBaseShapes.front()->shapeType(), thePipeAlgo->fromShapes(), "From_");
376
377   // Store to shapes.
378   storeShapes(aResultBody, theBaseShapes.back()->shapeType(), thePipeAlgo->toShapes(), "To_");
379
380
381   setResult(aResultBody, theResultIndex);
382 }
383
384 //==================================================================================================
385 void FeaturesPlugin_Pipe::storeShapes(ResultBodyPtr theResultBody,
386                                       const GeomAPI_Shape::ShapeType theBaseShapeType,
387                                       const ListOfShape& theShapes,
388                                       const std::string theName)
389 {
390   GeomAPI_Shape::ShapeType aShapeTypeToExplore = GeomAPI_Shape::FACE;
391   std::string aShapeTypeStr = "Face";
392   switch(theBaseShapeType) {
393     case GeomAPI_Shape::VERTEX: {
394       aShapeTypeToExplore = GeomAPI_Shape::VERTEX;
395       aShapeTypeStr = "Vertex";
396       break;
397     }
398     case GeomAPI_Shape::EDGE:
399     case GeomAPI_Shape::WIRE: {
400       aShapeTypeToExplore = GeomAPI_Shape::EDGE;
401       aShapeTypeStr = "Edge";
402       break;
403     }
404     case GeomAPI_Shape::FACE:
405     case GeomAPI_Shape::SHELL: {
406       aShapeTypeToExplore = GeomAPI_Shape::FACE;
407       aShapeTypeStr = "Face";
408       break;
409     }
410     case GeomAPI_Shape::COMPOUND: {
411       aShapeTypeToExplore = GeomAPI_Shape::COMPOUND;
412       break;
413     }
414   }
415
416   // Store shapes.
417   int aShapeIndex = 1;
418   int aFaceIndex = 1;
419   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
420     GeomShapePtr aShape = *anIt;
421
422     if(aShapeTypeToExplore == GeomAPI_Shape::COMPOUND) {
423       std::string aName = theName + (aShape->shapeType() == GeomAPI_Shape::EDGE ? "Edge" : "Face");
424       storeSubShape(theResultBody, aShape,
425                     aShape->shapeType(),
426                     aName,
427                     aShape->shapeType() == GeomAPI_Shape::EDGE ? aShapeIndex : aFaceIndex);
428     } else {
429       std::string aName = theName + aShapeTypeStr;
430       storeSubShape(theResultBody, aShape, aShapeTypeToExplore, aName, aShapeIndex);
431     }
432   }
433 }
434
435 //==================================================================================================
436 void storeSubShape(ResultBodyPtr theResultBody,
437                    const GeomShapePtr theShape,
438                    const GeomAPI_Shape::ShapeType theType,
439                    const std::string theName,
440                    int& theShapeIndex)
441 {
442   for(GeomAPI_ShapeExplorer anExp(theShape, theType); anExp.more(); anExp.next()) {
443     GeomShapePtr aSubShape = anExp.current();
444     std::ostringstream aStr;
445     aStr << theName << "_" << theShapeIndex++;
446     theResultBody->generated(aSubShape, aStr.str());
447   }
448 }