1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/
20 // File : SALOMEDSClient_StudyManager.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDS_StudyManager.hxx"
27 #include "SALOMEDSImpl_Study.hxx"
28 #include "SALOMEDS_Study.hxx"
29 #include "SALOMEDS_SObject.hxx"
30 #include "SALOMEDS_Driver_i.hxx"
32 #include "Utils_ORB_INIT.hxx"
33 #include "Utils_SINGLETON.hxx"
38 #include <sys/types.h>
43 #include <TCollection_AsciiString.hxx>
44 #include <TColStd_HSequenceOfTransient.hxx>
50 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
52 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
56 long pid = (long)_getpid();
58 long pid = (long)getpid();
61 long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
63 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
64 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
68 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
74 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
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) );
84 long pid = (long)_getpid();
86 long pid = (long)getpid();
89 long addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
91 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
92 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
96 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
100 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
104 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
106 //SRN: Pure CORBA NewStudy as it does more initialization than the local one
107 SALOMEDSClient_Study* aStudy = NULL;
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);
113 return _PTR(Study)(aStudy);
116 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
118 //SRN: Pure CORBA Open as it does more initialization than the local one
119 SALOMEDSClient_Study* aStudy = NULL;
121 SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
122 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
124 aStudy = new SALOMEDS_Study(aStudy_impl.in());
126 return _PTR(Study)(aStudy);
129 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
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);
136 void SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
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);
143 void SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
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);
150 void SALOMEDS_StudyManager::SaveAs(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
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);
157 void SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
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);
164 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
166 std::vector<std::string> aVector;
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());
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());
184 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName)
186 SALOMEDSClient_Study* aStudy = NULL;
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);
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);
197 return _PTR(Study)(aStudy);
200 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID)
202 SALOMEDSClient_Study* aStudy = NULL;
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);
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);
213 return _PTR(Study)(aStudy);
216 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
218 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
228 ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
234 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
236 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
245 ret = _corba_impl->Copy(aSO->GetCORBAImpl());
250 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
252 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
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);
262 ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
268 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
270 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
271 SALOMEDSClient_SObject* aResult = NULL;
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);
278 if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
279 aResult = new SALOMEDS_SObject(aNewSO);
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);
287 return _PTR(SObject)(aResult);
291 void SALOMEDS_StudyManager::init_orb()
293 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
294 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
295 _orb = init(0 , 0 ) ;
298 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
300 SALOMEDS_Driver_i* driver = NULL;
302 Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
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);