Salome HOME
Porting to CentOS
[modules/shaper.git] / src / Model / Model_Application.cpp
index 40704967e8de9cef5e8a568dac24330bd0fc5f37..128f6ee4dffa76228158d713d4a5967ffabb6d8b 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:       Model_Application.cxx
 // Created:    Fri Sep 2 2011
 // Author:     Mikhail PONIKAROV
@@ -24,14 +26,33 @@ const std::shared_ptr<Model_Document>& Model_Application::getDocument(string the
   if (myDocs.find(theDocID) != myDocs.end())
     return myDocs[theDocID];
 
-  std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID));
+  static const std::string thePartSetKind("PartSet");
+  static const std::string thePartKind("Part");
+  std::shared_ptr<Model_Document> aNew(
+    new Model_Document(theDocID, theDocID == "root" ? thePartSetKind : thePartKind));
   myDocs[theDocID] = aNew;
+  // load it if it must be loaded by demand
+  if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
+    aNew->load(myPath.c_str());
+    myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
+  }
+
   return myDocs[theDocID];
 }
 
 void Model_Application::deleteDocument(string theDocID)
 {
-  myDocs.erase(theDocID);
+  if (myDocs.find(theDocID) != myDocs.end()) {
+    myDocs[theDocID]->close(true);
+    myDocs.erase(theDocID);
+  }
+  myLoadedByDemand.clear();
+}
+
+void Model_Application::deleteAllDocuments()
+{
+  myDocs.clear();
+  myLoadedByDemand.clear();
 }
 
 //=======================================================================
@@ -40,6 +61,51 @@ bool Model_Application::hasDocument(std::string theDocID)
   return myDocs.find(theDocID) != myDocs.end();
 }
 
+//=======================================================================
+void Model_Application::setLoadPath(std::string thePath)
+{
+  myPath = thePath;
+}
+
+//=======================================================================
+const std::string& Model_Application::loadPath() const
+{
+  return myPath;
+}
+
+//=======================================================================
+void Model_Application::setLoadByDemand(std::string theID)
+{
+  myLoadedByDemand.insert(theID);
+}
+
+//=======================================================================
+bool Model_Application::isLoadByDemand(std::string theID)
+{
+  return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
+}
+
+//=======================================================================
+void Model_Application::removeUselessDocuments(
+  std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
+{
+  std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
+  while(aDoc != myDocs.end()) {
+    bool aFound = false;
+    std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
+    for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
+      aFound = aDoc->second == *aUsed;
+    }
+    if (!aFound) { // remove the useless
+      aDoc->second->close();
+      myDocs.erase(aDoc);
+      aDoc = myDocs.begin();
+    } else {
+      aDoc++;
+    }
+  }
+}
+
 //=======================================================================
 Model_Application::Model_Application()
 {
@@ -51,7 +117,7 @@ Model_Application::Model_Application()
 //=======================================================================
 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
 {
-  theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
+  theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
 }
 
 //=======================================================================