Salome HOME
Fix for the issue #1755 : for now the sketch solver receives all modified entities...
[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
19 using namespace std;
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   data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
30
31   myPersistent = boolean(PERSISTENT())->value();
32   synchronizeRegistered();
33 }
34
35 void FeaturesPlugin_Recover::execute()
36 {
37   synchronizeRegistered();
38 }
39
40 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
41 {
42   synchronizeRegistered();
43 }
44
45 void FeaturesPlugin_Recover::synchronizeRegistered()
46 {
47   FeaturePtr aBase = baseFeature();
48   bool aNewPersistent = boolean(PERSISTENT())->value();
49   if (aNewPersistent != myPersistent || myCurrentBase != aBase)
50     clearRegistered();
51
52   std::set<ObjectPtr> aRecoveredInList;
53   // add unconcealed which are not in the myRegistered map
54   if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
55     AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
56     for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
57       ObjectPtr anObj = aRecovered->object(anIndex);
58       aRecoveredInList.insert(anObj);
59       if (myRegistered.find(anObj) == myRegistered.end()) {
60         // not found, so register a new
61         ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
62         if (aRes.get()) { // this may be on first update after "open"
63           ModelAPI_Session::get()->validators()->registerUnconcealment(
64             aRes, aNewPersistent ? FeaturePtr() : aBase);
65           myRegistered.insert(anObj);
66         }
67       }
68     }
69   }
70   // remove unconcealed which are not in the stored list, but in the map
71   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
72   while(aMyReg != myRegistered.end()) {
73     if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
74       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
75       ModelAPI_Session::get()->validators()->disableUnconcealment(
76         aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
77       myRegistered.erase(aMyReg);
78       aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
79     } else {
80       aMyReg++;
81     }
82   }
83   myCurrentBase = aBase;
84   myPersistent = aNewPersistent;
85 }
86
87 void FeaturesPlugin_Recover::erase()
88 {
89   // clears myRegistered before all information is destroyed
90   clearRegistered();
91   ModelAPI_Feature::erase();
92 }
93
94 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
95 {
96   bool aRes = ModelAPI_Feature::setStable(theFlag);
97   synchronizeRegistered();
98   return aRes;
99 }
100
101 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
102 {
103   bool aRes = ModelAPI_Feature::setDisabled(theFlag);
104   synchronizeRegistered();
105   return aRes;
106 }
107
108 FeaturePtr FeaturesPlugin_Recover::baseFeature()
109 {
110   // for the current moment it can be result of feature of feature: GUI is not debugged
111   ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
112   FeaturePtr aResult;
113   if (aBaseObj.get() == NULL)
114     return aResult;
115   aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
116   if (aResult.get() == NULL)
117     aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
118   return aResult;
119 }
120
121 void FeaturesPlugin_Recover::clearRegistered()
122 {
123   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
124   for(; aMyReg != myRegistered.end(); aMyReg++) {
125     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
126     ModelAPI_Session::get()->validators()->disableUnconcealment(
127       aRes, myPersistent ? FeaturePtr() : myCurrentBase);
128   }
129   myRegistered.clear();
130 }