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