Salome HOME
Fix for the issue #1156
[modules/shaper.git] / src / Model / Model_Update.cpp
index 49657767605ea735537cec2d23ce1cb13f6b1316..9d655c03c64a57db054c18350b13bd99554b50fc 100644 (file)
@@ -65,6 +65,7 @@ Model_Update::Model_Update()
   myIsParamUpdated = false;
   myIsFinish = false;
   myModification = 0;
+  myModificationInStartProcessing = 0;
 }
 
 void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessage)
@@ -109,6 +110,8 @@ void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessag
       if ((*anObjIter)->groupName() == ModelAPI_ResultParameter::group()) {
         myIsParamUpdated = true;
       }
+      if (myIsExecuted) // modifications from outside are with never IDs to take them into account in the current updates
+        myModification++;
       // on undo/redo, abort do not update persisten features
       FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>(*anObjIter);
       if (std::dynamic_pointer_cast<Model_Document>((*anObjIter)->document())->executeFeatures() ||
@@ -169,7 +172,8 @@ void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessag
     // place where results are cleared)
     myIsParamUpdated = false;
     myUpdated.clear();
-    myModification = 0;
+    // do not erase it since there may be modification increment on start of operation
+    //myModification = 0;
     myWaitForFinish.clear();
   }
 }
@@ -223,33 +227,13 @@ void Model_Update::iterateUpdateBreak(std::shared_ptr<ModelAPI_Feature> theFeatu
 
 void Model_Update::processOperation(const bool theTotalUpdate, const bool theFinish)
 {
-  /* cancel hardcode due to issue 948
-  if (theFinish) {
-    // the hardcode (DBC asked): hide the sketch referenced by extrusion on apply
-    std::set<std::shared_ptr<ModelAPI_Object> >::iterator aFIter;
-    for(aFIter = myWaitForFinish.begin(); aFIter != myWaitForFinish.end(); aFIter++)
-    {
-      FeaturePtr aF = std::dynamic_pointer_cast<ModelAPI_Feature>(*aFIter);
-      if (aF && aF->data()->isValid() && 
-           (aF->getKind() == "Extrusion" || aF->getKind() == "Revolution")) {
-        AttributeSelectionListPtr aBase = aF->selectionList("base");
-        if (aBase.get()) {
-          for(int a = aBase->size() - 1; a >= 0; a--) {
-            ResultPtr aSketchRes = aBase->value(a)->context();
-            if (aSketchRes) {
-              aSketchRes->setDisplayed(false);
-            }
-          }
-        }
-      }
-    }
-  } */
   // perform update of everything if needed
   if (!myIsExecuted) {
     #ifdef DEB_UPDATE
       std::cout<<"****** Start processing"<<std::endl;
     #endif
     myIsExecuted = true;
+    myModificationInStartProcessing = myModification;
 
     bool isAutomaticChanged = false;
 
@@ -257,15 +241,22 @@ void Model_Update::processOperation(const bool theTotalUpdate, const bool theFin
       isAutomaticChanged = true;
       myIsAutomatic = true;
     }
+    // modifications inside of the iteration will be different from modification that comes outside
+    myModification++;
     // init iteration from the root document
     iterateUpdate(CompositeFeaturePtr());
 
     if (isAutomaticChanged) myIsAutomatic = false;
     myIsExecuted = false;
+    // flush updates just before "myModification" increment: to distinguish
+    // updates by "execute" produced by this updater and other updates, coming outside,
+    // which are really important for "processEvent" of this updater
+    static Events_Loop* aLoop = Events_Loop::loop();
+    static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
+    aLoop->flush(kUpdatedEvent);
     myModification++;
 
     // flush to update display
-    static Events_Loop* aLoop = Events_Loop::loop();
     static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
     aLoop->flush(EVENT_DISP);
     #ifdef DEB_UPDATE
@@ -274,6 +265,12 @@ void Model_Update::processOperation(const bool theTotalUpdate, const bool theFin
   }
 }
 
+bool Model_Update::isProcessed(const int theModificationID)
+{
+  return theModificationID >= myModificationInStartProcessing && 
+         theModificationID <= myModification;
+}
+
 void Model_Update::updateFeature(FeaturePtr theFeature)
 {
   // check all features this feature depended on (recursive call of updateFeature)
@@ -303,7 +300,7 @@ void Model_Update::updateFeature(FeaturePtr theFeature)
   // If automatice update is not needed and feature attributes were not updated right now,
   // do not execute it and do not update arguments.
   if (!myIsAutomatic && 
-       (myUpdated.find(theFeature) == myUpdated.end() || myUpdated[theFeature] != myModification)
+       (myUpdated.find(theFeature) == myUpdated.end() || !isProcessed(myUpdated[theFeature]))
        && !aCompos.get()) {
     // execute will be performed later, but some features may have not-result 
     // presentations, so call update for them (like coincidence in the sketcher)
@@ -338,7 +335,7 @@ void Model_Update::updateFeature(FeaturePtr theFeature)
   if (aJustUpdated) {
     // if preview is not needed, the created feature was not updated before, so, myModification is not actual for this
     if (theFeature->isPreviewNeeded()) {
-      aJustUpdated = myUpdated[theFeature] == myModification;
+      aJustUpdated = isProcessed(myUpdated[theFeature]);
     }
   }
 
@@ -404,9 +401,11 @@ void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_Ex
 {
   // make updated and redisplay all results
   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
-  const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
-  std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
-  for (; aRIter != aResults.cend(); aRIter++) {
+
+  std::list<ResultPtr> allResults;
+  ModelAPI_Tools::allResults(theFeature, allResults);
+  std::list<ResultPtr>::iterator aRIter = allResults.begin();
+  for (; aRIter != allResults.cend(); aRIter++) {
     std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
     if (!aRes->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
       aRes->data()->execState(theState);
@@ -417,27 +416,11 @@ void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_Ex
       aRes->data()->setUpdateID(theFeature->data()->updateID());
     }
     ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
-    // iterate sub-bodies of compsolid
-    ResultCompSolidPtr aComp = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aRes);
-    if (aComp.get()) {
-      int aNumSub = aComp->numberOfSubs();
-      for(int a = 0; a < aNumSub; a++) {
-        ResultPtr aSub = aComp->subResult(a);
-        if (!aSub->isDisabled()) {// update state only for enabled results (Placement Result Part may make the original Part Result as invalid)
-          aSub->data()->execState(theState);
-          if (theState == ModelAPI_StateDone) // feature become "done", so execution changed results
-            myUpdated[aSub] = myModification;
-        }
-        if (theFeature->data()->updateID() > aSub->data()->updateID()) {
-          aSub->data()->setUpdateID(theFeature->data()->updateID());
-        }
-        ModelAPI_EventCreator::get()->sendUpdated(aSub, EVENT_DISP);
-      }
-    }
   }
   // to redisplay "presentable" feature (for ex. distance constraint)
   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
   theFeature->data()->execState(theState);
+  myUpdated[theFeature] = myModification; // feature is also updated to avoid re-updation of it
 }
 
 /// Updates the state by the referenced object: if something bad with it, set state for this one
@@ -465,6 +448,27 @@ bool Model_Update::isOlder(std::shared_ptr<ModelAPI_Feature> theFeature,
   std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator anAIter = myUpdated.find(theArgument);
   if (anAIter == myUpdated.end())
     return false;
+  // for the modification IDs compare results: modification ID of feature means only that attributes
+  // of this feature were updated, but if results are obsolete relatively to the referenced results,
+  // the feature must be updated
+  std::list<ResultPtr> aResults;
+  ModelAPI_Tools::allResults(theFeature, aResults);
+  std::list<ResultPtr>::iterator aRIter = aResults.begin();
+  for (; aRIter != aResults.cend(); aRIter++) {
+    std::shared_ptr<ModelAPI_Result> aRes = *aRIter;
+    if (!aRes->isDisabled()) {
+      std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator anRIter = myUpdated.find(aRes);
+      int aResultID = aRes->data()->updateID();
+      if (aResultID < anArgID)
+        return true;
+      if (anRIter == myUpdated.end()) // not updated at all
+        return true;
+      if (anRIter->second < anAIter->second)
+        return true;
+    }
+  }
+  // also check a feature: some have no parameters,
+  // but must be updated anyway (like Coincidence of sketch) to be redisplayed
   std::map<std::shared_ptr<ModelAPI_Object>, int >::iterator aFIter = myUpdated.find(theFeature);
   if (aFIter == myUpdated.end())
     return true; // argument is updated, but feature is not updated at all
@@ -660,13 +664,14 @@ void Model_Update::executeFeature(FeaturePtr theFeature)
       aState = ModelAPI_StateExecFailed;
     } else {
       aState = ModelAPI_StateDone;
-      myWaitForFinish.insert(theFeature);
     }
   } catch(...) {
     aState = ModelAPI_StateExecFailed;
     Events_Error::send(
       "Feature " + theFeature->getKind() + " has failed during the execution");
   }
+  // The macro feature has to be deleted in any case even its execution is failed 
+  myWaitForFinish.insert(theFeature);
   if (aState != ModelAPI_StateDone) {
     theFeature->eraseResults();
   }