Salome HOME
Issue #1786 : make Recover feature duplicate the result, not re-create a previous...
[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_Tools.h>
18 #include <GeomAlgoAPI_Copy.h>
19
20 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
21 {
22 }
23
24 void FeaturesPlugin_Recover::initAttributes()
25 {
26   data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
27   data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
28
29 }
30
31 void FeaturesPlugin_Recover::execute()
32 {
33   int aResultIndex = 0;
34   AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
35   for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
36     ObjectPtr anObj = aRecovered->object(anIndex);
37     if (!anObj.get())
38       continue;
39     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
40     if (!aResult.get())
41       continue;
42     GeomShapePtr aShape = aResult->shape();
43     if (!aShape.get())
44       continue;
45
46     // Copy shape.
47     GeomAlgoAPI_Copy aCopyAlgo(aShape);
48     // Check that algo is done.
49     if(!aCopyAlgo.isDone()) {
50       setError("Error: recover algorithm failed.");
51       return;
52     }
53     // Check if shape is not null.
54     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
55       setError("Error: resulting shape is null.");
56       return;
57     }
58     // Check that resulting shape is valid.
59     if(!aCopyAlgo.isValid()) {
60       setError("Error: resulting shape is not valid.");
61       return;
62     }
63
64     // Store result.
65     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
66     aResultBody->store(aCopyAlgo.shape());//, aCopyAlgo.shape());
67     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aCopyAlgo.mapOfSubShapes();
68     // like in import: forget any history
69     int aTag(1);
70     std::string aNameMS = "Shape";
71     aResultBody->loadFirstLevel(aCopyAlgo.shape(), aNameMS, aTag);
72
73     setResult(aResultBody, aResultIndex);
74     ++aResultIndex;
75   }
76
77   removeResults(aResultIndex);
78 }