Salome HOME
Fix for the issue #577
[modules/shaper.git] / src / Model / Model_Objects.cpp
index 6bab4002bb9c3049dc51a0e44afffadb4413fe85..1361a931a49baca81ba39b0b83ee3ac10d237a62 100644 (file)
@@ -128,6 +128,7 @@ void Model_Objects::addFeature(FeaturePtr theFeature, const FeaturePtr theAfterT
     updateHistory(ModelAPI_Feature::group());
   } else { // make feature has not-null data anyway
     theFeature->setData(Model_Data::invalidData());
+    theFeature->setDoc(myDoc);
   }
 }
 
@@ -217,6 +218,10 @@ 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();
 
@@ -224,8 +229,6 @@ void Model_Objects::removeFeature(FeaturePtr theFeature)
     if (myFeatures.IsBound(aFeatureLabel))
       myFeatures.UnBind(aFeatureLabel);
 
-    clearHistory(theFeature);
-
     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
@@ -249,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
+      }
     }
   }
 }
@@ -331,6 +339,8 @@ ObjectPtr Model_Objects::object(TDF_Label theLabel)
 
 ObjectPtr Model_Objects::object(const std::string& theGroupID, const int theIndex)
 {
+  if (theIndex == -1)
+    return ObjectPtr();
   createHistory(theGroupID);
   return myHistory[theGroupID][theIndex];
 }
@@ -465,7 +475,7 @@ void Model_Objects::synchronizeFeatures(
   static Events_ID aRedispEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
   static Events_ID aDeleteEvent = Events_Loop::eventByName(EVENT_OBJECT_DELETED);
   static Events_ID aToHideEvent = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
-  aLoop->activateFlushes(false);
+  bool isActive = aLoop->activateFlushes(false);
 
   // update all objects by checking are they on labels or not
   std::set<FeaturePtr> aNewFeatures, aKeptFeatures;
@@ -548,7 +558,7 @@ void Model_Objects::synchronizeFeatures(
   }
 
   anOwner->executeFeatures() = false;
-  aLoop->activateFlushes(true);
+  aLoop->activateFlushes(isActive);
 
   if (theFlush) {
     aLoop->flush(aCreateEvent);
@@ -753,6 +763,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
@@ -789,8 +815,10 @@ void Model_Objects::updateResults(FeaturePtr theFeature)
         if (aGroup->Get() == ModelAPI_ResultBody::group().c_str()) {
           aNewBody = createBody(theFeature->data(), aResIndex);
         } else if (aGroup->Get() == ModelAPI_ResultPart::group().c_str()) {
-          //aNewBody = createPart(theFeature->data(), aResIndex); 
-          theFeature->execute(); // create the part result
+          std::shared_ptr<ModelAPI_ResultPart> aNewP = createPart(theFeature->data(), aResIndex); 
+          theFeature->setResult(aNewP, aResIndex);
+          if (!aNewP->partDoc().get())
+            theFeature->execute(); // create the part result: it is better to restore the previous result if it is possible
           break;
         } else if (aGroup->Get() == ModelAPI_ResultConstruction::group().c_str()) {
           theFeature->execute(); // construction shapes are needed for sketch solver
@@ -871,6 +899,20 @@ FeaturePtr Model_Objects::lastFeature()
   return FeaturePtr(); // no features at all
 }
 
+std::list<std::shared_ptr<ModelAPI_Feature> > Model_Objects::allFeatures()
+{
+  std::list<std::shared_ptr<ModelAPI_Feature> > aResult;
+  Handle(TDataStd_ReferenceArray) aRefs;
+  if (featuresLabel().FindAttribute(TDataStd_ReferenceArray::GetID(), aRefs)) {
+    for(int a = aRefs->Lower(); a <= aRefs->Upper(); a++) {
+      FeaturePtr aFeature = feature(aRefs->Value(a));
+      if (aFeature.get())
+        aResult.push_back(aFeature);
+    }
+  }
+  return aResult;
+}
+
 Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper)
 {
   return TDF_LabelMapHasher::HashCode(theLab, theUpper);