Salome HOME
Prepare version 1.2.1: quick fix for iteration 2 release
[modules/shaper.git] / src / Model / Model_Objects.cpp
index 98ead6c683a63cca7fdcce4127b58a5fdc24fdb1..54033a49b0e8c519d48f41d548692550fe8c5934 100644 (file)
@@ -105,11 +105,8 @@ void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterT
   if (!theFeature->isAction()) {  // do not add action to the data model
     TDF_Label aFeaturesLab = featuresLabel();
     TDF_Label aFeatureLab = aFeaturesLab.NewChild();
-    initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
-    // keep the feature ID to restore document later correctly
-    TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
-    myFeatures.Bind(aFeatureLab, theFeature);
-    // store feature in the features array
+    // store feature in the features array: before "initData" because in macro features
+    // in initData it creates new features, appeared later than this
     TDF_Label aPrevFeateureLab;
     if (theAfterThis.get()) { // searching for the previous feature label
       std::shared_ptr<Model_Data> aPrevData = 
@@ -119,11 +116,19 @@ void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterT
       }
     }
     AddToRefArray(aFeaturesLab, aFeatureLab, aPrevFeateureLab);
+
+    initData(theFeature, aFeatureLab, TAG_FEATURE_ARGUMENTS);
+    // keep the feature ID to restore document later correctly
+    TDataStd_Comment::Set(aFeatureLab, theFeature->getKind().c_str());
+    myFeatures.Bind(aFeatureLab, theFeature);
     // event: feature is added
     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
     ModelAPI_EventCreator::get()->sendUpdated(theFeature, anEvent);
     theFeature->setDisabled(false); // by default created feature is enabled
     updateHistory(ModelAPI_Feature::group());
+  } else { // make feature has not-null data anyway
+    theFeature->setData(Model_Data::invalidData());
+    theFeature->setDoc(myDoc);
   }
 }
 
@@ -201,14 +206,7 @@ void Model_Objects::refsToFeature(FeaturePtr theFeature,
 void Model_Objects::removeFeature(FeaturePtr theFeature)
 {
   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theFeature->data());
-  if (aData) {
-    TDF_Label aFeatureLabel = aData->label().Father();
-    if (myFeatures.IsBound(aFeatureLabel))
-      myFeatures.UnBind(aFeatureLabel);
-    else
-      return;  // not found feature => do not remove
-
-    clearHistory(theFeature);
+  if (aData && aData->isValid()) {
     // checking that the sub-element of composite feature is removed: if yes, inform the owner
     std::set<std::shared_ptr<ModelAPI_Feature> > aRefs;
     refsToFeature(theFeature, aRefs, false);
@@ -220,8 +218,17 @@ void Model_Objects::removeFeature(FeaturePtr theFeature)
         aComposite->removeFeature(theFeature);
       }
     }
+    // this must be before erase since theFeature erasing removes all information about
+    // the feature results and groups of results
+    // To reproduce: create sketch, extrusion, remove sketch => constructions tree is not updated
+    clearHistory(theFeature);
     // erase fields
     theFeature->erase();
+
+    TDF_Label aFeatureLabel = aData->label().Father();
+    if (myFeatures.IsBound(aFeatureLabel))
+      myFeatures.UnBind(aFeatureLabel);
+
     static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
     ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
     // erase all attributes under the label of feature
@@ -245,8 +252,13 @@ void Model_Objects::clearHistory(ObjectPtr theObj)
       myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
     if (theObj->groupName() == ModelAPI_Feature::group()) { // clear results group of the feature
       FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
-      if (aFeature->firstResult().get())
-        clearHistory(aFeature->firstResult());
+      std::string aResultGroup = featureResultGroup(aFeature);
+      if (!aResultGroup.empty()) {
+        std::map<std::string, std::vector<ObjectPtr> >::iterator aHIter = 
+          myHistory.find(aResultGroup);
+        if (aHIter != myHistory.end())
+          myHistory.erase(aHIter); // erase from map => this means that it is not synchronized
+      }
     }
   }
 }
@@ -328,6 +340,9 @@ ObjectPtr Model_Objects::object(TDF_Label theLabel)
 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
 {
   createHistory(theGroupID);
+  //TODO: mpv stabilization hotfix
+  if (myHistory[theGroupID].size() <= theIndex)
+    return ObjectPtr();
   return myHistory[theGroupID][theIndex];
 }
 
@@ -749,6 +764,22 @@ std::shared_ptr<ModelAPI_Feature> Model_Objects::feature(
   return FeaturePtr();
 }
 
+std::string Model_Objects::featureResultGroup(FeaturePtr theFeature)
+{
+  if (theFeature->data()->isValid()) {
+    TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
+    if (aLabIter.More()) {
+      TDF_Label anArgLab = aLabIter.Value();
+      Handle(TDataStd_Comment) aGroup;
+      if (aLabIter.Value().FindAttribute(TDataStd_Comment::GetID(), aGroup)) {
+        return TCollection_AsciiString(aGroup->Get()).ToCString();
+      }
+    }
+  }
+  static std::string anEmpty;
+  return anEmpty; // not found
+}
+
 void Model_Objects::updateResults(FeaturePtr theFeature)
 {
   // for not persistent is will be done by parametric updater automatically
@@ -757,10 +788,10 @@ void Model_Objects::updateResults(FeaturePtr theFeature)
   std::list<ResultPtr>::const_iterator aResIter = theFeature->results().cbegin();
   while(aResIter != theFeature->results().cend()) {
     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(*aResIter);
-    if (aBody) {
+    if (aBody.get()) {
       if (!aBody->data()->isValid()) { 
         // found a disappeared result => remove it
-        theFeature->removeResult(aBody);
+        theFeature->eraseResultFromList(aBody);
         // start iterate from beginning because iterator is corrupted by removing
         aResIter = theFeature->results().cbegin();
         continue;
@@ -832,7 +863,7 @@ ResultPtr Model_Objects::findByName(const std::string theName)
 FeaturePtr Model_Objects::nextFeature(FeaturePtr theCurrent, const bool theReverse)
 {
   std::shared_ptr<Model_Data> aData = std::static_pointer_cast<Model_Data>(theCurrent->data());
-  if (aData) {
+  if (aData && aData->isValid()) {
     TDF_Label aFeatureLabel = aData->label().Father();
     Handle(TDataStd_ReferenceArray) aRefs;
     if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {