Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Recover.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        FeaturesPlugin_Recover.cpp
4 // Created:     29 Jul 2016
5 // Author:      Natalia ERMOLAEVA
6
7 #include "FeaturesPlugin_Recover.h"
8
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_AttributeReference.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <ModelAPI_Session.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_Result.h>
17 #include <ModelAPI_ResultBody.h>
18 #include <ModelAPI_Tools.h>
19 #include <GeomAlgoAPI_Copy.h>
20
21 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
22 {
23 }
24
25 void FeaturesPlugin_Recover::initAttributes()
26 {
27   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
28   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
29
30 }
31
32 void FeaturesPlugin_Recover::execute()
33 {
34   int aResultIndex = 0;
35   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
36   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
37     ObjectPtr anObj = aRecovered->object(anIndex);
38     if (!anObj.get())
39       continue;
40     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
41     if (!aResult.get())
42       continue;
43     GeomShapePtr aShape = aResult->shape();
44     if (!aShape.get())
45       continue;
46
47     // Copy shape.
48     GeomAlgoAPI_Copy aCopyAlgo(aShape);
49     // Check that algo is done.
50     if(!aCopyAlgo.isDone()) {
51       setError("Error: recover algorithm failed.");
52       return;
53     }
54     // Check if shape is not null.
55     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
56       setError("Error: resulting shape is null.");
57       return;
58     }
59     // Check that resulting shape is valid.
60     if(!aCopyAlgo.isValid()) {
61       setError("Error: resulting shape is not valid.");
62       return;
63     }
64
65     // Store result.
66     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
67     aResultBody->store(aCopyAlgo.shape());//, aCopyAlgo.shape());
68     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aCopyAlgo.mapOfSubShapes();
69     // like in import: forget any history
70     int aTag(1);
71     std::string aNameMS = "Shape";
72     aResultBody->loadFirstLevel(aCopyAlgo.shape(), aNameMS, aTag);
73
74     setResult(aResultBody, aResultIndex);
75     ++aResultIndex;
76   }
77
78   removeResults(aResultIndex);
79 }