Salome HOME
Synchronize adm files
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyManager.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SALOMEDSClient_StudyManager.cxx
24 //  Author : Sergey RUIN
25 //  Module : SALOME
26 //
27 #include "SALOMEDS_StudyManager.hxx"
28
29 #include "SALOMEDS.hxx"
30 #include "SALOMEDS_Study.hxx"
31 #include "SALOMEDS_SObject.hxx"
32 #include "SALOMEDS_Driver_i.hxx"
33 #include "SALOMEDS_SimanStudy.hxx"
34
35 #include "SALOMEDSImpl_Study.hxx"
36
37 #include "Utils_ORB_INIT.hxx" 
38 #include "Utils_SINGLETON.hxx" 
39
40 #include "Basics_Utils.hxx"
41
42 #ifdef WIN32
43 #include <process.h>
44 #else
45 #include <sys/types.h>
46 #include <unistd.h>
47 #endif
48
49 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
50
51 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
52 {
53
54 #ifdef WIN32
55   long pid =  (long)_getpid();
56 #else
57   long pid =  (long)getpid();
58 #endif  
59
60   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
61   if(_isLocal) {
62     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
63     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
64   }
65   else {
66     _local_impl = NULL;
67     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
68   }
69
70   init_orb();
71 }
72
73 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
74 {
75   init_orb();
76
77   SALOME_NamingService namingService(_orb);
78   CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
79   SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
80   ASSERT( !CORBA::is_nil(theManager) );
81
82 #ifdef WIN32
83   long pid =  (long)_getpid();
84 #else
85   long pid =  (long)getpid();
86 #endif  
87
88   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
89   if(_isLocal) {
90     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
91     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
92   }
93   else {
94     _local_impl = NULL;
95     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
96   }
97 }
98
99 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
100 {
101 }
102
103 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
104 {
105   //SRN: Pure CORBA NewStudy as it does more initialization than the local one   
106   SALOMEDSClient_Study* aStudy = NULL;
107
108   SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
109   if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
110   aStudy = new SALOMEDS_Study(aStudy_impl);
111
112   return _PTR(Study)(aStudy);
113 }
114
115 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
116 {
117   //SRN: Pure CORBA Open as it does more initialization than the local one   
118   SALOMEDSClient_Study* aStudy = NULL;
119
120   SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
121   if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy);
122     
123   aStudy = new SALOMEDS_Study(aStudy_impl.in());
124
125   return _PTR(Study)(aStudy);
126 }
127  
128 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
129 {
130   //SRN: Pure CORBA close as it does more cleaning than the local one
131   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
132   _corba_impl->Close(aStudy);
133 }
134  
135 bool SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
136 {
137   //SRN: Pure CORBA save as the save operation require CORBA in any case 
138   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
139   return _corba_impl->Save(aStudy, theMultiFile);
140 }
141  
142 bool SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
143 {
144   //SRN: Pure CORBA save as the save operation require CORBA in any case 
145   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
146   return _corba_impl->SaveASCII(aStudy, theMultiFile);
147 }
148  
149 bool SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
150 {
151   //SRN: Pure CORBA save as the save operation require CORBA in any case 
152   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
153   return _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
154 }
155  
156 bool SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
157 {
158   //SRN: Pure CORBA save as the save operation require CORBA in any case 
159   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
160   return _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
161 }
162
163 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
164 {
165   std::vector<std::string> aVector;
166   int aLength, i;
167
168   if (_isLocal) {
169     SALOMEDS::Locker lock;
170
171     std::vector<SALOMEDSImpl_Study*> aSeq = _local_impl->GetOpenStudies();
172     aLength = aSeq.size();
173     for(i = 0; i < aLength; i++) 
174       aVector.push_back(aSeq[i]->Name());
175   }
176   else {
177     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
178     aLength = aSeq->length();
179     for(i = 0; i < aLength; i++) 
180       aVector.push_back(aSeq[i].in());
181   }
182   return aVector;
183 }
184
185 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName) 
186 {
187   SALOMEDSClient_Study* aStudy = NULL;
188   if (_isLocal) {
189     SALOMEDS::Locker lock;
190
191     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByName(theStudyName);
192     if(!aStudy_impl) return _PTR(Study)(aStudy);
193     aStudy = new SALOMEDS_Study(aStudy_impl);
194   }
195   else  {
196     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
197     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
198     aStudy = new SALOMEDS_Study(aStudy_impl);
199   }
200   return _PTR(Study)(aStudy);
201 }
202
203 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID) 
204 {
205   SALOMEDSClient_Study* aStudy = NULL;
206   if (_isLocal) {
207     SALOMEDS::Locker lock;
208
209     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByID(theStudyID);
210     if(!aStudy_impl) return _PTR(Study)(aStudy);
211     aStudy = new SALOMEDS_Study(aStudy_impl);
212   }
213   else { 
214     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
215     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
216     aStudy = new SALOMEDS_Study(aStudy_impl);
217   }
218   return _PTR(Study)(aStudy);
219 }
220
221 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
222 {
223   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
224   bool ret;
225
226   if (_isLocal) {
227     SALOMEDS::Locker lock;
228
229     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
230     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
231     ret = _local_impl->CanCopy(aSO_impl, aDriver);
232     delete aDriver;
233   }
234   else {
235     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
236   }
237
238   return ret;
239 }
240
241 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
242 {
243   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
244   bool ret;
245   if (_isLocal) {
246     SALOMEDS::Locker lock;
247
248     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
249     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
250     ret = _local_impl->Copy(aSO_impl, aDriver);
251     delete aDriver;
252   }
253   else {
254     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
255   }
256   return ret;
257 }
258
259 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
260 {
261   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
262   bool ret;
263
264   if (_isLocal) {
265     SALOMEDS::Locker lock;
266
267     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
268     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
269     ret = _local_impl->CanPaste(aSO_impl, aDriver);
270     delete aDriver;
271   }
272   else {
273     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
274   }
275
276   return ret;
277 }
278
279 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
280 {
281   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
282   SALOMEDSClient_SObject* aResult = NULL;
283
284   if (_isLocal) {
285     SALOMEDS::Locker lock;
286
287     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
288     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
289     SALOMEDSImpl_SObject aNewSO = _local_impl->Paste(aSO_impl, aDriver);
290     delete aDriver;
291     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
292     aResult = new SALOMEDS_SObject(aNewSO);
293   }
294   else {
295     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
296     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
297     aResult = new SALOMEDS_SObject(aNewSO);
298   }
299
300   return _PTR(SObject)(aResult);
301 }
302
303
304 void SALOMEDS_StudyManager::init_orb()
305 {
306   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
307   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
308   _orb = init(0 , 0 );
309 }
310
311 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
312 {
313   SALOMEDS_Driver_i* driver = NULL;
314   
315   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
316   if(!aSCO.IsNull()) {
317     std::string IOREngine = aSCO.GetIOR();
318     if(!IOREngine.empty()) {
319       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
320       Engines::EngineComponent_var Engine = Engines::EngineComponent::_narrow(obj) ;
321       driver = new SALOMEDS_Driver_i(Engine, orb);
322     }
323   }
324
325   return driver;
326 }
327
328 _PTR(SimanStudy) SALOMEDS_StudyManager::GetSimanStudy()
329 {
330   SALOMEDSClient_SimanStudy* aSiman = NULL;
331   /*if (_isLocal) {
332     SALOMEDS::Locker lock;
333
334     SALOMEDSImpl_SimanStudy* aSiman_impl = _local_impl->GetSimanStudy();
335     if(!aSiman_impl) return _PTR(SimanStudy)(aSiman);
336     aSiman = new SALOMEDS_SimanStudy(aSiman_impl);
337   }
338   else { 
339     SALOMEDS::SimanStudy_var aSiman_impl = _corba_impl->GetSimanStudy();
340     if(CORBA::is_nil(aSiman_impl)) return _PTR(SimanStudy)(aSiman);
341     aSiman = new SALOMEDS_SimanStudy(aSiman_impl);
342   }*/
343   SALOMEDS::SimanStudy_var aSiman_impl = _corba_impl->GetSimanStudy();
344   if(CORBA::is_nil(aSiman_impl)) return _PTR(SimanStudy)(aSiman);
345   aSiman = new SALOMEDS_SimanStudy(aSiman_impl);
346   return _PTR(SimanStudy)(aSiman);
347 }