1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Application.cxx
4 // Created: Fri Sep 2 2011
5 // Author: Mikhail PONIKAROV
7 #include <Model_Application.h>
8 #include <Model_Document.h>
10 #include <ModelAPI_Events.h>
12 IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
13 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
15 static Handle_Model_Application TheApplication = new Model_Application;
17 //=======================================================================
18 Handle(Model_Application) Model_Application::getApplication()
20 return TheApplication;
23 //=======================================================================
24 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
26 if (myDocs.find(theDocID) != myDocs.end())
27 return myDocs[theDocID];
28 return std::shared_ptr<Model_Document>(); // not loaded, so return null
31 //=======================================================================
32 void Model_Application::createDocument(const int theDocID)
34 static const std::string thePartSetKind("PartSet");
35 static const std::string thePartKind("Part");
36 std::shared_ptr<Model_Document> aNew(
37 new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
38 myDocs[theDocID] = aNew;
41 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
42 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
43 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
44 aMessage->setDocument(aNew);
45 Events_Loop::loop()->send(aMessage);
48 //=======================================================================
49 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
51 static const std::string thePartKind("Part"); // root document is never loaded here
52 std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
53 myDocs[theDocID] = aNew;
56 // load it if it must be loaded by demand
57 if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
58 aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
59 myLoadedByDemand.erase(theDocName); // done, don't do it anymore
67 //=======================================================================
68 void Model_Application::deleteDocument(const int theDocID)
70 if (myDocs.find(theDocID) != myDocs.end()) {
71 myDocs[theDocID]->close(true);
72 myDocs.erase(theDocID);
74 myLoadedByDemand.clear();
77 //=======================================================================
78 void Model_Application::deleteAllDocuments()
80 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
81 for(; aDoc != myDocs.end(); aDoc++) {
82 if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
83 aDoc->second->close(true);
86 myLoadedByDemand.clear();
89 //=======================================================================
90 bool Model_Application::hasDocument(const int theDocID)
92 return myDocs.find(theDocID) != myDocs.end();
95 //=======================================================================
96 bool Model_Application::hasRoot()
98 return !myDocs.empty();
101 //=======================================================================
102 std::shared_ptr<Model_Document> Model_Application::rootDocument()
107 //=======================================================================
108 void Model_Application::setLoadPath(std::string thePath)
113 //=======================================================================
114 const std::string& Model_Application::loadPath() const
119 //=======================================================================
120 void Model_Application::setLoadByDemand(std::string theID)
122 myLoadedByDemand.insert(theID);
125 //=======================================================================
126 bool Model_Application::isLoadByDemand(std::string theID)
128 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
131 //=======================================================================
132 void Model_Application::removeUselessDocuments(
133 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
135 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
136 while(aDoc != myDocs.end()) {
138 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
139 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
140 aFound = aDoc->second == *aUsed;
142 if (!aFound) { // remove the useless
143 aDoc->second->close();
145 aDoc = myDocs.begin();
152 int Model_Application::generateDocumentId()
154 int aResult = int(myDocs.size());
155 for(; myDocs.find(aResult) != myDocs.end(); aResult++) {} // count until the result id is unique
159 //=======================================================================
160 Model_Application::Model_Application()
162 // store handle to the application to avoid nullification
163 static Handle(Model_Application) TheKeepHandle;
164 TheKeepHandle = this;
167 //=======================================================================
168 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
170 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
173 //=======================================================================
174 Standard_CString Model_Application::ResourcesName()
176 return Standard_CString("Standard");