Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into Dev_2.8.0
[modules/shaper.git] / src / Model / Model_Application.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Model_Application.h>
22 #include <Model_Document.h>
23
24 #include <ModelAPI_Events.h>
25
26 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
27
28 static Handle_Model_Application TheApplication = new Model_Application;
29
30 //=======================================================================
31 Handle(Model_Application) Model_Application::getApplication()
32 {
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, const int theDocID)
134 {
135   myLoadedByDemand[theID] = theDocID;
136 }
137
138 //=======================================================================
139 bool Model_Application::isLoadByDemand(std::string theID, const int theDocIndex)
140 {
141   return myLoadedByDemand.find(theID) != myLoadedByDemand.end() &&
142     myLoadedByDemand[theID] == theDocIndex;
143 }
144
145 //=======================================================================
146 void Model_Application::removeUselessDocuments(
147   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
148 {
149   std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
150   while(aDoc != myDocs.end()) {
151     bool aFound = false;
152     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
153     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
154       aFound = aDoc->second == *aUsed;
155     }
156     if (!aFound) { // remove the useless
157       aDoc->second->close();
158       myDocs.erase(aDoc);
159       aDoc = myDocs.begin();
160     } else {
161       aDoc++;
162     }
163   }
164 }
165
166 int Model_Application::generateDocumentId()
167 {
168   int aResult;
169   // count until the result id is unique
170   for(aResult = int(myDocs.size()); true; aResult++) {
171     if (myDocs.find(aResult) == myDocs.end()) {
172       bool aFound = false;
173       std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
174       for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
175         if (aLBDIter->second == aResult) {
176           aFound = true;
177           break;
178         }
179       }
180       if (!aFound) break;
181     }
182   }
183   return aResult;
184 }
185
186 //=======================================================================
187 Model_Application::Model_Application()
188 {
189   // store handle to the application to avoid nullification
190   static Handle(Model_Application) TheKeepHandle;
191   TheKeepHandle = this;
192 }
193
194 //=======================================================================
195 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
196 {
197   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
198 }
199
200 //=======================================================================
201 Standard_CString Model_Application::ResourcesName()
202 {
203   return Standard_CString("Standard");
204 }