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