Salome HOME
ed730d1c8578812e38c508d1cd45f5bc3522bca5
[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)
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;
168   // count until the result id is unique
169   for(aResult = int(myDocs.size()); true; aResult++) {
170     if (myDocs.find(aResult) == myDocs.end()) {
171       bool aFound = false;
172       std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
173       for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
174         if (aLBDIter->second == aResult) {
175           aFound = true;
176           break;
177         }
178       }
179       if (!aFound) break;
180     }
181   }
182   return aResult;
183 }
184
185 //=======================================================================
186 Model_Application::Model_Application()
187 {
188   // store handle to the application to avoid nullification
189   static Handle(Model_Application) TheKeepHandle;
190   TheKeepHandle = this;
191 }
192
193 //=======================================================================
194 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
195 {
196   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
197 }
198
199 //=======================================================================
200 Standard_CString Model_Application::ResourcesName()
201 {
202   return Standard_CString("Standard");
203 }