Salome HOME
Merge branch 'master' into cgt/devCEA
[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_RTTIEXT(Model_Application, TDocStd_Application)
13
14 static Handle_Model_Application TheApplication = new Model_Application;
15
16 //=======================================================================
17 Handle(Model_Application) Model_Application::getApplication()
18 {
19   return TheApplication;
20 }
21
22 //=======================================================================
23 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
24 {
25   if (myDocs.find(theDocID) != myDocs.end())
26     return myDocs[theDocID];
27   return std::shared_ptr<Model_Document>(); // not loaded, so return null
28 }
29
30 //=======================================================================
31 void Model_Application::createDocument(const int theDocID)
32 {
33   static const std::string thePartSetKind("PartSet");
34   static const std::string thePartKind("Part");
35   std::shared_ptr<Model_Document> aNew(
36     new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
37   myDocs[theDocID] = aNew;
38
39   aNew->setThis(aNew);
40   static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
41   std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
42     <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
43   aMessage->setDocument(aNew);
44   Events_Loop::loop()->send(aMessage);
45 }
46
47 //=======================================================================
48 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
49 {
50   static const std::string thePartKind("Part"); // root document is never loaded here
51   std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
52   myDocs[theDocID] = aNew;
53
54   bool aRes = true;
55   // load it if it must be loaded by demand
56   if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
57     aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
58     myLoadedByDemand.erase(theDocName);  // done, don't do it anymore
59   } else { // error
60     aRes = false;
61   }
62
63   return aRes;
64 }
65
66 //=======================================================================
67 void Model_Application::deleteDocument(const int theDocID)
68 {
69   if (myDocs.find(theDocID) != myDocs.end()) {
70     myDocs[theDocID]->close(true);
71     myDocs.erase(theDocID);
72   }
73   myLoadedByDemand.clear();
74 }
75
76 //=======================================================================
77 void Model_Application::deleteAllDocuments()
78 {
79   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
80   for(; aDoc != myDocs.end(); aDoc++) {
81     if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
82       aDoc->second->close(true);
83   }
84   myDocs.clear();
85   myLoadedByDemand.clear();
86 }
87
88 //=======================================================================
89 bool Model_Application::hasDocument(const int theDocID)
90 {
91   return myDocs.find(theDocID) != myDocs.end();
92 }
93
94 //=======================================================================
95 bool Model_Application::hasRoot()
96 {
97   return !myDocs.empty();
98 }
99
100 //=======================================================================
101 std::shared_ptr<Model_Document> Model_Application::rootDocument()
102 {
103   return myDocs[0];
104 }
105
106 //=======================================================================
107 void Model_Application::setLoadPath(std::string thePath)
108 {
109   myPath = thePath;
110 }
111
112 //=======================================================================
113 const std::string& Model_Application::loadPath() const
114 {
115   return myPath;
116 }
117
118 //=======================================================================
119 void Model_Application::setLoadByDemand(std::string theID)
120 {
121   myLoadedByDemand.insert(theID);
122 }
123
124 //=======================================================================
125 bool Model_Application::isLoadByDemand(std::string theID)
126 {
127   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
128 }
129
130 //=======================================================================
131 void Model_Application::removeUselessDocuments(
132   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
133 {
134   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
135   while(aDoc != myDocs.end()) {
136     bool aFound = false;
137     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
138     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
139       aFound = aDoc->second == *aUsed;
140     }
141     if (!aFound) { // remove the useless
142       aDoc->second->close();
143       myDocs.erase(aDoc);
144       aDoc = myDocs.begin();
145     } else {
146       aDoc++;
147     }
148   }
149 }
150
151 int Model_Application::generateDocumentId()
152 {
153   int aResult = int(myDocs.size());
154   for(; myDocs.find(aResult) != myDocs.end(); aResult++) {} // count until the result id is unique
155   return aResult;
156 }
157
158 //=======================================================================
159 Model_Application::Model_Application()
160 {
161   // store handle to the application to avoid nullification
162   static Handle(Model_Application) TheKeepHandle;
163   TheKeepHandle = this;
164 }
165
166 //=======================================================================
167 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
168 {
169   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
170 }
171
172 //=======================================================================
173 Standard_CString Model_Application::ResourcesName()
174 {
175   return Standard_CString("Standard");
176 }