1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: FeaturesPlugin_Recover.cpp
4 // Created: 29 Jul 2016
5 // Author: Natalia ERMOLAEVA
7 #include "FeaturesPlugin_Recover.h"
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>
21 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
25 void FeaturesPlugin_Recover::initAttributes()
27 data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
28 data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
29 data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
31 myPersistent = boolean(PERSISTENT())->value();
32 synchronizeRegistered();
35 void FeaturesPlugin_Recover::execute()
37 synchronizeRegistered();
40 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
42 synchronizeRegistered();
45 void FeaturesPlugin_Recover::synchronizeRegistered()
47 FeaturePtr aBase = baseFeature();
48 bool aNewPersistent = boolean(PERSISTENT())->value();
49 if (aNewPersistent != myPersistent || myCurrentBase != aBase)
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);
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
83 myCurrentBase = aBase;
84 myPersistent = aNewPersistent;
87 void FeaturesPlugin_Recover::erase()
89 // clears myRegistered before all information is destroyed
91 ModelAPI_Feature::erase();
94 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
96 bool aRes = ModelAPI_Feature::setStable(theFlag);
97 synchronizeRegistered();
101 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
103 bool aRes = ModelAPI_Feature::setDisabled(theFlag);
104 synchronizeRegistered();
108 FeaturePtr FeaturesPlugin_Recover::baseFeature()
110 // for the current moment it can be result of feature of feature: GUI is not debugged
111 ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
113 if (aBaseObj.get() == NULL)
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));
121 void FeaturesPlugin_Recover::clearRegistered()
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);
129 myRegistered.clear();