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