Back to index

salome-paravis  6.5.0
PARAVIS_Gen_i.cc
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 //  File   : PARAVIS_Gen_i.cc
00020 //  Author : Vitaly Smetannikov
00021 //  Module : PARAVIS
00022 //
00023 
00024 
00025 #include "PARAVIS_Gen_i.hh"
00026 
00027 // IDL Headers
00028 #include <omnithread.h>
00029 #include CORBA_SERVER_HEADER(SALOME_Session)
00030 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
00031 
00032 #include <CAM_Module.h>
00033 #include "PVGUI_Module.h"
00034 #include <SalomeApp_Application.h>
00035 #include <SUIT_ResourceMgr.h>
00036 
00037 #include <SALOMEDS_Tool.hxx>
00038 #include <QFileInfo>
00039 
00040 #include "PV_Events.h"
00041 #include "PV_Tools.h"
00042 
00043 #include "QDomDocument"
00044 #include "QDomNode"
00045 #include "QDomElement"
00046 #include "QFile"
00047 #include "QDir"
00048 #include "QTextStream"
00049 
00050 #include "vtkWrapIDL.h"
00051 
00052 #include <pqServer.h>
00053 #include <pqServerResource.h>
00054 
00055 
00056 #ifdef _DEBUG_
00057 static int MYDEBUG = 1;
00058 #else
00059 static int MYDEBUG = 0;
00060 #endif
00061 
00062 using namespace std;
00063 
00064 extern PARAVIS::PARAVIS_Base_i* CreateInstance(::vtkObjectBase* Inst, const QString&);
00065 
00066 extern "C" 
00067 PARAVIS_I_EXPORT PARAVIS::PARAVIS_Gen_ptr GetImpl(CORBA::ORB_ptr theORB, 
00068                                                   PortableServer::POA_ptr thePOA,
00069                                                   SALOME_NamingService* theNamingService,
00070                                                   QMutex* theMutex)
00071 {
00072   if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
00073   PARAVIS::PARAVIS_Gen_i *aPARAVIS_Gen = new PARAVIS::PARAVIS_Gen_i(theORB, 
00074                                                                     thePOA, 
00075                                                                     theNamingService, 
00076                                                                     theMutex);
00077   return aPARAVIS_Gen->_this();
00078 }
00079 
00080 
00081 
00082 namespace PARAVIS
00083 {
00084 
00085   const char* checkNullStr(const char* theStr)
00086   {
00087     if (strcmp(theStr, "NULL") == 0)
00088       return NULL;
00089     else
00090       return theStr;
00091   }
00092 
00093   std::string tabify( const std::string& source, bool isTabify )
00094   {
00095     std::string result = source;
00096     if ( isTabify && !result.empty() ) {
00097       std::string caret = "\n";
00098       int idx = result.rfind( caret );
00099       while ( idx != std::string::npos ) {
00100        result.replace( idx, caret.size(), "\n\t" );
00101        idx = result.rfind( caret, idx-1 );
00102       }
00103       result.insert(0, "\t" );
00104     }
00105     return result;
00106   }
00107 
00108   PARAVIS_Base_i::~PARAVIS_Base_i() {
00109     if(mySmartPointer != NULL) mySmartPointer->Delete();
00110   }
00111 
00112   void PARAVIS_Base_i::Init(::vtkObjectBase* base) {
00113     if (mySmartPointer != NULL) mySmartPointer->Delete();
00114     mySmartPointer = base;
00115   }
00116 
00117   ::vtkObjectBase* PARAVIS_Base_i::getVTKObject(PARAVIS_Base_ptr theBase) {
00118     PARAVIS_Base_i* aBase = GET_SERVANT(theBase);
00119     return (aBase != NULL)? aBase->getVTKObject() : NULL;
00120   }
00121 
00122   CORBA::Boolean PARAVIS_Base_i::IsSame(PARAVIS_Base_ptr theOther)
00123   {
00124     PARAVIS_Base_i* aBase = GET_SERVANT(theOther);
00125     if (aBase == NULL) 
00126       return false;
00127     return mySmartPointer.GetPointer() == aBase->getVTKObject();
00128   }
00129 
00130 
00131 
00132 
00133   PARAVIS_Gen_i*          PARAVIS_Gen_i::myParavisGenImpl;
00134   CORBA::ORB_var          PARAVIS_Gen_i::myOrb;
00135   PortableServer::POA_var PARAVIS_Gen_i::myPoa;
00136   SALOME_LifeCycleCORBA*  PARAVIS_Gen_i::myEnginesLifeCycle;
00137   SALOME_NamingService*   PARAVIS_Gen_i::myNamingService;
00138   QMutex*                 PARAVIS_Gen_i::myMutex;
00139   SalomeApp_Application*  PARAVIS_Gen_i::mySalomeApp = 0;
00140 
00141 
00142   //----------------------------------------------------------------------------
00143   PARAVIS_Gen_i::PARAVIS_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
00144                                SALOME_NamingService* theNamingService, QMutex* theMutex) :
00145     Engines_Component_i()
00146   {
00147     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i");
00148     if (mySalomeApp) return;
00149 
00150     myMutex = theMutex;
00151     myOrb = CORBA::ORB::_duplicate(theORB);
00152     myPoa = PortableServer::POA::_duplicate(thePOA);
00153     myParavisGenImpl = this;
00154     myNamingService = theNamingService;
00155 
00156     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
00157     myEnginesLifeCycle = &aEnginesLifeCycle;
00158 
00159     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
00160     if (!CORBA::is_nil(anObj)) {
00161       SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
00162       SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
00163       if(aListOfOpenStudies->length() > 0) {
00164         CORBA::String_var aStudyName = aListOfOpenStudies[0];
00165         myStudyDocument = aStudyManager->GetStudyByName(aStudyName);
00166         if (!myStudyDocument->_is_nil()) {
00167           mySalomeApp = ProcessEvent(new TGetGUIApplication(myStudyDocument->StudyId()));
00168           if(!myStudyDocument->GetProperties()->IsLocked())
00169             FindOrCreateParaVisComponent(myStudyDocument);
00170         }
00171       } else
00172         if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : there is no opened study in StudyManager !!!");
00173     } else
00174       if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : Can't find StudyManager !!!");
00175 
00176   }
00177 
00178   //----------------------------------------------------------------------------
00179   PARAVIS_Gen_i::~PARAVIS_Gen_i()
00180   {
00181     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::~PARAVIS_Gen_i");
00182   }
00183 
00184   //----------------------------------------------------------------------------
00185   char* PARAVIS_Gen_i::GetIOR()
00186   {
00187     if(myIOR == ""){
00188       CORBA::Object_var anObject = _this();
00189       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
00190       myIOR = anIOR.in();
00191     }
00192     return CORBA::string_dup(myIOR.c_str());
00193   }
00194 
00195   //----------------------------------------------------------------------------
00196   void PARAVIS_Gen_i::ImportFile(const char* theFileName)
00197   {
00198     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ImportFile: " <<theFileName);
00199     ProcessVoidEvent(new TImportFile(mySalomeApp, theFileName));
00200   }
00201 
00202   void PARAVIS_Gen_i::ExecuteScript(const char* script)
00203   {
00204     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ExecuteScript: " <<script);
00205     ProcessVoidEvent(new TExecuteScript(mySalomeApp, script));
00206   }
00207 
00208   //----------------------------------------------------------------------------
00209   char* PARAVIS_Gen_i::GetTrace()
00210   {
00211     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PrintTrace: ");
00212     return CORBA::string_dup(ProcessEvent(new TGetTrace(mySalomeApp)).c_str());
00213   }
00214 
00215   //----------------------------------------------------------------------------
00216   void PARAVIS_Gen_i::SaveTrace(const char* theFileName)
00217   {
00218     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::SaveTrace: " <<theFileName);
00219     ProcessVoidEvent(new TSaveTrace(mySalomeApp, theFileName));
00220   }
00221 
00222   //----------------------------------------------------------------------------
00223   void processElements(QDomNode& thePropertyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
00224   {
00225     QDomNode aElementNode = thePropertyNode.firstChild();
00226     while (aElementNode.isElement()) {
00227       QDomElement aElement = aElementNode.toElement();
00228       if (aElement.tagName() == "Element") {
00229         QString aIndex = aElement.attribute("index");
00230         if (aIndex == "0") {
00231           QString aValue = aElement.attribute("value");
00232           if (!aValue.isNull()) {
00233             if (!theNewPath) {
00234               QFileInfo aFInfo(aValue);
00235               if (aFInfo.exists()) {
00236                 theFileNames<<aValue;
00237                 aElement.setAttribute("value", aFInfo.fileName());
00238               }
00239               break;
00240             } else {
00241               if (theRestore)
00242                 aElement.setAttribute("value", QString(theNewPath) + aValue);
00243             }
00244           }
00245         }
00246       }
00247       aElementNode = aElementNode.nextSibling();
00248     }
00249   }
00250 
00251 
00252   //----------------------------------------------------------------------------
00253   void processProperties(QDomNode& theProxyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
00254   {
00255     QDomNode aPropertyNode = theProxyNode.firstChild();
00256     while (aPropertyNode.isElement()) {
00257       QDomElement aProperty = aPropertyNode.toElement();
00258       QString aName = aProperty.attribute("name");
00259       if ((aName == "FileName") || (aName == "FileNameInfo") || (aName == "FileNames")) {
00260         processElements(aPropertyNode, theFileNames, theNewPath, theRestore);
00261       }
00262       aPropertyNode = aPropertyNode.nextSibling();
00263     }
00264   }  
00265 
00266 
00267   //----------------------------------------------------------------------------
00268   void processProxies(QDomNode& theNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
00269   {
00270     QDomNode aProxyNode = theNode.firstChild();
00271     while (aProxyNode.isElement()) {
00272       QDomElement aProxy = aProxyNode.toElement();
00273       if (aProxy.tagName() == "Proxy") {
00274         QString aGroup = aProxy.attribute("group");
00275         if (aGroup == "sources") {
00276           processProperties(aProxyNode, theFileNames, theNewPath, theRestore);
00277         }
00278       }
00279       aProxyNode = aProxyNode.nextSibling();
00280     }
00281   }
00282 
00283   //----------------------------------------------------------------------------
00284   bool processAllFilesInState(const char* aFileName, QStringList& theFileNames,
00285                               const char* theNewPath, bool theRestore = false)
00286   {
00287     QFile aFile(aFileName);
00288     if (!aFile.open(QFile::ReadOnly)) {
00289       MESSAGE("Can't open state file "<<aFileName);
00290       return false;
00291     }
00292     QDomDocument aDoc;
00293     bool aRes = aDoc.setContent(&aFile);
00294     aFile.close();
00295     
00296     if (!aRes) {
00297       MESSAGE("File "<<aFileName<<" is not XML document");
00298       return false;
00299     }
00300     
00301     QDomElement aRoot = aDoc.documentElement();
00302     if ( aRoot.isNull() ) {
00303       MESSAGE( "Invalid XML root" );
00304       return false;
00305     }
00306 
00307     QDomNode aNode = aRoot.firstChild();
00308     while (aRes  && !aNode.isNull() ) {
00309       aRes = aNode.isElement();
00310       if ( aRes ) {
00311         QDomElement aSection = aNode.toElement();
00312         if (aSection.tagName() == "ServerManagerState") {
00313           processProxies(aNode, theFileNames, theNewPath, theRestore);
00314         }
00315       }
00316       aNode = aNode.nextSibling();
00317     }
00318     if (!aFile.open(QFile::WriteOnly | QFile::Truncate)) {
00319       MESSAGE("Can't open state file "<<aFileName<<" for writing");
00320       return false;
00321     }
00322     QTextStream out(&aFile);
00323     aDoc.save(out, 2);
00324     aFile.close();
00325     
00326     return true;
00327   }
00328 
00329   //----------------------------------------------------------------------------
00330   QStringList getAllSavedStates(SALOMEDS::SComponent_ptr theComponent, 
00331                             QString theNewPath = QString())
00332   {
00333     QStringList aStateFiles;
00334 
00335     if (!CORBA::is_nil(theComponent)) {
00336       SALOMEDS::Study_var aStudy = theComponent->GetStudy();
00337       SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theComponent);
00338       for (; anIter->More(); anIter->Next()) {
00339        SALOMEDS::SObject_var aSObj = anIter->Value();
00340        SALOMEDS::GenericAttribute_var anAttr;
00341        if (!aSObj->FindAttribute(anAttr, "AttributeLocalID")) {
00342          continue;
00343        }
00344        SALOMEDS::AttributeLocalID_var anID = SALOMEDS::AttributeLocalID::_narrow(anAttr);
00345        if (!anID->Value() == PVSTATEID) {
00346          continue;
00347        }
00348        if (aSObj->FindAttribute(anAttr, "AttributeString")) {
00349          SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
00350          QString aStateFile(aStringAttr->Value());
00351          printf("getAllSavedStates, aStateFile = %s\n", aStateFile.toLatin1().constData());
00352          // Replace the old path with the new one
00353          if (!theNewPath.isEmpty()) {
00354            QFileInfo aFileInfo(aStateFile);
00355            QString aPath = aFileInfo.path();
00356            aStateFile.replace(aPath, theNewPath);
00357            aStringAttr->SetValue(aStateFile.toLatin1().constData());
00358            
00359            printf("getAllSavedStates, aStateFile NEW = %s\n", aStateFile.toLatin1().constData());
00360          }
00361          aStateFiles<<aStateFile;
00362        }
00363       }
00364     }
00365 
00366     return aStateFiles;
00367   }
00368 
00369   SALOMEDS::TMPFile* SaveState(long thePID, SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent, 
00370                                const char* theURL, bool isMultiFile)
00371   {
00372     std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
00373 
00374     std::ostringstream aStream;
00375     aStream<<"paravisstate:"<<thePID;
00376     std::string aFileName = "_" + aStream.str();
00377     if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
00378 
00379     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
00380 
00381     std::string aFile = aTmpDir + aFileName;
00382     ProcessVoidEvent(new TSaveStateFile(theApp, aFile.c_str()));
00383 
00384     // Get saved states
00385     QStringList aSavedStates = getAllSavedStates(theComponent);
00386 
00387     // Collect all files from state
00388     SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
00389     int aSavingType = aResourceMgr->integerValue( "PARAVIS", "savestate_type", 0 );
00390 
00391     QStringList aFileNames;
00392     QStringList aNames;
00393    
00394     switch (aSavingType) {
00395     case 0: // Save referenced files only for builin server
00396       {
00397         MESSAGE("Save as built in;")
00398         bool aIsBuiltIn = true;
00399         pqServer* aServer = ProcessEvent(new TGetActiveServer(theApp));
00400         if (aServer) 
00401           aIsBuiltIn != aServer->isRemote();
00402       
00403         if (aIsBuiltIn)
00404         {
00405          // Find referenced files and collect their paths nullyfying references:
00406          
00407          // for saved states
00408          foreach (QString aState, aSavedStates) {
00409            processAllFilesInState(aState.toLatin1().constData(), aFileNames, 0);
00410          }
00411 
00412          // for main state
00413           processAllFilesInState(aFile.c_str(), aFileNames, 0); 
00414 
00415           SetRestoreParam(theComponent, true);
00416         } else {
00417           SetRestoreParam(theComponent, false);
00418        }
00419       }
00420       break;
00421     case 1: //Save referenced files when they are accessible
00422       {
00423         // Find referenced files and collect their paths nullyfying references
00424 
00425        // for saved states
00426        foreach (QString aState, aSavedStates) {
00427          processAllFilesInState(aState.toLatin1().constData(), aFileNames, 0);
00428        }
00429        
00430        // for main state
00431         processAllFilesInState(aFile.c_str(), aFileNames, 0);
00432         SetRestoreParam(theComponent, true);
00433       }
00434       break;
00435     case 2: //Never save referenced files
00436       SetRestoreParam(theComponent, false);
00437       break;
00438     }
00439 
00440     // Add saved states
00441     foreach (QString aSavedState, aSavedStates) {
00442       aFileNames<<aSavedState;
00443     }
00444 
00445     // Add main state to the end of the list
00446     aFileNames<<QString(aFile.c_str());
00447 
00448     // File names for main state and its data files
00449     foreach(QString aFile, aFileNames) {
00450       QFileInfo aInfo(aFile);
00451       aNames<<aInfo.fileName();
00452     }
00453     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
00454     SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
00455 
00456     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
00457     
00458     return aStreamFile._retn();
00459   }
00460 
00461   //----------------------------------------------------------------------------
00462   SALOMEDS::TMPFile* PARAVIS_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent, 
00463                                          const char* theURL, bool isMultiFile)
00464   {
00465     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
00466     return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
00467   }
00468     
00469   //----------------------------------------------------------------------------
00470   SALOMEDS::TMPFile* PARAVIS_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
00471                                               const char* theURL, bool isMultiFile)
00472   {
00473     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
00474     return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
00475   }
00476     
00477 
00478   //----------------------------------------------------------------------------
00479   bool LoadState(SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent,
00480                  const SALOMEDS::TMPFile& theStream, const char* theURL, bool isMultiFile)
00481   {
00482     std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
00483     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
00484 
00485     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
00486     if (aSeq->length() == 0)
00487       return false;
00488 
00489     bool aRestore = GetRestoreParam(theComponent);
00490     MESSAGE("PARAVIS_Gen_i::Restore path - "<<aRestore);
00491 
00492     // Process main state
00493     std::string aFile = aTmpDir + std::string(aSeq[aSeq->length() - 1]);
00494     QStringList aEmptyList;
00495     processAllFilesInState(aFile.c_str(), aEmptyList, aTmpDir.c_str(), aRestore);
00496     ProcessVoidEvent(new TLoadStateFile(theApp, aFile.c_str()));
00497 
00498     // Process saved states
00499     QStringList aSavedStates = getAllSavedStates(theComponent, QString(aTmpDir.c_str()));
00500     foreach(QString aState, aSavedStates) {
00501       processAllFilesInState(aState.toLatin1().constData(), 
00502                           aEmptyList, aTmpDir.c_str(), aRestore);
00503     }
00504     
00505     return true;
00506   }
00507 
00508   //----------------------------------------------------------------------------
00509   bool PARAVIS_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream,
00510                            const char* theURL, bool isMultiFile)
00511   {
00512     return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
00513   }
00514 
00515   //----------------------------------------------------------------------------
00516   bool PARAVIS_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent, 
00517                                 const SALOMEDS::TMPFile& theStream,
00518                                 const char* theURL, bool isMultiFile)
00519   {
00520     return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
00521   }
00522     
00523   //----------------------------------------------------------------------------
00524   void PARAVIS_Gen_i::Close(SALOMEDS::SComponent_ptr IORSComponent)
00525   {
00526   }
00527   
00528   //----------------------------------------------------------------------------
00529   char* PARAVIS_Gen_i::ComponentDataType()
00530   {
00531     return CORBA::string_dup("PARAVIS");
00532   }
00533 
00534   //----------------------------------------------------------------------------
00535   char* PARAVIS_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, const char* IORString,
00536                                               CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
00537   {
00538     CORBA::String_var aString("");
00539     return aString._retn();
00540   }
00541 
00542   //----------------------------------------------------------------------------
00543   char* PARAVIS_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject, 
00544                                               const char* aLocalPersistentID,
00545                                               CORBA::Boolean isMultiFile, 
00546                                               CORBA::Boolean isASCII)
00547   {
00548     CORBA::String_var aString("");
00549     return aString._retn();
00550   }
00551 
00552   //----------------------------------------------------------------------------
00553   bool PARAVIS_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
00554   {
00555     return false;
00556   }
00557 
00558   //----------------------------------------------------------------------------
00559   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
00560                                                       SALOMEDS::SObject_ptr theSObject, 
00561                                                       CORBA::Object_ptr theObject,
00562                                                       const char* theName) 
00563     throw (SALOME::SALOME_Exception)
00564   {
00565     SALOMEDS::SObject_var aResultSO;
00566     return aResultSO._retn();
00567   }
00568 
00569   //----------------------------------------------------------------------------
00570   CORBA::Boolean PARAVIS_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
00571   {
00572     return false;
00573   }
00574 
00575   //----------------------------------------------------------------------------
00576   SALOMEDS::TMPFile* PARAVIS_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
00577   {
00578     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
00579     return aStreamFile._retn();
00580   }
00581 
00582   //----------------------------------------------------------------------------
00583   CORBA::Boolean PARAVIS_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID)
00584   {
00585     return false;
00586   }
00587 
00588   //----------------------------------------------------------------------------
00589   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
00590                                                  CORBA::Long theObjectID, SALOMEDS::SObject_ptr theObject)
00591   {
00592     return SALOMEDS::SObject::_nil();
00593   }
00594 
00595   //----------------------------------------------------------------------------
00596   PARAVIS::string_array* PARAVIS_Gen_i::GetClassesList()
00597   {
00598     uint k;
00599     for (k = 0; strcmp(wrapped_classes[k], "") != 0; k++);
00600     PARAVIS::string_array_var aNames = new PARAVIS::string_array();
00601     aNames->length(k);
00602     for (CORBA::ULong i = 0; i < k; i++)
00603       aNames[i] = CORBA::string_dup(wrapped_classes[i]);
00604     return aNames._retn();
00605   }
00606 
00607   //----------------------------------------------------------------------------
00608   PARAVIS_Base_ptr PARAVIS_Gen_i::CreateClass(const char* theClassName)
00609   {
00610     PARAVIS::PARAVIS_Base_i* aClass = CreateInstance(NULL, QString(theClassName));
00611     return aClass->_this();
00612   }
00613 
00614   //----------------------------------------------------------------------------
00615   void PARAVIS_Gen_i::GetConnectionParameters(CORBA::Long& theId, 
00616                                               CORBA::String_out theDHost, CORBA::Long& theDPort,
00617                                               CORBA::String_out theRHost, CORBA::Long& theRPort,
00618                                               CORBA::Long& theReversed)
00619   {
00620     pqServer* aServer = ProcessEvent(new TGetActiveServer(mySalomeApp));
00621     if (aServer) {
00622       theId = static_cast<int>(aServer->GetConnectionID());
00623 
00624       pqServerResource serverRes = aServer->getResource();
00625       theReversed = (serverRes.scheme() == "csrc" || serverRes.scheme() == "cdsrsrc")? 1 : 0;
00626 
00627       theDPort = serverRes.dataServerPort() < 0? serverRes.port() : serverRes.dataServerPort();
00628       theRPort = serverRes.renderServerPort() < 0 ? 0 : serverRes.renderServerPort();
00629 
00630       QString dsHost = serverRes.dataServerHost().isEmpty()? serverRes.host() : serverRes.dataServerHost();
00631       QString rsHost = serverRes.renderServerHost();
00632 
00633       theDHost = CORBA::string_dup(qPrintable(dsHost));
00634       theRHost = CORBA::string_dup(qPrintable(rsHost));
00635     }
00636   }
00637 
00638 
00639   //----------------------------------------------------------------------------
00640   Engines::TMPFile* PARAVIS_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
00641                                               CORBA::Boolean theIsPublished,
00642                                          CORBA::Boolean theIsMultiFile,
00643                                               CORBA::Boolean& theIsValidScript)
00644   {
00645     theIsValidScript = true;
00646     std::string aResult;
00647     if ( theIsMultiFile )
00648       aResult += "\ndef RebuildData(theStudy):\n";
00649     aResult += tabify(ProcessEvent(new TGetTrace(mySalomeApp)), theIsMultiFile );
00650     if ( theIsMultiFile )
00651       aResult += "\n\tpass\n";
00652     CORBA::ULong aSize = aResult.size() + 1;
00653     char* aBuffer = new char[aSize];
00654     memset(aBuffer, 0, aSize);
00655     strcpy(aBuffer, &aResult[0]);
00656     Engines::TMPFile_var aDump = new Engines::TMPFile(aSize,aSize,(CORBA::Octet*)aBuffer,1);
00657     return aDump._retn();
00658   }
00659 
00660   //----------------------------------------------------------------------------
00661   void PARAVIS_Gen_i::ActivateModule()
00662   {
00663     ProcessVoidEvent(new TActivateModule(mySalomeApp));
00664   }
00665 
00666   //----------------------------------------------------------------------------
00667   struct TSetStudyEvent: public SALOME_Event {
00668     std::string myStudyName;
00669     typedef SalomeApp_Application* TResult;
00670     TResult myResult;
00671     
00672     TSetStudyEvent(const std::string theStudyName):myStudyName(theStudyName), myResult(0) {}
00673     virtual void Execute()
00674     {
00675       bool isActive = false;
00676       SUIT_Session* aSession = SUIT_Session::session();
00677         QList<SUIT_Application*> anApplications = aSession->applications();
00678         QList<SUIT_Application*>::Iterator anIter = anApplications.begin();
00679         SUIT_Application* aFirstApp = *anIter;
00680         while (anIter != anApplications.end()) {
00681           SUIT_Application* anApp = *anIter;
00682           if (SUIT_Study* aSStudy = anApp->activeStudy()) {
00683             if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
00684               if (_PTR(Study) aCStudy = aStudy->studyDS()) {
00685                 if(MYDEBUG) MESSAGE("There is an application with active study : StudyId = "
00686                                     << aCStudy->StudyId() << "; Name = '" << aCStudy->Name() << "'");
00687                 if (myStudyName == aCStudy->Name()) {
00688                   isActive = true;
00689                   break;
00690                 }
00691               }
00692             }
00693           }
00694           anIter++;
00695         }
00696         SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aFirstApp);
00697         if (!isActive) {
00698           MESSAGE("!!! anApp->onLoadDoc(myStudyName) !!!");
00699           // Has to be loaded in an empty or in a new application
00700           anApp->onLoadDoc(myStudyName.c_str());
00701         }
00702         myResult = anApp;
00703     }
00704   };
00705 
00706   void PARAVIS_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy)
00707   {
00708     if (!CORBA::is_nil(theStudy)) {
00709       CORBA::String_var aName = theStudy->Name();
00710       std::string aStudyName (aName.in());
00711 
00712       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
00713       SalomeApp_Application*  anApp = ProcessEvent(new TSetStudyEvent(aStudyName));
00714       if (!mySalomeApp)
00715         mySalomeApp = anApp;
00716     } else {
00717       INFOS("CORBA::is_nil(theStudy)");
00718     }
00719   }
00720   
00721   
00722   //----------------------------------------------------------------------------
00723   SALOMEDS::Study_ptr PARAVIS_Gen_i::GetCurrentStudy()
00724   {
00725     return SALOMEDS::Study::_duplicate(myStudyDocument);
00726   }
00727 
00728 }