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>
39 #include <SUIT_ResourceMgr.h>
41 #include <SALOMEDS_Tool.hxx>
44 #include "PV_Events.h"
47 #include "QDomDocument"
49 #include "QDomElement"
52 #include "QTextStream"
54 #include "vtkWrapIDL.h"
57 #include <pqServerResource.h>
61 static int MYDEBUG = 1;
63 static int MYDEBUG = 0;
68 extern PARAVIS::PARAVIS_Base_i* CreateInstance(::vtkObjectBase* Inst, const QString&);
71 PARAVIS_I_EXPORT PARAVIS::PARAVIS_Gen_ptr GetImpl(CORBA::ORB_ptr theORB,
72 PortableServer::POA_ptr thePOA,
73 SALOME_NamingService* theNamingService,
76 if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
77 PARAVIS::PARAVIS_Gen_i *aPARAVIS_Gen = new PARAVIS::PARAVIS_Gen_i(theORB,
81 return aPARAVIS_Gen->_this();
88 PARAVIS_Base_i::~PARAVIS_Base_i() {
89 if(mySmartPointer != NULL) mySmartPointer->Delete();
92 void PARAVIS_Base_i::Init(::vtkObjectBase* base) {
93 if (mySmartPointer != NULL) mySmartPointer->Delete();
94 mySmartPointer = base;
97 ::vtkObjectBase* PARAVIS_Base_i::getVTKObject(PARAVIS_Base_ptr theBase) {
98 PARAVIS_Base_i* aBase = GET_SERVANT(theBase);
99 return (aBase != NULL)? aBase->getVTKObject() : NULL;
102 CORBA::Boolean PARAVIS_Base_i::IsSame(PARAVIS_Base_ptr theOther)
104 PARAVIS_Base_i* aBase = GET_SERVANT(theOther);
107 return mySmartPointer.GetPointer() == aBase->getVTKObject();
113 PARAVIS_Gen_i* PARAVIS_Gen_i::myParavisGenImpl;
114 CORBA::ORB_var PARAVIS_Gen_i::myOrb;
115 PortableServer::POA_var PARAVIS_Gen_i::myPoa;
116 SALOME_LifeCycleCORBA* PARAVIS_Gen_i::myEnginesLifeCycle;
117 SALOME_NamingService* PARAVIS_Gen_i::myNamingService;
118 QMutex* PARAVIS_Gen_i::myMutex;
119 SalomeApp_Application* PARAVIS_Gen_i::mySalomeApp = 0;
122 //----------------------------------------------------------------------------
123 PARAVIS_Gen_i::PARAVIS_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
124 SALOME_NamingService* theNamingService, QMutex* theMutex) :
125 Engines_Component_i()
127 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i");
128 if (mySalomeApp) return;
131 myOrb = CORBA::ORB::_duplicate(theORB);
132 myPoa = PortableServer::POA::_duplicate(thePOA);
133 myParavisGenImpl = this;
134 myNamingService = theNamingService;
136 static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
137 myEnginesLifeCycle = &aEnginesLifeCycle;
139 CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
140 if (!CORBA::is_nil(anObj)) {
141 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
142 SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
143 if(aListOfOpenStudies->length() > 0) {
144 CORBA::String_var aStudyName = aListOfOpenStudies[0];
145 myStudyDocument = aStudyManager->GetStudyByName(aStudyName);
146 if (!myStudyDocument->_is_nil()) {
147 mySalomeApp = ProcessEvent(new TGetGUIApplication(myStudyDocument->StudyId()));
148 if(!myStudyDocument->GetProperties()->IsLocked())
149 FindOrCreateParaVisComponent(myStudyDocument);
152 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : there is no opened study in StudyManager !!!");
154 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PARAVIS_Gen_i : Can't find StudyManager !!!");
158 //----------------------------------------------------------------------------
159 PARAVIS_Gen_i::~PARAVIS_Gen_i()
161 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::~PARAVIS_Gen_i");
164 //----------------------------------------------------------------------------
165 char* PARAVIS_Gen_i::GetIOR()
168 CORBA::Object_var anObject = _this();
169 CORBA::String_var anIOR = myOrb->object_to_string(anObject);
172 return CORBA::string_dup(myIOR.c_str());
175 //----------------------------------------------------------------------------
176 void PARAVIS_Gen_i::ImportFile(const char* theFileName)
178 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::ImportFile: " <<theFileName);
179 ProcessVoidEvent(new TImportFile(mySalomeApp, theFileName));
182 //----------------------------------------------------------------------------
183 char* PARAVIS_Gen_i::GetTrace()
185 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::PrintTrace: ");
186 return CORBA::string_dup(ProcessEvent(new TGetTrace(mySalomeApp)).c_str());
189 //----------------------------------------------------------------------------
190 void PARAVIS_Gen_i::SaveTrace(const char* theFileName)
192 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::SaveTrace: " <<theFileName);
193 ProcessVoidEvent(new TSaveTrace(mySalomeApp, theFileName));
196 //----------------------------------------------------------------------------
197 void processElements(QDomNode& thePropertyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
199 QDomNode aElementNode = thePropertyNode.firstChild();
200 while (aElementNode.isElement()) {
201 QDomElement aElement = aElementNode.toElement();
202 if (aElement.tagName() == "Element") {
203 QString aIndex = aElement.attribute("index");
205 QString aValue = aElement.attribute("value");
206 if (!aValue.isNull()) {
208 QFileInfo aFInfo(aValue);
209 if (aFInfo.exists()) {
210 theFileNames<<aValue;
211 aElement.setAttribute("value", aFInfo.fileName());
216 aElement.setAttribute("value", QString(theNewPath) + aValue);
221 aElementNode = aElementNode.nextSibling();
226 //----------------------------------------------------------------------------
227 void processProperties(QDomNode& theProxyNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
229 QDomNode aPropertyNode = theProxyNode.firstChild();
230 while (aPropertyNode.isElement()) {
231 QDomElement aProperty = aPropertyNode.toElement();
232 QString aName = aProperty.attribute("name");
233 if ((aName == "FileName") || (aName == "FileNameInfo") || (aName == "FileNames")) {
234 processElements(aPropertyNode, theFileNames, theNewPath, theRestore);
236 aPropertyNode = aPropertyNode.nextSibling();
241 //----------------------------------------------------------------------------
242 void processProxies(QDomNode& theNode, QStringList& theFileNames, const char* theNewPath, bool theRestore)
244 QDomNode aProxyNode = theNode.firstChild();
245 while (aProxyNode.isElement()) {
246 QDomElement aProxy = aProxyNode.toElement();
247 if (aProxy.tagName() == "Proxy") {
248 QString aGroup = aProxy.attribute("group");
249 if (aGroup == "sources") {
250 processProperties(aProxyNode, theFileNames, theNewPath, theRestore);
253 aProxyNode = aProxyNode.nextSibling();
257 //----------------------------------------------------------------------------
258 bool processAllFilesInState(const char* aFileName, QStringList& theFileNames,
259 const char* theNewPath, bool theRestore = false)
261 QFile aFile(aFileName);
262 if (!aFile.open(QFile::ReadOnly)) {
263 MESSAGE("Can't open state file "<<aFileName);
267 bool aRes = aDoc.setContent(&aFile);
271 MESSAGE("File "<<aFileName<<" is not XML document");
275 QDomElement aRoot = aDoc.documentElement();
276 if ( aRoot.isNull() /*|| aRoot.tagName() != "SALOME" Names are different in various versions */ ) {
277 MESSAGE( "Invalid XML root" );
281 QDomNode aNode = aRoot.firstChild();
282 while (aRes && !aNode.isNull() ) {
283 aRes = aNode.isElement();
285 QDomElement aSection = aNode.toElement();
286 if (aSection.tagName() == "ServerManagerState") {
287 processProxies(aNode, theFileNames, theNewPath, theRestore);
290 aNode = aNode.nextSibling();
292 if (!aFile.open(QFile::WriteOnly | QFile::Truncate)) {
293 MESSAGE("Can't open state file "<<aFileName<<" for writing");
296 QTextStream out(&aFile);
304 SALOMEDS::TMPFile* SaveState(long thePID, SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent,
305 const char* theURL, bool isMultiFile)
307 std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
309 std::ostringstream aStream;
310 aStream<<"paravisstate:"<<thePID;
311 std::string aFileName = "_" + aStream.str();
312 if(MYDEBUG) MESSAGE("aFileName = '"<<aFileName);
314 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
316 std::string aFile = aTmpDir + aFileName;
317 ProcessVoidEvent(new TSaveStateFile(theApp, aFile.c_str()));
319 // Collect all files from state
320 SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
321 int aSavingType = aResourceMgr->integerValue( "PARAVIS", "savestate_type", 0 );
323 QStringList aFileNames;
326 switch (aSavingType) {
327 case 0: // Save referenced files only for builin server
329 MESSAGE("Save as built in;")
330 bool aIsBuiltIn = true;
331 pqServer* aServer = ProcessEvent(new TGetActiveServer(theApp));
333 aIsBuiltIn != aServer->isRemote();
337 // Find referenced files and collect their paths nullyfying references
338 processAllFilesInState(aFile.c_str(), aFileNames, 0);
339 SetRestoreParam(theComponent, true);
341 SetRestoreParam(theComponent, false);
345 case 1: //Save referenced files when they are accessible
347 // Find referenced files and collect their paths nullyfying references
348 processAllFilesInState(aFile.c_str(), aFileNames, 0);
349 SetRestoreParam(theComponent, true);
352 case 2: //Never save referenced files
353 SetRestoreParam(theComponent, false);
356 aFileNames<<QString(aFile.c_str());
357 foreach(QString aFile, aFileNames) {
358 QFileInfo aInfo(aFile);
359 aNames<<aInfo.fileName();
361 SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
362 SALOMEDS::ListOfFileNames_var aListOfNames = GetListOfFileNames(aNames);
364 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFileNames.in(), aListOfNames.in());
365 //SALOMEDS_Tool::RemoveTemporaryFiles(theURL, aListOfFileNames, true);
367 return aStreamFile._retn();
370 //----------------------------------------------------------------------------
371 SALOMEDS::TMPFile* PARAVIS_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
372 const char* theURL, bool isMultiFile)
374 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
375 return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
378 //----------------------------------------------------------------------------
379 SALOMEDS::TMPFile* PARAVIS_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
380 const char* theURL, bool isMultiFile)
382 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Save - theURL = '"<<theURL<<"'");
383 return SaveState((long) this, mySalomeApp, theComponent, theURL, isMultiFile);
387 //----------------------------------------------------------------------------
388 bool LoadState(SalomeApp_Application* theApp, SALOMEDS::SComponent_ptr theComponent,
389 const SALOMEDS::TMPFile& theStream, const char* theURL, bool isMultiFile)
391 std::string aTmpDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
392 if(MYDEBUG) MESSAGE("PARAVIS_Gen_i::Load - "<<aTmpDir);
394 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
395 if (aSeq->length() == 0)
398 bool aRestore = GetRestoreParam(theComponent);
399 MESSAGE("PARAVIS_Gen_i::Restore path - "<<aRestore);
401 std::string aFile = aTmpDir + std::string(aSeq[aSeq->length() - 1]);
402 QStringList aEmptyList;
403 processAllFilesInState(aFile.c_str(), aEmptyList, aTmpDir.c_str(), aRestore);
404 ProcessVoidEvent(new TLoadStateFile(theApp, aFile.c_str()));
409 //----------------------------------------------------------------------------
410 bool PARAVIS_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream,
411 const char* theURL, bool isMultiFile)
413 return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
416 //----------------------------------------------------------------------------
417 bool PARAVIS_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
418 const SALOMEDS::TMPFile& theStream,
419 const char* theURL, bool isMultiFile)
421 return LoadState(mySalomeApp, theComponent, theStream, theURL, isMultiFile);
424 //----------------------------------------------------------------------------
425 void PARAVIS_Gen_i::Close(SALOMEDS::SComponent_ptr IORSComponent)
429 //----------------------------------------------------------------------------
430 char* PARAVIS_Gen_i::ComponentDataType()
432 return CORBA::string_dup("PARAVIS");
435 //----------------------------------------------------------------------------
436 char* PARAVIS_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject, const char* IORString,
437 CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
439 CORBA::String_var aString("");
440 return aString._retn();
443 //----------------------------------------------------------------------------
444 char* PARAVIS_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
445 const char* aLocalPersistentID,
446 CORBA::Boolean isMultiFile,
447 CORBA::Boolean isASCII)
449 CORBA::String_var aString("");
450 return aString._retn();
453 //----------------------------------------------------------------------------
454 bool PARAVIS_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
459 //----------------------------------------------------------------------------
460 SALOMEDS::SObject_ptr PARAVIS_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
461 SALOMEDS::SObject_ptr theSObject,
462 CORBA::Object_ptr theObject,
464 throw (SALOME::SALOME_Exception)
466 SALOMEDS::SObject_var aResultSO;
467 return aResultSO._retn();
470 //----------------------------------------------------------------------------
471 CORBA::Boolean PARAVIS_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
476 //----------------------------------------------------------------------------
477 SALOMEDS::TMPFile* PARAVIS_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID)
479 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
480 return aStreamFile._retn();
483 //----------------------------------------------------------------------------
484 CORBA::Boolean PARAVIS_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID)
489 //----------------------------------------------------------------------------
490 SALOMEDS::SObject_ptr PARAVIS_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
491 CORBA::Long theObjectID, SALOMEDS::SObject_ptr theObject)
493 return SALOMEDS::SObject::_nil();
496 //----------------------------------------------------------------------------
497 PARAVIS::string_array* PARAVIS_Gen_i::GetClassesList()
500 for (k = 0; strcmp(wrapped_classes[k], "") != 0; k++);
501 PARAVIS::string_array_var aNames = new PARAVIS::string_array();
503 for (CORBA::ULong i = 0; i < k; i++)
504 aNames[i] = CORBA::string_dup(wrapped_classes[i]);
505 return aNames._retn();
508 //----------------------------------------------------------------------------
509 PARAVIS_Base_ptr PARAVIS_Gen_i::CreateClass(const char* theClassName)
511 PARAVIS::PARAVIS_Base_i* aClass = CreateInstance(NULL, QString(theClassName));
512 return aClass->_this();
515 //----------------------------------------------------------------------------
516 void PARAVIS_Gen_i::GetConnectionParameters(CORBA::Long& theId,
517 CORBA::String_out theDHost, CORBA::Long& theDPort,
518 CORBA::String_out theRHost, CORBA::Long& theRPort,
519 CORBA::Long& theReversed)
521 pqServer* aServer = ProcessEvent(new TGetActiveServer(mySalomeApp));
523 theId = static_cast<int>(aServer->GetConnectionID());
525 pqServerResource serverRes = aServer->getResource();
526 theReversed = (serverRes.scheme() == "csrc" || serverRes.scheme() == "cdsrsrc")? 1 : 0;
528 theDPort = serverRes.dataServerPort() < 0? serverRes.port() : serverRes.dataServerPort();
529 theRPort = serverRes.renderServerPort() < 0 ? 0 : serverRes.renderServerPort();
531 QString dsHost = serverRes.dataServerHost().isEmpty()? serverRes.host() : serverRes.dataServerHost();
532 QString rsHost = serverRes.renderServerHost();
534 theDHost = CORBA::string_dup(qPrintable(dsHost));
535 theRHost = CORBA::string_dup(qPrintable(rsHost));
540 //----------------------------------------------------------------------------
541 Engines::TMPFile* PARAVIS_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
542 CORBA::Boolean theIsPublished,
543 CORBA::Boolean& theIsValidScript)
545 theIsValidScript = true;
546 std::string aResult(ProcessEvent(new TGetTrace(mySalomeApp)));
547 aResult += "\ndef RebuildData(theStudy):\n pass\n";
548 CORBA::ULong aSize = aResult.size() + 1;
549 char* aBuffer = new char[aSize];
550 memset(aBuffer, 0, aSize);
551 strcpy(aBuffer, &aResult[0]);
552 Engines::TMPFile_var aDump = new Engines::TMPFile(aSize,aSize,(CORBA::Octet*)aBuffer,1);
553 return aDump._retn();
556 //----------------------------------------------------------------------------
557 void PARAVIS_Gen_i::ActivateModule()
559 ProcessVoidEvent(new TActivateModule(mySalomeApp));