1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Model_Application.h>
21 #include <Model_Document.h>
23 #include <ModelAPI_Events.h>
25 #include <PCDM_RetrievalDriver.hxx>
27 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
29 static Handle_Model_Application TheApplication = new Model_Application;
31 //=======================================================================
32 Handle(Model_Application) Model_Application::getApplication()
34 return TheApplication;
37 //=======================================================================
38 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
40 if (myDocs.find(theDocID) != myDocs.end())
41 return myDocs[theDocID];
42 return std::shared_ptr<Model_Document>(); // not loaded, so return null
45 //=======================================================================
46 void Model_Application::createDocument(const int theDocID)
48 static const std::string thePartSetKind("PartSet");
49 static const std::string thePartKind("Part");
50 std::shared_ptr<Model_Document> aNew(
51 new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
52 myDocs[theDocID] = aNew;
55 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
56 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
57 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
58 aMessage->setDocument(aNew);
59 Events_Loop::loop()->send(aMessage);
62 //=======================================================================
63 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
65 static const std::string thePartKind("Part"); // root document is never loaded here
66 std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
67 myDocs[theDocID] = aNew;
70 // load it if it must be loaded by demand
71 if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
72 aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
73 myLoadedByDemand.erase(theDocName); // done, don't do it anymore
81 //=======================================================================
82 void Model_Application::deleteDocument(const int theDocID)
84 if (myDocs.find(theDocID) != myDocs.end()) {
85 myDocs[theDocID]->close(true);
86 myDocs.erase(theDocID);
88 myLoadedByDemand.clear();
91 //=======================================================================
92 void Model_Application::deleteAllDocuments()
94 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
95 for(; aDoc != myDocs.end(); aDoc++) {
96 if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
97 aDoc->second->close(true);
100 myLoadedByDemand.clear();
103 //=======================================================================
104 bool Model_Application::hasDocument(const int theDocID)
106 return myDocs.find(theDocID) != myDocs.end();
109 //=======================================================================
110 bool Model_Application::hasRoot()
112 return !myDocs.empty();
115 //=======================================================================
116 std::shared_ptr<Model_Document> Model_Application::rootDocument()
121 //=======================================================================
122 void Model_Application::setLoadPath(std::string thePath)
127 //=======================================================================
128 const std::string& Model_Application::loadPath() const
133 //=======================================================================
134 void Model_Application::setLoadByDemand(std::string theID, const int theDocID)
136 myLoadedByDemand[theID] = theDocID;
139 //=======================================================================
140 bool Model_Application::isLoadByDemand(std::string theID, const int theDocIndex)
142 return myLoadedByDemand.find(theID) != myLoadedByDemand.end() &&
143 myLoadedByDemand[theID] == theDocIndex;
146 //=======================================================================
147 int Model_Application::generateDocumentId()
150 // count until the result id is unique
151 for(aResult = int(myDocs.size()); true; aResult++) {
152 if (myDocs.find(aResult) == myDocs.end()) {
154 std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
155 for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
156 if (aLBDIter->second == aResult) {
167 //=======================================================================
168 Model_Application::Model_Application()
170 // store handle to the application to avoid nullification
171 static Handle(Model_Application) TheKeepHandle;
172 TheKeepHandle = this;
173 // additional file format supported
174 static TCollection_ExtendedString THE_DOC_FORMAT("BinShaperPart");
175 static TCollection_ExtendedString THE_FILE_EXT("shaperpart");
176 Handle(PCDM_RetrievalDriver) aReader =
177 Handle(PCDM_RetrievalDriver)::DownCast(TheKeepHandle->ReaderFromFormat("BinOcaf"));
178 Handle(PCDM_StorageDriver) aWriter = TheKeepHandle->WriterFromFormat("BinOcaf");
179 TheKeepHandle->DefineFormat(THE_DOC_FORMAT, "Shaper Part document", THE_FILE_EXT,
183 //=======================================================================
184 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
186 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
189 //=======================================================================
190 Standard_CString Model_Application::ResourcesName()
192 return Standard_CString("Standard");