Salome HOME
Default planes and origin initialization
[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   std::shared_ptr<Model_Document> aNew(
34     new Model_Document(theDocID, theDocID == "root" ? thePartSetKind : thePartKind));
35   myDocs[theDocID] = aNew;
36
37   Events_ID anId = ModelAPI_DocumentCreatedMessage::eventId();
38   std::shared_ptr<ModelAPI_DocumentCreatedMessage> aMessage =
39         std::shared_ptr<ModelAPI_DocumentCreatedMessage>(new ModelAPI_DocumentCreatedMessage(anId, this));
40   aMessage->setDocument(aNew);
41   Events_Loop::loop()->send(aMessage);
42   // load it if it must be loaded by demand
43   if (myLoadedByDemand.find(theDocID) != myLoadedByDemand.end() && !myPath.empty()) {
44     aNew->load(myPath.c_str());
45     myLoadedByDemand.erase(theDocID);  // done, don't do it anymore
46   }
47
48   return myDocs[theDocID];
49 }
50
51 void Model_Application::deleteDocument(string theDocID)
52 {
53   if (myDocs.find(theDocID) != myDocs.end()) {
54     myDocs[theDocID]->close(true);
55     myDocs.erase(theDocID);
56   }
57   myLoadedByDemand.clear();
58 }
59
60 void Model_Application::deleteAllDocuments()
61 {
62   myDocs.clear();
63   myLoadedByDemand.clear();
64 }
65
66 //=======================================================================
67 bool Model_Application::hasDocument(std::string theDocID)
68 {
69   return myDocs.find(theDocID) != myDocs.end();
70 }
71
72 //=======================================================================
73 void Model_Application::setLoadPath(std::string thePath)
74 {
75   myPath = thePath;
76 }
77
78 //=======================================================================
79 const std::string& Model_Application::loadPath() const
80 {
81   return myPath;
82 }
83
84 //=======================================================================
85 void Model_Application::setLoadByDemand(std::string theID)
86 {
87   myLoadedByDemand.insert(theID);
88 }
89
90 //=======================================================================
91 bool Model_Application::isLoadByDemand(std::string theID)
92 {
93   return myLoadedByDemand.find(theID) != myLoadedByDemand.end();
94 }
95
96 //=======================================================================
97 void Model_Application::removeUselessDocuments(
98   std::list<std::shared_ptr<ModelAPI_Document> > theUsedDocs)
99 {
100   std::map<std::string, std::shared_ptr<Model_Document> >::iterator aDoc = myDocs.begin();
101   while(aDoc != myDocs.end()) {
102     bool aFound = false;
103     std::list<std::shared_ptr<ModelAPI_Document> >::iterator aUsed = theUsedDocs.begin();
104     for(; !aFound && aUsed != theUsedDocs.end(); aUsed++) {
105       aFound = aDoc->second == *aUsed;
106     }
107     if (!aFound) { // remove the useless
108       aDoc->second->close();
109       myDocs.erase(aDoc);
110       aDoc = myDocs.begin();
111     } else {
112       aDoc++;
113     }
114   }
115 }
116
117 //=======================================================================
118 Model_Application::Model_Application()
119 {
120   // store handle to the application to avoid nullification
121   static Handle(Model_Application) TheKeepHandle;
122   TheKeepHandle = this;
123 }
124
125 //=======================================================================
126 void Model_Application::Formats(TColStd_SequenceOfExtendedString& theFormats)
127 {
128   theFormats.Append(TCollection_ExtendedString("BinOcaf"));  // standard binary schema
129 }
130
131 //=======================================================================
132 Standard_CString Model_Application::ResourcesName()
133 {
134   return Standard_CString("Standard");
135 }