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>
13 #include <DFBrowserAPI_PluginMgr.h>
14 #include <DFBrowserAPI_Communicator.h>
16 static bool FirstCall = true;
19 IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
20 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
22 static Handle_Model_Application TheApplication = new Model_Application;
24 //=======================================================================
25 Handle(Model_Application) Model_Application::getApplication()
29 DFBrowserAPI_PluginMgr::activateBrowser("OCAFBrowser.dll", TheApplication);
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)
135 myLoadedByDemand.insert(theID);
138 //=======================================================================
139 bool Model_Application::isLoadByDemand(std::string theID)
141 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
144 //=======================================================================
145 void Model_Application::removeUselessDocuments(
146 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
148 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
149 while(aDoc != myDocs.end()) {
151 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
152 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
153 aFound = aDoc->second == *aUsed;
155 if (!aFound) { // remove the useless
156 aDoc->second->close();
158 aDoc = myDocs.begin();
165 int Model_Application::generateDocumentId()
167 int aResult = int(myDocs.size());
168 for(; myDocs.find(aResult) != myDocs.end(); aResult++) {} // count until the result id is unique
172 //=======================================================================
173 Model_Application::Model_Application()
175 // store handle to the application to avoid nullification
176 static Handle(Model_Application) TheKeepHandle;
177 TheKeepHandle = this;
180 //=======================================================================
181 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
183 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
186 //=======================================================================
187 Standard_CString Model_Application::ResourcesName()
189 return Standard_CString("Standard");