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