2 // File : VISU_Gen_i.cc file
4 // Author : Alexey Petrov
6 // Copyright : OPEN CASCADE
9 #include "VISU_Gen_i.hh"
10 #include "VISU_Result_i.hh"
11 #include "VISU_Convertor.hxx"
12 #include "VISU_PrsObject_i.hh"
13 #include "VISU_ViewManager_i.hh"
14 #include "VISU_Table_i.hh"
16 #include "QAD_Application.h"
17 #include "QAD_Desktop.h"
18 #include "QAD_Study.h"
19 #include "SALOMEDS_Tool.hxx"
21 #include <TCollection_AsciiString.hxx>
22 #include <TColStd_SequenceOfAsciiString.hxx>
23 #include <vtkUnstructuredGridReader.h>
24 #include <vtkUnstructuredGridWriter.h>
26 #include <omnithread.h>
27 #include CORBA_SERVER_HEADER(SALOME_Session)
30 #include <qfileinfo.h>
31 static QFileInfo aFileInfo;
34 static int MYDEBUG = 1;
36 static int MYDEBUG = 0;
39 #include <utilities.h>
41 //static int IS_LOADED = (cout<<"\n----------------- VisuEngine_factory is loaded -----------------\n", 1);
43 PortableServer::ObjectId *
44 VISUEngine_factory(CORBA::ORB_ptr orb,
45 PortableServer::POA_ptr poa,
46 PortableServer::ObjectId * contId,
47 const char *instanceName,
48 const char *interfaceName)
50 if(MYDEBUG) MESSAGE("VisuEngine_factory : "<<interfaceName);
51 VISU::VISU_Gen_i * pVISU_Gen = new VISU::VISU_Gen_i(orb, poa, contId, instanceName, interfaceName);
52 return pVISU_Gen->getId() ;
56 // GetVisuGen(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex* theMutex){
57 // if(MYDEBUG) MESSAGE("extern \"C\" GetVisuGen");
58 // VISU::VISU_Gen_i * aVISU_Gen = new VISU::VISU_Gen_i(orb,poa,theMutex);
59 // return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
62 Engines::Component_ptr GetVisuGen(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, QMutex* theMutex){
63 if(MYDEBUG) MESSAGE("extern \"C\" GetVisuGen");
64 VISU::VISU_Gen_i * aVISU_Gen = new VISU::VISU_Gen_i(theORB,thePOA,theMutex);
65 VISU::VISU_Gen_var aVisuGen = VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
66 return Engines::Component::_narrow( aVisuGen );
71 //===========================================================================
72 QMutex* Base_i::myMutex = NULL;
73 CORBA::ORB_var Base_i::myOrb;
74 PortableServer::POA_var Base_i::myPOA;
76 char* Base_i::GetID(){
77 //CORBA::Object_var anObject = _this();
78 //return CORBA::string_dup(myOrb->object_to_string(anObject));
79 return CORBA::string_dup(myOrb->object_to_string(_this()));
81 //===========================================================================
82 static int mySCnt = 0;
83 static int myQCnt = 0;
84 Mutex::Mutex(QMutex* theMutex, QApplication* theQApp) :
85 myQApp(theQApp), isQAppLocked(theQApp->locked()),
86 myMutex(theMutex), isSessionLocked(theMutex->locked())
88 if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<!isQAppLocked<<" "<<!isSessionLocked);
89 if(!isSessionLocked) { myMutex->lock(); mySCnt++; }
90 if(!isQAppLocked) { myQApp->lock(); myQCnt++; }
92 //if(MYDEBUG) MESSAGE("Mutex::Mutex - "<<myQCnt<<" "<<mySCnt);
95 if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<!isQAppLocked<<" "<<!isSessionLocked);
97 if(!isQAppLocked) { myQApp->unlock(); myQCnt--; }
98 if(!isSessionLocked) { myMutex->unlock(); mySCnt--; }
99 //if(MYDEBUG) MESSAGE("Mutex::~Mutex - "<<myQCnt<<" "<<mySCnt);
100 // myQApp->processEvents(); myQApp->processEvents(); myQApp->processEvents();
102 //===========================================================================
103 static Storable::TCallbackMap VisuStoreMap;
104 const char* Storable::ToString(){
106 Storable::DataToStream( strOut, "myComment", GetComment() );
109 if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
112 void Storable::Registry(const char* theComment, TStorableEngine theEngine)
113 throw(std::logic_error&)
115 if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
116 if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
117 throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
120 void RegistryStorable() throw(std::logic_error&) {
121 //Storable::Registry(View3D_i::myComment.c_str(),&View3DRestore);
122 Storable::Registry(Result_i::myComment.c_str(),&ResultRestore);
123 Storable::Registry(Mesh_i::myComment.c_str(),&MeshRestore);
124 Storable::Registry(ScalarMap_i::myComment.c_str(),&ScalarMapRestore);
125 Storable::Registry(DeformedShape_i::myComment.c_str(),&DeformedShapeRestore);
126 Storable::Registry(CutPlanes_i::myComment.c_str(),&CutPlanesRestore);
127 Storable::Registry(IsoSurfaces_i::myComment.c_str(),&IsoSurfacesRestore);
128 Storable::Registry(StreamLines_i::myComment.c_str(),&StreamLinesRestore);
129 Storable::Registry(Vectors_i::myComment.c_str(),&VectorsRestore);
130 Storable::Registry(Table_i::myComment.c_str(),&TableRestore);
131 Storable::Registry(Curve_i::myComment.c_str(),&CurveRestore);
132 Storable::Registry(Container_i::myComment.c_str(),&ContainerRestore);
135 SALOMEDS::SObject_ptr Storable::GetResultSO(SALOMEDS::SObject_ptr theSObject) {
136 CORBA::String_var aComponentID(theSObject->GetFatherComponent()->GetID());
137 SALOMEDS::SObject_var aResult = SALOMEDS::SObject::_duplicate(theSObject);
138 CORBA::String_var aFatherID(aResult->GetFather()->GetID());
139 while(strcmp(aComponentID, aFatherID)) {
140 aResult = aResult->GetFather();
141 aFatherID = aResult->GetFather()->GetID();
142 if (strlen(aFatherID) < 3) break;
144 return aResult._retn();
147 void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
148 if(MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
149 QStringList strList = QStringList::split( ";", theStr, false );
150 for ( int i = 0; i < strList.count(); i++ ) {
151 QString next = strList[ i ];
152 int pos = next.find("=");
153 QString aName, aValue;
155 aName = next.stripWhiteSpace();
159 aName = next.left( pos ).stripWhiteSpace();
160 aValue = next.mid( pos+1 );
161 if ( aValue.isNull() )
164 if ( !aName.isEmpty() )
165 theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );
168 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const QString& theVal) {
169 QString output = ( !theName.isNull() ? theName : QString("") )
171 + ( !theVal.isNull() ? theVal : QString("") );
172 theStr<<output.latin1()<<";";
174 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const int theVal) {
175 QString output = ( !theName.isNull() ? theName : QString("") )
177 + QString::number( theVal );
178 theStr<<output.latin1()<<";";
180 void Storable::DataToStream(ostrstream& theStr, const QString& theName, const double theVal) {
181 QString output = ( !theName.isNull() ? theName : QString("") )
183 + QString::number( theVal );
184 theStr<<output.latin1()<<";";
186 Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
187 const string& thePrefix, const string& theLocalPersistentID)
188 throw (std::runtime_error&)
191 QString strIn( theLocalPersistentID.c_str() );
193 StrToMap(strIn,aMap);
194 //CORBA::String_var aResultID(GetResultSO(theSObject)->GetID());
195 //aMap.insert(TRestoringMap::value_type("ResultID", strdup(aResultID)));
197 QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
199 TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
200 if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
201 if(i == VisuStoreMap.end()) return NULL;
202 return (i->second)(theSObject,thePrefix,aMap);
204 }catch(std::logic_error& exc){
205 MESSAGE("Follow exception was accured :\n"<<exc.what());
207 MESSAGE("Unknown exception was accured!");
211 const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
212 throw(std::logic_error&)
214 TRestoringMap::const_iterator i = theMap.find(theArg);
215 if(i == theMap.end()) {
216 if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
217 if(isFind != NULL) *isFind = false;
218 //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
219 static QString BAD_VALUE("NULL");
222 if(isFind != NULL) *isFind = true;
226 //===========================================================================
227 PortableServer::Servant GetServant(CORBA::Object_ptr theObject){
228 PortableServer::POA_var aPOA = GetPOA();
229 if(CORBA::is_nil(theObject))
230 if(MYDEBUG) MESSAGE("GetServant - CORBA::is_nil(theObject)");
231 return aPOA->reference_to_servant(theObject);
233 //===========================================================================
234 CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
235 SALOMEDS::GenericAttribute_var anAttr;
236 CORBA::Object_var anObj;
237 if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
238 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
239 CORBA::String_var aValue = anIOR->Value();
240 CORBA::ORB_var anORB = GetORB();
241 if(strcmp(aValue,"") != 0)
242 anObj = anORB->string_to_object(aValue);
246 //===========================================================================
247 SALOMEDS::SComponent_var FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument){
248 SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
249 if (aSComponent->_is_nil()) {
250 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
251 aStudyBuilder->NewCommand();
252 int aLocked = theStudyDocument->GetProperties()->IsLocked();
253 if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
254 aSComponent = aStudyBuilder->NewComponent("VISU");
255 SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
256 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
257 //NRI aName->SetValue("Visu");
259 CORBA::ORB_var anORB = GetORB();
260 SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
261 CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
262 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
263 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
264 if ( !Comp->_is_nil() ) {
265 aName->SetValue( Comp->componentusername() );
268 anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
269 SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
270 aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
272 VISU_Gen_var aVisuGen = GetVisu();
273 aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
274 if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
275 aStudyBuilder->CommitCommand();
279 //===========================================================================
280 string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
281 const char* theComment, int IsAllLevels)
283 if(MYDEBUG) MESSAGE("FindEntryWithComment - '"<<theComment<<"'");
284 SALOMEDS::ChildIterator_var anIter =
285 theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
286 anIter->InitEx(IsAllLevels);
287 SALOMEDS::SObject_var aFieldSO;
288 for(;anIter->More();anIter->Next()) {
289 SALOMEDS::GenericAttribute_var anAttr;
290 if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
291 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
292 CORBA::String_var aString = aCmnt->Value();
293 string aValue(aString);
294 if (aValue == theComment) {
295 aFieldSO = anIter->Value();
296 aString = aFieldSO->GetID();
304 //===========================================================================
305 string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument,
306 const char* theFatherEntry, const char* theRefFatherEntry,
307 const char* theIOR, const char* theName,
308 const char* thePersistentRef, const char* theComment,
309 CORBA::Boolean theCreateNew)
312 MESSAGE("CreateAttributes - theName = "<<theName<<"; theComment = '"<<theComment<<
313 "'; theFatherEntry = "<<theFatherEntry);
314 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
315 SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
316 SALOMEDS::SObject_var newObj;
317 if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
318 else newObj = aFather;
319 SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
320 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
321 anIOR->SetValue(theIOR);
322 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
323 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
324 aName->SetValue(theName);
325 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
326 SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
327 aPRef->SetValue(thePersistentRef);
328 anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
329 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
330 aCmnt->SetValue(theComment);
331 if(strcmp(theRefFatherEntry,"") != 0){
332 SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
333 SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
334 aStudyBuilder->Addreference(anObj,newObj);
336 CORBA::String_var anEntry = newObj->GetID();
337 string aRet(anEntry);
338 if(MYDEBUG) MESSAGE("CreateAttributes - anEntry = "<<aRet<<"; IOR = "<<theIOR);
341 //===========================================================================
342 static CORBA::ORB_var VisuORB;
343 static PortableServer::POA_var VisuPOA;
344 static SALOMEDS::SComponent_var VisuSComponent;
345 static string VisuTmpDir;
346 static VISU_Gen_i* VisuGen;
347 static CORBA::Boolean myIsMultiFile;
348 CORBA::ORB_var GetORB() { return VisuORB;}
349 PortableServer::POA_var GetPOA() { return VisuPOA;}
350 VISU_Gen_var GetVisu() {
351 return VISU_Gen::_duplicate(VisuGen->_this());
352 //return VisuGen->_this();
354 const CORBA::Boolean IsMultifile() { return myIsMultiFile;}
355 //===========================================================================
356 bool VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
357 const SALOMEDS::TMPFile & theStream,
361 if(MYDEBUG) MESSAGE("VISU_Gen_i::Load - myMutex = "<<myMutex);
363 Mutex mt(myMutex,qApp);
364 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
365 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
366 TCollection_AsciiString aTmpDir =
367 isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
368 VisuSComponent = SALOMEDS::SComponent::_duplicate(theComponent);
369 VisuTmpDir = aTmpDir.ToCString();
370 SALOMEDS::ListOfFileNames_var aSeq =
371 SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
372 myIsMultiFile = isMultiFile;
375 return myVisuGen->Load(theComponent,theStream,theURL,isMultiFile);
378 char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
379 const char* aLocalPersistentID,
380 CORBA::Boolean isMultiFile) {
382 Mutex mt(myMutex,qApp);
383 CORBA::String_var aString("");
384 if(strcmp(aLocalPersistentID,"") != 0) {
385 Storable* aStorable =
386 Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
387 if(aStorable != NULL) aString = aStorable->GetID();
389 return aString._retn();
391 return myVisuGen->LocalPersistentIDToIOR(theSObject, aLocalPersistentID, isMultiFile);
393 //===========================================================================
394 SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
398 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
400 Mutex mt(myMutex,qApp);
401 TCollection_AsciiString aTmpDir =
402 (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
403 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
405 TColStd_SequenceOfAsciiString aFileNames;
406 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
407 SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
408 for (; itBig->More(); itBig->Next()) {
409 SALOMEDS::SObject_var gotBranch = itBig->Value();
410 CORBA::Object_var anObj = SObjectToObject(gotBranch);
411 if(CORBA::is_nil(anObj)) continue;
412 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
414 const QFileInfo& aFileInfo = pResult->GetFileInfo();
415 QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
416 QString aFileName = aPrefix + "_" + pResult->GetName();
417 static QString aCommand;
418 aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
419 if(system(aCommand) == -1){
420 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
423 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
424 TCollection_AsciiString aString(strdup(aFileName.latin1()));
425 aFileNames.Append(aString);
428 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
429 if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
430 if(aFileNames.Length() > 0){
431 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
432 aSeq->length(aFileNames.Length());
433 for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
434 aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
435 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
437 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
439 return aStreamFile._retn();
441 return myVisuGen->Save(theComponent,theURL,isMultiFile);
444 char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
445 const char* IORString,
446 CORBA::Boolean isMultiFile) {
447 if(MYDEBUG) MESSAGE("VISU_Gen_i::IORToLocalPersistentID - myMutex = "<<myMutex);
449 Mutex mt(myMutex,qApp);
450 CORBA::ORB_var anORB = GetORB();
451 CORBA::String_var aString(IORString);
452 if(strcmp(IORString,"") != 0){
453 CORBA::Object_var anObj = anORB->string_to_object(aString);
454 if(!CORBA::is_nil(anObj)){
455 Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj));
456 if(pStorable != NULL){
457 aString = pStorable->ToString();
458 return aString._retn();
462 return aString._retn();
464 return myVisuGen->IORToLocalPersistentID(theSObject, IORString, isMultiFile);
467 //===========================================================================
468 VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr orb,
469 PortableServer::POA_ptr poa,
470 PortableServer::ObjectId * contId,
471 const char *instanceName,
472 const char *interfaceName) :
473 Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
475 if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<myMutex);
477 _id = _poa->activate_object(_thisObj);
478 SALOME_NamingService aNamingService(orb);
479 CORBA::Object_ptr anObject = aNamingService.Resolve("/Kernel/Session");
480 SALOME::Session_var aSession = SALOME::Session::_narrow(anObject);
481 //aSession->GetInterface();
482 //myVisuGen = aSession->GetVisuGen();
483 myVisuGen = VISU::VISU_Gen::_narrow( aSession->GetVisuComponent() );
485 VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, QMutex* theMutex) :
486 Engines_Component_i()
488 if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
489 Base_i::myMutex = theMutex;
490 Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
491 Base_i::myPOA = PortableServer::POA::_duplicate(thePOA);
492 Mutex mt(myMutex,qApp);
496 VisuPOA->activate_object(this);
498 if(!QAD_Application::getDesktop()->getActiveStudy())
499 QAD_Application::getDesktop()->createStudy();
501 QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
503 VISU_Gen_i::~VISU_Gen_i(){
504 if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
506 void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
507 //if(MYDEBUG) MESSAGE("VISU_Gen_i::SetCurrentStudy : "<<(!theStudy->_is_nil()));
509 myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
511 myVisuGen->SetCurrentStudy(theStudy);
513 SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
514 if(MYDEBUG) MESSAGE("VISU_Gen_i::GetCurrentStudy : "<<myMutex);
516 return SALOMEDS::Study::_duplicate(myStudyDocument);
517 return myVisuGen->GetCurrentStudy();
519 ViewManager_ptr VISU_Gen_i::GetViewManager(){
521 //omni_mutex_lock aMutexLock(aMutex);
522 if(MYDEBUG) MESSAGE("VISU_Gen_i::GetViewManager : "<<myMutex);
524 Mutex mt(myMutex,qApp);
525 ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
526 return ViewManager::_duplicate(aViewManager->_this());
528 return myVisuGen->GetViewManager();
531 SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
533 if(myStudyDocument->GetProperties()->IsLocked()) return SALOMEDS::SObject::_nil();
534 Mutex mt(myMutex,qApp);
535 SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
538 return myVisuGen->ImportTables(theFileName);
541 Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
543 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
544 Mutex mt(myMutex,qApp);
545 aFileInfo.setFile(theFileName);
546 Result_i* pResult = new Result_i(myStudyDocument);
547 if(pResult->Create(theFileName) != NULL)
548 return Result::_duplicate(pResult->_this());
549 return Result::_nil();
551 return myVisuGen->ImportFile(theFileName);
553 Result_ptr VISU_Gen_i::ImportMed(SALOME_MED::FIELD_ptr theField){
554 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMed : "<<myMutex);
556 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
557 Mutex mt(myMutex,qApp);
558 Result_i* pResult = new Result_i(myStudyDocument);
559 if(pResult->Create(theField) != NULL)
560 return Result::_duplicate(pResult->_this());
561 return Result::_nil();
563 return myVisuGen->ImportMed(theField);
565 Result_ptr VISU_Gen_i::ImportMedMesh(SALOME_MED::MESH_ptr theMesh){
566 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedMesh : "<<myMutex);
568 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
569 Mutex mt(myMutex,qApp);
570 Result_i* pResult = new Result_i(myStudyDocument);
571 if(pResult->Create(theMesh) != NULL)
572 return Result::_duplicate(pResult->_this());
573 return Result::_nil();
575 return myVisuGen->ImportMedMesh(theMesh);
578 Result_ptr VISU_Gen_i::ImportMedSupport(SALOME_MED::SUPPORT_ptr theSupport){
579 if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedSupport : "<<myMutex);
581 if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
582 Mutex mt(myMutex,qApp);
583 Result_i* pResult = new Result_i(myStudyDocument);
584 if(pResult->Create(theSupport) != NULL)
585 return Result::_duplicate(pResult->_this());
586 return Result::_nil();
588 return myVisuGen->ImportMedSupport(theSupport);
591 Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity){
592 if(MYDEBUG) MESSAGE("VISU_Gen_i::MeshOnEntity : "<<myMutex);
594 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
595 Mutex mt(myMutex,qApp);
596 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
597 Mesh_i* aPresent = new Mesh_i(pResult);
598 if(aPresent->Create(theMeshName,theEntity) != NULL)
599 return Mesh::_duplicate(aPresent->_this());
602 return myVisuGen->MeshOnEntity(theResult,theMeshName,theEntity);
605 Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName,
606 VISU::Entity theEntity,
607 const char* theFamilyName){
608 if(MYDEBUG) MESSAGE("VISU_Gen_i::FamilyMeshOnEntity : "<<myMutex);
610 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
611 Mutex mt(myMutex,qApp);
612 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
613 Mesh_i* aPresent = new Mesh_i(pResult);
614 if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL)
615 return Mesh::_duplicate(aPresent->_this());
618 return myVisuGen->FamilyMeshOnEntity(theResult,theMeshName,theEntity,theFamilyName);
621 Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, const char* theMeshName,
622 const char* theGroupName){
623 if(MYDEBUG) MESSAGE("VISU_Gen_i::GroupMesh : "<<myMutex);
625 if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
626 Mutex mt(myMutex,qApp);
627 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
628 Mesh_i* aPresent = new Mesh_i(pResult);
629 if(aPresent->Create(theMeshName,theGroupName) != NULL)
630 return Mesh::_duplicate(aPresent->_this());
633 return myVisuGen->GroupMesh(theResult,theMeshName,theGroupName);
636 ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
637 const char* theFieldName, CORBA::Double theIteration){
638 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
640 if(myStudyDocument->GetProperties()->IsLocked()) return ScalarMap::_nil();
641 Mutex mt(myMutex,qApp);
642 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
643 if(ScalarMap_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
644 ScalarMap_i* aPresent = new ScalarMap_i(pResult);
645 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
646 return ScalarMap::_duplicate(aPresent->_this());
648 return ScalarMap::_nil();
650 return myVisuGen->ScalarMapOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
653 DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
654 const char* theFieldName, CORBA::Double theIteration){
655 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
657 if(myStudyDocument->GetProperties()->IsLocked()) return DeformedShape::_nil();
658 Mutex mt(myMutex,qApp);
659 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
660 if(DeformedShape_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
661 DeformedShape_i* aPresent = new DeformedShape_i(pResult);
662 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
663 return DeformedShape::_duplicate(aPresent->_this());
665 return DeformedShape::_nil();
667 return myVisuGen->DeformedShapeOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
670 Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
671 const char* theFieldName, CORBA::Double theIteration){
672 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
674 if(myStudyDocument->GetProperties()->IsLocked()) return Vectors::_nil();
675 Mutex mt(myMutex,qApp);
676 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
677 if(Vectors_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
678 Vectors_i* aPresent = new Vectors_i(pResult);
679 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
680 return Vectors::_duplicate(aPresent->_this());
682 return Vectors::_nil();
684 return myVisuGen->VectorsOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
687 IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
688 const char* theFieldName, CORBA::Double theIteration){
689 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
691 if(myStudyDocument->GetProperties()->IsLocked()) return IsoSurfaces::_nil();
692 Mutex mt(myMutex,qApp);
693 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
694 if(IsoSurfaces_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
695 IsoSurfaces_i* aPresent = new IsoSurfaces_i(pResult);
696 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
697 return IsoSurfaces::_duplicate(aPresent->_this());
699 return IsoSurfaces::_nil();
701 return myVisuGen->IsoSurfacesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
704 StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
705 const char* theFieldName, CORBA::Double theIteration){
706 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
708 if(myStudyDocument->GetProperties()->IsLocked()) return StreamLines::_nil();
709 Mutex mt(myMutex,qApp);
710 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
711 if(StreamLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
712 StreamLines_i* aPresent = new StreamLines_i(pResult);
713 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
714 return StreamLines::_duplicate(aPresent->_this());
716 return StreamLines::_nil();
718 return myVisuGen->StreamLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
721 CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity,
722 const char* theFieldName, CORBA::Double theIteration){
723 if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
725 if(myStudyDocument->GetProperties()->IsLocked()) return CutPlanes::_nil();
726 Mutex mt(myMutex,qApp);
727 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
728 if(CutPlanes_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
729 CutPlanes_i* aPresent = new CutPlanes_i(pResult);
730 if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL)
731 return CutPlanes::_duplicate(aPresent->_this());
733 return CutPlanes::_nil();
735 return myVisuGen->CutPlanesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
738 Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
739 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateTable : "<<myMutex);
741 if(myStudyDocument->GetProperties()->IsLocked()) return Table::_nil();
742 Mutex mt(myMutex,qApp);
743 Table_i* aPresent = new Table_i(myStudyDocument,theTableEntry);
744 if(aPresent->Create() != NULL)
745 return Table::_duplicate(aPresent->_this());
746 return Table::_nil();
748 return myVisuGen->CreateTable(theTableEntry);
751 Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, CORBA::Long theHRow, CORBA::Long theVRow){
752 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateCurve : "<<myMutex);
754 if(myStudyDocument->GetProperties()->IsLocked()) return Curve::_nil();
755 Mutex mt(myMutex,qApp);
756 PortableServer::POA_var aPOA = GetPOA();
757 Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
758 Curve_i* aPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
759 if(aPresent->Create() != NULL) {
760 return Curve::_duplicate(aPresent->_this());
762 return Curve::_nil();
764 return myVisuGen->CreateCurve(theTable,theHRow,theVRow);
766 Container_ptr VISU_Gen_i::CreateContainer(){
767 if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateContainer : "<<myMutex);
769 if(myStudyDocument->GetProperties()->IsLocked()) return Container::_nil();
770 Mutex mt(myMutex,qApp);
771 Container_i* aPresent = new Container_i(myStudyDocument);
772 if(aPresent->Create() != NULL) {
773 return Container::_duplicate(aPresent->_this());
775 return Container::_nil();
777 return myVisuGen->CreateContainer();
780 void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
781 if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : "<<myMutex);
783 Mutex mt(myMutex,qApp);
784 SALOMEDS::Study_var aStudy = theComponent->GetStudy();
785 if(!aStudy->_is_nil()){
786 SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
787 for (int i = 0; itBig->More(); itBig->Next(),i++) {
788 SALOMEDS::SObject_var gotBranch = itBig->Value();
789 if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : itBig->Next() = "<<i);
790 CORBA::Object_var anObj = SObjectToObject(gotBranch);
791 if(CORBA::is_nil(anObj)) continue;
792 Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
793 if(pResult->IsRestored()){ //Try remove its file and directory
794 const QFileInfo& aFileInfo = pResult->GetFileInfo();
795 static QString aCommand;
796 aCommand.sprintf("rm %s",aFileInfo.filePath().latin1());
797 if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
798 aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",aFileInfo.dirPath().latin1());
799 if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
805 myVisuGen->Close(theComponent);
808 char* VISU_Gen_i::ComponentDataType(){
809 return CORBA::string_dup("VISU");
812 bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
813 Result_var aResultObj = Result::_narrow(theIOR);
814 return !(aResultObj->_is_nil());
817 SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
818 SALOMEDS::SObject_ptr theSObject,
819 CORBA::Object_ptr theObject,
821 throw (SALOME::SALOME_Exception)
823 if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
825 Mutex mt(myMutex,qApp);
826 SALOMEDS::SObject_var aResultSO;
827 PortableServer::POA_var aPOA = GetPOA();
828 Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(theObject));
829 if (!aResultObj) return aResultSO._retn();
831 CORBA::String_var anEntry;
832 const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
833 anEntry = strdup(aResultObj->Create(aFileInfo.filePath().latin1())->GetID());
834 // if (CORBA::is_nil(theSObject)) {
835 // anEntry = strdup(aResultObj->Create(SALOMEDS::SObject::_nil(), theName));
837 // if (!theSObject->ReferencedObject(aResultSO))
838 // THROW_SALOME_CORBA_EXCEPTION("Publish error: bad referenced SObject",SALOME::BAD_PARAM);
839 // anEntry = strdup(aResultObj->Create(aResultSO, (const char *)theName));
841 aResultSO = theStudy->FindObjectID(anEntry);
842 return aResultSO._retn();
844 return myVisuGen->PublishInStudy(theStudy, theSObject, theObject, theName);
847 CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
848 if(MYDEBUG) MESSAGE("VISU_Gen_i::CanCopy : "<<myMutex);
850 //Mutex mt(myMutex,qApp);
851 SALOMEDS::GenericAttribute_var anAttr;
852 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
854 CORBA::Object_var anObj = GetORB()->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
855 if (CORBA::is_nil(anObj)) return false;
857 Result_var aResultObj = Result::_narrow(anObj);
858 if (aResultObj->_is_nil()) return false;
865 return myVisuGen->CanCopy(theObject);
868 SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
869 if(MYDEBUG) MESSAGE("VISU_Gen_i::CopyFrom : "<<myMutex);
871 Mutex mt(myMutex,qApp);
874 SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
875 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
877 PortableServer::POA_var aPOA = GetPOA();
879 SALOMEDS::GenericAttribute_var anAttr;
880 if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
881 SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
882 CORBA::Object_var aCorbaObj;
884 aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
886 aStreamFile->length(1);
887 aStreamFile[0] = strdup("E")[0];
888 return aStreamFile._retn();
890 if (CORBA::is_nil(aCorbaObj)) {
894 Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj));
898 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
900 char* aStr = strdup(pStorable->ToString());
901 ofstream stmOut2((string(aTmpDir) + string("copy_persistent")).c_str(),ios::out);
905 Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj));
907 string aStudyPrefix = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + string("_");
909 const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
910 static QString aCommand;
911 aCommand.sprintf("cp %s %s",
912 aFileInfo.filePath().latin1(),
913 (string(aTmpDir)+aStudyPrefix + string(aResultObj->GetName())).c_str());
914 if(system(aCommand) == -1) {
915 if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
920 aSeq[0] = "copy_persistent";
921 aSeq[1] = (aStudyPrefix + string(aResultObj->GetName())).c_str();
924 aSeq[0] = "copy_persistent";
926 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
927 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
929 // Assign an ID = 1 the the type VISU::Result
931 return aStreamFile._retn();
933 return myVisuGen->CopyFrom(theObject, theObjectID);
936 CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
937 // The VISU component can paste only objects copied by VISU component
938 // and with the object type = 1
939 if(MYDEBUG) MESSAGE("VISU_Gen_i::CanPaste : "<<myMutex);
940 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
944 SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
945 CORBA::Long theObjectID,
946 SALOMEDS::SObject_ptr theObject) {
947 if(MYDEBUG) MESSAGE("VISU_Gen_i::PasteInto : "<<myMutex);
949 Mutex mt(myMutex,qApp);
950 SALOMEDS::SObject_var aResultSO;
951 if (theObjectID != 1) return aResultSO._retn();
953 CORBA::String_var aTmpDir = strdup(SALOMEDS_Tool::GetTmpDir());
954 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
956 SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
957 SALOMEDS::Study_var aStudy = theObject->GetStudy();
958 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
959 CORBA::ORB_var anORB = GetORB();
961 ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str());
962 // ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str(),ios::in);
963 stmIn.seekg(0, ios::end);
964 int aLength = stmIn.tellg();
965 stmIn.seekg(0, ios::beg);
966 char* aString = new char[aLength+1];
967 stmIn.read(aString, aLength);
968 aString[aLength] = 0;
969 myIsMultiFile = true;
970 Storable* aStorable = Storable::Create(theObject,aTmpDir.in(),aString);
972 SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
974 aSeqToRm[0] = "copy_persistent";
975 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeqToRm.in(), true);
977 CORBA::String_var aComponentIDString(aComponent->GetID());
978 if (strcmp(aComponentIDString, theObject->GetID()) == 0) {
979 // create the new result SObject
980 aResultSO = aStudyBuilder->NewObject(aComponent);
982 aResultSO = SALOMEDS::SObject::_duplicate(theObject);
984 SALOMEDS::AttributeIOR_var anIOR =
985 SALOMEDS::AttributeIOR::_narrow(aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR"));
986 anIOR->SetValue(aStorable->GetID());
987 return aResultSO._retn();
989 return myVisuGen->PasteInto(theStream,theObjectID,theObject);