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