1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <Model_Application.h>
22 #include <Model_Document.h>
24 #include <ModelAPI_Events.h>
26 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
28 static Handle_Model_Application TheApplication = new Model_Application;
30 //=======================================================================
31 Handle(Model_Application) Model_Application::getApplication()
33 return TheApplication;
36 //=======================================================================
37 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
39 if (myDocs.find(theDocID) != myDocs.end())
40 return myDocs[theDocID];
41 return std::shared_ptr<Model_Document>(); // not loaded, so return null
44 //=======================================================================
45 void Model_Application::createDocument(const int theDocID)
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;
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);
61 //=======================================================================
62 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
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;
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
80 //=======================================================================
81 void Model_Application::deleteDocument(const int theDocID)
83 if (myDocs.find(theDocID) != myDocs.end()) {
84 myDocs[theDocID]->close(true);
85 myDocs.erase(theDocID);
87 myLoadedByDemand.clear();
90 //=======================================================================
91 void Model_Application::deleteAllDocuments()
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);
99 myLoadedByDemand.clear();
102 //=======================================================================
103 bool Model_Application::hasDocument(const int theDocID)
105 return myDocs.find(theDocID) != myDocs.end();
108 //=======================================================================
109 bool Model_Application::hasRoot()
111 return !myDocs.empty();
114 //=======================================================================
115 std::shared_ptr<Model_Document> Model_Application::rootDocument()
120 //=======================================================================
121 void Model_Application::setLoadPath(std::string thePath)
126 //=======================================================================
127 const std::string& Model_Application::loadPath() const
132 //=======================================================================
133 void Model_Application::setLoadByDemand(std::string theID, const int theDocID)
135 myLoadedByDemand[theID] = theDocID;
138 //=======================================================================
139 bool Model_Application::isLoadByDemand(std::string theID, const int theDocIndex)
141 return myLoadedByDemand.find(theID) != myLoadedByDemand.end() &&
142 myLoadedByDemand[theID] == theDocIndex;
145 //=======================================================================
146 void Model_Application::removeUselessDocuments(
147 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
149 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
150 while(aDoc != myDocs.end()) {
152 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
153 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
154 aFound = aDoc->second == *aUsed;
156 if (!aFound) { // remove the useless
157 aDoc->second->close();
159 aDoc = myDocs.begin();
166 int Model_Application::generateDocumentId()
169 // count until the result id is unique
170 for(aResult = int(myDocs.size()); true; aResult++) {
171 if (myDocs.find(aResult) == myDocs.end()) {
173 std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
174 for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
175 if (aLBDIter->second == aResult) {
186 //=======================================================================
187 Model_Application::Model_Application()
189 // store handle to the application to avoid nullification
190 static Handle(Model_Application) TheKeepHandle;
191 TheKeepHandle = this;
194 //=======================================================================
195 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
197 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
200 //=======================================================================
201 Standard_CString Model_Application::ResourcesName()
203 return Standard_CString("Standard");