Salome HOME
Issue #1860: fix end lines with spaces
[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
76           aFaceExp(*anIt, GeomAPI_Shape::FACE); aFaceExp.more(); aFaceExp.next()) {
77         GeomShapePtr aFace = aFaceExp.current();
78         ListOfShape aHistory;
79         aSewingAlgo.modified(aFace, aHistory);
80         for(ListOfShape::const_iterator aHistoryIt = aHistory.cbegin();
81             aHistoryIt != aHistory.cend();
82             ++aHistoryIt) {
83           GeomShapePtr aHistoryShape = *aHistoryIt;
84           if(aMapOfShapes->isBound(aHistoryShape)) {
85             aHistoryShape = aMapOfShapes->find(aHistoryShape);
86           }
87           if(aShell->isSubShape(aHistoryShape)) {
88             aResultBody->loadAndOrientModifiedShapes(&aSewingAlgo, aFace, GeomAPI_Shape::EDGE,
89                                                      1, "Modified_Edge", *aMapOfShapes.get());
90             aResultBody->loadAndOrientModifiedShapes(&aSewingAlgo, aFace, GeomAPI_Shape::FACE,
91                                                      2, "Modified_Face", *aMapOfShapes.get());
92             break;
93           }
94         }
95       }
96     }
97     setResult(aResultBody, anIndex);
98     ++anIndex;
99   }
100
101   removeResults(anIndex);
102 }