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_RTTIEXT(Model_Application, TDocStd_Application)
14 static Handle_Model_Application TheApplication = new Model_Application;
16 //=======================================================================
17 Handle(Model_Application) Model_Application::getApplication()
19 return TheApplication;
22 //=======================================================================
23 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
25 if (myDocs.find(theDocID) != myDocs.end())
26 return myDocs[theDocID];
27 return std::shared_ptr<Model_Document>(); // not loaded, so return null
30 //=======================================================================
31 void Model_Application::createDocument(const int theDocID)
33 static const std::string thePartSetKind("PartSet");
34 static const std::string thePartKind("Part");
35 std::shared_ptr<Model_Document> aNew(
36 new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
37 myDocs[theDocID] = aNew;
40 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
41 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
42 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
43 aMessage->setDocument(aNew);
44 Events_Loop::loop()->send(aMessage);
47 //=======================================================================
48 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
50 static const std::string thePartKind("Part"); // root document is never loaded here
51 std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
52 myDocs[theDocID] = aNew;
55 // load it if it must be loaded by demand
56 if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
57 aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
58 myLoadedByDemand.erase(theDocName); // done, don't do it anymore
66 //=======================================================================
67 void Model_Application::deleteDocument(const int theDocID)
69 if (myDocs.find(theDocID) != myDocs.end()) {
70 myDocs[theDocID]->close(true);
71 myDocs.erase(theDocID);
73 myLoadedByDemand.clear();
76 //=======================================================================
77 void Model_Application::deleteAllDocuments()
79 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
80 for(; aDoc != myDocs.end(); aDoc++) {
81 if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
82 aDoc->second->close(true);
85 myLoadedByDemand.clear();
88 //=======================================================================
89 bool Model_Application::hasDocument(const int theDocID)
91 return myDocs.find(theDocID) != myDocs.end();
94 //=======================================================================
95 bool Model_Application::hasRoot()
97 return !myDocs.empty();
100 //=======================================================================
101 std::shared_ptr<Model_Document> Model_Application::rootDocument()
106 //=======================================================================
107 void Model_Application::setLoadPath(std::string thePath)
112 //=======================================================================
113 const std::string& Model_Application::loadPath() const
118 //=======================================================================
119 void Model_Application::setLoadByDemand(std::string theID, const int theDocID)
121 myLoadedByDemand[theID] = theDocID;
124 //=======================================================================
125 bool Model_Application::isLoadByDemand(std::string theID)
127 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
130 //=======================================================================
131 void Model_Application::removeUselessDocuments(
132 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
134 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
135 while(aDoc != myDocs.end()) {
137 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
138 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
139 aFound = aDoc->second == *aUsed;
141 if (!aFound) { // remove the useless
142 aDoc->second->close();
144 aDoc = myDocs.begin();
151 int Model_Application::generateDocumentId()
154 // count until the result id is unique
155 for(aResult = int(myDocs.size()); true; aResult++) {
156 if (myDocs.find(aResult) == myDocs.end()) {
158 std::map<std::string, int>::iterator aLBDIter = myLoadedByDemand.begin();
159 for(; aLBDIter != myLoadedByDemand.end(); aLBDIter++) {
160 if (aLBDIter->second == aResult) {
171 //=======================================================================
172 Model_Application::Model_Application()
174 // store handle to the application to avoid nullification
175 static Handle(Model_Application) TheKeepHandle;
176 TheKeepHandle = this;
179 //=======================================================================
180 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
182 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
185 //=======================================================================
186 Standard_CString Model_Application::ResourcesName()
188 return Standard_CString("Standard");