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