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/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSClient_StudyManager.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDS_StudyManager.hxx"
27 #include "SALOMEDS.hxx"
28 #include "SALOMEDS_Study.hxx"
29 #include "SALOMEDS_SObject.hxx"
31 #include "SALOMEDS_Driver_i.hxx"
33 #include "SALOMEDSImpl_Study.hxx"
35 #include "Utils_ORB_INIT.hxx"
36 #include "Utils_SINGLETON.hxx"
41 #include <sys/types.h>
46 #include <TCollection_AsciiString.hxx>
47 #include <TColStd_HSequenceOfTransient.hxx>
53 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb);
55 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
59 long pid = (long)_getpid();
61 long pid = (long)getpid();
64 CORBA::LongLong addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
66 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
67 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
71 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
77 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
81 SALOME_NamingService namingService(_orb);
82 CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
83 SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
84 ASSERT( !CORBA::is_nil(theManager) );
87 long pid = (long)_getpid();
89 long pid = (long)getpid();
92 CORBA::LongLong addr = theManager->GetLocalImpl(GetHostname().c_str(), pid, _isLocal);
94 _local_impl = ((SALOMEDSImpl_StudyManager*)(addr));
95 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
99 _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
103 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
107 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
109 //SRN: Pure CORBA NewStudy as it does more initialization than the local one
110 SALOMEDSClient_Study* aStudy = NULL;
112 SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
113 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
114 aStudy = new SALOMEDS_Study(aStudy_impl);
116 return _PTR(Study)(aStudy);
119 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
121 //SRN: Pure CORBA Open as it does more initialization than the local one
122 SALOMEDSClient_Study* aStudy = NULL;
124 SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
125 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
127 aStudy = new SALOMEDS_Study(aStudy_impl.in());
129 return _PTR(Study)(aStudy);
132 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
134 //SRN: Pure CORBA close as it does more cleaning than the local one
135 SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
136 _corba_impl->Close(aStudy);
139 bool SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
141 //SRN: Pure CORBA save as the save operation require CORBA in any case
142 SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
143 return _corba_impl->Save(aStudy, theMultiFile);
146 bool SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
148 //SRN: Pure CORBA save as the save operation require CORBA in any case
149 SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
150 return _corba_impl->SaveASCII(aStudy, theMultiFile);
153 bool SALOMEDS_StudyManager::SaveAs(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
155 //SRN: Pure CORBA save as the save operation require CORBA in any case
156 SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
157 return _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
160 bool SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl, const _PTR(Study)& theStudy, bool theMultiFile)
162 //SRN: Pure CORBA save as the save operation require CORBA in any case
163 SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
164 return _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
167 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
169 std::vector<std::string> aVector;
173 SALOMEDS::Locker lock;
175 Handle(TColStd_HSequenceOfTransient) aSeq = _local_impl->GetOpenStudies();
176 aLength = aSeq->Length();
177 for(i = 1; i <= aLength; i++)
178 aVector.push_back(Handle(SALOMEDSImpl_Study)::DownCast(aSeq->Value(i))->Name().ToCString());
181 SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
182 aLength = aSeq->length();
183 for(i = 0; i < aLength; i++)
184 aVector.push_back(aSeq[i].in());
189 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName)
191 SALOMEDSClient_Study* aStudy = NULL;
193 SALOMEDS::Locker lock;
195 Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByName((char*)theStudyName.c_str());
196 if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
197 aStudy = new SALOMEDS_Study(aStudy_impl);
200 SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
201 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
202 aStudy = new SALOMEDS_Study(aStudy_impl);
204 return _PTR(Study)(aStudy);
207 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID)
209 SALOMEDSClient_Study* aStudy = NULL;
211 SALOMEDS::Locker lock;
213 Handle(SALOMEDSImpl_Study) aStudy_impl = _local_impl->GetStudyByID(theStudyID);
214 if(aStudy_impl.IsNull()) return _PTR(Study)(aStudy);
215 aStudy = new SALOMEDS_Study(aStudy_impl);
218 SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
219 if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
220 aStudy = new SALOMEDS_Study(aStudy_impl);
222 return _PTR(Study)(aStudy);
225 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
227 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
231 SALOMEDS::Locker lock;
233 Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
234 SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
235 ret = _local_impl->CanCopy(aSO_impl, aDriver);
239 ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
245 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
247 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
250 SALOMEDS::Locker lock;
252 Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
253 SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
254 ret = _local_impl->Copy(aSO_impl, aDriver);
258 ret = _corba_impl->Copy(aSO->GetCORBAImpl());
263 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
265 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
269 SALOMEDS::Locker lock;
271 Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
272 SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
273 ret = _local_impl->CanPaste(aSO_impl, aDriver);
277 ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
283 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
285 SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
286 SALOMEDSClient_SObject* aResult = NULL;
289 SALOMEDS::Locker lock;
291 Handle(SALOMEDSImpl_SObject) aSO_impl = aSO->GetLocalImpl();
292 SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
293 Handle(SALOMEDSImpl_SObject) aNewSO = _local_impl->Paste(aSO_impl, aDriver);
295 if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
296 aResult = new SALOMEDS_SObject(aNewSO);
299 SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
300 if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
301 aResult = new SALOMEDS_SObject(aNewSO);
304 return _PTR(SObject)(aResult);
308 void SALOMEDS_StudyManager::init_orb()
310 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
311 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
315 SALOMEDS_Driver_i* GetDriver(const Handle(SALOMEDSImpl_SObject)& theObject, CORBA::ORB_ptr orb)
317 SALOMEDS_Driver_i* driver = NULL;
319 Handle(SALOMEDSImpl_SComponent) aSCO = theObject->GetFatherComponent();
321 TCollection_AsciiString IOREngine = aSCO->GetIOR();
322 if(!IOREngine.IsEmpty()) {
323 CORBA::Object_var obj = orb->string_to_object(IOREngine.ToCString());
324 SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
325 driver = new SALOMEDS_Driver_i(Engine, orb);