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)
17 static Handle_Model_Application TheApplication = new Model_Application;
19 //=======================================================================
20 Handle(Model_Application) Model_Application::getApplication()
22 return TheApplication;
25 //=======================================================================
26 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
28 if (myDocs.find(theDocID) != myDocs.end())
29 return myDocs[theDocID];
30 return std::shared_ptr<Model_Document>(); // not loaded, so return null
33 //=======================================================================
34 void Model_Application::createDocument(const int theDocID)
36 static const std::string thePartSetKind("PartSet");
37 static const std::string thePartKind("Part");
38 std::shared_ptr<Model_Document> aNew(
39 new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
40 myDocs[theDocID] = aNew;
43 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
44 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
45 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
46 aMessage->setDocument(aNew);
47 Events_Loop::loop()->send(aMessage);
50 //=======================================================================
51 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
53 static const std::string thePartKind("Part"); // root document is never loaded here
54 std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
55 myDocs[theDocID] = aNew;
58 // load it if it must be loaded by demand
59 if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
60 aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
61 myLoadedByDemand.erase(theDocName); // done, don't do it anymore
69 //=======================================================================
70 void Model_Application::deleteDocument(const int theDocID)
72 if (myDocs.find(theDocID) != myDocs.end()) {
73 myDocs[theDocID]->close(true);
74 myDocs.erase(theDocID);
76 myLoadedByDemand.clear();
79 //=======================================================================
80 void Model_Application::deleteAllDocuments()
82 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
83 for(; aDoc != myDocs.end(); aDoc++) {
84 if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
85 aDoc->second->close(true);
88 myLoadedByDemand.clear();
91 //=======================================================================
92 bool Model_Application::hasDocument(const int theDocID)
94 return myDocs.find(theDocID) != myDocs.end();
97 //=======================================================================
98 bool Model_Application::hasRoot()
100 return !myDocs.empty();
103 //=======================================================================
104 std::shared_ptr<Model_Document> Model_Application::rootDocument()
109 //=======================================================================
110 void Model_Application::setLoadPath(std::string thePath)
115 //=======================================================================
116 const std::string& Model_Application::loadPath() const
121 //=======================================================================
122 void Model_Application::setLoadByDemand(std::string theID)
124 myLoadedByDemand.insert(theID);
127 //=======================================================================
128 bool Model_Application::isLoadByDemand(std::string theID)
130 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
133 //=======================================================================
134 void Model_Application::removeUselessDocuments(
135 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
137 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
138 while(aDoc != myDocs.end()) {
140 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
141 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
142 aFound = aDoc->second == *aUsed;
144 if (!aFound) { // remove the useless
145 aDoc->second->close();
147 aDoc = myDocs.begin();
154 int Model_Application::generateDocumentId()
156 int aResult = int(myDocs.size());
157 for(; myDocs.find(aResult) != myDocs.end(); aResult++); // count until the result id is unique
161 //=======================================================================
162 Model_Application::Model_Application()
164 // store handle to the application to avoid nullification
165 static Handle(Model_Application) TheKeepHandle;
166 TheKeepHandle = this;
169 //=======================================================================
170 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
172 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
175 //=======================================================================
176 Standard_CString Model_Application::ResourcesName()
178 return Standard_CString("Standard");