Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[modules/shaper.git] / src / Model / Model_Application.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Application.cxx
4 // Created:     Fri Sep 2 2011
5 // Author:      Mikhail PONIKAROV
6
7 #include <Model_Application.h>
8 #include <Model_Document.h>
9
10 #include <ModelAPI_Events.h>
11
12 IMPLEMENT_STANDARD_HANDLE(Model_Application, TDocStd_Application)
13 IMPLEMENT_STANDARD_RTTIEXT(Model_Application, TDocStd_Application)
14
15 using namespace std;
16
17 static Handle_Model_Application TheApplication = new Model_Application;
18
19 //=======================================================================
20 Handle(Model_Application) Model_Application::getApplication()
21 {
22   return TheApplication;
23 }
24
25 //=======================================================================
26 const std::shared_ptr<Model_Document>& Model_Application::getDocument(string theDocID)
27 {
28   if (myDocs.find(theDocID) != myDocs.end())
29     return myDocs[theDocID];
30
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;
37
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());
41     myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
42   } else {
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);
48   }
49
50   return myDocs[theDocID];
51 }
52
53 void Model_Application::deleteDocument(string theDocID)
54 {
55   if (myDocs.find(theDocID) != myDocs.end()) {
56     myDocs[theDocID]->close(true);
57     myDocs.erase(theDocID);
58   }
59   myLoadedByDemand.clear();
60 }
61
62 void Model_Application::deleteAllDocuments()
63 {
64   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
65   for(; aDoc != myDocs.end(); aDoc++) {
66     aDoc->second->close();
67   }
68   myDocs.clear();
69   myLoadedByDemand.clear();
70 }
71
72 //=======================================================================
73 bool Model_Application::hasDocument(std::string theDocID)
74 {
75   return myDocs.find(theDocID) != myDocs.end();
76 }
77
78 //=======================================================================
79 void Model_Application::setLoadPath(std::string thePath)
80 {
81   myPath = thePath;
82 }
83
84 //=======================================================================
85 const std::string& Model_Application::loadPath() const
86 {
87   return myPath;
88 }
89
90 //=======================================================================
91 void Model_Application::setLoadByDemand(std::string theID)
92 {
93   myLoadedByDemand.insert(theID);
94 }
95
96 //=======================================================================
97 bool Model_Application::isLoadByDemand(std::string theID)
98 {
99   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
100 }
101
102 //=======================================================================
103 void Model_Application::removeUselessDocuments(
104   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
105 {
106   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
107   while(aDoc != myDocs.end()) {
108     bool aFound = false;
109     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
110     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
111       aFound = aDoc->second == *aUsed;
112     }
113     if (!aFound) { // remove the useless
114       aDoc->second->close();
115       myDocs.erase(aDoc);
116       aDoc = myDocs.begin();
117     } else {
118       aDoc++;
119     }
120   }
121 }
122
123 //=======================================================================
124 Model_Application::Model_Application()
125 {
126   // store handle to the application to avoid nullification
127   static Handle(Model_Application) TheKeepHandle;
128   TheKeepHandle = this;
129 }
130
131 //=======================================================================
132 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
133 {
134   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
135 }
136
137 //=======================================================================
138 Standard_CString Model_Application::ResourcesName()
139 {
140   return Standard_CString("Standard");
141 }