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