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)
24 static Handle_Model_Application TheApplication = new Model_Application;
26 //=======================================================================
27 Handle(Model_Application) Model_Application::getApplication()
31 DFBrowserAPI_PluginMgr::activateBrowser("OCAFBrowser.dll", TheApplication);
35 return TheApplication;
38 //=======================================================================
39 std::shared_ptr<Model_Document> Model_Application::document(const int theDocID)
41 if (myDocs.find(theDocID) != myDocs.end())
42 return myDocs[theDocID];
43 return std::shared_ptr<Model_Document>(); // not loaded, so return null
46 //=======================================================================
47 void Model_Application::createDocument(const int theDocID)
49 static const std::string thePartSetKind("PartSet");
50 static const std::string thePartKind("Part");
51 std::shared_ptr<Model_Document> aNew(
52 new Model_Document(theDocID, theDocID == 0 ? thePartSetKind : thePartKind));
53 myDocs[theDocID] = aNew;
56 static Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
57 std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage = std::shared_ptr
58 <ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
59 aMessage->setDocument(aNew);
60 Events_Loop::loop()->send(aMessage);
63 //=======================================================================
64 bool Model_Application::loadDocument(const std::string theDocName, const int theDocID)
66 static const std::string thePartKind("Part"); // root document is never loaded here
67 std::shared_ptr<Model_Document> aNew(new Model_Document(theDocID, thePartKind));
68 myDocs[theDocID] = aNew;
71 // load it if it must be loaded by demand
72 if (myLoadedByDemand.find(theDocName) != myLoadedByDemand.end() && !myPath.empty()) {
73 aRes = aNew->load(myPath.c_str(), theDocName.c_str(), aNew);
74 myLoadedByDemand.erase(theDocName); // done, don't do it anymore
82 //=======================================================================
83 void Model_Application::deleteDocument(const int theDocID)
85 if (myDocs.find(theDocID) != myDocs.end()) {
86 myDocs[theDocID]->close(true);
87 myDocs.erase(theDocID);
89 myLoadedByDemand.clear();
92 //=======================================================================
93 void Model_Application::deleteAllDocuments()
95 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
96 for(; aDoc != myDocs.end(); aDoc++) {
97 if (aDoc->second->isOpened()) // here is main document was closed before subs and closed subs
98 aDoc->second->close(true);
101 myLoadedByDemand.clear();
104 //=======================================================================
105 bool Model_Application::hasDocument(const int theDocID)
107 return myDocs.find(theDocID) != myDocs.end();
110 //=======================================================================
111 bool Model_Application::hasRoot()
113 return !myDocs.empty();
116 //=======================================================================
117 std::shared_ptr<Model_Document> Model_Application::rootDocument()
122 //=======================================================================
123 void Model_Application::setLoadPath(std::string thePath)
128 //=======================================================================
129 const std::string& Model_Application::loadPath() const
134 //=======================================================================
135 void Model_Application::setLoadByDemand(std::string theID)
137 myLoadedByDemand.insert(theID);
140 //=======================================================================
141 bool Model_Application::isLoadByDemand(std::string theID)
143 return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
146 //=======================================================================
147 void Model_Application::removeUselessDocuments(
148 std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
150 std::map<int, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
151 while(aDoc != myDocs.end()) {
153 std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
154 for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
155 aFound = aDoc->second == *aUsed;
157 if (!aFound) { // remove the useless
158 aDoc->second->close();
160 aDoc = myDocs.begin();
167 int Model_Application::generateDocumentId()
169 int aResult = int(myDocs.size());
170 for(; myDocs.find(aResult) != myDocs.end(); aResult++) {} // count until the result id is unique
174 //=======================================================================
175 Model_Application::Model_Application()
177 // store handle to the application to avoid nullification
178 static Handle(Model_Application) TheKeepHandle;
179 TheKeepHandle = this;
182 //=======================================================================
183 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
185 theFormats.Append(TCollection_ExtendedString("BinOcaf")); // standard binary schema
188 //=======================================================================
189 Standard_CString Model_Application::ResourcesName()
191 return Standard_CString("Standard");