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>
19 FeaturesPlugin_Recover::FeaturesPlugin_Recover()
23 void FeaturesPlugin_Recover::initAttributes()
25 data()->addAttribute(BASE_FEATURE(), ModelAPI_AttributeReference::typeId());
26 data()->addAttribute(RECOVERED_ENTITIES(), ModelAPI_AttributeRefList::typeId());
27 data()->addAttribute(PERSISTENT(), ModelAPI_AttributeBoolean::typeId());
29 myPersistent = boolean(PERSISTENT())->value();
30 synchronizeRegistered();
33 void FeaturesPlugin_Recover::execute()
35 synchronizeRegistered();
38 void FeaturesPlugin_Recover::attributeChanged(const std::string& theID)
40 synchronizeRegistered();
43 void FeaturesPlugin_Recover::synchronizeRegistered()
45 FeaturePtr aBase = baseFeature();
46 bool aNewPersistent = boolean(PERSISTENT())->value();
47 if (aNewPersistent != myPersistent || myCurrentBase != aBase)
50 std::set<ObjectPtr> aRecoveredInList;
51 // add unconcealed which are not in the myRegistered map
52 if (isStable() && !isDisabled()) { // if unstable, clear any unconcealment effect
53 AttributeRefListPtr aRecovered = reflist(RECOVERED_ENTITIES());
54 for(int anIndex = aRecovered->size() - 1; anIndex >= 0; anIndex--) {
55 ObjectPtr anObj = aRecovered->object(anIndex);
56 aRecoveredInList.insert(anObj);
57 if (myRegistered.find(anObj) == myRegistered.end()) {
58 // not found, so register a new
59 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
60 if (aRes.get()) { // this may be on first update after "open"
61 ModelAPI_Session::get()->validators()->registerUnconcealment(
62 aRes, aNewPersistent ? FeaturePtr() : aBase);
63 myRegistered.insert(anObj);
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
81 myCurrentBase = aBase;
82 myPersistent = aNewPersistent;
85 void FeaturesPlugin_Recover::erase()
87 // clears myRegistered before all information is destroyed
89 ModelAPI_Feature::erase();
92 bool FeaturesPlugin_Recover::setStable(const bool theFlag)
94 bool aRes = ModelAPI_Feature::setStable(theFlag);
95 synchronizeRegistered();
99 bool FeaturesPlugin_Recover::setDisabled(const bool theFlag)
101 bool aRes = ModelAPI_Feature::setDisabled(theFlag);
102 synchronizeRegistered();
106 FeaturePtr FeaturesPlugin_Recover::baseFeature()
108 // for the current moment it can be result of feature of feature: GUI is not debugged
109 ObjectPtr aBaseObj = reference(BASE_FEATURE())->value();
111 if (aBaseObj.get() == NULL)
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));
119 void FeaturesPlugin_Recover::clearRegistered()
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);
127 myRegistered.clear();