Salome HOME
NRI : Set correct name of MED component for import MED object.
[modules/visu.git] / src / VISU_I / VISU_Gen_i.cc
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : VISU_Gen_i.cc file 
6 //  Author : Alexey Petrov
7 //  Module : VISU
8 //  $Header: 
9
10 using namespace std;
11 #include "VISU_Gen_i.hh"
12 #include "VISU_Result_i.hh"
13 #include "VISU_Convertor.hxx"
14 #include "VISU_PrsObject_i.hh"
15 #include "VISU_ViewManager_i.hh"
16 #include "VISU_TimeAnimation.h"
17 #include "VISU_Table_i.hh"
18
19 #include "HDFascii.hxx"
20 #include "SALOMEDS_Tool.hxx"
21 #include <strstream>    
22 #include <TCollection_AsciiString.hxx>
23 #include <TColStd_SequenceOfAsciiString.hxx>
24 #include <vtkUnstructuredGridReader.h>
25 #include <vtkUnstructuredGridWriter.h>
26
27 #include <omnithread.h> 
28 #include CORBA_SERVER_HEADER(SALOME_Session)
29 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
30
31 #include <qstring.h>
32 #include <qfileinfo.h>
33 static QFileInfo aFileInfo;
34
35 #ifdef DEBUG
36 static int MYDEBUG = 0;
37 #else
38 static int MYDEBUG = 0;
39 #endif
40
41 #include <utilities.h>
42
43 //static int IS_LOADED = (cout<<"\n----------------- VisuEngine_factory is loaded -----------------\n", 1);
44 extern "C" {
45   PortableServer::ObjectId * 
46     VISUEngine_factory(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa, PortableServer::ObjectId * contId,
47                        const char *instanceName, const char *interfaceName) 
48       {
49         if(MYDEBUG) MESSAGE("VisuEngine_factory : "<<interfaceName);
50         VISU::VISU_Gen_i * pVISU_Gen = new VISU::VISU_Gen_i(orb, poa, contId, instanceName, interfaceName);
51         return pVISU_Gen->getId() ;
52       }
53   VISU::VISU_Gen_ptr  
54     GetVisuGen(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, 
55                SALOME_NamingService* theNamingService, QMutex* theMutex)
56       {
57         if(MYDEBUG) MESSAGE("extern \"C\" GetVisuGen");
58         VISU::VISU_Gen_i *aVISU_Gen = new VISU::VISU_Gen_i(theORB,thePOA,theNamingService,theMutex);
59         return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
60         //return aVISU_Gen->_this();
61       }
62 }
63
64 namespace VISU{
65   //===========================================================================
66   //apo - static QMutex VISUMutex;
67   QMutex* Base_i::myMutex = NULL; //apo - &VISUMutex;
68   CORBA::ORB_var Base_i::myOrb;
69   PortableServer::POA_var Base_i::myPOA;
70   SALOME_NamingService* Base_i::myNamingService;
71   SALOME_LifeCycleCORBA* Base_i::myEnginesLifeCycle;
72   VISU_Gen_i* Base_i::myVisuGenImpl;
73   VISU_Gen_var Base_i::GetVisuGenInter() { return myVisuGenImpl->_this();}
74   Base_i::~Base_i() {}
75   char* Base_i::GetID(){ 
76     //CORBA::Object_var anObject = _this();
77     //return CORBA::string_dup(myOrb->object_to_string(anObject));
78     return CORBA::string_dup(myOrb->object_to_string(_this()));
79   }
80   //===========================================================================
81   static int mySCnt = 0;
82   static int myQCnt = 0;
83   static int myIsBatchMode = 0;
84
85   static int QApp_Counter = 0;
86   static int Session_Counter = 0;
87   static int COUNTER = 0;
88
89   Mutex::Mutex(QMutex* theMutex, QApplication* theQApp, int theDelay) :
90     myQApp(theQApp), isQAppLocked(theQApp->locked()), myDelay(theDelay),
91     myMutex(theMutex), isSessionLocked(theMutex->locked())
92   {
93     if(MYDEBUG) MESSAGE("Mutex::Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
94     if(!myIsBatchMode && isQAppLocked) myIsBatchMode++;
95     if(!isSessionLocked && !mySCnt) { myMutex->lock();}; mySCnt++;
96     if(!isQAppLocked && !myQCnt) { 
97       myQApp->lock(); 
98       myQApp->syncX();
99     };
100     myQCnt++;
101   }
102   Mutex::~Mutex(){
103     myQCnt--; 
104     if(!isQAppLocked && !myQCnt) { 
105       myQApp->flushX(); 
106       //if(myDelay > 0)
107         myQApp->processEvents(myDelay+3);
108       myQApp->unlock();
109     }
110     mySCnt--; if(!isSessionLocked && !mySCnt) { myMutex->unlock();}
111     if(MYDEBUG) MESSAGE("Mutex::~Mutex : "<<(!isQAppLocked && !myQCnt)<<" "<<(!isSessionLocked && !mySCnt));
112   }
113   //===========================================================================
114   static Storable::TCallbackMap VisuStoreMap;
115   string Storable::ToString(){
116     ostringstream strOut;
117     Storable::DataToStream( strOut, "myComment", GetComment() );
118     ToStream(strOut);
119     strOut<<ends;
120     if(MYDEBUG) MESSAGE("Storable::ToString - "<<strOut.str());
121     //apo - auto_ptr<char> aRet(strOut.str());
122     return strOut.str(); 
123   }
124
125   void Storable::Registry(const char* theComment, TStorableEngine theEngine)
126     throw(std::logic_error&)
127       {
128         if(!VisuStoreMap.insert(TCallbackMap::value_type(theComment,theEngine)).second){
129           if(MYDEBUG) MESSAGE("Storable::Registry >> dupliacte registring !!!");
130           throw std::logic_error("Storable::Registry >> dupliacte registring !!!");
131         }
132       }
133
134   void RegistryStorable() throw(std::logic_error&) {
135     //Storable::Registry(View3D_i::myComment.c_str(),&View3DRestore);
136     Storable::Registry(Result_i::myComment.c_str(),&ResultRestore);
137     Storable::Registry(Mesh_i::myComment.c_str(),&MeshRestore);
138     Storable::Registry(ScalarMap_i::myComment.c_str(),&ScalarMapRestore);
139     Storable::Registry(DeformedShape_i::myComment.c_str(),&DeformedShapeRestore);
140     Storable::Registry(CutPlanes_i::myComment.c_str(),&CutPlanesRestore);
141     Storable::Registry(CutLines_i::myComment.c_str(),&CutLinesRestore);
142     Storable::Registry(IsoSurfaces_i::myComment.c_str(),&IsoSurfacesRestore);
143     Storable::Registry(StreamLines_i::myComment.c_str(),&StreamLinesRestore);
144     Storable::Registry(Vectors_i::myComment.c_str(),&VectorsRestore);
145     Storable::Registry(Table_i::myComment.c_str(),&TableRestore);
146     Storable::Registry(Curve_i::myComment.c_str(),&CurveRestore);
147     Storable::Registry(Container_i::myComment.c_str(),&ContainerRestore);
148   }
149
150   void Storable::StrToMap(const QString& theStr, VISU::Storable::TRestoringMap& theMap){
151     if(0 && MYDEBUG) MESSAGE("Storable::StrToMap : string="<<theStr);
152     QStringList strList = QStringList::split( ";", theStr, false );
153     for ( int i = 0; i < strList.count(); i++ ) {
154       QString next = strList[ i ];
155       int pos = next.find("=");
156       QString aName, aValue;
157       if ( pos < 0 ) {
158         aName = next.stripWhiteSpace();
159         aValue = "";
160       }
161       else {
162         aName = next.left( pos ).stripWhiteSpace();
163         aValue = next.mid( pos+1 );
164         if ( aValue.isNull() )
165           aValue = "";
166       }
167       if ( !aName.isEmpty() )
168         theMap.insert( TRestoringMap::value_type( aName.latin1(), aValue ) );   
169     }
170   }
171   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const QString& theVal) {
172     QString output = ( !theName.isNull() ? theName : QString("") ) 
173                    + QString( "=" ) 
174                    + ( !theVal.isNull()  ? theVal  : QString("") );
175     theStr<<output.latin1()<<";";
176   }
177   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const int theVal) {
178     QString output = ( !theName.isNull() ? theName : QString("") ) 
179                    + QString( "=" ) 
180                    + QString::number( theVal );
181     theStr<<output.latin1()<<";";
182   }
183   void Storable::DataToStream(ostringstream& theStr, const QString& theName, const double theVal) {
184     QString output = ( !theName.isNull() ? theName : QString("") ) 
185                    + QString( "=" ) 
186                    + QString::number( theVal );
187     theStr<<output.latin1()<<";";
188   }
189   Storable* Storable::Create(SALOMEDS::SObject_ptr theSObject,
190                              const string& thePrefix, const string& theLocalPersistentID) 
191     throw (std::runtime_error&)
192       {
193         try{
194           QString strIn( theLocalPersistentID.c_str() );
195           TRestoringMap aMap;
196           StrToMap(strIn,aMap);
197           //CORBA::String_var aResultID(GetResultSO(theSObject)->GetID());
198           //aMap.insert(TRestoringMap::value_type("ResultID", strdup(aResultID)));
199           bool isExist;
200           QString aComment = VISU::Storable::FindValue(aMap,"myComment",&isExist);
201           if ( isExist ) {
202             TCallbackMap::const_iterator i = VisuStoreMap.find(aComment.latin1());
203             if(MYDEBUG) MESSAGE("Storable::Create - "<<aComment.latin1()<<" "<<(i != VisuStoreMap.end()));
204             if(i == VisuStoreMap.end()) return NULL;
205             return (i->second)(theSObject,thePrefix,aMap);
206           }
207         }catch(std::logic_error& exc){
208           MESSAGE("Follow exception was accured :\n"<<exc.what());
209         }catch(...){
210           MESSAGE("Unknown exception was accured!");
211         }
212         return NULL;
213       }
214   const QString& Storable::FindValue(const TRestoringMap& theMap, const string& theArg, bool* isFind)
215     throw(std::logic_error&)
216       {
217         TRestoringMap::const_iterator i = theMap.find(theArg);
218         if(i == theMap.end()) {
219           if(MYDEBUG) MESSAGE("Storable::Init >> there is no value for "<<theArg);
220           if(isFind != NULL) *isFind = false;
221           //throw std::logic_error(string("Storable::Init >> there is no value for ") + theArg);
222           static QString BAD_VALUE("NULL");
223           return BAD_VALUE;
224         }
225         if(isFind != NULL) *isFind = true;
226         return i->second;
227       }
228
229   //===========================================================================
230   PortableServer::Servant GetServant(CORBA::Object_ptr theObject){
231     PortableServer::POA_ptr aPOA = Base_i::GetPOA();
232     if(CORBA::is_nil(theObject)) 
233       if(MYDEBUG) MESSAGE("GetServant - CORBA::is_nil(theObject)");
234     return aPOA->reference_to_servant(theObject);
235   }
236   //===========================================================================
237   CORBA::Object_var SObjectToObject(SALOMEDS::SObject_ptr theSObject){
238     SALOMEDS::GenericAttribute_var anAttr;
239     CORBA::Object_var anObj;
240     try{
241       if(theSObject->FindAttribute(anAttr, "AttributeIOR")){
242         SALOMEDS::AttributeIOR_var anIOR  = SALOMEDS::AttributeIOR::_narrow(anAttr);
243         CORBA::String_var aValue = anIOR->Value();
244         CORBA::ORB_ptr anORB = Base_i::GetORB();
245         if(strcmp(aValue,"") != 0)
246           anObj = anORB->string_to_object(aValue);
247       }
248     }catch(...){
249       MESSAGE("SObjectToObject - Unknown exception was accured!");
250     }
251     return anObj;
252   }
253   //===========================================================================
254   SALOMEDS::SComponent_var FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument){
255     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
256     if (aSComponent->_is_nil()) {
257       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
258       aStudyBuilder->NewCommand();
259       int aLocked = theStudyDocument->GetProperties()->IsLocked();
260       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
261       aSComponent = aStudyBuilder->NewComponent("VISU"); 
262       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
263       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
264       //NRI      aName->SetValue("Visu");
265
266       CORBA::ORB_var anORB = Base_i::GetORB();
267       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
268       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
269       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
270       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
271       if ( !Comp->_is_nil() ) {
272         aName->SetValue( Comp->componentusername() );
273       }
274
275       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
276       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
277       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
278       
279       VISU_Gen_var aVisuGen = Base_i::GetVisuGenInter();
280       aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
281       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
282       aStudyBuilder->CommitCommand();
283     }
284     return aSComponent;
285   }
286   //===========================================================================
287   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
288                                    const char* theComment, int IsAllLevels)
289     {
290       if(0 && MYDEBUG) MESSAGE("FindEntryWithComment - '"<<theComment<<"'");
291       SALOMEDS::ChildIterator_var anIter = 
292         theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
293       anIter->InitEx(IsAllLevels);
294       SALOMEDS::SObject_var aFieldSO;
295       for(;anIter->More();anIter->Next()) {
296         SALOMEDS::GenericAttribute_var anAttr;
297         if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
298           SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
299           CORBA::String_var aString = aCmnt->Value();
300           string aValue(aString);
301           if (aValue == theComment) {
302             aFieldSO = anIter->Value();
303             aString = aFieldSO->GetID();
304             aValue = aString;
305             return aValue;
306           }
307         }
308       }
309       return "";
310     }
311   //===========================================================================
312   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument, 
313                                const char* theFatherEntry, const char* theRefFatherEntry,
314                                const char* theIOR, const char* theName, 
315                                const char* thePersistentRef, const char* theComment,
316                                CORBA::Boolean theCreateNew)
317     {
318       if(0 && MYDEBUG) 
319         MESSAGE("CreateAttributes - theName = "<<theName<<"; theComment = '"<<theComment<<
320                 "'; theFatherEntry = "<<theFatherEntry);
321       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
322       SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
323       SALOMEDS::SObject_var newObj;
324       if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
325       else newObj = aFather;
326       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
327       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
328       anIOR->SetValue(theIOR); 
329       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
330       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
331       aName->SetValue(theName); 
332       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
333       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
334       aPRef->SetValue(thePersistentRef); 
335       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
336       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
337       aCmnt->SetValue(theComment);
338       if(strcmp(theRefFatherEntry,"") != 0){
339         SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
340         SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
341         aStudyBuilder->Addreference(anObj,newObj);
342       }
343       CORBA::String_var anEntry = newObj->GetID();
344       string aRet(anEntry);
345       if(0 && MYDEBUG) MESSAGE("CreateAttributes - anEntry = "<<aRet<<"; IOR = "<<theIOR);
346       return aRet;
347     }
348   //===========================================================================
349   static SALOMEDS::SComponent_var VisuSComponent;
350   static string VisuTmpDir;
351   static CORBA::Boolean myIsMultiFile;
352   const CORBA::Boolean IsMultifile() { 
353     return myIsMultiFile;
354   }
355   //===========================================================================
356   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr orb,
357                          PortableServer::POA_ptr poa,
358                          PortableServer::ObjectId * contId, 
359                          const char *instanceName, 
360                          const char *interfaceName) :
361     Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
362   {
363     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<myMutex);
364     _thisObj = this ;
365     _id = _poa->activate_object(_thisObj);
366     SALOME_NamingService aNamingService(orb);
367     CORBA::Object_ptr anObject = aNamingService.Resolve("/Kernel/Session");
368     SALOME::Session_var aSession = SALOME::Session::_narrow(anObject);
369     //aSession->GetInterface(); 
370     Engines::Component_var aComponent = aSession->GetVisuComponent();
371     myVisuGen = VISU::VISU_Gen::_narrow(aComponent);
372   }
373
374   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, 
375                          SALOME_NamingService* theNamingService, QMutex* theMutex) :
376     Engines_Component_i()
377   {
378     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
379     Mutex mt(theMutex,qApp);
380     Base_i::myMutex = theMutex;  //apo
381     Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
382     Base_i::myPOA = PortableServer::POA::_duplicate(thePOA);
383     Base_i::myNamingService = theNamingService;
384     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
385     Base_i::myEnginesLifeCycle = &aEnginesLifeCycle;
386     Base_i::myVisuGenImpl = this;
387     RegistryStorable();
388
389     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
390     SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
391     SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
392     if(aListOfOpenStudies->length() > 0) {
393       CORBA::String_var aStudyName = aListOfOpenStudies[0];
394       aFileInfo.setFile(aStudyName.in());
395       myStudyDocument = aStudyManager->GetStudyByName(aFileInfo.baseName());
396     }else{
397       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
398     }
399
400     Base_i::myPOA->activate_object(this);
401   }
402
403   VISU_Gen_i::~VISU_Gen_i(){
404     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
405   }
406   //===========================================================================
407   bool VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
408                       const SALOMEDS::TMPFile & theStream,
409                       const char* theURL,
410                       bool isMultiFile)
411   {
412     if(MYDEBUG) MESSAGE("VISU_Gen_i::Load - myMutex = "<<myMutex);
413     if(myMutex){
414       Mutex mt(myMutex,qApp);
415       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
416       SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder(); 
417       TCollection_AsciiString aTmpDir =
418         isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
419       VisuSComponent = SALOMEDS::SComponent::_duplicate(theComponent);
420       VisuTmpDir = aTmpDir.ToCString();
421       SALOMEDS::ListOfFileNames_var aSeq =
422         SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
423       myIsMultiFile = isMultiFile;
424       return true;
425     }
426     return myVisuGen->Load(theComponent,theStream,theURL,isMultiFile);
427   }
428
429   bool VISU_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
430                              const SALOMEDS::TMPFile & theStream,
431                              const char* theURL,
432                              bool isMultiFile) {
433     return Load(theComponent, theStream, theURL, isMultiFile);
434   }
435
436   char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
437                                            const char* aLocalPersistentID,
438                                            CORBA::Boolean isMultiFile,
439                                            CORBA::Boolean isASCII) {
440     if(myMutex){
441       CORBA::String_var aString("");
442       if(strcmp(aLocalPersistentID,"") != 0) {
443         Mutex mt(myMutex,qApp);
444         Storable* aStorable =
445           Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
446         if(aStorable != NULL) aString = aStorable->GetID();
447       }
448       return aString._retn();
449     }
450     return myVisuGen->LocalPersistentIDToIOR(theSObject, aLocalPersistentID, isMultiFile, isASCII);
451   }
452   //===========================================================================
453   SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
454                                     const char* theURL,
455                                     bool isMultiFile)
456   {
457     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
458     if(myMutex){
459       Mutex mt(myMutex,qApp);
460       CORBA::String_var aString = SALOMEDS_Tool::GetTmpDir();
461       TCollection_AsciiString aTmpDir = isMultiFile? (const Standard_CString)theURL: (const Standard_CString)aString.in();
462       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
463       int aCounter = 0;
464       TColStd_SequenceOfAsciiString aFileNames;
465       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
466       SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
467       for (; itBig->More(); itBig->Next()) {
468         SALOMEDS::SObject_var gotBranch = itBig->Value();
469         CORBA::Object_var anObj = SObjectToObject(gotBranch);
470         if(CORBA::is_nil(anObj)) continue;
471         Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
472         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
473           const QFileInfo& aFileInfo = pResult->GetFileInfo();
474           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
475           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
476           static QString aCommand;
477           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
478           if(system(aCommand) == -1){
479             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
480             continue;
481           }else
482             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
483           TCollection_AsciiString aString(strdup(aFileName.latin1()));
484           aFileNames.Append(aString);
485         }
486       }
487       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
488       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
489       if(aFileNames.Length() > 0){
490         SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
491         aSeq->length(aFileNames.Length());
492         for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
493           aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
494         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
495         if(!isMultiFile)
496           SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
497       }
498       return aStreamFile._retn();
499     }
500     return myVisuGen->Save(theComponent,theURL,isMultiFile);
501   }
502
503   SALOMEDS::TMPFile* VISU_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
504                                       const char* theURL,
505                                       bool isMultiFile) {
506     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
507     if(myMutex){
508       Mutex mt(myMutex,qApp);
509       CORBA::String_var aString = SALOMEDS_Tool::GetTmpDir();
510       TCollection_AsciiString aTmpDir = isMultiFile? (const Standard_CString)theURL: (const Standard_CString)aString.in();
511       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
512       int aCounter = 0;
513       TColStd_SequenceOfAsciiString aFileNames;
514       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
515       SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
516       for (; itBig->More(); itBig->Next()) {
517         SALOMEDS::SObject_var gotBranch = itBig->Value();
518         CORBA::Object_var anObj = SObjectToObject(gotBranch);
519         if(CORBA::is_nil(anObj)) continue;
520         Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
521         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
522           const QFileInfo& aFileInfo = pResult->GetFileInfo();
523           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
524           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
525           static QString aCommand;
526           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
527           
528           if(system(aCommand) == -1){
529             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
530             continue;
531           }else
532             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
533           TCollection_AsciiString aString(strdup(aFileName.latin1()));
534
535           HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir + aString).ToCString()), true);
536
537           aFileNames.Append(aString);
538         }
539       }
540       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
541       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
542       if(aFileNames.Length() > 0){
543         SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
544         aSeq->length(aFileNames.Length());
545         for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
546           aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
547         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
548         if(!isMultiFile)
549           SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
550       }
551       return aStreamFile._retn();
552     }
553     return myVisuGen->Save(theComponent,theURL,isMultiFile);
554   }
555
556   char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
557                                            const char* IORString,
558                                            CORBA::Boolean isMultiFile,
559                                            CORBA::Boolean isASCII) {
560     if(MYDEBUG) MESSAGE("VISU_Gen_i::IORToLocalPersistentID - myMutex = "<<myMutex);
561     if(myMutex){
562       CORBA::String_var aString(IORString);
563       if(strcmp(IORString,"") != 0){ 
564         CORBA::ORB_ptr anORB = GetORB();
565         CORBA::Object_var anObj = anORB->string_to_object(aString);
566         if(!CORBA::is_nil(anObj)){
567           Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj));
568           if(pStorable != NULL){
569             Mutex mt(myMutex,qApp);
570             aString = pStorable->ToString().c_str();
571             return aString._retn();
572           }
573         }
574       }
575       return aString._retn();
576     }
577     return myVisuGen->IORToLocalPersistentID(theSObject, IORString, isMultiFile, isASCII);
578   }
579
580   void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
581     //if(MYDEBUG) MESSAGE("VISU_Gen_i::SetCurrentStudy : "<<(!theStudy->_is_nil()));
582     if(myMutex)
583       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
584     else
585       myVisuGen->SetCurrentStudy(theStudy);
586   }
587   SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
588     if(MYDEBUG) MESSAGE("VISU_Gen_i::GetCurrentStudy : "<<myMutex);
589     if(myMutex)
590       return SALOMEDS::Study::_duplicate(myStudyDocument);
591     return myVisuGen->GetCurrentStudy();
592   }
593   ViewManager_ptr VISU_Gen_i::GetViewManager(){
594     //omni_mutex aMutex;
595     //omni_mutex_lock aMutexLock(aMutex);
596     if(MYDEBUG) MESSAGE("VISU_Gen_i::GetViewManager : "<<myMutex);
597     if(myMutex){
598       Mutex mt(myMutex,qApp);
599       ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
600       return ViewManager::_duplicate(aViewManager->_this());
601     }
602     return myVisuGen->GetViewManager();
603   }
604
605   SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
606     if(myMutex){
607       if(myStudyDocument->GetProperties()->IsLocked()) return SALOMEDS::SObject::_nil();
608       Mutex mt(myMutex,qApp);
609       SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
610       return aRes._retn();
611     }
612     return myVisuGen->ImportTables(theFileName);
613   }
614
615   Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
616     if(myMutex){
617       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
618       Mutex mt(myMutex,qApp);
619       aFileInfo.setFile(theFileName);
620       Result_i* pResult = new Result_i(myStudyDocument);
621       if(pResult->Create(theFileName) != NULL) 
622         return Result::_duplicate(pResult->_this());
623       return Result::_nil();
624     }
625     return myVisuGen->ImportFile(theFileName);
626   }
627
628   Result_ptr VISU_Gen_i::ImportMed(SALOMEDS::SObject_ptr theMedSObject){
629     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMed : "<<myMutex);
630     if(myMutex){
631       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
632       Mutex mt(myMutex,qApp);
633       Result_i* pResult = new Result_i(myStudyDocument);
634       if(pResult->Create(theMedSObject) != NULL) 
635         return Result::_duplicate(pResult->_this());
636       return Result::_nil();
637     }
638     return myVisuGen->ImportMed(theMedSObject);
639   }
640
641   Result_ptr VISU_Gen_i::ImportMedField(SALOME_MED::FIELD_ptr theField){
642     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedField : "<<myMutex);
643     if(myMutex){
644       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
645       Mutex mt(myMutex,qApp);
646       Result_i* pResult = new Result_i(myStudyDocument);
647       if(pResult->Create(theField) != NULL) 
648         return Result::_duplicate(pResult->_this());
649       return Result::_nil();
650     }
651     return myVisuGen->ImportMedField(theField);
652   }
653
654   Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity){
655     if(MYDEBUG) MESSAGE("VISU_Gen_i::MeshOnEntity : "<<myMutex);
656     if(myMutex){
657       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
658       Mutex mt(myMutex,qApp);
659       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
660       Mesh_i* aPresent = new Mesh_i(pResult);
661       if(aPresent->Create(theMeshName,theEntity) != NULL) 
662         return Mesh::_duplicate(aPresent->_this());
663       return Mesh::_nil();
664     }
665     return myVisuGen->MeshOnEntity(theResult,theMeshName,theEntity);
666   }
667
668   Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName, 
669                                           VISU::Entity theEntity, 
670                                           const char* theFamilyName){
671     if(MYDEBUG) MESSAGE("VISU_Gen_i::FamilyMeshOnEntity : "<<myMutex);
672     if(myMutex){
673       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
674       Mutex mt(myMutex,qApp);
675       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
676       Mesh_i* aPresent = new Mesh_i(pResult);
677       if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL) 
678         return Mesh::_duplicate(aPresent->_this());
679       return Mesh::_nil();
680     }
681     return myVisuGen->FamilyMeshOnEntity(theResult,theMeshName,theEntity,theFamilyName);
682   }
683
684   Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, const char* theMeshName, 
685                                  const char* theGroupName){
686     if(MYDEBUG) MESSAGE("VISU_Gen_i::GroupMesh : "<<myMutex);
687     if(myMutex){
688       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
689       Mutex mt(myMutex,qApp);
690       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
691       Mesh_i* aPresent = new Mesh_i(pResult);
692       if(aPresent->Create(theMeshName,theGroupName) != NULL) 
693         return Mesh::_duplicate(aPresent->_this());
694       return Mesh::_nil();
695     }
696     return myVisuGen->GroupMesh(theResult,theMeshName,theGroupName);
697   }
698
699   ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
700                                              const char* theFieldName, CORBA::Double theIteration){
701     if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
702     if(myMutex){
703       if(myStudyDocument->GetProperties()->IsLocked()) return ScalarMap::_nil();
704       Mutex mt(myMutex,qApp);
705       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
706       if(ScalarMap_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
707         ScalarMap_i* aPresent = new ScalarMap_i(pResult);
708         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL){
709           //Base_i::myPOA->activate_object(aPresent);
710           //aPresent->_remove_ref();
711           return aPresent->_this();
712         }
713       }
714       return ScalarMap::_nil();
715     }
716     return myVisuGen->ScalarMapOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
717   }
718
719   DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
720                                                      const char* theFieldName, CORBA::Double theIteration){
721     if(MYDEBUG) MESSAGE("VISU_Gen_i::DeformedShapeOnField : "<<myMutex);
722     if(myMutex){
723       if(myStudyDocument->GetProperties()->IsLocked()) return DeformedShape::_nil();
724       Mutex mt(myMutex,qApp);
725       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
726       if(DeformedShape_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
727         DeformedShape_i* aPresent = new DeformedShape_i(pResult);
728         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
729           return DeformedShape::_duplicate(aPresent->_this());
730       }
731       return DeformedShape::_nil();
732     }
733     return myVisuGen->DeformedShapeOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
734   }
735
736   Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
737                                                      const char* theFieldName, CORBA::Double theIteration){
738     if(MYDEBUG) MESSAGE("VISU_Gen_i::VectorsOnField : "<<myMutex);
739     if(myMutex){
740       if(myStudyDocument->GetProperties()->IsLocked()) return Vectors::_nil();
741       Mutex mt(myMutex,qApp);
742       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
743       if(Vectors_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
744         Vectors_i* aPresent = new Vectors_i(pResult);
745         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
746           return Vectors::_duplicate(aPresent->_this());
747       }
748       return Vectors::_nil();
749     }
750     return myVisuGen->VectorsOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
751   }
752
753   IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
754                                                      const char* theFieldName, CORBA::Double theIteration){
755     if(MYDEBUG) MESSAGE("VISU_Gen_i::IsoSurfacesOnField : "<<myMutex);
756     if(myMutex){
757       if(myStudyDocument->GetProperties()->IsLocked()) return IsoSurfaces::_nil();
758       Mutex mt(myMutex,qApp);
759       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
760       if(IsoSurfaces_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
761         IsoSurfaces_i* aPresent = new IsoSurfaces_i(pResult);
762         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
763           return IsoSurfaces::_duplicate(aPresent->_this());
764       }
765       return IsoSurfaces::_nil();
766     }
767     return myVisuGen->IsoSurfacesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
768   }
769
770   StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
771                                                      const char* theFieldName, CORBA::Double theIteration){
772     if(MYDEBUG) MESSAGE("VISU_Gen_i::StreamLinesOnField : "<<myMutex);
773     if(myMutex){
774       if(myStudyDocument->GetProperties()->IsLocked()) return StreamLines::_nil();
775       Mutex mt(myMutex,qApp);
776       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
777       if(StreamLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
778         StreamLines_i* aPresent = new StreamLines_i(pResult);
779         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
780           return StreamLines::_duplicate(aPresent->_this());
781       }
782       return StreamLines::_nil();
783     }
784     return myVisuGen->StreamLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
785   }
786
787   CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
788                                                      const char* theFieldName, CORBA::Double theIteration){
789     if(MYDEBUG) MESSAGE("VISU_Gen_i::CutPlanesOnField : "<<myMutex);
790     if(myMutex){
791       if(myStudyDocument->GetProperties()->IsLocked()) return CutPlanes::_nil();
792       Mutex mt(myMutex,qApp);
793       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
794       if(CutPlanes_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
795         CutPlanes_i* aPresent = new CutPlanes_i(pResult);
796         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
797           return CutPlanes::_duplicate(aPresent->_this());
798       }
799       return CutPlanes::_nil();
800     }
801     return myVisuGen->CutPlanesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
802   }
803
804   CutLines_ptr VISU_Gen_i::CutLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
805                                             const char* theFieldName, CORBA::Double theIteration){
806     if(MYDEBUG) MESSAGE("VISU_Gen_i::CutLinesOnField : "<<myMutex);
807     if(myMutex){
808       if(myStudyDocument->GetProperties()->IsLocked()) return CutLines::_nil();
809       Mutex mt(myMutex,qApp);
810       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
811       if(CutLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
812         CutLines_i* aPresent = new CutLines_i(pResult);
813         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
814           return CutLines::_duplicate(aPresent->_this());
815       }
816       return CutLines::_nil();
817     }
818     return myVisuGen->CutLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
819   }
820
821   Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
822     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateTable : "<<myMutex);
823     if(myMutex){
824       if(myStudyDocument->GetProperties()->IsLocked()) return Table::_nil();
825       Mutex mt(myMutex,qApp);
826       Table_i* aPresent = new Table_i(myStudyDocument,theTableEntry);
827       if(aPresent->Create() != NULL) 
828         return Table::_duplicate(aPresent->_this());
829       return Table::_nil();
830     }
831     return myVisuGen->CreateTable(theTableEntry);
832   }
833
834   Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, CORBA::Long theHRow, CORBA::Long theVRow){
835     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateCurve : "<<myMutex);
836     if(myMutex){
837       if(myStudyDocument->GetProperties()->IsLocked()) return Curve::_nil();
838       Mutex mt(myMutex,qApp);
839       PortableServer::POA_ptr aPOA = GetPOA();
840       Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
841       Curve_i* aPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
842       if(aPresent->Create() != NULL) { 
843         return Curve::_duplicate(aPresent->_this());
844       }
845       return Curve::_nil();
846     }
847     return myVisuGen->CreateCurve(theTable,theHRow,theVRow);
848   }
849   Container_ptr VISU_Gen_i::CreateContainer(){
850     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateContainer : "<<myMutex);
851     if(myMutex){
852       if(myStudyDocument->GetProperties()->IsLocked()) return Container::_nil();
853       Mutex mt(myMutex,qApp);
854       Container_i* aPresent = new Container_i(myStudyDocument);
855       if(aPresent->Create() != NULL) {
856         return Container::_duplicate(aPresent->_this());
857       }
858       return Container::_nil();
859     }
860     return myVisuGen->CreateContainer();
861   }
862
863   Animation_ptr VISU_Gen_i::CreateAnimation(View3D_ptr theView3D){
864     if(myMutex){
865       if(myStudyDocument->GetProperties()->IsLocked()) return Animation::_nil();
866       Mutex mt(myMutex,qApp);
867       if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateAnimation : "<<myMutex);
868       VISU_TimeAnimation* anAnim = new VISU_TimeAnimation(myStudyDocument,theView3D);
869       return Animation::_duplicate(anAnim->_this());
870     }
871     return myVisuGen->CreateAnimation(theView3D);
872   }
873
874   void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
875     if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : "<<myMutex);
876     if(myMutex){
877       Mutex mt(myMutex,qApp);
878       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
879       if(!aStudy->_is_nil()){
880         SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
881         for (int i = 0; itBig->More(); itBig->Next(),i++) {
882           SALOMEDS::SObject_var gotBranch = itBig->Value();
883           if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : itBig->Next() = "<<i);
884           CORBA::Object_var anObj = SObjectToObject(gotBranch);
885           if(CORBA::is_nil(anObj)) continue;
886           Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
887           if(!pResult) continue;
888           if(pResult->GetSourceId() == Result_i::eRestoredFile){ //Try remove its file and directory
889             const QFileInfo& aFileInfo = pResult->GetFileInfo();
890             static QString aCommand;
891             aCommand.sprintf("rm %s",aFileInfo.filePath().latin1());
892             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
893             aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",aFileInfo.dirPath().latin1());
894             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
895           }
896         }
897       }
898       return;
899     }
900     myVisuGen->Close(theComponent);
901   }
902
903   char* VISU_Gen_i::ComponentDataType(){
904     return CORBA::string_dup("VISU");
905   }
906
907   bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
908     Result_var aResultObj = Result::_narrow(theIOR);
909     return !(aResultObj->_is_nil());
910   }
911
912   SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
913                                                    SALOMEDS::SObject_ptr theSObject,
914                                                    CORBA::Object_ptr theObject,
915                                                    const char* theName) 
916   throw (SALOME::SALOME_Exception) 
917   {
918     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
919     if(myMutex){
920       Mutex mt(myMutex,qApp);
921       SALOMEDS::SObject_var aResultSO;
922       Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject));
923       if (!aResultObj) return aResultSO._retn();
924       const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
925       CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
926       aResultSO = theStudy->FindObjectID(anEntry);
927       return aResultSO._retn();
928     }
929     return myVisuGen->PublishInStudy(theStudy, theSObject, theObject, theName);
930   }
931     
932   CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
933     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanCopy : "<<myMutex);
934     if(myMutex){
935       //Mutex mt(myMutex,qApp);
936       SALOMEDS::GenericAttribute_var anAttr;
937       if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
938       try {
939         SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
940         CORBA::String_var aValue = anIOR->Value();
941         if(strcmp(aValue,"") != 0){
942           CORBA::Object_ptr anObj = GetORB()->string_to_object(aValue);
943           if (!CORBA::is_nil(anObj)){
944             Result_var aResultObj = Result::_narrow(anObj);
945             if(!aResultObj->_is_nil()){
946               Result_i* pResult = dynamic_cast<Result_i*>(GetServant(aResultObj));
947               if(pResult != NULL){
948                 if(abs(pResult->GetSourceId()) > Result_i::eFile) 
949                   if((pResult->GetFileInfo()).filePath() == "MED") return false;
950                 return true;
951               }
952             }
953           }
954         }
955       } catch (...) {
956         MESSAGE("Unknown exception was accured!");
957       }
958       return false;
959     }
960     return myVisuGen->CanCopy(theObject);
961   }
962
963   SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
964     if(MYDEBUG) MESSAGE("VISU_Gen_i::CopyFrom : "<<myMutex);
965     if(myMutex){
966       Mutex mt(myMutex,qApp);
967
968       theObjectID = 0;
969       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
970       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
971
972       PortableServer::POA_ptr aPOA = GetPOA();
973       
974       SALOMEDS::GenericAttribute_var anAttr;
975       if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
976       SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
977       CORBA::Object_var aCorbaObj;
978       try {
979         aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
980       } catch(...) {
981         aStreamFile->length(1);
982         aStreamFile[0] = strdup("E")[0];
983         return aStreamFile._retn();
984       }
985       if (CORBA::is_nil(aCorbaObj)) {
986         return NULL;
987       }
988
989       Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj));
990       if (!pStorable) {
991         return NULL;
992       }
993       CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
994
995       string aStr = pStorable->ToString().c_str();
996       ofstream stmOut2((string(aTmpDir) + string("copy_persistent")).c_str(),ios::out);
997       stmOut2<<aStr<<endl;
998       stmOut2.close();
999
1000       Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj));
1001       if (aResultObj) {
1002         string aFileName = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + "_";
1003         const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
1004         aFileName += aFileInfo.fileName().latin1();
1005         static QString aCommand;
1006         aCommand.sprintf("cp %s %s",
1007                          aFileInfo.filePath().latin1(),
1008                          (string(aTmpDir) + aFileName).c_str());
1009         if(system(aCommand) == -1) {
1010           if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
1011           return NULL;
1012         }
1013
1014         aSeq->length(2);
1015         aSeq[0] = "copy_persistent";
1016         aSeq[1] = aFileName.c_str();
1017       } else {
1018         aSeq->length(1);
1019         aSeq[0] = "copy_persistent";
1020       }
1021       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
1022       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
1023
1024       // Assign an ID = 1 the the type VISU::Result
1025       theObjectID = 1;
1026       return aStreamFile._retn();
1027     }
1028     return myVisuGen->CopyFrom(theObject, theObjectID);
1029   }
1030
1031   CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1032     // The VISU component can paste only objects copied by VISU component
1033     // and with the object type = 1
1034     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanPaste : "<<myMutex);
1035     if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
1036     return true;
1037   }
1038
1039   SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
1040                                               CORBA::Long theObjectID,
1041                                               SALOMEDS::SObject_ptr theObject) {
1042     if(MYDEBUG) MESSAGE("VISU_Gen_i::PasteInto : "<<myMutex);
1043     if(myMutex){
1044       Mutex mt(myMutex,qApp);
1045       SALOMEDS::SObject_var aResultSO;
1046       if (theObjectID != 1) return aResultSO._retn();
1047       
1048       CORBA::String_var aTmpDir = (const char*)SALOMEDS_Tool::GetTmpDir();
1049       SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1050
1051       ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str());
1052 //        ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str(),ios::in);
1053       stmIn.seekg(0, ios::end);
1054       int aLength = stmIn.tellg();
1055       stmIn.seekg(0, ios::beg);
1056       char* aString = new char[aLength+1];
1057       stmIn.read(aString, aLength);
1058       aString[aLength] = 0;
1059       myIsMultiFile = true;
1060       
1061       string aFileName(aTmpDir.in());
1062       if(aSeq->length() > 1) aFileName += aSeq[1].in();
1063       Storable* aStorable = Storable::Create(theObject,aFileName,aString);
1064
1065       SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
1066       aSeqToRm->length(1);
1067       aSeqToRm[0] = "copy_persistent";
1068       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeqToRm.in(), true);
1069
1070       SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
1071       SALOMEDS::Study_var aStudy = theObject->GetStudy();
1072       SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1073       CORBA::String_var aComponentID(aComponent->GetID()), aSObjID(theObject->GetID());
1074       if (strcmp(aComponentID, aSObjID) == 0) //create the new result SObject
1075         aResultSO = aStudyBuilder->NewObject(aComponent);
1076       else 
1077         aResultSO = SALOMEDS::SObject::_duplicate(theObject);
1078       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
1079       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1080       CORBA::String_var anIORValue(aStorable->GetID());
1081       anIOR->SetValue(anIORValue);
1082       return aResultSO._retn();
1083     }
1084     return myVisuGen->PasteInto(theStream,theObjectID,theObject);
1085   }
1086
1087 };