Salome HOME
Issue #1369: Added "Create Shell" feature.
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Shell.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_Shell.cpp
4 // Created:     14 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Shell.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12
13 #include <GeomAlgoAPI_Sewing.h>
14 #include <GeomAPI_ShapeExplorer.h>
15
16 //=================================================================================================
17 BuildPlugin_Shell::BuildPlugin_Shell()
18 {
19 }
20
21 //=================================================================================================
22 void BuildPlugin_Shell::initAttributes()
23 {
24   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
25 }
26
27 //=================================================================================================
28 void BuildPlugin_Shell::execute()
29 {
30   // Get base objects list.
31   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
32
33   // Collect base shapes.
34   ListOfShape aShapes;
35   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
36     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
37     GeomShapePtr aShape = aSelection->value();
38     if(!aShape.get()) {
39       aShape = aSelection->context()->shape();
40     }
41     aShapes.push_back(aShape);
42   }
43
44   // Sew faces.
45   GeomAlgoAPI_Sewing aSewingAlgo(aShapes);
46
47   // Check that algo is done.
48   if(!aSewingAlgo.isDone()) {
49     setError("Error: " + getKind() + " algorithm failed.");
50     return;
51   }
52
53   // Check if shape is not null.
54   if(!aSewingAlgo.shape().get() || aSewingAlgo.shape()->isNull()) {
55     setError("Error: Resulting shape is null.");
56     return;
57   }
58
59   // Check that resulting shape is valid.
60   if(!aSewingAlgo.isValid()) {
61     setError("Error: Resulting shape is not valid.");
62     return;
63   }
64
65   // Store result.
66   GeomShapePtr aResult = aSewingAlgo.shape();
67   std::shared_ptr<GeomAPI_DataMapOfShapeShape> aMapOfShapes = aSewingAlgo.mapOfSubShapes();
68
69   int anIndex = 0;
70   for(GeomAPI_ShapeExplorer anExp(aResult, GeomAPI_Shape::SHELL); anExp.more(); anExp.next()) {
71     GeomShapePtr aShell = anExp.current();
72     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
73     aResultBody->store(aShell);
74     for(ListOfShape::const_iterator anIt = aShapes.cbegin(); anIt != aShapes.cend(); ++anIt) {
75       for(GeomAPI_ShapeExplorer aFaceExp(*anIt, GeomAPI_Shape::FACE); aFaceExp.more(); aFaceExp.next()) {
76         GeomShapePtr aFace = aFaceExp.current();
77         ListOfShape aHistory;
78         aSewingAlgo.modified(aFace, aHistory);
79         for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
80             aHistoryIt != aHistory.cend();
81             ++aHistoryIt) {
82           if(aShell->isSubShape(*aHistoryIt)) {
83             aResultBody->loadAndOrientModifiedShapes(&aSewingAlgo, aFace, GeomAPI_Shape::EDGE,
84                                                      1, "Modified", *aMapOfShapes.get());
85           }
86         }
87       }
88     }
89     setResult(aResultBody, anIndex);
90     ++anIndex;
91   }
92
93   removeResults(anIndex);
94 }