Back to index

salome-kernel  6.5.0
SALOMEDS_StudyManager.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOMEDSClient_StudyManager.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include "SALOMEDS_StudyManager.hxx"
00028 
00029 #include "SALOMEDS.hxx"
00030 #include "SALOMEDS_Study.hxx"
00031 #include "SALOMEDS_SObject.hxx"
00032 
00033 #include "SALOMEDS_Driver_i.hxx"
00034 
00035 #include "SALOMEDSImpl_Study.hxx"
00036 
00037 #include "Utils_ORB_INIT.hxx" 
00038 #include "Utils_SINGLETON.hxx" 
00039 
00040 #include "Basics_Utils.hxx"
00041 
00042 #ifdef WIN32
00043 #include <process.h>
00044 #else
00045 #include <sys/types.h>
00046 #include <unistd.h>
00047 #endif
00048 
00049 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb);
00050 
00051 SALOMEDS_StudyManager::SALOMEDS_StudyManager(SALOMEDS::StudyManager_ptr theManager)
00052 {
00053 
00054 #ifdef WIN32
00055   long pid =  (long)_getpid();
00056 #else
00057   long pid =  (long)getpid();
00058 #endif  
00059 
00060   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
00061   if(_isLocal) {
00062     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
00063     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
00064   }
00065   else {
00066     _local_impl = NULL;
00067     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
00068   }
00069 
00070   init_orb();
00071 }
00072 
00073 SALOMEDS_StudyManager::SALOMEDS_StudyManager()
00074 {
00075   init_orb();
00076 
00077   SALOME_NamingService namingService(_orb);
00078   CORBA::Object_var obj = namingService.Resolve( "/myStudyManager" );
00079   SALOMEDS::StudyManager_var theManager = SALOMEDS::StudyManager::_narrow( obj );
00080   ASSERT( !CORBA::is_nil(theManager) );
00081 
00082 #ifdef WIN32
00083   long pid =  (long)_getpid();
00084 #else
00085   long pid =  (long)getpid();
00086 #endif  
00087 
00088   CORBA::LongLong addr = theManager->GetLocalImpl(Kernel_Utils::GetHostname().c_str(), pid, _isLocal);
00089   if(_isLocal) {
00090     _local_impl = reinterpret_cast<SALOMEDSImpl_StudyManager*>(addr);
00091     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
00092   }
00093   else {
00094     _local_impl = NULL;
00095     _corba_impl = SALOMEDS::StudyManager::_duplicate(theManager);
00096   }
00097 }
00098 
00099 SALOMEDS_StudyManager::~SALOMEDS_StudyManager()
00100 {
00101 }
00102 
00103 _PTR(Study) SALOMEDS_StudyManager::NewStudy(const std::string& study_name)
00104 {
00105   //SRN: Pure CORBA NewStudy as it does more initialization than the local one   
00106   SALOMEDSClient_Study* aStudy = NULL;
00107 
00108   SALOMEDS::Study_var aStudy_impl = _corba_impl->NewStudy((char*)study_name.c_str());
00109   if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
00110   aStudy = new SALOMEDS_Study(aStudy_impl);
00111 
00112   return _PTR(Study)(aStudy);
00113 }
00114 
00115 _PTR(Study) SALOMEDS_StudyManager::Open(const std::string& theStudyUrl)
00116 {
00117   //SRN: Pure CORBA Open as it does more initialization than the local one   
00118   SALOMEDSClient_Study* aStudy = NULL;
00119 
00120   SALOMEDS::Study_var aStudy_impl = _corba_impl->Open((char*)theStudyUrl.c_str());
00121   if(CORBA::is_nil(aStudy_impl)) return  _PTR(Study)(aStudy);
00122     
00123   aStudy = new SALOMEDS_Study(aStudy_impl.in());
00124 
00125   return _PTR(Study)(aStudy);
00126 }
00127  
00128 void SALOMEDS_StudyManager::Close(const _PTR(Study)& theStudy)
00129 {
00130   //SRN: Pure CORBA close as it does more cleaning than the local one
00131   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
00132   _corba_impl->Close(aStudy);
00133 }
00134  
00135 bool SALOMEDS_StudyManager::Save(const _PTR(Study)& theStudy, bool theMultiFile)
00136 {
00137   //SRN: Pure CORBA save as the save operation require CORBA in any case 
00138   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
00139   return _corba_impl->Save(aStudy, theMultiFile);
00140 }
00141  
00142 bool SALOMEDS_StudyManager::SaveASCII(const _PTR(Study)& theStudy, bool theMultiFile)
00143 {
00144   //SRN: Pure CORBA save as the save operation require CORBA in any case 
00145   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
00146   return _corba_impl->SaveASCII(aStudy, theMultiFile);
00147 }
00148  
00149 bool SALOMEDS_StudyManager::SaveAs(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
00150 {
00151   //SRN: Pure CORBA save as the save operation require CORBA in any case 
00152   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
00153   return _corba_impl->SaveAs((char*)theUrl.c_str(), aStudy, theMultiFile);
00154 }
00155  
00156 bool SALOMEDS_StudyManager::SaveAsASCII(const std::string& theUrl,  const _PTR(Study)& theStudy, bool theMultiFile)
00157 {
00158   //SRN: Pure CORBA save as the save operation require CORBA in any case 
00159   SALOMEDS::Study_var aStudy = _corba_impl->GetStudyByID(theStudy->StudyId());
00160   return _corba_impl->SaveAsASCII((char*)theUrl.c_str(), aStudy, theMultiFile);
00161 }
00162 
00163 std::vector<std::string> SALOMEDS_StudyManager::GetOpenStudies()
00164 {
00165   std::vector<std::string> aVector;
00166   int aLength, i;
00167 
00168   if (_isLocal) {
00169     SALOMEDS::Locker lock;
00170 
00171     std::vector<SALOMEDSImpl_Study*> aSeq = _local_impl->GetOpenStudies();
00172     aLength = aSeq.size();
00173     for(i = 0; i < aLength; i++) 
00174       aVector.push_back(aSeq[i]->Name());
00175   }
00176   else {
00177     SALOMEDS::ListOfOpenStudies_var aSeq = _corba_impl->GetOpenStudies();
00178     aLength = aSeq->length();
00179     for(i = 0; i < aLength; i++) 
00180       aVector.push_back(aSeq[i].in());
00181   }
00182   return aVector;
00183 }
00184 
00185 _PTR(Study) SALOMEDS_StudyManager::GetStudyByName(const std::string& theStudyName) 
00186 {
00187   SALOMEDSClient_Study* aStudy = NULL;
00188   if (_isLocal) {
00189     SALOMEDS::Locker lock;
00190 
00191     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByName(theStudyName);
00192     if(!aStudy_impl) return _PTR(Study)(aStudy);
00193     aStudy = new SALOMEDS_Study(aStudy_impl);
00194   }
00195   else  {
00196     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByName((char*)theStudyName.c_str());
00197     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
00198     aStudy = new SALOMEDS_Study(aStudy_impl);
00199   }
00200   return _PTR(Study)(aStudy);
00201 }
00202 
00203 _PTR(Study) SALOMEDS_StudyManager::GetStudyByID(int theStudyID) 
00204 {
00205   SALOMEDSClient_Study* aStudy = NULL;
00206   if (_isLocal) {
00207     SALOMEDS::Locker lock;
00208 
00209     SALOMEDSImpl_Study* aStudy_impl = _local_impl->GetStudyByID(theStudyID);
00210     if(!aStudy_impl) return _PTR(Study)(aStudy);
00211     aStudy = new SALOMEDS_Study(aStudy_impl);
00212   }
00213   else { 
00214     SALOMEDS::Study_var aStudy_impl = _corba_impl->GetStudyByID(theStudyID);
00215     if(CORBA::is_nil(aStudy_impl)) return _PTR(Study)(aStudy);
00216     aStudy = new SALOMEDS_Study(aStudy_impl);
00217   }
00218   return _PTR(Study)(aStudy);
00219 }
00220 
00221 bool SALOMEDS_StudyManager::CanCopy(const _PTR(SObject)& theSO)
00222 {
00223   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
00224   bool ret;
00225 
00226   if (_isLocal) {
00227     SALOMEDS::Locker lock;
00228 
00229     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
00230     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
00231     ret = _local_impl->CanCopy(aSO_impl, aDriver);
00232     delete aDriver;
00233   }
00234   else {
00235     ret = _corba_impl->CanCopy(aSO->GetCORBAImpl());
00236   }
00237 
00238   return ret;
00239 }
00240 
00241 bool SALOMEDS_StudyManager::Copy(const _PTR(SObject)& theSO)
00242 {
00243   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
00244   bool ret;
00245   if (_isLocal) {
00246     SALOMEDS::Locker lock;
00247 
00248     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
00249     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
00250     ret = _local_impl->Copy(aSO_impl, aDriver);
00251     delete aDriver;
00252   }
00253   else {
00254     ret = _corba_impl->Copy(aSO->GetCORBAImpl());
00255   }
00256   return ret;
00257 }
00258 
00259 bool SALOMEDS_StudyManager::CanPaste(const _PTR(SObject)& theSO)
00260 {
00261   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
00262   bool ret;
00263 
00264   if (_isLocal) {
00265     SALOMEDS::Locker lock;
00266 
00267     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
00268     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
00269     ret = _local_impl->CanPaste(aSO_impl, aDriver);
00270     delete aDriver;
00271   }
00272   else {
00273     ret = _corba_impl->CanPaste(aSO->GetCORBAImpl());
00274   }
00275 
00276   return ret;
00277 }
00278 
00279 _PTR(SObject) SALOMEDS_StudyManager::Paste(const _PTR(SObject)& theSO)
00280 {
00281   SALOMEDS_SObject* aSO = dynamic_cast<SALOMEDS_SObject*>(theSO.get());
00282   SALOMEDSClient_SObject* aResult = NULL;
00283 
00284   if (_isLocal) {
00285     SALOMEDS::Locker lock;
00286 
00287     SALOMEDSImpl_SObject aSO_impl = *(aSO->GetLocalImpl());
00288     SALOMEDS_Driver_i* aDriver = GetDriver(aSO_impl, _orb);
00289     SALOMEDSImpl_SObject aNewSO = _local_impl->Paste(aSO_impl, aDriver);
00290     delete aDriver;
00291     if(aNewSO.IsNull()) return _PTR(SObject)(aResult);
00292     aResult = new SALOMEDS_SObject(aNewSO);
00293   }
00294   else {
00295     SALOMEDS::SObject_ptr aNewSO = _corba_impl->Paste(aSO->GetCORBAImpl());
00296     if(CORBA::is_nil(aNewSO)) return _PTR(SObject)(aResult);
00297     aResult = new SALOMEDS_SObject(aNewSO);
00298   }
00299 
00300   return _PTR(SObject)(aResult);
00301 }
00302 
00303 
00304 void SALOMEDS_StudyManager::init_orb()
00305 {
00306   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
00307   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00308   _orb = init(0 , 0 );
00309 }
00310 
00311 SALOMEDS_Driver_i* GetDriver(const SALOMEDSImpl_SObject& theObject, CORBA::ORB_ptr orb)
00312 {
00313   SALOMEDS_Driver_i* driver = NULL;
00314   
00315   SALOMEDSImpl_SComponent aSCO = theObject.GetFatherComponent();
00316   if(!aSCO.IsNull()) {
00317     std::string IOREngine = aSCO.GetIOR();
00318     if(!IOREngine.empty()) {
00319       CORBA::Object_var obj = orb->string_to_object(IOREngine.c_str());
00320       SALOMEDS::Driver_var Engine = SALOMEDS::Driver::_narrow(obj) ;
00321       driver = new SALOMEDS_Driver_i(Engine, orb);
00322     }
00323   }
00324 
00325   return driver;
00326 }