1 // File : SALOMEDSClient_StudyManager.cxx
2 // Author : Sergey RUIN
6 #include "SALOMEDS_StudyManager.hxx"
8 #include "SALOMEDSImpl_Study.hxx"
9 #include "SALOMEDS_Study.hxx"
10 #include "SALOMEDS_SObject.hxx"
11 #include "SALOMEDS_Driver_i.hxx"
13 #include "Utils_ORB_INIT.hxx"
14 #include "Utils_SINGLETON.hxx"
19 #include <sys/types.h>
24 #include <TCollection_AsciiString.hxx>
25 #include <TColStd_HSequenceOfTransient.hxx>
31 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
33 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
37 long pid = (long)_getpid();
39 long pid = (long)getpid();
42 long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
44 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
45 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
49 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
55 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
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) );
65 long pid = (long)_getpid();
67 long pid = (long)getpid();
70 long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
72 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
73 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
77 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
81 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
85 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
87 //SRN: Pure CORBA NewStudy as it does more initialization than the local one
88 SALOMEDSClient_Study* aStudy = NULL;
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);
94 return _PTR(Study)(aStudy);
97 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
99 //SRN: Pure CORBA Open as it does more initialization than the local one
100 SALOMEDSClient_Study* aStudy = NULL;
102 SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
103 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
105 aStudy = new SALOMEDS_Study(aStudy_impl.in());
107 return _PTR(Study)(aStudy);
110 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
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);
117 void SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
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);
124 void SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
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);
131 void SALOMEDS_StudyManager::SaveAs(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
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);
138 void SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
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);
145 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
147 std::vector<std::string> aVector;
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());
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());
165 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName)
167 SALOMEDSClient_Study* aStudy = NULL;
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);
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);
178 return _PTR(Study)(aStudy);
181 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID)
183 SALOMEDSClient_Study* aStudy = NULL;
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);
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);
194 return _PTR(Study)(aStudy);
197 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
199 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
209 ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
215 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
217 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
226 ret = _corba_impl->Copy(aSO->GetCORBAImpl());
231 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
233 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
243 ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
249 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
251 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
252 SALOMEDSClient_SObject* aResult = NULL;
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);
259 if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
260 aResult = new SALOMEDS_SObject(aNewSO);
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);
268 return _PTR(SObject)(aResult);
272 void SALOMEDS_StudyManager::init_orb()
274 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
275 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
276 _orb = init(0 , 0 ) ;
279 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
281 SALOMEDS_Driver_i* driver = NULL;
283 Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
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);