1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // PARAVIS OBJECT : interactive object for PARAVIS entities implementation
23 // File : PARAVIS_Gen_i.cc
24 // Author : Vitaly Smetannikov
29 #include "PARAVIS_Gen_i.hh"
32 #include <omnithread.h>
33 #include CORBA_SERVER_HEADER(SALOME_Session)
34 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
36 #include <CAM_Module.h>
37 #include "PVGUI_Module.h"
38 #include <SalomeApp_Application.h>
40 #include <SALOMEDS_Tool.hxx>
43 #include "PV_Events.h"
46 #include "QDomDocument"
48 #include "QDomElement"
50 #include "QTextStream"
52 #include "vtkWrapIDL.h"
55 #include <pqServerResource.h>
59 static int MYDEBUG = 1;
61 static int MYDEBUG = 0;
66 extern PARAVIS::PARAVIS_Base_i* CreateInstance(::vtkObjectBase* Inst, const QString&);
69 PARAVIS_I_EXPORT PARAVIS::PARAVIS_Gen_ptr GetImpl(CORBA::ORB_ptr theORB,
70 PortableServer::POA_ptr thePOA,
71 SALOME_NamingService* theNamingService,
74 if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
75 PARAVIS::PARAVIS_Gen_i *aPARAVIS_Gen = new PARAVIS::PARAVIS_Gen_i(theORB,
79 return aPARAVIS_Gen->_this();
86 PARAVIS_Base_i::~PARAVIS_Base_i() {
87 if(mySmartPointer != NULL) mySmartPointer->Delete();
90 void PARAVIS_Base_i::Init(::vtkObjectBase* base) {
91 if (mySmartPointer != NULL) mySmartPointer->Delete();
92 mySmartPointer = base;
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;
100 CORBA::Boolean PARAVIS_Base_i::IsSame(PARAVIS_Base_ptr theOther)
102 PARAVIS_Base_i* aBase = GET_SERVANT(theOther);
105 return mySmartPointer.GetPointer() == aBase->getVTKObject();
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;
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()
125 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i");
126 if (mySalomeApp) return;
129 myOrb = CORBA::ORB::_duplicate(theORB);
130 myPoa = PortableServer::POA::_duplicate(thePOA);
131 myParavisGenImpl = this;
132 myNamingService = theNamingService;
134 static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
135 myEnginesLifeCycle = &aEnginesLifeCycle;
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);
150 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : there is no opened study in StudyManager !!!");
152 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : Can't find StudyManager !!!");
156 //----------------------------------------------------------------------------
157 PARAVIS_Gen_i::~PARAVIS_Gen_i()
159 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::~PARAVIS_Gen_i");
162 //----------------------------------------------------------------------------
163 char* PARAVIS_Gen_i::GetIOR()
166 CORBA::Object_var anObject = _this();
167 CORBA::String_var anIOR = myOrb->object_to_string(anObject);
170 return CORBA::string_dup(myIOR.c_str());
173 //----------------------------------------------------------------------------
174 void PARAVIS_Gen_i::ImportFile(const char* theFileName)
176 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ImportFile: " <<theFileName);
177 ProcessVoidEvent(new TImportFile(mySalomeApp, theFileName));
180 //----------------------------------------------------------------------------
181 char* PARAVIS_Gen_i::GetTrace()
183 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PrintTrace: ");
184 return CORBA::string_dup(ProcessEvent(new TGetTrace(mySalomeApp)));
187 //----------------------------------------------------------------------------
188 void PARAVIS_Gen_i::SaveTrace(const char* theFileName)
190 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::SaveTrace: " <<theFileName);
191 ProcessVoidEvent(new TSaveTrace(mySalomeApp, theFileName));
194 //----------------------------------------------------------------------------
195 void processElements(QDomNode& thePropertyNode, QStringList& theFileNames, const char* theNewPath)
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");
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());
211 aElement.setAttribute("value", QString(theNewPath) + aValue);
216 aElementNode = aElementNode.nextSibling();
221 //----------------------------------------------------------------------------
222 void processProperties(QDomNode& theProxyNode, QStringList& theFileNames, const char* theNewPath)
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);
231 aPropertyNode = aPropertyNode.nextSibling();
236 //----------------------------------------------------------------------------
237 void processProxies(QDomNode& theNode, QStringList& theFileNames, const char* theNewPath)
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);
248 aProxyNode = aProxyNode.nextSibling();
252 //----------------------------------------------------------------------------
253 bool processAllFilesInState(const char* aFileName, QStringList& theFileNames, const char* theNewPath)
255 QFile aFile(aFileName);
256 if (!aFile.open(QFile::ReadOnly)) {
257 MESSAGE("Can't open state file "<<aFileName);
261 bool aRes = aDoc.setContent(&aFile);
265 MESSAGE("File "<<aFileName<<" is not XML document");
269 QDomElement aRoot = aDoc.documentElement();
270 if ( aRoot.isNull() || aRoot.tagName() != "ParaView" ) {
271 MESSAGE( "Invalid XML root" );
275 QDomNode aNode = aRoot.firstChild();
276 while (aRes && !aNode.isNull() ) {
277 aRes = aNode.isElement();
279 QDomElement aSection = aNode.toElement();
280 if (aSection.tagName() == "ServerManagerState") {
281 processProxies(aNode, theFileNames, theNewPath);
284 aNode = aNode.nextSibling();
286 if (!aFile.open(QFile::WriteOnly | QFile::Truncate)) {
287 MESSAGE("Can't open state file "<<aFileName<<" for writing");
290 QTextStream out(&aFile);
298 //----------------------------------------------------------------------------
299 SALOMEDS::TMPFile* PARAVIS_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
300 const char* theURL, bool isMultiFile)
302 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
304 std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
306 std::ostringstream aStream;
307 aStream<<"paravisstate:"<<(long)this;
308 std::string aFileName = "_" + aStream.str();
309 if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
311 //std::vector<std::string> aFileNames;
312 //aFileNames.push_back(aFileName);
314 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
316 std::string aFile = aTmpDir + aFileName;
317 ProcessVoidEvent(new TSaveStateFile(mySalomeApp, aFile.c_str()));
319 // TO Do: Collect all files from state
320 QStringList aFileNames;
321 processAllFilesInState(aFile.c_str(), aFileNames, 0);
322 aFileNames<<QString(aFile.c_str());
325 foreach(QString aFile, aFileNames) {
326 QFileInfo aInfo(aFile);
327 aNames<<aInfo.fileName();
330 SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
331 SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
333 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
334 SALOMEDS_Tool::RemoveTemporaryFiles(theURL, aListOfFileNames, true);
336 return aStreamFile._retn();
339 //----------------------------------------------------------------------------
340 SALOMEDS::TMPFile* PARAVIS_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
341 const char* theURL, bool isMultiFile)
343 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
345 std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
347 std::ostringstream aStream;
348 aStream<<"paravisstate:"<<(long)this;
349 std::string aFileName = "_" + aStream.str();
350 if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
352 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
354 std::string aFile = aTmpDir + aFileName;
355 ProcessVoidEvent(new TSaveStateFile(mySalomeApp, aFile.c_str()));
357 //Collect all files from state
358 QStringList aFileNames;
359 processAllFilesInState(aFile.c_str(), aFileNames, 0);
360 aFileNames<<QString(aFile.c_str());
363 foreach(QString aFile, aFileNames) {
364 QFileInfo aInfo(aFile);
365 aNames<<aInfo.fileName();
368 SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
369 SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
371 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
372 SALOMEDS_Tool::RemoveTemporaryFiles(theURL, aListOfFileNames, true);
374 return aStreamFile._retn();
378 //----------------------------------------------------------------------------
379 bool PARAVIS_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream,
380 const char* theURL, bool isMultiFile)
382 std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
383 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
385 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
386 if (aSeq->length() == 0)
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()));
397 //----------------------------------------------------------------------------
398 bool PARAVIS_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
399 const SALOMEDS::TMPFile& theStream,
400 const char* theURL, bool isMultiFile)
402 std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
403 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
405 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
406 if (aSeq->length() == 0)
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()));
417 //----------------------------------------------------------------------------
418 void PARAVIS_Gen_i::Close(SALOMEDS::SComponent_ptr IORSComponent)
422 //----------------------------------------------------------------------------
423 char* PARAVIS_Gen_i::ComponentDataType()
425 return CORBA::string_dup("PARAVIS");
428 //----------------------------------------------------------------------------
429 char* PARAVIS_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, const char* IORString,
430 CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
432 CORBA::String_var aString("");
433 return aString._retn();
436 //----------------------------------------------------------------------------
437 char* PARAVIS_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
438 const char* aLocalPersistentID,
439 CORBA::Boolean isMultiFile,
440 CORBA::Boolean isASCII)
442 CORBA::String_var aString("");
443 return aString._retn();
446 //----------------------------------------------------------------------------
447 bool PARAVIS_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
452 //----------------------------------------------------------------------------
453 SALOMEDS::SObject_ptr PARAVIS_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
454 SALOMEDS::SObject_ptr theSObject,
455 CORBA::Object_ptr theObject,
457 throw (SALOME::SALOME_Exception)
459 SALOMEDS::SObject_var aResultSO;
460 return aResultSO._retn();
463 //----------------------------------------------------------------------------
464 CORBA::Boolean PARAVIS_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
469 //----------------------------------------------------------------------------
470 SALOMEDS::TMPFile* PARAVIS_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
472 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
473 return aStreamFile._retn();
476 //----------------------------------------------------------------------------
477 CORBA::Boolean PARAVIS_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID)
482 //----------------------------------------------------------------------------
483 SALOMEDS::SObject_ptr PARAVIS_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
484 CORBA::Long theObjectID, SALOMEDS::SObject_ptr theObject)
486 return SALOMEDS::SObject::_nil();
489 //----------------------------------------------------------------------------
490 PARAVIS::string_array* PARAVIS_Gen_i::GetClassesList()
493 for (k = 0; strcmp(wrapped_classes[k], "") != 0; k++);
494 PARAVIS::string_array_var aNames = new PARAVIS::string_array();
496 for (CORBA::ULong i = 0; i < k; i++)
497 aNames[i] = CORBA::string_dup(wrapped_classes[i]);
498 return aNames._retn();
501 //----------------------------------------------------------------------------
502 PARAVIS_Base_ptr PARAVIS_Gen_i::CreateClass(const char* theClassName)
504 PARAVIS::PARAVIS_Base_i* aClass = CreateInstance(NULL, QString(theClassName));
505 return aClass->_this();
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)
514 pqServer* aServer = ProcessEvent(new TGetActiveServer(mySalomeApp));
516 theId = static_cast<int>(aServer->GetConnectionID());
518 pqServerResource serverRes = aServer->getResource();
519 theReversed = (serverRes.scheme() == "csrc" || serverRes.scheme() == "cdsrsrc")? 1 : 0;
521 theDPort = serverRes.dataServerPort() < 0? serverRes.port() : serverRes.dataServerPort();
522 theRPort = serverRes.renderServerPort() < 0 ? 0 : serverRes.renderServerPort();
524 QString dsHost = serverRes.dataServerHost().isEmpty()? serverRes.host() : serverRes.dataServerHost();
525 QString rsHost = serverRes.renderServerHost();
527 theDHost = CORBA::string_dup(qPrintable(dsHost));
528 theRHost = CORBA::string_dup(qPrintable(rsHost));
533 //----------------------------------------------------------------------------
534 Engines::TMPFile* PARAVIS_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
535 CORBA::Boolean theIsPublished,
536 CORBA::Boolean& theIsValidScript)
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();
548 //----------------------------------------------------------------------------
549 void PARAVIS_Gen_i::ActivateModule()
551 ProcessVoidEvent(new TActivateModule(mySalomeApp));