Salome HOME
ENV: Windows porting.
[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   SALOMEDSClient_Study* aStudy = NULL;
88
89   if(_isLocal) {
90     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->NewStudy((char*)study_name.c_str());
91     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
92     aStudy = new SALOMEDS_Study(aStudy_impl);
93   }
94   else {
95     SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
96     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
97     aStudy = new SALOMEDS_Study(aStudy_impl);
98   }
99
100   return _PTR(Study)(aStudy);
101 }
102
103 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
104 {
105   SALOMEDSClient_Study* aStudy = NULL;
106
107   if(_isLocal) {
108     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->Open((char*)theStudyUrl.c_str());
109     if(aStudy_impl.IsNull()) return  _PTR(Study)(aStudy);
110     aStudy = new SALOMEDS_Study(aStudy_impl);
111   }
112   else {
113     SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
114     if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy); 
115     
116     aStudy = new SALOMEDS_Study(aStudy_impl.in());
117   }
118
119   return _PTR(Study)(aStudy);
120 }
121  
122 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
123 {
124   //SRN: Pure CORBA close as it does more cleaning then the local one
125   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
126   _corba_impl->Close(aStudy);
127 }
128  
129 void SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
130 {
131   //SRN: Pure CORBA save as the save operation require CORBA in any case 
132   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
133   _corba_impl->Save(aStudy, theMultiFile);
134 }
135  
136 void SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
137 {
138   //SRN: Pure CORBA save as the save operation require CORBA in any case 
139   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
140   _corba_impl->SaveASCII(aStudy, theMultiFile);
141 }
142  
143 void SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
144 {
145   //SRN: Pure CORBA save as the save operation require CORBA in any case 
146   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
147   _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
148 }
149  
150 void SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
151 {
152   //SRN: Pure CORBA save as the save operation require CORBA in any case 
153   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
154   _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
155 }
156
157 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
158 {
159   std::vector<std::string> aVector;
160   int aLength, i;
161
162   if(_isLocal) {
163     Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetOpenStudies();
164     aLength = aSeq->Length();
165     for(i = 1; i <= aLength; i++) 
166       aVector.push_back(Handle(SALOMEDSImpl_Study)::DownCast(aSeq->Value(i))->Name().ToCString());
167   }
168   else {
169     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
170     aLength = aSeq->length();
171     for(i = 0; i < aLength; i++) 
172       aVector.push_back(aSeq[i].in());
173   }
174   return aVector;
175 }
176  
177 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName) 
178 {
179   SALOMEDSClient_Study* aStudy = NULL;
180   if(_isLocal) {
181     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByName((char*)theStudyName.c_str());
182     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
183     aStudy = new SALOMEDS_Study(aStudy_impl);
184   }
185   else  {
186     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
187     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
188     aStudy = new SALOMEDS_Study(aStudy_impl);
189   }
190   return _PTR(Study)(aStudy);
191 }
192
193 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID) 
194 {
195   SALOMEDSClient_Study* aStudy = NULL;
196   if(_isLocal) {
197     Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByID(theStudyID);
198     if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
199     aStudy = new SALOMEDS_Study(aStudy_impl);
200   }
201   else { 
202     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
203     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy); 
204     aStudy = new SALOMEDS_Study(aStudy_impl);
205   }
206   return _PTR(Study)(aStudy); 
207 }
208  
209 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
210 {
211   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
212   bool ret;
213
214   if(_isLocal) {
215     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
216     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
217     ret = _local_impl->CanCopy(aSO_impl, aDriver);
218     delete aDriver;
219   }
220   else {
221     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
222   }
223
224   return ret;
225 }
226  
227 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
228 {
229   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
230   bool ret;
231   if(_isLocal) {
232     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
233     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
234     ret = _local_impl->Copy(aSO_impl, aDriver);
235     delete aDriver;
236   }
237   else {
238     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
239   }
240   return ret;
241 }
242  
243 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
244 {
245   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
246   bool ret;
247
248   if(_isLocal) {
249     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
250     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
251     ret = _local_impl->CanPaste(aSO_impl, aDriver);
252     delete aDriver;
253   }
254   else {
255     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
256   }
257
258   return ret;
259 }
260  
261 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
262 {
263   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
264   SALOMEDSClient_SObject* aResult = NULL;
265
266   if(_isLocal) {
267     Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
268     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
269     Handle(SALOMEDSImpl_SObject) aNewSO = _local_impl->Paste(aSO_impl, aDriver);
270     delete aDriver;
271     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
272     aResult = new SALOMEDS_SObject(aNewSO);
273   }
274   else {
275     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
276     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
277     aResult = new SALOMEDS_SObject(aNewSO);
278   }
279
280   return _PTR(SObject)(aResult);
281 }
282
283
284 void SALOMEDS_StudyManager::init_orb()
285 {
286   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
287   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()); 
288   _orb = init(0 , 0 ) ;     
289 }
290
291 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
292 {
293   SALOMEDS_Driver_i* driver = NULL;
294   
295   Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
296   if(!aSCO.IsNull()) {
297     TCollection_AsciiString IOREngine = aSCO->GetIOR();
298     if(!IOREngine.IsEmpty()) {
299       CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
300       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
301       driver = new SALOMEDS_Driver_i(Engine, orb);
302     }
303   }  
304
305   return driver;
306 }