]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Recover.cpp
Salome HOME
Fix for the issue #1682 and the problem of selection of planes for partition in test...
[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         ModelAPI_Session::get()->validators()->registerUnconcealment(
63           aRes, aNewPersistent ? FeaturePtr() : aBase);
64         myRegistered.insert(anObj);
65       }
66     }
67   }
68   // remove unconcealed which are not in the stored list, but in the map
69   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
70   while(aMyReg != myRegistered.end()) {
71     if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
72       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
73       ModelAPI_Session::get()->validators()->disableUnconcealment(
74         aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
75       myRegistered.erase(aMyReg);
76       aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
77     } else {
78       aMyReg++;
79     }
80   }
81   myCurrentBase = aBase;
82   myPersistent = aNewPersistent;
83 }
84
85 void FeaturesPlugin_Recover::erase()
86 {
87   // clears myRegistered before all information is destroyed
88   clearRegistered();
89   ModelAPI_Feature::erase();
90 }
91
92 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
93 {
94   bool aRes = ModelAPI_Feature::setStable(theFlag);
95   synchronizeRegistered();
96   return aRes;
97 }
98
99 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
100 {
101   bool aRes = ModelAPI_Feature::setDisabled(theFlag);
102   synchronizeRegistered();
103   return aRes;
104 }
105
106 FeaturePtr FeaturesPlugin_Recover::baseFeature()
107 {
108   // for the current moment it can be result of feature of feature: GUI is not debugged
109   ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
110   FeaturePtr aResult;
111   if (aBaseObj.get() == NULL)
112     return aResult;
113   aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
114   if (aResult.get() == NULL)
115     aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
116   return aResult;
117 }
118
119 void FeaturesPlugin_Recover::clearRegistered()
120 {
121   std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
122   for(; aMyReg != myRegistered.end(); aMyReg++) {
123     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
124     ModelAPI_Session::get()->validators()->disableUnconcealment(
125       aRes, myPersistent ? FeaturePtr() : myCurrentBase);
126   }
127   myRegistered.clear();
128 }