Salome HOME
Fix solvespace version number in environment.
[modules/shaper.git] / src / Model / Model_Session.cpp
index 6b3194d5a08b73e3065f9495fdc9393646cd7933..e336d1652d3f7d2166cc095b231232cd09ece395 100644 (file)
@@ -38,13 +38,13 @@ static Model_Session* myImpl = new Model_Session();
 
 bool Model_Session::load(const char* theFileName)
 {
-  bool aRes = ROOT_DOC->load(theFileName, ROOT_DOC);
+  bool aRes = ROOT_DOC->load(theFileName, "root", ROOT_DOC);
   return aRes;
 }
 
 bool Model_Session::save(const char* theFileName, std::list<std::string>& theResults)
 {
-  return ROOT_DOC->save(theFileName, theResults);
+  return ROOT_DOC->save(theFileName, "root", theResults);
 }
 
 void Model_Session::closeAll()
@@ -186,34 +186,27 @@ FeaturePtr Model_Session::createFeature(string theFeatureID, Model_Document* the
 
 std::shared_ptr<ModelAPI_Document> Model_Session::moduleDocument()
 {
-  bool aFirstCall = !Model_Application::getApplication()->hasDocument("root");
+  Handle(Model_Application) anApp = Model_Application::getApplication();
+  bool aFirstCall = !anApp->hasRoot();
   if (aFirstCall) {
     // creation of the root document is always outside of the transaction, so, avoid checking it
     setCheckTransactions(false);
-  }
-  std::shared_ptr<ModelAPI_Document> aDoc = std::shared_ptr<ModelAPI_Document>(
-      Model_Application::getApplication()->getDocument("root"));
-  if (aFirstCall) {
+    anApp->createDocument(0); // 0 is a root ID
     // creation of the root document is always outside of the transaction, so, avoid checking it
     setCheckTransactions(true);
   }
-  return aDoc;
+  return anApp->rootDocument();
 }
 
-std::shared_ptr<ModelAPI_Document> Model_Session::document(std::string theDocID)
+std::shared_ptr<ModelAPI_Document> Model_Session::document(int theDocID)
 {
   return std::shared_ptr<ModelAPI_Document>(
-      Model_Application::getApplication()->getDocument(theDocID));
+      Model_Application::getApplication()->document(theDocID));
 }
 
 bool Model_Session::hasModuleDocument()
 {
-  return Model_Application::getApplication()->hasDocument("root");
-}
-
-bool Model_Session::hasDocument(std::string theDocID)
-{
-  return Model_Application::getApplication()->hasDocument(theDocID);
+  return Model_Application::getApplication()->hasRoot();
 }
 
 std::shared_ptr<ModelAPI_Document> Model_Session::activeDocument()
@@ -252,6 +245,18 @@ void Model_Session::setActiveDocument(
     std::shared_ptr<ModelAPI_Document> aPrevious = myCurrentDoc;
     myCurrentDoc = theDoc;
     if (theDoc.get() && theSendSignal) {
+      // this must be before the synchronisation call because features in PartSet lower than this
+      // part feature must be disabled and don't recomputed anymore (issue 1156,
+      // translation feature is failed on activation of Part 2)
+      if (isOperation()) { // do it only in transaction, not on opening of document
+        DocumentPtr aRoot = moduleDocument();
+        if (myCurrentDoc != aRoot) {
+          FeaturePtr aPartFeat = ModelAPI_Tools::findPartFeature(aRoot, myCurrentDoc);
+          if (aPartFeat.get()) {
+            aRoot->setCurrentFeature(aPartFeat, false);
+          }
+        }
+      }
       // syncronize the document: it may be just opened or opened but removed before
       std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(theDoc);
       if (aDoc.get()) {
@@ -275,14 +280,6 @@ void Model_Session::setActiveDocument(
       } else {
         // make the current feature the latest in sub, root current feature becomes this sub
         makeCurrentLast(myCurrentDoc);
-        DocumentPtr aRoot = moduleDocument();
-        ResultPtr aPartRes = ModelAPI_Tools::findPartResult(aRoot, myCurrentDoc);
-        if (aPartRes.get()) {
-          FeaturePtr aPartFeat = aRoot->feature(aPartRes);
-          if (aPartFeat.get()) {
-            aRoot->setCurrentFeature(aPartFeat, false);
-          }
-        }
       }
     }
   }
@@ -298,12 +295,10 @@ std::list<std::shared_ptr<ModelAPI_Document> > Model_Session::allOpenedDocuments
     DocumentPtr anAPIDoc = *aDoc;
     std::shared_ptr<Model_Document> aDoc = std::dynamic_pointer_cast<Model_Document>(anAPIDoc);
     if (aDoc) {
-      const std::set<std::string> aSubs = aDoc->subDocuments(true);
-      std::set<std::string>::const_iterator aSubIter = aSubs.cbegin();
+      const std::set<int> aSubs = aDoc->subDocuments();
+      std::set<int>::const_iterator aSubIter = aSubs.cbegin();
       for(; aSubIter != aSubs.cend(); aSubIter++) {
-        if (!Model_Application::getApplication()->isLoadByDemand(*aSubIter)) {
-          aResult.push_back(Model_Application::getApplication()->getDocument(*aSubIter));
-        }
+        aResult.push_back(Model_Application::getApplication()->document(*aSubIter));
       }
     }
   }
@@ -316,11 +311,9 @@ bool Model_Session::isLoadByDemand(const std::string theDocID)
 }
 
 std::shared_ptr<ModelAPI_Document> Model_Session::copy(
-    std::shared_ptr<ModelAPI_Document> theSource, std::string theID)
+    std::shared_ptr<ModelAPI_Document> theSource, const int theDestID)
 {
-  // create a new document
-  std::shared_ptr<Model_Document> aNew = std::dynamic_pointer_cast<Model_Document>(
-      Model_Application::getApplication()->getDocument(theID));
+  std::shared_ptr<Model_Document> aNew = Model_Application::getApplication()->document(theDestID);
   // make a copy of all labels
   TDF_Label aSourceRoot = std::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main()
       .Father();
@@ -406,10 +399,25 @@ void Model_Session::processEvent(const std::shared_ptr<Events_Message>& theMessa
       std::shared_ptr<ModelAPI_ObjectDeletedMessage> aDeleted =
         std::dynamic_pointer_cast<ModelAPI_ObjectDeletedMessage>(theMessage);
       if (aDeleted && 
-          aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end() &&
-          !ModelAPI_Tools::findPartResult(moduleDocument(), activeDocument()).get()) // another part may be disabled
+          aDeleted->groups().find(ModelAPI_ResultPart::group()) != aDeleted->groups().end())
       {
-        setActiveDocument(moduleDocument());
+         // check that the current feature of the session is still the active Part (even disabled)
+        bool aFound = false;
+        FeaturePtr aCurrentPart = moduleDocument()->currentFeature(true);
+        if (aCurrentPart.get()) {
+          const std::list<std::shared_ptr<ModelAPI_Result> >& aResList = aCurrentPart->results();
+          std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResList.begin();
+          for(; !aFound && aRes != aResList.end(); aRes++) {
+            ResultPartPtr aPRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aRes);
+            if (aPRes.get() && aPRes->isActivated() && aPRes->partDoc() == activeDocument()) {
+              aFound = true;
+
+            }
+          }
+        }
+        if (!aFound) { // if not, the part was removed, so activate the module document
+          setActiveDocument(moduleDocument());
+        }
       }
     }
   }