]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Gen_i.cc
Salome HOME
3ec2cab59b3fee9115977b0cec6eae5727d40714
[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
22 #include <strstream>    
23 #include <TCollection_AsciiString.hxx>
24 #include <TColStd_SequenceOfAsciiString.hxx>
25 #include <vtkUnstructuredGridReader.h>
26 #include <vtkUnstructuredGridWriter.h>
27
28 #include <omnithread.h> 
29 #include CORBA_SERVER_HEADER(SALOME_Session)
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       aName->SetValue("Visu");
265
266       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
267       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
268       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
269       
270       VISU_Gen_var aVisuGen = Base_i::GetVisuGenInter();
271       aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
272       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
273       aStudyBuilder->CommitCommand();
274     }
275     return aSComponent;
276   }
277   //===========================================================================
278   string FindEntryWithComment(SALOMEDS::Study_ptr theStudyDocument, const char* theStartEntry,
279                                    const char* theComment, int IsAllLevels)
280     {
281       if(0 && MYDEBUG) MESSAGE("FindEntryWithComment - '"<<theComment<<"'");
282       SALOMEDS::ChildIterator_var anIter = 
283         theStudyDocument->NewChildIterator(theStudyDocument->FindObjectID(theStartEntry));
284       anIter->InitEx(IsAllLevels);
285       SALOMEDS::SObject_var aFieldSO;
286       for(;anIter->More();anIter->Next()) {
287         SALOMEDS::GenericAttribute_var anAttr;
288         if (anIter->Value()->FindAttribute(anAttr,"AttributeComment")) {
289           SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
290           CORBA::String_var aString = aCmnt->Value();
291           string aValue(aString);
292           if (aValue == theComment) {
293             aFieldSO = anIter->Value();
294             aString = aFieldSO->GetID();
295             aValue = aString;
296             return aValue;
297           }
298         }
299       }
300       return "";
301     }
302   //===========================================================================
303   string CreateAttributes(SALOMEDS::Study_ptr theStudyDocument, 
304                                const char* theFatherEntry, const char* theRefFatherEntry,
305                                const char* theIOR, const char* theName, 
306                                const char* thePersistentRef, const char* theComment,
307                                CORBA::Boolean theCreateNew)
308     {
309       if(0 && MYDEBUG) 
310         MESSAGE("CreateAttributes - theName = "<<theName<<"; theComment = '"<<theComment<<
311                 "'; theFatherEntry = "<<theFatherEntry);
312       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
313       SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry);
314       SALOMEDS::SObject_var newObj;
315       if (theCreateNew) newObj = aStudyBuilder->NewObject(aFather);
316       else newObj = aFather;
317       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
318       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
319       anIOR->SetValue(theIOR); 
320       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
321       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
322       aName->SetValue(theName); 
323       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePersistentRef");
324       SALOMEDS::AttributePersistentRef_var aPRef = SALOMEDS::AttributePersistentRef::_narrow(anAttr);
325       aPRef->SetValue(thePersistentRef); 
326       anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeComment");
327       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
328       aCmnt->SetValue(theComment);
329       if(strcmp(theRefFatherEntry,"") != 0){
330         SALOMEDS::SObject_var aRefFather = theStudyDocument->FindObjectID(theRefFatherEntry);
331         SALOMEDS::SObject_var anObj = aStudyBuilder->NewObject(aRefFather);
332         aStudyBuilder->Addreference(anObj,newObj);
333       }
334       CORBA::String_var anEntry = newObj->GetID();
335       string aRet(anEntry);
336       if(0 && MYDEBUG) MESSAGE("CreateAttributes - anEntry = "<<aRet<<"; IOR = "<<theIOR);
337       return aRet;
338     }
339   //===========================================================================
340   static SALOMEDS::SComponent_var VisuSComponent;
341   static string VisuTmpDir;
342   static CORBA::Boolean myIsMultiFile;
343   const CORBA::Boolean IsMultifile() { 
344     return myIsMultiFile;
345   }
346   //===========================================================================
347   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr orb,
348                          PortableServer::POA_ptr poa,
349                          PortableServer::ObjectId * contId, 
350                          const char *instanceName, 
351                          const char *interfaceName) :
352     Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
353   {
354     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<myMutex);
355     _thisObj = this ;
356     _id = _poa->activate_object(_thisObj);
357     SALOME_NamingService aNamingService(orb);
358     CORBA::Object_ptr anObject = aNamingService.Resolve("/Kernel/Session");
359     SALOME::Session_var aSession = SALOME::Session::_narrow(anObject);
360     //aSession->GetInterface(); 
361     Engines::Component_var aComponent = aSession->GetVisuGen();
362     myVisuGen = VISU::VISU_Gen::_narrow(aComponent);
363   }
364
365   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, 
366                          SALOME_NamingService* theNamingService, QMutex* theMutex) :
367     Engines_Component_i()
368   {
369     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
370     Mutex mt(theMutex,qApp);
371     Base_i::myMutex = theMutex;  //apo
372     Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
373     Base_i::myPOA = PortableServer::POA::_duplicate(thePOA);
374     Base_i::myNamingService = theNamingService;
375     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
376     Base_i::myEnginesLifeCycle = &aEnginesLifeCycle;
377     Base_i::myVisuGenImpl = this;
378     RegistryStorable();
379
380     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
381     SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
382     SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
383     if(aListOfOpenStudies->length() > 0) {
384       CORBA::String_var aStudyName = aListOfOpenStudies[0];
385       aFileInfo.setFile(aStudyName.in());
386       myStudyDocument = aStudyManager->GetStudyByName(aFileInfo.baseName());
387     }else{
388       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
389     }
390
391     Base_i::myPOA->activate_object(this);
392   }
393
394   VISU_Gen_i::~VISU_Gen_i(){
395     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
396   }
397   //===========================================================================
398   bool VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
399                       const SALOMEDS::TMPFile & theStream,
400                       const char* theURL,
401                       bool isMultiFile)
402   {
403     if(MYDEBUG) MESSAGE("VISU_Gen_i::Load - myMutex = "<<myMutex);
404     if(myMutex){
405       Mutex mt(myMutex,qApp);
406       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
407       SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder(); 
408       TCollection_AsciiString aTmpDir =
409         isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
410       VisuSComponent = SALOMEDS::SComponent::_duplicate(theComponent);
411       VisuTmpDir = aTmpDir.ToCString();
412       SALOMEDS::ListOfFileNames_var aSeq =
413         SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
414       myIsMultiFile = isMultiFile;
415       return true;
416     }
417     return myVisuGen->Load(theComponent,theStream,theURL,isMultiFile);
418   }
419
420   bool VISU_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
421                              const SALOMEDS::TMPFile & theStream,
422                              const char* theURL,
423                              bool isMultiFile) {
424     return Load(theComponent, theStream, theURL, isMultiFile);
425   }
426
427   char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
428                                            const char* aLocalPersistentID,
429                                            CORBA::Boolean isMultiFile,
430                                            CORBA::Boolean isASCII) {
431     if(myMutex){
432       CORBA::String_var aString("");
433       if(strcmp(aLocalPersistentID,"") != 0) {
434         Mutex mt(myMutex,qApp);
435         Storable* aStorable =
436           Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
437         if(aStorable != NULL) aString = aStorable->GetID();
438       }
439       return aString._retn();
440     }
441     return myVisuGen->LocalPersistentIDToIOR(theSObject, aLocalPersistentID, isMultiFile, isASCII);
442   }
443   //===========================================================================
444   SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
445                                     const char* theURL,
446                                     bool isMultiFile)
447   {
448     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
449     if(myMutex){
450       Mutex mt(myMutex,qApp);
451       CORBA::String_var aString = SALOMEDS_Tool::GetTmpDir();
452       TCollection_AsciiString aTmpDir = isMultiFile? (const Standard_CString)theURL: (const Standard_CString)aString.in();
453       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
454       int aCounter = 0;
455       TColStd_SequenceOfAsciiString aFileNames;
456       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
457       SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
458       for (; itBig->More(); itBig->Next()) {
459         SALOMEDS::SObject_var gotBranch = itBig->Value();
460         CORBA::Object_var anObj = SObjectToObject(gotBranch);
461         if(CORBA::is_nil(anObj)) continue;
462         Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
463         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
464           const QFileInfo& aFileInfo = pResult->GetFileInfo();
465           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
466           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
467           static QString aCommand;
468           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
469           if(system(aCommand) == -1){
470             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
471             continue;
472           }else
473             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
474           TCollection_AsciiString aString(strdup(aFileName.latin1()));
475           aFileNames.Append(aString);
476         }
477       }
478       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
479       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
480       if(aFileNames.Length() > 0){
481         SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
482         aSeq->length(aFileNames.Length());
483         for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
484           aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
485         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
486         if(!isMultiFile)
487           SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
488       }
489       return aStreamFile._retn();
490     }
491     return myVisuGen->Save(theComponent,theURL,isMultiFile);
492   }
493
494   SALOMEDS::TMPFile* VISU_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
495                                       const char* theURL,
496                                       bool isMultiFile) {
497     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
498     if(myMutex){
499       Mutex mt(myMutex,qApp);
500       CORBA::String_var aString = SALOMEDS_Tool::GetTmpDir();
501       TCollection_AsciiString aTmpDir = isMultiFile? (const Standard_CString)theURL: (const Standard_CString)aString.in();
502       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
503       int aCounter = 0;
504       TColStd_SequenceOfAsciiString aFileNames;
505       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
506       SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
507       for (; itBig->More(); itBig->Next()) {
508         SALOMEDS::SObject_var gotBranch = itBig->Value();
509         CORBA::Object_var anObj = SObjectToObject(gotBranch);
510         if(CORBA::is_nil(anObj)) continue;
511         Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
512         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
513           const QFileInfo& aFileInfo = pResult->GetFileInfo();
514           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
515           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
516           static QString aCommand;
517           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
518           
519           if(system(aCommand) == -1){
520             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
521             continue;
522           }else
523             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
524           TCollection_AsciiString aString(strdup(aFileName.latin1()));
525
526           HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir + aString).ToCString()), true);
527
528           aFileNames.Append(aString);
529         }
530       }
531       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
532       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
533       if(aFileNames.Length() > 0){
534         SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
535         aSeq->length(aFileNames.Length());
536         for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
537           aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
538         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
539         if(!isMultiFile)
540           SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
541       }
542       return aStreamFile._retn();
543     }
544     return myVisuGen->Save(theComponent,theURL,isMultiFile);
545   }
546
547   char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
548                                            const char* IORString,
549                                            CORBA::Boolean isMultiFile,
550                                            CORBA::Boolean isASCII) {
551     if(MYDEBUG) MESSAGE("VISU_Gen_i::IORToLocalPersistentID - myMutex = "<<myMutex);
552     if(myMutex){
553       CORBA::String_var aString(IORString);
554       if(strcmp(IORString,"") != 0){ 
555         CORBA::ORB_ptr anORB = GetORB();
556         CORBA::Object_var anObj = anORB->string_to_object(aString);
557         if(!CORBA::is_nil(anObj)){
558           Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj));
559           if(pStorable != NULL){
560             Mutex mt(myMutex,qApp);
561             aString = pStorable->ToString().c_str();
562             return aString._retn();
563           }
564         }
565       }
566       return aString._retn();
567     }
568     return myVisuGen->IORToLocalPersistentID(theSObject, IORString, isMultiFile, isASCII);
569   }
570
571   void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
572     //if(MYDEBUG) MESSAGE("VISU_Gen_i::SetCurrentStudy : "<<(!theStudy->_is_nil()));
573     if(myMutex)
574       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
575     else
576       myVisuGen->SetCurrentStudy(theStudy);
577   }
578   SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
579     if(MYDEBUG) MESSAGE("VISU_Gen_i::GetCurrentStudy : "<<myMutex);
580     if(myMutex)
581       return SALOMEDS::Study::_duplicate(myStudyDocument);
582     return myVisuGen->GetCurrentStudy();
583   }
584   ViewManager_ptr VISU_Gen_i::GetViewManager(){
585     //omni_mutex aMutex;
586     //omni_mutex_lock aMutexLock(aMutex);
587     if(MYDEBUG) MESSAGE("VISU_Gen_i::GetViewManager : "<<myMutex);
588     if(myMutex){
589       Mutex mt(myMutex,qApp);
590       ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
591       return ViewManager::_duplicate(aViewManager->_this());
592     }
593     return myVisuGen->GetViewManager();
594   }
595
596   SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
597     if(myMutex){
598       if(myStudyDocument->GetProperties()->IsLocked()) return SALOMEDS::SObject::_nil();
599       Mutex mt(myMutex,qApp);
600       SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
601       return aRes._retn();
602     }
603     return myVisuGen->ImportTables(theFileName);
604   }
605
606   Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
607     if(myMutex){
608       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
609       Mutex mt(myMutex,qApp);
610       aFileInfo.setFile(theFileName);
611       Result_i* pResult = new Result_i(myStudyDocument);
612       if(pResult->Create(theFileName) != NULL) 
613         return Result::_duplicate(pResult->_this());
614       return Result::_nil();
615     }
616     return myVisuGen->ImportFile(theFileName);
617   }
618
619   Result_ptr VISU_Gen_i::ImportMed(SALOMEDS::SObject_ptr theMedSObject){
620     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMed : "<<myMutex);
621     if(myMutex){
622       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
623       Mutex mt(myMutex,qApp);
624       Result_i* pResult = new Result_i(myStudyDocument);
625       if(pResult->Create(theMedSObject) != NULL) 
626         return Result::_duplicate(pResult->_this());
627       return Result::_nil();
628     }
629     return myVisuGen->ImportMed(theMedSObject);
630   }
631
632   Result_ptr VISU_Gen_i::ImportMedField(SALOME_MED::FIELD_ptr theField){
633     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedField : "<<myMutex);
634     if(myMutex){
635       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
636       Mutex mt(myMutex,qApp);
637       Result_i* pResult = new Result_i(myStudyDocument);
638       if(pResult->Create(theField) != NULL) 
639         return Result::_duplicate(pResult->_this());
640       return Result::_nil();
641     }
642     return myVisuGen->ImportMedField(theField);
643   }
644
645   Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity){
646     if(MYDEBUG) MESSAGE("VISU_Gen_i::MeshOnEntity : "<<myMutex);
647     if(myMutex){
648       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
649       Mutex mt(myMutex,qApp);
650       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
651       Mesh_i* aPresent = new Mesh_i(pResult);
652       if(aPresent->Create(theMeshName,theEntity) != NULL) 
653         return Mesh::_duplicate(aPresent->_this());
654       return Mesh::_nil();
655     }
656     return myVisuGen->MeshOnEntity(theResult,theMeshName,theEntity);
657   }
658
659   Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName, 
660                                           VISU::Entity theEntity, 
661                                           const char* theFamilyName){
662     if(MYDEBUG) MESSAGE("VISU_Gen_i::FamilyMeshOnEntity : "<<myMutex);
663     if(myMutex){
664       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
665       Mutex mt(myMutex,qApp);
666       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
667       Mesh_i* aPresent = new Mesh_i(pResult);
668       if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL) 
669         return Mesh::_duplicate(aPresent->_this());
670       return Mesh::_nil();
671     }
672     return myVisuGen->FamilyMeshOnEntity(theResult,theMeshName,theEntity,theFamilyName);
673   }
674
675   Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, const char* theMeshName, 
676                                  const char* theGroupName){
677     if(MYDEBUG) MESSAGE("VISU_Gen_i::GroupMesh : "<<myMutex);
678     if(myMutex){
679       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
680       Mutex mt(myMutex,qApp);
681       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
682       Mesh_i* aPresent = new Mesh_i(pResult);
683       if(aPresent->Create(theMeshName,theGroupName) != NULL) 
684         return Mesh::_duplicate(aPresent->_this());
685       return Mesh::_nil();
686     }
687     return myVisuGen->GroupMesh(theResult,theMeshName,theGroupName);
688   }
689
690   ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
691                                              const char* theFieldName, CORBA::Double theIteration){
692     if(MYDEBUG) MESSAGE("VISU_Gen_i::ScalarMapOnField : "<<myMutex);
693     if(myMutex){
694       if(myStudyDocument->GetProperties()->IsLocked()) return ScalarMap::_nil();
695       Mutex mt(myMutex,qApp);
696       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
697       if(ScalarMap_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
698         ScalarMap_i* aPresent = new ScalarMap_i(pResult);
699         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL){
700           //Base_i::myPOA->activate_object(aPresent);
701           //aPresent->_remove_ref();
702           return aPresent->_this();
703         }
704       }
705       return ScalarMap::_nil();
706     }
707     return myVisuGen->ScalarMapOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
708   }
709
710   DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
711                                                      const char* theFieldName, CORBA::Double theIteration){
712     if(MYDEBUG) MESSAGE("VISU_Gen_i::DeformedShapeOnField : "<<myMutex);
713     if(myMutex){
714       if(myStudyDocument->GetProperties()->IsLocked()) return DeformedShape::_nil();
715       Mutex mt(myMutex,qApp);
716       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
717       if(DeformedShape_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
718         DeformedShape_i* aPresent = new DeformedShape_i(pResult);
719         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
720           return DeformedShape::_duplicate(aPresent->_this());
721       }
722       return DeformedShape::_nil();
723     }
724     return myVisuGen->DeformedShapeOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
725   }
726
727   Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
728                                                      const char* theFieldName, CORBA::Double theIteration){
729     if(MYDEBUG) MESSAGE("VISU_Gen_i::VectorsOnField : "<<myMutex);
730     if(myMutex){
731       if(myStudyDocument->GetProperties()->IsLocked()) return Vectors::_nil();
732       Mutex mt(myMutex,qApp);
733       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
734       if(Vectors_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
735         Vectors_i* aPresent = new Vectors_i(pResult);
736         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
737           return Vectors::_duplicate(aPresent->_this());
738       }
739       return Vectors::_nil();
740     }
741     return myVisuGen->VectorsOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
742   }
743
744   IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
745                                                      const char* theFieldName, CORBA::Double theIteration){
746     if(MYDEBUG) MESSAGE("VISU_Gen_i::IsoSurfacesOnField : "<<myMutex);
747     if(myMutex){
748       if(myStudyDocument->GetProperties()->IsLocked()) return IsoSurfaces::_nil();
749       Mutex mt(myMutex,qApp);
750       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
751       if(IsoSurfaces_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
752         IsoSurfaces_i* aPresent = new IsoSurfaces_i(pResult);
753         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
754           return IsoSurfaces::_duplicate(aPresent->_this());
755       }
756       return IsoSurfaces::_nil();
757     }
758     return myVisuGen->IsoSurfacesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
759   }
760
761   StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
762                                                      const char* theFieldName, CORBA::Double theIteration){
763     if(MYDEBUG) MESSAGE("VISU_Gen_i::StreamLinesOnField : "<<myMutex);
764     if(myMutex){
765       if(myStudyDocument->GetProperties()->IsLocked()) return StreamLines::_nil();
766       Mutex mt(myMutex,qApp);
767       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
768       if(StreamLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
769         StreamLines_i* aPresent = new StreamLines_i(pResult);
770         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
771           return StreamLines::_duplicate(aPresent->_this());
772       }
773       return StreamLines::_nil();
774     }
775     return myVisuGen->StreamLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
776   }
777
778   CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
779                                                      const char* theFieldName, CORBA::Double theIteration){
780     if(MYDEBUG) MESSAGE("VISU_Gen_i::CutPlanesOnField : "<<myMutex);
781     if(myMutex){
782       if(myStudyDocument->GetProperties()->IsLocked()) return CutPlanes::_nil();
783       Mutex mt(myMutex,qApp);
784       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
785       if(CutPlanes_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
786         CutPlanes_i* aPresent = new CutPlanes_i(pResult);
787         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
788           return CutPlanes::_duplicate(aPresent->_this());
789       }
790       return CutPlanes::_nil();
791     }
792     return myVisuGen->CutPlanesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
793   }
794
795   CutLines_ptr VISU_Gen_i::CutLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
796                                             const char* theFieldName, CORBA::Double theIteration){
797     if(MYDEBUG) MESSAGE("VISU_Gen_i::CutLinesOnField : "<<myMutex);
798     if(myMutex){
799       if(myStudyDocument->GetProperties()->IsLocked()) return CutLines::_nil();
800       Mutex mt(myMutex,qApp);
801       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
802       if(CutLines_i::IsPossible(pResult,theMeshName,theEntity,theFieldName,theIteration)){
803         CutLines_i* aPresent = new CutLines_i(pResult);
804         if(aPresent->Create(theMeshName,theEntity,theFieldName,theIteration) != NULL) 
805           return CutLines::_duplicate(aPresent->_this());
806       }
807       return CutLines::_nil();
808     }
809     return myVisuGen->CutLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
810   }
811
812   Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
813     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateTable : "<<myMutex);
814     if(myMutex){
815       if(myStudyDocument->GetProperties()->IsLocked()) return Table::_nil();
816       Mutex mt(myMutex,qApp);
817       Table_i* aPresent = new Table_i(myStudyDocument,theTableEntry);
818       if(aPresent->Create() != NULL) 
819         return Table::_duplicate(aPresent->_this());
820       return Table::_nil();
821     }
822     return myVisuGen->CreateTable(theTableEntry);
823   }
824
825   Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, CORBA::Long theHRow, CORBA::Long theVRow){
826     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateCurve : "<<myMutex);
827     if(myMutex){
828       if(myStudyDocument->GetProperties()->IsLocked()) return Curve::_nil();
829       Mutex mt(myMutex,qApp);
830       PortableServer::POA_ptr aPOA = GetPOA();
831       Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
832       Curve_i* aPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
833       if(aPresent->Create() != NULL) { 
834         return Curve::_duplicate(aPresent->_this());
835       }
836       return Curve::_nil();
837     }
838     return myVisuGen->CreateCurve(theTable,theHRow,theVRow);
839   }
840   Container_ptr VISU_Gen_i::CreateContainer(){
841     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateContainer : "<<myMutex);
842     if(myMutex){
843       if(myStudyDocument->GetProperties()->IsLocked()) return Container::_nil();
844       Mutex mt(myMutex,qApp);
845       Container_i* aPresent = new Container_i(myStudyDocument);
846       if(aPresent->Create() != NULL) {
847         return Container::_duplicate(aPresent->_this());
848       }
849       return Container::_nil();
850     }
851     return myVisuGen->CreateContainer();
852   }
853
854   Animation_ptr VISU_Gen_i::CreateAnimation(View3D_ptr theView3D){
855     if(myMutex){
856       if(myStudyDocument->GetProperties()->IsLocked()) return Animation::_nil();
857       Mutex mt(myMutex,qApp);
858       if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateAnimation : "<<myMutex);
859       VISU_TimeAnimation* anAnim = new VISU_TimeAnimation(myStudyDocument,theView3D);
860       return Animation::_duplicate(anAnim->_this());
861     }
862     return myVisuGen->CreateAnimation(theView3D);
863   }
864
865   void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
866     if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : "<<myMutex);
867     if(myMutex){
868       Mutex mt(myMutex,qApp);
869       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
870       if(!aStudy->_is_nil()){
871         SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
872         for (int i = 0; itBig->More(); itBig->Next(),i++) {
873           SALOMEDS::SObject_var gotBranch = itBig->Value();
874           if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : itBig->Next() = "<<i);
875           CORBA::Object_var anObj = SObjectToObject(gotBranch);
876           if(CORBA::is_nil(anObj)) continue;
877           Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
878           if(!pResult) continue;
879           if(pResult->GetSourceId() == Result_i::eRestoredFile){ //Try remove its file and directory
880             const QFileInfo& aFileInfo = pResult->GetFileInfo();
881             static QString aCommand;
882             aCommand.sprintf("rm %s",aFileInfo.filePath().latin1());
883             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
884             aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",aFileInfo.dirPath().latin1());
885             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
886           }
887         }
888       }
889       return;
890     }
891     myVisuGen->Close(theComponent);
892   }
893
894   char* VISU_Gen_i::ComponentDataType(){
895     return CORBA::string_dup("VISU");
896   }
897
898   bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
899     Result_var aResultObj = Result::_narrow(theIOR);
900     return !(aResultObj->_is_nil());
901   }
902
903   SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
904                                                    SALOMEDS::SObject_ptr theSObject,
905                                                    CORBA::Object_ptr theObject,
906                                                    const char* theName) 
907   throw (SALOME::SALOME_Exception) 
908   {
909     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
910     if(myMutex){
911       Mutex mt(myMutex,qApp);
912       SALOMEDS::SObject_var aResultSO;
913       Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject));
914       if (!aResultObj) return aResultSO._retn();
915       const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
916       CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
917       aResultSO = theStudy->FindObjectID(anEntry);
918       return aResultSO._retn();
919     }
920     return myVisuGen->PublishInStudy(theStudy, theSObject, theObject, theName);
921   }
922     
923   CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
924     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanCopy : "<<myMutex);
925     if(myMutex){
926       //Mutex mt(myMutex,qApp);
927       SALOMEDS::GenericAttribute_var anAttr;
928       if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
929       try {
930         SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
931         CORBA::String_var aValue = anIOR->Value();
932         if(strcmp(aValue,"") != 0){
933           CORBA::Object_ptr anObj = GetORB()->string_to_object(aValue);
934           if (!CORBA::is_nil(anObj)){
935             Result_var aResultObj = Result::_narrow(anObj);
936             if(!aResultObj->_is_nil()){
937               Result_i* pResult = dynamic_cast<Result_i*>(GetServant(aResultObj));
938               if(pResult != NULL){
939                 if(abs(pResult->GetSourceId()) > Result_i::eFile) 
940                   if((pResult->GetFileInfo()).filePath() == "Med") return false;
941                 return true;
942               }
943             }
944           }
945         }
946       } catch (...) {
947         MESSAGE("Unknown exception was accured!");
948       }
949       return false;
950     }
951     return myVisuGen->CanCopy(theObject);
952   }
953
954   SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
955     if(MYDEBUG) MESSAGE("VISU_Gen_i::CopyFrom : "<<myMutex);
956     if(myMutex){
957       Mutex mt(myMutex,qApp);
958
959       theObjectID = 0;
960       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
961       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
962
963       PortableServer::POA_ptr aPOA = GetPOA();
964       
965       SALOMEDS::GenericAttribute_var anAttr;
966       if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
967       SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
968       CORBA::Object_var aCorbaObj;
969       try {
970         aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
971       } catch(...) {
972         aStreamFile->length(1);
973         aStreamFile[0] = strdup("E")[0];
974         return aStreamFile._retn();
975       }
976       if (CORBA::is_nil(aCorbaObj)) {
977         return NULL;
978       }
979
980       Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj));
981       if (!pStorable) {
982         return NULL;
983       }
984       CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
985
986       string aStr = pStorable->ToString().c_str();
987       ofstream stmOut2((string(aTmpDir) + string("copy_persistent")).c_str(),ios::out);
988       stmOut2<<aStr<<endl;
989       stmOut2.close();
990
991       Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj));
992       if (aResultObj) {
993         string aFileName = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + "_";
994         const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
995         aFileName += aFileInfo.fileName().latin1();
996         static QString aCommand;
997         aCommand.sprintf("cp %s %s",
998                          aFileInfo.filePath().latin1(),
999                          (string(aTmpDir) + aFileName).c_str());
1000         if(system(aCommand) == -1) {
1001           if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
1002           return NULL;
1003         }
1004
1005         aSeq->length(2);
1006         aSeq[0] = "copy_persistent";
1007         aSeq[1] = aFileName.c_str();
1008       } else {
1009         aSeq->length(1);
1010         aSeq[0] = "copy_persistent";
1011       }
1012       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
1013       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
1014
1015       // Assign an ID = 1 the the type VISU::Result
1016       theObjectID = 1;
1017       return aStreamFile._retn();
1018     }
1019     return myVisuGen->CopyFrom(theObject, theObjectID);
1020   }
1021
1022   CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1023     // The VISU component can paste only objects copied by VISU component
1024     // and with the object type = 1
1025     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanPaste : "<<myMutex);
1026     if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
1027     return true;
1028   }
1029
1030   SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
1031                                               CORBA::Long theObjectID,
1032                                               SALOMEDS::SObject_ptr theObject) {
1033     if(MYDEBUG) MESSAGE("VISU_Gen_i::PasteInto : "<<myMutex);
1034     if(myMutex){
1035       Mutex mt(myMutex,qApp);
1036       SALOMEDS::SObject_var aResultSO;
1037       if (theObjectID != 1) return aResultSO._retn();
1038       
1039       CORBA::String_var aTmpDir = (const char*)SALOMEDS_Tool::GetTmpDir();
1040       SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1041
1042       ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str());
1043 //        ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str(),ios::in);
1044       stmIn.seekg(0, ios::end);
1045       int aLength = stmIn.tellg();
1046       stmIn.seekg(0, ios::beg);
1047       char* aString = new char[aLength+1];
1048       stmIn.read(aString, aLength);
1049       aString[aLength] = 0;
1050       myIsMultiFile = true;
1051       
1052       string aFileName(aTmpDir.in());
1053       if(aSeq->length() > 1) aFileName += aSeq[1].in();
1054       Storable* aStorable = Storable::Create(theObject,aFileName,aString);
1055
1056       SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
1057       aSeqToRm->length(1);
1058       aSeqToRm[0] = "copy_persistent";
1059       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeqToRm.in(), true);
1060
1061       SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
1062       SALOMEDS::Study_var aStudy = theObject->GetStudy();
1063       SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1064       CORBA::String_var aComponentID(aComponent->GetID()), aSObjID(theObject->GetID());
1065       if (strcmp(aComponentID, aSObjID) == 0) //create the new result SObject
1066         aResultSO = aStudyBuilder->NewObject(aComponent);
1067       else 
1068         aResultSO = SALOMEDS::SObject::_duplicate(theObject);
1069       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
1070       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1071       CORBA::String_var anIORValue(aStorable->GetID());
1072       anIOR->SetValue(anIORValue);
1073       return aResultSO._retn();
1074     }
1075     return myVisuGen->PasteInto(theStream,theObjectID,theObject);
1076   }
1077
1078 };