Salome HOME
Fix for the issue #2450 : consequent creation of fillet requires extra click
[modules/shaper.git] / src / Model / Model_Update.cpp
index f95f59d7c0f81a202ca8d4f42d8eba5f1338feb4..44f7a47e94c7abb217565f1732083eee156a2d3a 100755 (executable)
@@ -1,8 +1,22 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        Model_Update.cxx
-// Created:     25 Jun 2014
-// Author:      Mikhail PONIKAROV
+// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or
+// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+//
 
 #include <Model_Update.h>
 #include <Model_Document.h>
@@ -24,6 +38,7 @@
 #include <ModelAPI_CompositeFeature.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Tools.h>
+#include <GeomAPI_Shape.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
 #include <Events_Loop.h>
@@ -72,9 +87,11 @@ bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
   if (!theFeature->data()->isValid())
     return false; // delete an extrusion created on the sketch
 
+
   bool isNotExecuted = theFeature->isPersistentResult() &&
     !std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures();
   if (isNotExecuted) {
+    redisplayWithResults(theFeature, ModelAPI_StateNothing, false); // redisplay even not executed
     if (!theReason.get()) // no reason => no construction reason
       return false;
     if (myNotPersistentRefs.find(theFeature) == myNotPersistentRefs.end()) {
@@ -89,7 +106,10 @@ bool Model_Update::addModified(FeaturePtr theFeature, FeaturePtr theReason) {
 
   // update arguments for "apply button" state change
   if ((!theFeature->isPreviewNeeded() && !myIsFinish) || myIsPreviewBlocked) {
-    myProcessOnFinish.insert(theFeature);
+    if (theReason.get())
+      myProcessOnFinish[theFeature].insert(theReason);
+    else if (myProcessOnFinish.find(theFeature) == myProcessOnFinish.end())
+      myProcessOnFinish[theFeature] = std::set<std::shared_ptr<ModelAPI_Feature> >();
 #ifdef DEB_UPDATE
       std::cout<<"*** Add process on finish "<<theFeature->name()<<std::endl;
 #endif
@@ -282,12 +302,16 @@ void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessag
         const std::set<std::shared_ptr<ModelAPI_Attribute> >&
           aRefs = (*anObjIter)->data()->refsToMe();
         std::set<std::shared_ptr<ModelAPI_Attribute> >::const_iterator aRefIter = aRefs.cbegin();
+        FeaturePtr aReason;
+        ResultPtr aReasonResult = std::dynamic_pointer_cast<ModelAPI_Result>(*anObjIter);
+        if (aReasonResult.get())
+          aReason = (*anObjIter)->document()->feature(aReasonResult);
         for(; aRefIter != aRefs.cend(); aRefIter++) {
           if (!(*aRefIter)->owner()->data()->isValid())
             continue;
           FeaturePtr anUpdated = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIter)->owner());
           if (anUpdated.get()) {
-            if (addModified(anUpdated, FeaturePtr()))
+            if (addModified(anUpdated, aReason))
               aSomeModified = true;
           }
         }
@@ -304,18 +328,41 @@ void Model_Update::processEvent(const std::shared_ptr<Events_Message>& theMessag
     if (theMessage->eventID() == kOpFinishEvent) {
       myIsFinish = true;
       // add features that wait for finish as modified
-      std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aFeature = myProcessOnFinish.begin();
-      for(; aFeature != myProcessOnFinish.end(); aFeature++)
-        if ((*aFeature)->data()->isValid()) // there may be already removed wait for features
-          addModified(*aFeature, FeaturePtr());
+      std::map<std::shared_ptr<ModelAPI_Feature>, std::set<std::shared_ptr<ModelAPI_Feature> > >::
+        iterator aFeature = myProcessOnFinish.begin();
+      for(; aFeature != myProcessOnFinish.end(); aFeature++) {
+        if (aFeature->first->data()->isValid()) {// there may be already removed while wait
+          if (aFeature->second.empty()) {
+            addModified(aFeature->first, FeaturePtr());
+            continue;
+          }
+          std::set<std::shared_ptr<ModelAPI_Feature> >::iterator aReasons;
+          for(aReasons = aFeature->second.begin(); aReasons != aFeature->second.end(); aReasons++) {
+            addModified(aFeature->first, *aReasons);
+          }
+        }
+      }
       myIsFinish = false;
     }
     // processed features must be only on finish, so clear anyway (to avoid reimport on load)
     myProcessOnFinish.clear();
 
+    // #2156: current must be sketch, left after the macro execution
+    DocumentPtr anActiveDoc = ModelAPI_Session::get()->activeDocument();
+    FeaturePtr aCurrent;
+    if (anActiveDoc.get())
+      aCurrent = anActiveDoc->currentFeature(false);
+
     if (!(theMessage->eventID() == kOpStartEvent)) {
       processFeatures(false);
     }
+
+    if (anActiveDoc.get() && aCurrent.get() && aCurrent->data()->isValid()) {
+      if (anActiveDoc->currentFeature(false) != aCurrent)
+        anActiveDoc->setCurrentFeature(aCurrent, false); // #2156 make the current feature back
+    }
+
+
     // remove all macros before clearing all created
     std::set<FeaturePtr>::iterator anUpdatedIter = myWaitForFinish.begin();
     while(anUpdatedIter != myWaitForFinish.end()) {
@@ -484,11 +531,21 @@ bool Model_Update::processFeature(FeaturePtr theFeature)
         }
       }
     }
+    std::shared_ptr<GeomAPI_Shape> aShapeBefore = anExtSel->value();
+    if (!aShapeBefore.get() && anExtSel->context()) aShapeBefore = anExtSel->context()->shape();
     updateArguments(theFeature);
-    // send event that sketch is prepared to be recomputed
-    static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
-    std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
-    Events_Loop::loop()->send(aMsg);
+    std::shared_ptr<GeomAPI_Shape> aShapeAfter = anExtSel->value();
+    if (!aShapeAfter.get() && anExtSel->context()) aShapeAfter = anExtSel->context()->shape();
+    // if selected plane is changed, try to re-take external references of all subs of the sketch
+    if (aShapeBefore.get() && !aShapeBefore->isEqual(aShapeAfter)) {
+      std::set<FeaturePtr> aWholeR;
+      allReasons(theFeature, aWholeR);
+      std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
+      for(; aRIter != aWholeR.end(); aRIter++) {
+        if ((*aRIter)->data()->selection("External"))
+          (*aRIter)->attributeChanged("External");
+      }
+    }
   }
 
   if (!aIsModified) { // no modification is needed
@@ -541,6 +598,25 @@ bool Model_Update::processFeature(FeaturePtr theFeature)
     CompositeFeaturePtr aCurrentOwner =
       ModelAPI_Tools::compositeOwner(theFeature->document()->currentFeature(false));
     isPostponedMain = aCurrentOwner.get() && aCompos->isSub(aCurrentOwner);
+  } else if (theFeature->getKind() == "Sketch" &&
+    std::dynamic_pointer_cast<Model_Document>((theFeature)->document())->executeFeatures()) {
+    // send event that sketch is prepared to be recomputed
+    static Events_ID anID = Events_Loop::eventByName("SketchPrepared");
+    std::shared_ptr<Events_Message> aMsg(new Events_Message(anID, this));
+    Events_Loop* aLoop = Events_Loop::loop();
+    // in case it is finish operation, flush for the sketch other events (#2450)
+    aLoop->flush(aLoop->eventByName(EVENT_OBJECT_UPDATED));
+    aLoop->send(aMsg);
+    // check that sub-elements of sketch are updated => sketch must be re-processed
+    std::set<FeaturePtr> aWholeR;
+    allReasons(theFeature, aWholeR);
+    std::set<FeaturePtr>::iterator aRIter = aWholeR.begin();
+    for(; aRIter != aWholeR.end(); aRIter++) {
+      if (myModified.find(*aRIter) != myModified.end()) {
+        processFeature(theFeature);
+        return true;
+      }
+    }
   }
 
 #ifdef DEB_UPDATE
@@ -569,7 +645,7 @@ bool Model_Update::processFeature(FeaturePtr theFeature)
   #ifdef DEB_UPDATE
     std::cout<<"Invalid args "<<theFeature->name()<<std::endl;
   #endif
-    theFeature->eraseResults();
+    theFeature->eraseResults(false);
     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
     return true; // so, feature is modified (results are erased)
   }
@@ -584,13 +660,14 @@ bool Model_Update::processFeature(FeaturePtr theFeature)
     #ifdef DEB_UPDATE
       std::cout<<"Feature is not valid, erase results "<<theFeature->name()<<std::endl;
     #endif
-    theFeature->eraseResults();
+    theFeature->eraseResults(false);
     redisplayWithResults(theFeature, ModelAPI_StateInvalidArgument); // result also must be updated
   }
   return true;
 }
 
-void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_ExecState theState)
+void Model_Update::redisplayWithResults(
+  FeaturePtr theFeature, const ModelAPI_ExecState theState, bool theUpdateState)
 {
   // make updated and redisplay all results
   static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
@@ -603,7 +680,8 @@ void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_Ex
     if (!aRes->isDisabled()) {
       // update state only for enabled results
       // (Placement Result Part may make the original Part Result as invalid)
-      aRes->data()->execState(theState);
+      if (theUpdateState)
+        aRes->data()->execState(theState);
     }
     if (theFeature->data()->updateID() > aRes->data()->updateID()) {
       aRes->data()->setUpdateID(theFeature->data()->updateID());
@@ -612,7 +690,8 @@ void Model_Update::redisplayWithResults(FeaturePtr theFeature, const ModelAPI_Ex
   }
   // to redisplay "presentable" feature (for ex. distance constraint)
   ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
-  theFeature->data()->execState(theState);
+  if (theUpdateState)
+    theFeature->data()->execState(theState);
 }
 
 /// Updates the state by the referenced object: if something bad with it, set state for this one
@@ -633,7 +712,6 @@ ModelAPI_ExecState stateByReference(ObjectPtr theTarget, const ModelAPI_ExecStat
 void Model_Update::updateArguments(FeaturePtr theFeature) {
   // perform this method also for disabled features: to make "not done" state for
   // features referenced to the active and modified features
-
   static ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
 
   ModelAPI_ExecState aState = theFeature->data()->execState();
@@ -837,7 +915,7 @@ void Model_Update::executeFeature(FeaturePtr theFeature)
   // The macro feature has to be deleted in any case even its execution is failed
   myWaitForFinish.insert(theFeature);
   if (aState != ModelAPI_StateDone) {
-    theFeature->eraseResults();
+    theFeature->eraseResults(false);
   }
   theFeature->data()->setUpdateID(ModelAPI_Session::get()->transactionID());
   redisplayWithResults(theFeature, aState);