]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDS/SALOMEDS_StudyManager.cxx
Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager.cxx
1 //  File   : SALOMEDSClient_StudyManager.cxx
2 //  Author : Sergey RUIN
3 //  Module : SALOME
4
5
6 #include "SALOMEDS_StudyManager.hxx"
7
8 #include "SALOMEDSImpl_Study.hxx"
9 #include "SALOMEDS_Study.hxx"
10 #include "SALOMEDS_SObject.hxx"
11 #include "SALOMEDS_Driver_i.hxx"
12
13 #include "Utils_ORB_INIT.hxx" 
14 #include "Utils_SINGLETON.hxx" 
15
16 #ifdef WIN32
17 #include <process.h>
18 #else
19 #include <sys/types.h>
20 #include <unistd.h>
21 #endif
22
23 #include <string>
24 #include <TCollection_AsciiString.hxx> 
25 #include <TColStd_HSequenceOfTransient.hxx>
26
27 #include "OpUtil.hxx"
28
29 using namespace std; 
30
31 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
32
33 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
34 {
35
36 #ifdef WIN32
37   long pid =  (long)_getpid();
38 #else
39   long pid =  (long)getpid();
40 #endif  
41
42   long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
43   if(_isLocal) {
44     _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
45     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
46   }
47   else {
48     _local_impl = NULL;
49     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
50   }
51
52   init_orb();
53 }
54
55 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
56 {
57   init_orb();
58
59   SALOME_NamingService namingService(_orb);
60   CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
61   SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
62   ASSERT( !CORBA::is_nil(theManager) );
63
64 #ifdef WIN32
65   long pid =  (long)_getpid();
66 #else
67   long pid =  (long)getpid();
68 #endif  
69
70   long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
71   if(_isLocal) {
72     _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
73     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
74   }
75   else {
76     _local_impl = NULL;
77     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
78   }
79 }
80
81 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
82 {
83 }
84
85 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
86 {
87   //SRN: Pure CORBA NewStudy as it does more initialization than the local one   
88   SALOMEDSClient_Study* aStudy = NULL;
89
90   SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
91   if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
92   aStudy = new SALOMEDS_Study(aStudy_impl);
93
94   return _PTR(Study)(aStudy);
95 }
96
97 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
98 {
99   //SRN: Pure CORBA Open as it does more initialization than the local one   
100   SALOMEDSClient_Study* aStudy = NULL;
101
102   SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
103   if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy); 
104     
105   aStudy = new SALOMEDS_Study(aStudy_impl.in());
106
107   return _PTR(Study)(aStudy);
108 }
109  
110 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
111 {
112   //SRN: Pure CORBA close as it does more cleaning than the local one
113   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
114   _corba_impl->Close(aStudy);
115 }
116  
117 void SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
118 {
119   //SRN: Pure CORBA save as the save operation require CORBA in any case 
120   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
121   _corba_impl->Save(aStudy, theMultiFile);
122 }
123  
124 void SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
125 {
126   //SRN: Pure CORBA save as the save operation require CORBA in any case 
127   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
128   _corba_impl->SaveASCII(aStudy, theMultiFile);
129 }
130  
131 void SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
132 {
133   //SRN: Pure CORBA save as the save operation require CORBA in any case 
134   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
135   _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
136 }
137  
138 void SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
139 {
140   //SRN: Pure CORBA save as the save operation require CORBA in any case 
141   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
142   _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
143 }
144
145 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
146 {
147   std::vector<std::string> aVector;
148   int aLength, i;
149
150   if(_isLocal) {
151     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetOpenStudies();
152     aLength = aSeq->Length();
153     for(i = 1; i <= aLength; i++) 
154       aVector.push_back(Handle(SALOMEDSImpl_Study)::DownCast(aSeq->Value(i))->Name().ToCString());
155   }
156   else {
157     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
158     aLength = aSeq->length();
159     for(i = 0; i < aLength; i++) 
160       aVector.push_back(aSeq[i].in());
161   }
162   return aVector;
163 }
164  
165 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName) 
166 {
167   SALOMEDSClient_Study* aStudy = NULL;
168   if(_isLocal) {
169     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByName((char*)theStudyName.c_str());
170     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
171     aStudy = new SALOMEDS_Study(aStudy_impl);
172   }
173   else  {
174     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
175     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
176     aStudy = new SALOMEDS_Study(aStudy_impl);
177   }
178   return _PTR(Study)(aStudy);
179 }
180
181 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID) 
182 {
183   SALOMEDSClient_Study* aStudy = NULL;
184   if(_isLocal) {
185     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByID(theStudyID);
186     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
187     aStudy = new SALOMEDS_Study(aStudy_impl);
188   }
189   else { 
190     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
191     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
192     aStudy = new SALOMEDS_Study(aStudy_impl);
193   }
194   return _PTR(Study)(aStudy); 
195 }
196  
197 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
198 {
199   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
200   bool ret;
201
202   if(_isLocal) {
203     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
204     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
205     ret = _local_impl->CanCopy(aSO_impl, aDriver);
206     delete aDriver;
207   }
208   else {
209     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
210   }
211
212   return ret;
213 }
214  
215 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
216 {
217   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
218   bool ret;
219   if(_isLocal) {
220     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
221     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
222     ret = _local_impl->Copy(aSO_impl, aDriver);
223     delete aDriver;
224   }
225   else {
226     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
227   }
228   return ret;
229 }
230  
231 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
232 {
233   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
234   bool ret;
235
236   if(_isLocal) {
237     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
238     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
239     ret = _local_impl->CanPaste(aSO_impl, aDriver);
240     delete aDriver;
241   }
242   else {
243     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
244   }
245
246   return ret;
247 }
248  
249 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
250 {
251   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
252   SALOMEDSClient_SObject* aResult = NULL;
253
254   if(_isLocal) {
255     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
256     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
257     Handle(SALOMEDSImpl_SObject) aNewSO = _local_impl->Paste(aSO_impl, aDriver);
258     delete aDriver;
259     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
260     aResult = new SALOMEDS_SObject(aNewSO);
261   }
262   else {
263     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
264     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
265     aResult = new SALOMEDS_SObject(aNewSO);
266   }
267
268   return _PTR(SObject)(aResult);
269 }
270
271
272 void SALOMEDS_StudyManager::init_orb()
273 {
274   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
275   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()); 
276   _orb = init(0 , 0 ) ;     
277 }
278
279 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
280 {
281   SALOMEDS_Driver_i* driver = NULL;
282   
283   Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
284   if(!aSCO.IsNull()) {
285     TCollection_AsciiString IOREngine = aSCO->GetIOR();
286     if(!IOREngine.IsEmpty()) {
287       CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
288       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
289       driver = new SALOMEDS_Driver_i(Engine, orb);
290     }
291   }  
292
293   return driver;
294 }