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