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