Salome HOME
Make initialization plugin:
[modules/shaper.git] / src / Model / Model_Application.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Application.cxx
4 // Created:     Fri Sep 2 2011
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Application.h>
8 #include <Model_Document.h>
9
10 #include <ModelAPI_Events.h>
11
12 IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
13 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
14
15 using namespace std;
16
17 static Handle_Model_Application TheApplication = new Model_Application;
18
19 //=======================================================================
20 Handle(Model_Application) Model_Application::getApplication()
21 {
22   return TheApplication;
23 }
24
25 //=======================================================================
26 const std::shared_ptr<Model_Document>& Model_Application::getDocument(string theDocID)
27 {
28   if (myDocs.find(theDocID) != myDocs.end())
29     return myDocs[theDocID];
30
31   static const std::string thePartSetKind("PartSet");
32   static const std::string thePartKind("Part");
33   bool isRoot = theDocID == "root"; // the document is root
34   std::shared_ptr<Model_Document> aNew(
35     new Model_Document(theDocID, isRoot ? thePartSetKind : thePartKind));
36   myDocs[theDocID] = aNew;
37
38   // load it if it must be loaded by demand
39   if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
40     aNew->load(myPath.c_str());
41     myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
42   } else {
43     static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
44     std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
45       <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
46     aMessage->setDocument(aNew);
47     Events_Loop::loop()->send(aMessage);
48   }
49
50   return myDocs[theDocID];
51 }
52
53 void Model_Application::deleteDocument(string theDocID)
54 {
55   if (myDocs.find(theDocID) != myDocs.end()) {
56     myDocs[theDocID]->close(true);
57     myDocs.erase(theDocID);
58   }
59   myLoadedByDemand.clear();
60 }
61
62 void Model_Application::deleteAllDocuments()
63 {
64   myDocs.clear();
65   myLoadedByDemand.clear();
66 }
67
68 //=======================================================================
69 bool Model_Application::hasDocument(std::string theDocID)
70 {
71   return myDocs.find(theDocID) != myDocs.end();
72 }
73
74 //=======================================================================
75 void Model_Application::setLoadPath(std::string thePath)
76 {
77   myPath = thePath;
78 }
79
80 //=======================================================================
81 const std::string& Model_Application::loadPath() const
82 {
83   return myPath;
84 }
85
86 //=======================================================================
87 void Model_Application::setLoadByDemand(std::string theID)
88 {
89   myLoadedByDemand.insert(theID);
90 }
91
92 //=======================================================================
93 bool Model_Application::isLoadByDemand(std::string theID)
94 {
95   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
96 }
97
98 //=======================================================================
99 void Model_Application::removeUselessDocuments(
100   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
101 {
102   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
103   while(aDoc != myDocs.end()) {
104     bool aFound = false;
105     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
106     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
107       aFound = aDoc->second == *aUsed;
108     }
109     if (!aFound) { // remove the useless
110       aDoc->second->close();
111       myDocs.erase(aDoc);
112       aDoc = myDocs.begin();
113     } else {
114       aDoc++;
115     }
116   }
117 }
118
119 //=======================================================================
120 Model_Application::Model_Application()
121 {
122   // store handle to the application to avoid nullification
123   static Handle(Model_Application) TheKeepHandle;
124   TheKeepHandle = this;
125 }
126
127 //=======================================================================
128 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
129 {
130   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
131 }
132
133 //=======================================================================
134 Standard_CString Model_Application::ResourcesName()
135 {
136   return Standard_CString("Standard");
137 }