Salome HOME
Building Names. First part (to be debugged).
[modules/shaper.git] / src / Model / Model_Document.cpp
index 6c817c4b30fad1e75df83633028a9a18b0d6537d..c4fb2e7aaa8c1003d46b2782231fad0ed83f5ae3 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        Model_Document.cxx
 // Created:     28 Feb 2014
 // Author:      Mikhail PONIKAROV
@@ -24,6 +26,8 @@
 #include <TDF_Reference.hxx>
 #include <TDF_ChildIDIterator.hxx>
 #include <TDF_LabelMapHasher.hxx>
+#include <OSD_File.hxx>
+#include <OSD_Path.hxx>
 
 #include <climits>
 #ifndef WIN32
@@ -46,12 +50,18 @@ static const int TAG_HISTORY = 3;  // tag of the history sub-tree (python dump)
 static const int TAG_FEATURE_ARGUMENTS = 1;  ///< where the arguments are located
 static const int TAG_FEATURE_RESULTS = 2;  ///< where the results are located
 
+///
+/// 0:1:2 - where features are located
+/// 0:1:2:N:1 - data of the feature N
+/// 0:1:2:N:2:K:1 - data of the K result of the feature N
+
 Model_Document::Model_Document(const std::string theID, const std::string theKind)
     : myID(theID), myKind(theKind),
       myDoc(new TDocStd_Document("BinOcaf"))  // binary OCAF format
 {
   myDoc->SetUndoLimit(UNDO_LIMIT);  
-  myTransactionsAfterSave = 0;
+  myTransactionsCounter = 0;
+  myTransactionSave = 0;
   myNestedNum = -1;
   myExecuteFeatures = true;
   // to have something in the document and avoid empty doc open/save problem
@@ -158,6 +168,7 @@ bool Model_Document::load(const char* theFileName)
 bool Model_Document::save(const char* theFileName, std::list<std::string>& theResults)
 {
   // create a directory in the root document if it is not yet exist
+  Handle(Model_Application) anApp = Model_Application::getApplication();
   if (this == Model_Session::get()->moduleDocument().get()) {
 #ifdef WIN32
     CreateDirectory(theFileName, NULL);
@@ -169,7 +180,7 @@ bool Model_Document::save(const char* theFileName, std::list<std::string>& theRe
   TCollection_ExtendedString aPath(DocFileName(theFileName, myID));
   PCDM_StoreStatus aStatus;
   try {
-    aStatus = Model_Application::getApplication()->SaveAs(myDoc, aPath);
+    aStatus = anApp->SaveAs(myDoc, aPath);
   } catch (Standard_Failure) {
     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
     Events_Error::send(
@@ -191,13 +202,39 @@ bool Model_Document::save(const char* theFileName, std::list<std::string>& theRe
         break;
     }
   }
-  myTransactionsAfterSave = 0;
+  myTransactionSave = myTransactionsCounter;
   if (isDone) {  // save also sub-documents if any
     theResults.push_back(TCollection_AsciiString(aPath).ToCString());
     std::set<std::string>::iterator aSubIter = mySubs.begin();
     for (; aSubIter != mySubs.end() && isDone; aSubIter++) {
       isDone = subDoc(*aSubIter)->save(theFileName, theResults);
     }
+    if (isDone) { // also try to copy the not-activated sub-documents
+      // they are not in mySubs but as ResultParts
+      int aPartsNum = size(ModelAPI_ResultPart::group());
+      for(int aPart = 0; aPart < aPartsNum; aPart++) {
+        ResultPartPtr aPartRes = std::dynamic_pointer_cast<ModelAPI_ResultPart>
+          (object(ModelAPI_ResultPart::group(), aPart));
+        if (aPartRes) {
+          std::string aDocName = aPartRes->data()->name();
+          if (!aDocName.empty() && mySubs.find(aDocName) == mySubs.end()) {
+            // just copy file
+            TCollection_AsciiString aSubPath(DocFileName(anApp->loadPath().c_str(), aDocName));
+            OSD_Path aPath(aSubPath);
+            OSD_File aFile(aPath);
+            if (aFile.Exists()) {
+              TCollection_AsciiString aDestinationDir(DocFileName(theFileName, aDocName));
+              OSD_Path aDestination(aDestinationDir);
+              aFile.Copy(aDestination);
+              theResults.push_back(aDestinationDir.ToCString());
+            } else {
+              Events_Error::send(
+                std::string("Can not open file ") + aSubPath.ToCString() + " for saving");
+            }
+          }
+        }
+      }
+    }
   }
   return isDone;
 }
@@ -250,8 +287,8 @@ void Model_Document::startOperation()
       myNestedNum = 0;
       myDoc->InitDeltaCompaction();
     }
-    myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand();
-    myTransactionsAfterSave++;
+    myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand();
+    myTransactionsCounter++;
     myDoc->OpenCommand();
   } else {  // start the simple command
     myDoc->NewCommand();
@@ -266,15 +303,15 @@ bool Model_Document::compactNested()
 {
   bool allWasEmpty = true;
   while (myNestedNum != -1) {
-    myTransactionsAfterSave--;
-    if (!myIsEmptyTr[myTransactionsAfterSave]) {
+    myTransactionsCounter--;
+    if (!myIsEmptyTr[myTransactionsCounter]) {
       allWasEmpty = false;
     }
-    myIsEmptyTr.erase(myTransactionsAfterSave);
+    myIsEmptyTr.erase(myTransactionsCounter);
     myNestedNum--;
   }
-  myIsEmptyTr[myTransactionsAfterSave] = allWasEmpty;
-  myTransactionsAfterSave++;
+  myIsEmptyTr[myTransactionsCounter] = allWasEmpty;
+  myTransactionsCounter++;
   if (allWasEmpty) {
     // Issue 151: if everything is empty, it is a problem for OCCT to work with it, 
     // just commit the empty that returns nothing
@@ -329,8 +366,8 @@ void Model_Document::finishOperation()
     }
   } else {
     // returns false if delta is empty and no transaction was made
-    myIsEmptyTr[myTransactionsAfterSave] = !myDoc->CommitCommand();  // && (myNestedNum == -1);
-    myTransactionsAfterSave++;
+    myIsEmptyTr[myTransactionsCounter] = !myDoc->CommitCommand();  // && (myNestedNum == -1);
+    myTransactionsCounter++;
   }
 }
 
@@ -338,11 +375,12 @@ void Model_Document::abortOperation()
 {
   if (myNestedNum > 0 && !myDoc->HasOpenCommand()) {  // abort all what was done in nested
       // first compact all nested
-    compactNested();
-    myDoc->Undo();
+    if (compactNested()) {
+      myDoc->Undo(); // undo only compacted, if not: do not undo the empty transaction
+    }
     myDoc->ClearRedos();
-    myTransactionsAfterSave--;
-    myIsEmptyTr.erase(myTransactionsAfterSave);
+    myTransactionsCounter--;
+    myIsEmptyTr.erase(myTransactionsCounter);
   } else {
     if (myNestedNum == 0)  // abort only high-level
       myNestedNum = -1;
@@ -364,13 +402,13 @@ bool Model_Document::isOperation()
 bool Model_Document::isModified()
 {
   // is modified if at least one operation was commited and not undoed
-  return myTransactionsAfterSave > 0 || isOperation();
+  return myTransactionsCounter != myTransactionSave || isOperation();
 }
 
 bool Model_Document::canUndo()
 {
   if (myDoc->GetAvailableUndos() > 0 && myNestedNum != 0
-      && myTransactionsAfterSave != 0 /* for omitting the first useless transaction */)
+      && myTransactionsCounter != 0 /* for omitting the first useless transaction */)
     return true;
   // check other subs contains operation that can be undoed
   std::set<std::string>::iterator aSubIter = mySubs.begin();
@@ -382,10 +420,10 @@ bool Model_Document::canUndo()
 
 void Model_Document::undo()
 {
-  myTransactionsAfterSave--;
+  myTransactionsCounter--;
   if (myNestedNum > 0)
     myNestedNum--;
-  if (!myIsEmptyTr[myTransactionsAfterSave])
+  if (!myIsEmptyTr[myTransactionsCounter])
     myDoc->Undo();
   synchronizeFeatures(true, true);
   // undo for all subs
@@ -410,9 +448,9 @@ void Model_Document::redo()
 {
   if (myNestedNum != -1)
     myNestedNum++;
-  if (!myIsEmptyTr[myTransactionsAfterSave])
+  if (!myIsEmptyTr[myTransactionsCounter])
     myDoc->Redo();
-  myTransactionsAfterSave++;
+  myTransactionsCounter++;
   synchronizeFeatures(true, true);
   // redo for all subs
   std::set<std::string>::iterator aSubIter = mySubs.begin();
@@ -1013,6 +1051,9 @@ void Model_Document::updateResults(FeaturePtr theFeature)
     }
     aResIter++;
   }
+  // it may be on undo
+  if (!theFeature->data() || !theFeature->data()->isValid())
+    return;
   // check that results are presented on all labels
   int aResSize = theFeature->results().size();
   TDF_ChildIterator aLabIter(resultLabel(theFeature->data(), 0).Father());
@@ -1054,3 +1095,16 @@ Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2)
 {
   return TDF_LabelMapHasher::IsEqual(theLab1, theLab2);
 }
+
+void Model_Document::addNamingName(const TDF_Label theLabel, std::string theName)
+{
+  myNamingNames[theName] = theLabel;
+}
+
+TDF_Label Model_Document::findNamingName(std::string theName)
+{
+  std::map<std::string, TDF_Label>::iterator aFind = myNamingNames.find(theName);
+  if (aFind == myNamingNames.end())
+    return TDF_Label(); // not found
+  return aFind->second;
+}