]> SALOME platform Git repositories - modules/paravis.git/blob - src/PVGUI/PARAVIS_Gen_i.cc
Salome HOME
Merge from BR_PARAVIS_DEV 29Dec09
[modules/paravis.git] / src / PVGUI / PARAVIS_Gen_i.cc
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  PARAVIS OBJECT : interactive object for PARAVIS entities implementation
23 //  File   : PARAVIS_Gen_i.cc
24 //  Author : Vitaly Smetannikov
25 //  Module : PARAVIS
26 //
27
28
29 #include "PARAVIS_Gen_i.hh"
30
31 // IDL Headers
32 #include <omnithread.h>
33 #include CORBA_SERVER_HEADER(SALOME_Session)
34 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
35
36 #include <CAM_Module.h>
37 #include "PVGUI_Module.h"
38 #include <SalomeApp_Application.h>
39
40 #include <SALOMEDS_Tool.hxx>
41 #include <QFileInfo>
42
43 #include "PV_Events.h"
44 #include "PV_Tools.h"
45
46 #include "QDomDocument"
47 #include "QDomNode"
48 #include "QDomElement"
49 #include "QFile"
50 #include "QTextStream"
51
52 #include "vtkWrapIDL.h"
53
54 #include <pqServer.h>
55 #include <pqServerResource.h>
56
57
58 #ifdef _DEBUG_
59 static int MYDEBUG = 1;
60 #else
61 static int MYDEBUG = 0;
62 #endif
63
64 using namespace std;
65
66 extern PARAVIS::PARAVIS_Base_i* CreateInstance(::vtkObjectBase* Inst, const QString&);
67
68 extern "C" 
69 PARAVIS_I_EXPORT PARAVIS::PARAVIS_Gen_ptr GetImpl(CORBA::ORB_ptr theORB, 
70                                                   PortableServer::POA_ptr thePOA,
71                                                   SALOME_NamingService* theNamingService,
72                                                   QMutex* theMutex)
73 {
74   if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
75   PARAVIS::PARAVIS_Gen_i *aPARAVIS_Gen = new PARAVIS::PARAVIS_Gen_i(theORB, 
76                                                                     thePOA, 
77                                                                     theNamingService, 
78                                                                     theMutex);
79   return aPARAVIS_Gen->_this();
80 }
81
82
83
84 namespace PARAVIS
85 {
86   PARAVIS_Base_i::~PARAVIS_Base_i() {
87     if(mySmartPointer != NULL) mySmartPointer->Delete();
88   }
89
90   void PARAVIS_Base_i::Init(::vtkObjectBase* base) {
91     if (mySmartPointer != NULL) mySmartPointer->Delete();
92     mySmartPointer = base;
93   }
94
95   ::vtkObjectBase* PARAVIS_Base_i::getVTKObject(PARAVIS_Base_ptr theBase) {
96     PARAVIS_Base_i* aBase = GET_SERVANT(theBase);
97     return (aBase != NULL)? aBase->getVTKObject() : NULL;
98   }
99
100   CORBA::Boolean PARAVIS_Base_i::IsSame(PARAVIS_Base_ptr theOther)
101   {
102     PARAVIS_Base_i* aBase = GET_SERVANT(theOther);
103     if (aBase == NULL) 
104       return false;
105     return mySmartPointer.GetPointer() == aBase->getVTKObject();
106   }
107
108
109
110
111   PARAVIS_Gen_i*          PARAVIS_Gen_i::myParavisGenImpl;
112   CORBA::ORB_var          PARAVIS_Gen_i::myOrb;
113   PortableServer::POA_var PARAVIS_Gen_i::myPoa;
114   SALOME_LifeCycleCORBA*  PARAVIS_Gen_i::myEnginesLifeCycle;
115   SALOME_NamingService*   PARAVIS_Gen_i::myNamingService;
116   QMutex*                 PARAVIS_Gen_i::myMutex;
117   SalomeApp_Application*  PARAVIS_Gen_i::mySalomeApp = 0;
118
119
120   //----------------------------------------------------------------------------
121   PARAVIS_Gen_i::PARAVIS_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
122                                SALOME_NamingService* theNamingService, QMutex* theMutex) :
123     Engines_Component_i()
124   {
125     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i");
126     if (mySalomeApp) return;
127
128     myMutex = theMutex;
129     myOrb = CORBA::ORB::_duplicate(theORB);
130     myPoa = PortableServer::POA::_duplicate(thePOA);
131     myParavisGenImpl = this;
132     myNamingService = theNamingService;
133
134     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
135     myEnginesLifeCycle = &aEnginesLifeCycle;
136
137     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
138     if (!CORBA::is_nil(anObj)) {
139       SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
140       SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
141       if(aListOfOpenStudies->length() > 0) {
142         CORBA::String_var aStudyName = aListOfOpenStudies[0];
143         myStudyDocument = aStudyManager->GetStudyByName(aStudyName);
144         if (!myStudyDocument->_is_nil()) {
145           mySalomeApp = ProcessEvent(new TGetGUIApplication(myStudyDocument->StudyId()));
146           if(!myStudyDocument->GetProperties()->IsLocked())
147             FindOrCreateParaVisComponent(myStudyDocument);
148         }
149       } else
150         if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : there is no opened study in StudyManager !!!");
151     } else
152       if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : Can't find StudyManager !!!");
153
154   }
155
156   //----------------------------------------------------------------------------
157   PARAVIS_Gen_i::~PARAVIS_Gen_i()
158   {
159     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::~PARAVIS_Gen_i");
160   }
161
162   //----------------------------------------------------------------------------
163   char* PARAVIS_Gen_i::GetIOR()
164   {
165     if(myIOR == ""){
166       CORBA::Object_var anObject = _this();
167       CORBA::String_var anIOR = myOrb->object_to_string(anObject);
168       myIOR = anIOR.in();
169     }
170     return CORBA::string_dup(myIOR.c_str());
171   }
172
173   //----------------------------------------------------------------------------
174   void PARAVIS_Gen_i::ImportFile(const char* theFileName)
175   {
176     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ImportFile: " <<theFileName);
177     ProcessVoidEvent(new TImportFile(mySalomeApp, theFileName));
178   }
179
180   //----------------------------------------------------------------------------
181   char* PARAVIS_Gen_i::GetTrace()
182   {
183     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PrintTrace: ");
184     return CORBA::string_dup(ProcessEvent(new TGetTrace(mySalomeApp)));
185   }
186
187   //----------------------------------------------------------------------------
188   void PARAVIS_Gen_i::SaveTrace(const char* theFileName)
189   {
190     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::SaveTrace: " <<theFileName);
191     ProcessVoidEvent(new TSaveTrace(mySalomeApp, theFileName));
192   }
193
194   //----------------------------------------------------------------------------
195   void processElements(QDomNode& thePropertyNode, QStringList& theFileNames, const char* theNewPath)
196   {
197     QDomNode aElementNode = thePropertyNode.firstChild();
198     while (aElementNode.isElement()) {
199       QDomElement aElement = aElementNode.toElement();
200       if (aElement.tagName() == "Element") {
201         QString aIndex = aElement.attribute("index");
202         if (aIndex == "0") {
203           QString aValue = aElement.attribute("value");
204           if (!aValue.isNull()) {
205             if (theNewPath == 0) {
206               QFileInfo aFInfo(aValue);
207               theFileNames<<aValue;
208               aElement.setAttribute("value", aFInfo.fileName());
209               break;
210             } else {
211               aElement.setAttribute("value", QString(theNewPath) + aValue);            
212             }
213           }
214         }
215       }
216       aElementNode = aElementNode.nextSibling();
217     }
218   }
219
220
221   //----------------------------------------------------------------------------
222   void processProperties(QDomNode& theProxyNode, QStringList& theFileNames, const char* theNewPath)
223   {
224     QDomNode aPropertyNode = theProxyNode.firstChild();
225     while (aPropertyNode.isElement()) {
226       QDomElement aProperty = aPropertyNode.toElement();
227       QString aName = aProperty.attribute("name");
228       if ((aName == "FileName") || (aName == "FileNameInfo") || (aName == "FileNames")) {
229         processElements(aPropertyNode, theFileNames, theNewPath);
230       }
231       aPropertyNode = aPropertyNode.nextSibling();
232     }
233   }  
234
235
236   //----------------------------------------------------------------------------
237   void processProxies(QDomNode& theNode, QStringList& theFileNames, const char* theNewPath)
238   {
239     QDomNode aProxyNode = theNode.firstChild();
240     while (aProxyNode.isElement()) {
241       QDomElement aProxy = aProxyNode.toElement();
242       if (aProxy.tagName() == "Proxy") {
243         QString aGroup = aProxy.attribute("group");
244         if (aGroup == "sources") {
245           processProperties(aProxyNode, theFileNames, theNewPath);
246         }
247       }
248       aProxyNode = aProxyNode.nextSibling();
249     }
250   }
251
252   //----------------------------------------------------------------------------
253   bool processAllFilesInState(const char* aFileName, QStringList& theFileNames, const char* theNewPath)
254   {
255     QFile aFile(aFileName);
256     if (!aFile.open(QFile::ReadOnly)) {
257       MESSAGE("Can't open state file "<<aFileName);
258       return false;
259     }
260     QDomDocument aDoc;
261     bool aRes = aDoc.setContent(&aFile);
262     aFile.close();
263     
264     if (!aRes) {
265       MESSAGE("File "<<aFileName<<" is not XML document");
266       return false;
267     }
268     
269     QDomElement aRoot = aDoc.documentElement();
270     if ( aRoot.isNull() || aRoot.tagName() != "ParaView" ) {
271       MESSAGE( "Invalid XML root" );
272       return false;
273     }
274
275     QDomNode aNode = aRoot.firstChild();
276     while (aRes  && !aNode.isNull() ) {
277       aRes = aNode.isElement();
278       if ( aRes ) {
279         QDomElement aSection = aNode.toElement();
280         if (aSection.tagName() == "ServerManagerState") {
281           processProxies(aNode, theFileNames, theNewPath);
282         }
283       }
284       aNode = aNode.nextSibling();
285     }
286     if (!aFile.open(QFile::WriteOnly | QFile::Truncate)) {
287       MESSAGE("Can't open state file "<<aFileName<<" for writing");
288       return false;
289     }
290     QTextStream out(&aFile);
291     aDoc.save(out, 2);
292     aFile.close();
293     
294     return true;
295   }
296
297
298   //----------------------------------------------------------------------------
299   SALOMEDS::TMPFile* PARAVIS_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent, 
300                                          const char* theURL, bool isMultiFile)
301   {
302     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
303
304     std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
305
306     std::ostringstream aStream;
307     aStream<<"paravisstate:"<<(long)this;
308     std::string aFileName = "_" + aStream.str();
309     if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
310
311     //std::vector<std::string> aFileNames;
312     //aFileNames.push_back(aFileName);
313
314     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
315
316     std::string aFile = aTmpDir + aFileName;
317     ProcessVoidEvent(new TSaveStateFile(mySalomeApp, aFile.c_str()));
318
319     // TO Do: Collect all files from state
320     QStringList aFileNames;
321     processAllFilesInState(aFile.c_str(), aFileNames, 0);
322     aFileNames<<QString(aFile.c_str());
323
324     QStringList aNames;
325     foreach(QString aFile, aFileNames) {
326       QFileInfo aInfo(aFile);
327       aNames<<aInfo.fileName();
328     }
329     
330     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
331     SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
332
333     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
334     SALOMEDS_Tool::RemoveTemporaryFiles(theURL, aListOfFileNames, true);
335
336     return aStreamFile._retn();
337   }
338     
339   //----------------------------------------------------------------------------
340   SALOMEDS::TMPFile* PARAVIS_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
341                                               const char* theURL, bool isMultiFile)
342   {
343     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
344
345     std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
346
347     std::ostringstream aStream;
348     aStream<<"paravisstate:"<<(long)this;
349     std::string aFileName = "_" + aStream.str();
350     if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
351
352     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
353
354     std::string aFile = aTmpDir + aFileName;
355     ProcessVoidEvent(new TSaveStateFile(mySalomeApp, aFile.c_str()));
356
357     //Collect all files from state
358     QStringList aFileNames;
359     processAllFilesInState(aFile.c_str(), aFileNames, 0);
360     aFileNames<<QString(aFile.c_str());
361
362     QStringList aNames;
363     foreach(QString aFile, aFileNames) {
364       QFileInfo aInfo(aFile);
365       aNames<<aInfo.fileName();
366     }
367     
368     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
369     SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
370
371     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
372     SALOMEDS_Tool::RemoveTemporaryFiles(theURL, aListOfFileNames, true);
373
374     return aStreamFile._retn();
375   }
376     
377
378   //----------------------------------------------------------------------------
379   bool PARAVIS_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,        const SALOMEDS::TMPFile& theStream,
380                            const char* theURL, bool isMultiFile)
381   {
382     std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
383     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
384
385     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
386     if (aSeq->length() == 0)
387       return false;
388
389     std::string aFile = aTmpDir + std::string(aSeq[aSeq->length() - 1]);
390     QStringList aEmptyList;
391     processAllFilesInState(aFile.c_str(), aEmptyList, aTmpDir.c_str());
392     ProcessVoidEvent(new TLoadStateFile(mySalomeApp, aFile.c_str()));
393
394     return true;
395   }
396
397   //----------------------------------------------------------------------------
398   bool PARAVIS_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent, 
399                                 const SALOMEDS::TMPFile& theStream,
400                                 const char* theURL, bool isMultiFile)
401   {
402     std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
403     if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
404
405     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
406     if (aSeq->length() == 0)
407       return false;
408
409     std::string aFile = aTmpDir + std::string(aSeq[aSeq->length() - 1]);
410     QStringList aEmptyList;
411     processAllFilesInState(aFile.c_str(), aEmptyList, aTmpDir.c_str());
412     ProcessVoidEvent(new TLoadStateFile(mySalomeApp, aFile.c_str()));
413
414     return true;
415   }
416     
417   //----------------------------------------------------------------------------
418   void PARAVIS_Gen_i::Close(SALOMEDS::SComponent_ptr IORSComponent)
419   {
420   }
421   
422   //----------------------------------------------------------------------------
423   char* PARAVIS_Gen_i::ComponentDataType()
424   {
425     return CORBA::string_dup("PARAVIS");
426   }
427
428   //----------------------------------------------------------------------------
429   char* PARAVIS_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, const char* IORString,
430                                               CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
431   {
432     CORBA::String_var aString("");
433     return aString._retn();
434   }
435
436   //----------------------------------------------------------------------------
437   char* PARAVIS_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject, 
438                                               const char* aLocalPersistentID,
439                                               CORBA::Boolean isMultiFile, 
440                                               CORBA::Boolean isASCII)
441   {
442     CORBA::String_var aString("");
443     return aString._retn();
444   }
445
446   //----------------------------------------------------------------------------
447   bool PARAVIS_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
448   {
449     return false;
450   }
451
452   //----------------------------------------------------------------------------
453   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
454                                                       SALOMEDS::SObject_ptr theSObject, 
455                                                       CORBA::Object_ptr theObject,
456                                                       const char* theName) 
457     throw (SALOME::SALOME_Exception)
458   {
459     SALOMEDS::SObject_var aResultSO;
460     return aResultSO._retn();
461   }
462
463   //----------------------------------------------------------------------------
464   CORBA::Boolean PARAVIS_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
465   {
466     return false;
467   }
468
469   //----------------------------------------------------------------------------
470   SALOMEDS::TMPFile* PARAVIS_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
471   {
472     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
473     return aStreamFile._retn();
474   }
475
476   //----------------------------------------------------------------------------
477   CORBA::Boolean PARAVIS_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID)
478   {
479     return false;
480   }
481
482   //----------------------------------------------------------------------------
483   SALOMEDS::SObject_ptr PARAVIS_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
484                                                  CORBA::Long theObjectID, SALOMEDS::SObject_ptr theObject)
485   {
486     return SALOMEDS::SObject::_nil();
487   }
488
489   //----------------------------------------------------------------------------
490   PARAVIS::string_array* PARAVIS_Gen_i::GetClassesList()
491   {
492     int k;
493     for (k = 0; strcmp(wrapped_classes[k], "") != 0; k++);
494     PARAVIS::string_array_var aNames = new PARAVIS::string_array();
495     aNames->length(k);
496     for (CORBA::ULong i = 0; i < k; i++)
497       aNames[i] = CORBA::string_dup(wrapped_classes[i]);
498     return aNames._retn();
499   }
500
501   //----------------------------------------------------------------------------
502   PARAVIS_Base_ptr PARAVIS_Gen_i::CreateClass(const char* theClassName)
503   {
504     PARAVIS::PARAVIS_Base_i* aClass = CreateInstance(NULL, QString(theClassName));
505     return aClass->_this();
506   }
507
508   //----------------------------------------------------------------------------
509   void PARAVIS_Gen_i::GetConnectionParameters(CORBA::Long& theId, 
510                                               CORBA::String_out theDHost, CORBA::Long& theDPort,
511                                               CORBA::String_out theRHost, CORBA::Long& theRPort,
512                                               CORBA::Long& theReversed)
513   {
514     pqServer* aServer = ProcessEvent(new TGetActiveServer(mySalomeApp));
515     if (aServer) {
516       theId = static_cast<int>(aServer->GetConnectionID());
517
518       pqServerResource serverRes = aServer->getResource();
519       theReversed = (serverRes.scheme() == "csrc" || serverRes.scheme() == "cdsrsrc")? 1 : 0;
520
521       theDPort = serverRes.dataServerPort() < 0? serverRes.port() : serverRes.dataServerPort();
522       theRPort = serverRes.renderServerPort() < 0 ? 0 : serverRes.renderServerPort();
523
524       QString dsHost = serverRes.dataServerHost().isEmpty()? serverRes.host() : serverRes.dataServerHost();
525       QString rsHost = serverRes.renderServerHost();
526
527       theDHost = CORBA::string_dup(qPrintable(dsHost));
528       theRHost = CORBA::string_dup(qPrintable(rsHost));
529     }
530   }
531
532
533   //----------------------------------------------------------------------------
534   Engines::TMPFile* PARAVIS_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
535                                               CORBA::Boolean theIsPublished,
536                                               CORBA::Boolean& theIsValidScript)
537   {
538     theIsValidScript = true;
539     std::string aResult(ProcessEvent(new TGetTrace(mySalomeApp)));
540     aResult += "\ndef RebuildData(theStudy):\n  pass\n";
541     CORBA::ULong aSize = aResult.size() + 1;
542     char* aBuffer = new char[aSize];
543     strcpy(aBuffer, &aResult[0]);
544     Engines::TMPFile_var aDump = new Engines::TMPFile(aSize,aSize,(CORBA::Octet*)aBuffer,1);
545     return aDump._retn();
546   }
547
548   //----------------------------------------------------------------------------
549   void PARAVIS_Gen_i::ActivateModule()
550   {
551     ProcessVoidEvent(new TActivateModule(mySalomeApp));
552   }
553 }