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 const std::shared_ptr<Model_Document>& Model_Application::getDocument(string theDocID)
28 if (myDocs.find(theDocID) != myDocs.end())
29 return myDocs[theDocID];
31 static const std::string thePartSetKind("PartSet");
32 static const std::string thePartKind("Part");
33 bool isRoot = theDocID == "root"; // the document is root
34 std::shared_ptr<Model_Document> aNew(
35 new Model_Document(theDocID, isRoot ? thePartSetKind : thePartKind));
36 myDocs[theDocID] = aNew;
38 // load it if it must be loaded by demand
39 if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
40 aNew->load(myPath.c_str(), aNew);
41 myLoadedByDemand.erase(theDocID); // done, don't do it anymore
44 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
45 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
46 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
47 aMessage->setDocument(aNew);
48 Events_Loop::loop()->send(aMessage);
51 return myDocs[theDocID];
54 void Model_Application::deleteDocument(string theDocID)
56 if (myDocs.find(theDocID) != myDocs.end()) {
57 myDocs[theDocID]->close(true);
58 myDocs.erase(theDocID);
60 myLoadedByDemand.clear();
63 void Model_Application::deleteAllDocuments()
65 std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
66 for(; aDoc != myDocs.end(); aDoc++) {
67 aDoc->second->close(true);
70 myLoadedByDemand.clear();
73 //=======================================================================
74 bool Model_Application::hasDocument(std::string theDocID)
76 return myDocs.find(theDocID) != myDocs.end();
79 //=======================================================================
80 void Model_Application::setLoadPath(std::string thePath)
85 //=======================================================================
86 const std::string& Model_Application::loadPath() const
91 //=======================================================================
92 void Model_Application::setLoadByDemand(std::string theID)
94 myLoadedByDemand.insert(theID);
97 //=======================================================================
98 bool Model_Application::isLoadByDemand(std::string theID)
100 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
103 //=======================================================================
104 void Model_Application::removeUselessDocuments(
105 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
107 std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
108 while(aDoc != myDocs.end()) {
110 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
111 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
112 aFound = aDoc->second == *aUsed;
114 if (!aFound) { // remove the useless
115 aDoc->second->close();
117 aDoc = myDocs.begin();
124 //=======================================================================
125 Model_Application::Model_Application()
127 // store handle to the application to avoid nullification
128 static Handle(Model_Application) TheKeepHandle;
129 TheKeepHandle = this;
132 //=======================================================================
133 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
135 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
138 //=======================================================================
139 Standard_CString Model_Application::ResourcesName()
141 return Standard_CString("Standard");