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 // temporary modification for empty list
33 // ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), RECOVERED_ENTITIES());
35 synchronizeRegistered();
38 void FeaturesPlugin_Recover::execute()
40 synchronizeRegistered();
43 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
45 synchronizeRegistered();
48 void FeaturesPlugin_Recover::synchronizeRegistered()
50 FeaturePtr aBase = baseFeature();
51 bool aNewPersistent = boolean(PERSISTENT())->value();
52 if (aNewPersistent != myPersistent)
55 std::set<ObjectPtr> aRecoveredInList;
56 // add unconcealed which are not in the myRegistered map
57 if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
58 AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
59 for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
60 ObjectPtr anObj = aRecovered->object(anIndex);
61 aRecoveredInList.insert(anObj);
62 if (myRegistered.find(anObj) == myRegistered.end()) {
63 // not found, so register a new
64 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
65 ModelAPI_Session::get()->validators()->registerUnconcealment(
66 aRes, aNewPersistent ? FeaturePtr() : aBase);
67 myRegistered.insert(anObj);
71 // remove unconcealed which are not in the stored list, but in the map
72 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
73 while(aMyReg != myRegistered.end()) {
74 if (aRecoveredInList.find(*aMyReg) == aRecoveredInList.end()) {
75 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
76 ModelAPI_Session::get()->validators()->disableUnconcealment(
77 aRes, aNewPersistent ? FeaturePtr() : myCurrentBase);
78 myRegistered.erase(aMyReg);
79 aMyReg = myRegistered.begin(); // restart iteration because after erase iterator may be bad
84 myCurrentBase = aBase;
85 myPersistent = aNewPersistent;
88 void FeaturesPlugin_Recover::erase()
90 // clears myRegistered before all information is destroyed
92 ModelAPI_Feature::erase();
95 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
97 bool aRes = ModelAPI_Feature::setStable(theFlag);
98 synchronizeRegistered();
102 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
104 bool aRes = ModelAPI_Feature::setDisabled(theFlag);
105 synchronizeRegistered();
109 FeaturePtr FeaturesPlugin_Recover::baseFeature()
111 // for the current moment it can be result of feature of feature: GUI is not debugged
112 ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
114 if (aBaseObj.get() == NULL)
116 aResult = std::dynamic_pointer_cast<ModelAPI_Feature>(aBaseObj);
117 if (aResult.get() == NULL)
118 aResult = aBaseObj->document()->feature(std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObj));
122 void FeaturesPlugin_Recover::clearRegistered()
124 std::set<std::shared_ptr<ModelAPI_Object> >::iterator aMyReg = myRegistered.begin();
125 for(; aMyReg != myRegistered.end(); aMyReg++) {
126 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(*aMyReg);
127 ModelAPI_Session::get()->validators()->disableUnconcealment(
128 aRes, myPersistent ? FeaturePtr() : myCurrentBase);
130 myRegistered.clear();