]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Gen_i.cc
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / VISU_I / VISU_Gen_i.cc
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //  File   : VISU_Gen_i.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_Gen_i.hh"
28 #include "VISU_Result_i.hh"
29 #include "VISU_PrsObject_i.hh"
30 #include "VISU_ViewManager_i.hh"
31
32 #include "VISU_Prs3d_i.hh"
33 #include "VISU_Mesh_i.hh"
34 #include "VISU_Table_i.hh"
35 #include "VISU_TimeAnimation.h"
36
37 #include "VISU_ColoredPrs3dFactory.hh"
38 #include "VISU_ColoredPrs3dCache_i.hh"
39 #include "VISU_ColoredPrs3dHolder_i.hh"
40
41 #include "VISU_Actor.h"
42
43 #include "HDFascii.hxx"
44 #include "SALOMEDS_Tool.hxx"
45
46 #include "SALOMEDSClient_AttributeName.hxx"
47 #include "SALOMEDSClient_AttributePixMap.hxx"
48
49 #include "SUIT_Session.h"
50 #include "SalomeApp_Study.h"
51 #include "SalomeApp_Application.h"
52 #include "LightApp_SelectionMgr.h"
53 #include "SVTK_ViewModel.h"
54 #include "SVTK_ViewWindow.h"
55 #include "SALOME_Event.hxx"
56 #include "SALOME_ListIO.hxx"
57 #include "SALOME_ListIteratorOfListIO.hxx"
58
59 #include "utilities.h"
60
61 // IDL Headers
62 #include <omnithread.h>
63 #include CORBA_SERVER_HEADER(SALOME_Session)
64 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
65 #include CORBA_SERVER_HEADER(MED_Gen)
66
67 // QT Includes
68 #include <qdir.h>
69 #include <qfileinfo.h>
70
71 // VTK Includes
72 #include <vtkRenderer.h>
73 #include <vtkActorCollection.h>
74
75 // OCCT Includes
76 #include <TCollection_AsciiString.hxx>
77 #include <TColStd_SequenceOfAsciiString.hxx>
78
79 #include "Utils_ExceptHandlers.hxx"
80 UNEXPECT_CATCH(SalomeException, SALOME::SALOME_Exception);
81
82 // STL Includes
83 #include <strstream>
84
85 #include <boost/filesystem/path.hpp>
86 #include <boost/filesystem/operations.hpp>
87 namespace filesystem = boost::filesystem;
88
89
90 #ifdef _DEBUG_
91 static int MYDEBUG = 0;
92 #else
93 static int MYDEBUG = 0;
94 #endif
95
96 extern "C" 
97 VISU_I_EXPORT VISU::VISU_Gen_ptr
98 GetImpl(CORBA::ORB_ptr theORB,
99         PortableServer::POA_ptr thePOA,
100         SALOME_NamingService* theNamingService,
101         QMutex* theMutex)
102 {
103   if(MYDEBUG) MESSAGE("extern 'C' GetImpl");
104   VISU::VISU_Gen_i *aVISU_Gen = new VISU::VISU_Gen_i(theORB,thePOA,theNamingService,theMutex);
105   //return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
106   return aVISU_Gen->_this();
107 }
108
109 namespace VISU
110 {
111   //----------------------------------------------------------------------------
112   static std::string VISU_TMP_DIR;
113
114   static CORBA::Boolean myIsMultiFile;
115   const CORBA::Boolean IsMultiFile() 
116   { 
117     return myIsMultiFile;
118   }
119
120   //----------------------------------------------------------------------------
121   _PTR(SComponent) 
122   ClientFindOrCreateVisuComponent (_PTR(Study) theStudyDocument)
123   {
124     _PTR(SComponent) aSComponent = theStudyDocument->FindComponent("VISU");
125     if (!aSComponent) {
126       _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
127       aStudyBuilder->NewCommand();
128       int aLocked = theStudyDocument->GetProperties()->IsLocked();
129       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
130       aSComponent = aStudyBuilder->NewComponent("VISU");
131       _PTR(GenericAttribute) anAttr =
132         aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
133       _PTR(AttributeName) aName (anAttr);
134
135       CORBA::ORB_var anORB = Base_i::GetORB();
136       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
137       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
138       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue =
139         SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
140       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
141       if (!Comp->_is_nil()) {
142         aName->SetValue(Comp->componentusername());
143       }
144
145       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
146       _PTR(AttributePixMap) aPixmap (anAttr);
147       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
148
149       VISU_Gen_var aVisuGen = Base_i::GetVisuGenImpl()->_this();
150       aStudyBuilder->DefineComponentInstance(aSComponent, aVisuGen->GetID());
151       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
152       aStudyBuilder->CommitCommand();
153     }
154     return aSComponent;
155   }
156
157
158   //----------------------------------------------------------------------------
159   SALOMEDS::SComponent_var 
160   FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument)
161   {
162     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
163     if (aSComponent->_is_nil()) {
164       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
165       aStudyBuilder->NewCommand();
166       int aLocked = theStudyDocument->GetProperties()->IsLocked();
167       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
168       aSComponent = aStudyBuilder->NewComponent("VISU");
169       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
170       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
171
172       //NRI      aName->SetValue("Visu");
173       CORBA::ORB_var anORB = Base_i::GetORB();
174       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
175       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
176       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
177       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
178       if ( !Comp->_is_nil() ) {
179         aName->SetValue( Comp->componentusername() );
180       }
181
182       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
183       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
184       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
185
186       VISU_Gen_var aVisuGen = Base_i::GetVisuGenImpl()->_this();
187       aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
188       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
189       aStudyBuilder->CommitCommand();
190     }
191     return aSComponent;
192   }
193
194
195   //----------------------------------------------------------------------------
196   void
197   RegistryStorable() 
198   {
199     Storable::RegistryStorableEngine(Result_i::myComment.c_str(),&(Result_i::StorableEngine));
200
201     Storable::RegistryStorableEngine(Mesh_i::myComment.c_str(),&(Mesh_i::StorableEngine));
202
203     Storable::RegistryStorableEngine(ScalarMap_i::myComment.c_str(),&(StorableEngine<ScalarMap_i>));
204     Storable::RegistryStorableEngine(GaussPoints_i::myComment.c_str(),&(StorableEngine<GaussPoints_i>));
205     Storable::RegistryStorableEngine(DeformedShape_i::myComment.c_str(),&(StorableEngine<DeformedShape_i>));
206     Storable::RegistryStorableEngine(CutPlanes_i::myComment.c_str(),&(StorableEngine<CutPlanes_i>));
207     Storable::RegistryStorableEngine(CutLines_i::myComment.c_str(),&(StorableEngine<CutLines_i>));
208     Storable::RegistryStorableEngine(IsoSurfaces_i::myComment.c_str(),&(StorableEngine<IsoSurfaces_i>));
209     Storable::RegistryStorableEngine(StreamLines_i::myComment.c_str(),&(StorableEngine<StreamLines_i>));
210     Storable::RegistryStorableEngine(Plot3D_i::myComment.c_str(),&(StorableEngine<Plot3D_i>));
211     Storable::RegistryStorableEngine(Vectors_i::myComment.c_str(),&(StorableEngine<Vectors_i>));
212     Storable::RegistryStorableEngine(ScalarMapOnDeformedShape_i::myComment.c_str(),&(StorableEngine<ScalarMapOnDeformedShape_i>));
213
214     Storable::RegistryStorableEngine(ColoredPrs3dHolder_i::myComment.c_str(),&(ColoredPrs3dHolder_i::StorableEngine));
215     Storable::RegistryStorableEngine(ColoredPrs3dCache_i::myComment.c_str(),&(ColoredPrs3dCache_i::StorableEngine));
216
217     Storable::RegistryStorableEngine(Table_i::myComment.c_str(),&(Table_i::StorableEngine));
218     Storable::RegistryStorableEngine(Curve_i::myComment.c_str(),&(Curve_i::StorableEngine));
219     Storable::RegistryStorableEngine(Container_i::myComment.c_str(),&(Container_i::StorableEngine));
220   }
221
222
223   //----------------------------------------------------------------------------
224   SALOMEDS::ListOfFileNames*
225   GetListOfFileNames(const Result_i::TFileNames& theFileNames)
226   {
227     SALOMEDS::ListOfFileNames_var aListOfFileNames = new SALOMEDS::ListOfFileNames;
228     if(!theFileNames.empty()){
229       aListOfFileNames->length(theFileNames.size());
230       for(int aCounter = theFileNames.size(); aCounter > 0; aCounter--)
231         aListOfFileNames[aCounter-1] = theFileNames[aCounter-1].c_str();
232     }
233     return aListOfFileNames._retn();
234   }
235
236
237   //----------------------------------------------------------------------------
238   VISU_Gen_i
239   ::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
240                SALOME_NamingService* theNamingService, QMutex* theMutex) :
241     Engines_Component_i()
242   {
243     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
244     Base_i::myMutex = theMutex;  //apo
245     Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
246     Base_i::myPoa = PortableServer::POA::_duplicate(thePOA);
247     Base_i::myNamingService = theNamingService;
248     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
249     Base_i::myEnginesLifeCycle = &aEnginesLifeCycle;
250     Base_i::myVisuGenImpl = this;
251     RegistryStorable();
252
253     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
254     SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
255     SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
256     if(aListOfOpenStudies->length() > 0) {
257       CORBA::String_var aStudyName = aListOfOpenStudies[0];
258       //aFileInfo.setFile(aStudyName.in());
259       myStudyDocument = aStudyManager->GetStudyByName(aStudyName/*aFileInfo.baseName()*/);
260     }else
261       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
262   }
263
264
265   //----------------------------------------------------------------------------
266   Prs3d_ptr
267   VISU_Gen_i
268   ::CreatePrs3d(VISUType theType,
269                 SALOMEDS::Study_ptr theStudy)
270   {
271     if(ColoredPrs3d_i* aPrs3d = CreatePrs3d_i(theType, theStudy, ColoredPrs3d_i::EPublishIndependently))
272       return aPrs3d->_this();
273     return Prs3d::_nil();
274   }
275
276
277   //----------------------------------------------------------------------------
278   VISU_Gen_i
279   ::~VISU_Gen_i()
280   {
281     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
282   }
283
284
285   //----------------------------------------------------------------------------
286   void
287   CorrectSObjectType(SALOMEDS::SObject_ptr theSObject, 
288                      SALOMEDS::StudyBuilder_ptr theBuilder)
289   {
290     SALOMEDS::GenericAttribute_var anAttr;
291     bool isAttrStringFound = false;
292
293     if( theSObject->FindAttribute(anAttr, "AttributeComment") ) {
294       //SRN: Replace an AttributeComment with AttributeString
295       SALOMEDS::AttributeComment_var aComment = SALOMEDS::AttributeComment::_narrow(anAttr);
296       string aValue = aComment->Value();
297       theBuilder->RemoveAttribute(theSObject, "AttributeComment");
298       anAttr = theBuilder->FindOrCreateAttribute(theSObject, "AttributeString");
299       SALOMEDS::AttributeString_var aStringAttr = SALOMEDS::AttributeString::_narrow(anAttr);
300       aStringAttr->SetValue(aValue.c_str());
301       isAttrStringFound = true;
302     }
303
304     if ( isAttrStringFound || theSObject->FindAttribute(anAttr, "AttributeString") ) {
305       SALOMEDS::AttributeString_var aAttComment = SALOMEDS::AttributeString::_narrow(anAttr);
306       if ( aAttComment ) {
307         CORBA::String_var aValue = aAttComment->Value();
308         std::string aString = Storable::CorrectPersistentString(aValue.in());
309         aAttComment->SetValue( aString.c_str() );
310       }
311     }
312   }
313
314
315   //----------------------------------------------------------------------------
316   CORBA::Boolean 
317   VISU_Gen_i
318   ::Load(SALOMEDS::SComponent_ptr theComponent,
319          const SALOMEDS::TMPFile & theStream,
320          const char* theURL,
321          CORBA::Boolean theIsMultiFile)
322   {
323     Mutex mt(myMutex);
324     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
325
326     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theComponent);
327     SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder();
328
329     for (anIter->InitEx(true); anIter->More(); anIter->Next()) {
330       SALOMEDS::SObject_var aSObject = anIter->Value();
331       CorrectSObjectType(aSObject, aStudyBuilder);      
332     }
333
334    
335     VISU_TMP_DIR = theIsMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
336     SALOMEDS::ListOfFileNames_var aSeq =
337       SALOMEDS_Tool::PutStreamToFiles(theStream, VISU_TMP_DIR, theIsMultiFile);
338     myIsMultiFile = theIsMultiFile;
339     return true;
340   }
341
342
343   //----------------------------------------------------------------------------
344   CORBA::Boolean 
345   VISU_Gen_i
346   ::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
347               const SALOMEDS::TMPFile & theStream,
348               const char* theURL,
349               bool theIsMultiFile)
350   {
351     return Load(theComponent, theStream, theURL, theIsMultiFile);
352   }
353
354
355   //----------------------------------------------------------------------------
356   char* 
357   VISU_Gen_i
358   ::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
359                            const char* theLocalPersistentID,
360                            CORBA::Boolean theIsMultiFile,
361                            CORBA::Boolean theIsASCII)
362   {
363     CORBA::String_var aString("");
364     if(strcmp(theLocalPersistentID,"") != 0) {
365       Storable* aStorable = Storable::Create(theSObject, 
366                                              theLocalPersistentID,
367                                              VISU_TMP_DIR,
368                                              theIsMultiFile);
369       if(aStorable != NULL) 
370         aString = aStorable->GetID();
371     }
372     return aString._retn();
373   }
374
375
376   //----------------------------------------------------------------------------
377   SALOMEDS::TMPFile* 
378   VISU_Gen_i
379   ::Save(SALOMEDS::SComponent_ptr theComponent,
380          const char* theURL,
381          bool theIsMultiFile)
382   {
383     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - theURL = '"<<theURL<<"'");
384
385     Result_i::TFileNames aFileNames;
386     Result_i::TFileNames aFiles;
387
388     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
389     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theComponent);
390     for (; anIter->More(); anIter->Next()) {
391       SALOMEDS::SObject_var aSObject = anIter->Value();
392       CORBA::Object_var anObj = SObjectToObject(aSObject);
393       if(Result_i* aResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
394         aResult->Save(theComponent, 
395                       theURL, 
396                       theIsMultiFile,
397                       false,
398                       aFileNames, 
399                       aFiles);
400       }
401     }
402     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.size() - "<<aFileNames.size());
403
404     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
405     if(aFileNames.empty())
406       return aStreamFile._retn();
407     
408     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
409     SALOMEDS::ListOfFileNames_var aListOfFiles = GetListOfFileNames(aFiles);
410
411     if(theIsMultiFile)
412       aStreamFile = SALOMEDS_Tool::PutFilesToStream(theURL, aListOfFiles.in(), theIsMultiFile);
413     else
414       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aListOfFiles.in(), aListOfFileNames.in());
415
416     return aStreamFile._retn();
417   }
418
419
420   //----------------------------------------------------------------------------
421   SALOMEDS::TMPFile* 
422   VISU_Gen_i
423   ::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
424               const char* theURL,
425               bool theIsMultiFile)
426   {
427     std::string anURL = theIsMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
428     if(MYDEBUG) MESSAGE("VISU_Gen_i::SaveASCII - "<<anURL);
429
430     Result_i::TFileNames aFileNames;
431     Result_i::TFileNames aFiles;
432
433     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
434     SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator(theComponent);
435     for (; anIter->More(); anIter->Next()) {
436       SALOMEDS::SObject_var aSObject = anIter->Value();
437       CORBA::Object_var anObj = SObjectToObject(aSObject);
438       if(Result_i* aResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
439         aResult->Save(theComponent, 
440                       anURL, 
441                       theIsMultiFile,
442                       true,
443                       aFileNames, 
444                       aFiles);
445       }
446     }
447     if(MYDEBUG) MESSAGE("VISU_Gen_i::SaveASCII - aFileNames.size() - "<<aFileNames.size());
448
449     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
450     if(aFileNames.empty())
451       return aStreamFile._retn();
452
453     SALOMEDS::ListOfFileNames_var aListOfFileNames = GetListOfFileNames(aFileNames);
454     aStreamFile = SALOMEDS_Tool::PutFilesToStream(anURL, aListOfFileNames.in(), theIsMultiFile);
455
456     if(!theIsMultiFile)
457       SALOMEDS_Tool::RemoveTemporaryFiles(anURL, aListOfFileNames, true);
458
459     return aStreamFile._retn();
460   }
461
462
463   //----------------------------------------------------------------------------
464   char* 
465   VISU_Gen_i
466   ::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
467                            const char* theIORString,
468                            CORBA::Boolean theIsMultiFile,
469                            CORBA::Boolean theIsASCII)
470   {
471     CORBA::String_var aString("");
472     if(strcmp(theIORString, "") != 0){
473       CORBA::Object_var anObj = GetORB()->string_to_object(theIORString);
474       if(Storable* aStorable = dynamic_cast<Storable*>(GetServant(anObj).in())){
475         aString = aStorable->ToString().c_str();
476         return aString._retn();
477       }
478     }
479     return aString._retn();
480   }
481
482
483   //----------------------------------------------------------------------------
484   char* 
485   VISU_Gen_i
486   ::GetID()
487   {
488     return Base_i::GetID();
489   }
490
491
492   //----------------------------------------------------------------------------
493   void
494   VISU_Gen_i
495   ::SetCurrentStudy(SALOMEDS::Study_ptr theStudy)
496   {
497     class TEvent: public SALOME_Event {
498       std::string myStudyName;
499     public:
500       TEvent(const std::string theStudyName):myStudyName(theStudyName)
501         {}
502       virtual void Execute()
503         {
504           bool isActive = false;
505           SUIT_Session* aSession = SUIT_Session::session();
506           QPtrList<SUIT_Application> anApplications = aSession->applications();
507           QPtrListIterator<SUIT_Application> anIter (anApplications);
508           SUIT_Application* aFirstApp = anIter.current();
509           while (SUIT_Application* anApp = anIter.current()) {
510             ++anIter;
511             if (SUIT_Study* aSStudy = anApp->activeStudy()) {
512               if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
513                 if (_PTR(Study) aCStudy = aStudy->studyDS()) {
514                   if(MYDEBUG) MESSAGE("There is an application with active study : StudyId = "
515                                       << aCStudy->StudyId() << "; Name = '" << aCStudy->Name() << "'");
516                   if (myStudyName == aCStudy->Name()) {
517                     isActive = true;
518                     break;
519                   }
520                 }
521               }
522             }
523           }
524           if (!isActive) {
525             MESSAGE("!!! anApp->onLoadDoc(myStudyName) !!!");
526             // Has to be loaded in an empty or in a new application
527             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aFirstApp);
528             anApp->onLoadDoc(myStudyName.c_str());
529           }
530         }
531     };
532
533     if (!CORBA::is_nil(theStudy))
534     {
535       CORBA::String_var aName = theStudy->Name();
536       std::string aStudyName (aName.in());
537       if(MYDEBUG) MESSAGE("StudyId = " << theStudy->StudyId() << "; Name = '" << aName.in() << "'");
538       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
539
540       ProcessVoidEvent(new TEvent(aStudyName));
541
542       // Load MED component if necessary
543       if(!myStudyDocument->FindComponent("MED")->_is_nil())
544         {
545           SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
546           Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","MED");
547           SALOME_MED::MED_Gen_var aMedEngine = SALOME_MED::MED_Gen::_narrow(aComponent);
548           
549           if(!CORBA::is_nil(aMedEngine))
550             {
551               SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
552               try {
553                 aStudyBuilder->LoadWith( myStudyDocument->FindComponent( "MED" ), aMedEngine );
554               }
555               catch( const SALOME::SALOME_Exception& ) {
556                 // Oops, something went wrong while loading
557                 // See also SalomeApp_Study::openDataModel()
558               }
559             }
560         }
561     } else {
562       INFOS("CORBA::is_nil(theStudy)");
563     }
564   }
565
566
567   //----------------------------------------------------------------------------
568   SALOMEDS::Study_ptr 
569   VISU_Gen_i
570   ::GetCurrentStudy()
571   {
572     return SALOMEDS::Study::_duplicate(myStudyDocument);
573   }
574
575
576   //----------------------------------------------------------------------------
577   ViewManager_ptr 
578   VISU_Gen_i
579   ::GetViewManager()
580   {
581     Mutex mt(myMutex);
582     ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
583     return ViewManager::_duplicate(aViewManager->_this());
584   }
585
586
587   //----------------------------------------------------------------------------
588   SALOMEDS::SObject_ptr 
589   VISU_Gen_i
590   ::ImportTables(const char* theFileName)
591   {
592     if(myStudyDocument->GetProperties()->IsLocked())
593       return SALOMEDS::SObject::_nil();
594     SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
595     return aRes._retn();
596   }
597
598
599   //----------------------------------------------------------------------------
600   CORBA::Boolean 
601   VISU_Gen_i
602   ::ExportTableToFile(SALOMEDS::SObject_ptr theTable,
603                       const char* theFileName)
604   {
605     return VISU::ExportTableToFile(theTable, theFileName);
606   }
607
608
609   //----------------------------------------------------------------------------
610   Result_ptr
611   VISU_Gen_i
612   ::ImportFile(const char* theFileName)
613   {
614     if(myStudyDocument->GetProperties()->IsLocked())
615       return Result::_nil();
616
617     Result_i* aResult = Result_i::New(myStudyDocument,
618                                       Result_i::eFile,
619                                       Result_i::eImportFile,
620                                       true,
621                                       true,
622                                       true,
623                                       true);
624
625     if(aResult->Create(theFileName) != NULL)
626       return aResult->_this();
627     else
628       aResult->_remove_ref();
629
630     return VISU::Result::_nil();
631   }
632
633
634   //----------------------------------------------------------------------------
635   Result_ptr
636   VISU_Gen_i
637   ::CreateResult(const char* theFileName)
638   {
639     if(myStudyDocument->GetProperties()->IsLocked())
640       return Result::_nil();
641
642     Result_i* aResult = Result_i::New(myStudyDocument,
643                                       Result_i::eFile,
644                                       Result_i::eImportFile,
645                                       false,
646                                       true,
647                                       true,
648                                       true);
649
650     if(aResult->Create(theFileName) != NULL)
651       return aResult->_this();
652     else
653       aResult->_remove_ref();
654
655     return VISU::Result::_nil();
656   }
657
658
659   //----------------------------------------------------------------------------
660   Result_ptr
661   VISU_Gen_i
662   ::CopyAndImportFile(const char* theFileName)
663   {
664     if(myStudyDocument->GetProperties()->IsLocked())
665       return Result::_nil();
666
667     Result_i* aResult = Result_i::New(myStudyDocument,
668                                       Result_i::eRestoredFile,
669                                       Result_i::eCopyAndImportFile,
670                                       true,
671                                       true,
672                                       true,
673                                       true);
674     if(aResult->Create(theFileName) != NULL)
675       return aResult->_this();
676     else
677       aResult->_remove_ref();
678
679     return VISU::Result::_nil();
680   }
681
682
683   //----------------------------------------------------------------------------
684   Result_ptr VISU_Gen_i::ImportMed(SALOMEDS::SObject_ptr theMedSObject)
685   {
686     if (myStudyDocument->GetProperties()->IsLocked())
687       return Result::_nil();
688
689     Result_i* aResult = Result_i::New(myStudyDocument,
690                                       Result_i::eComponent,
691                                       Result_i::eImportMed,
692                                       true,
693                                       true,
694                                       true,
695                                       true);
696     if (aResult->Create(theMedSObject) != NULL)
697     {
698       return aResult->_this();
699     }
700     else
701       aResult->_remove_ref();
702     
703     return VISU::Result::_nil();
704   }
705
706
707   //----------------------------------------------------------------------------
708   Result_ptr
709   VISU_Gen_i
710   ::ImportMedField (SALOME_MED::FIELD_ptr theField)
711   {
712     if (myStudyDocument->GetProperties()->IsLocked())
713       return Result::_nil();
714
715     Result_i* aResult = Result_i::New(myStudyDocument,
716                                       Result_i::eComponent,
717                                       Result_i::eImportMedField,
718                                       true,
719                                       true,
720                                       true,
721                                       true);
722
723     if (aResult->Create(theField) != NULL)
724       return aResult->_this();
725     else
726       aResult->_remove_ref();
727     
728     return VISU::Result::_nil();
729   }
730
731   void
732   VISU_Gen_i
733   ::RenameMeshInStudy(Result_ptr theResult,
734                       const std::string& theMeshName,
735                       int theEntity, // -1 for group indication
736                       const std::string& theSubMeshName, // Family or Group name
737                       const std::string& theNewName)
738   {
739     Result_i* aResult = dynamic_cast<Result_i*>(GetServant(theResult).in());
740     if (!aResult) 
741       return;
742
743     SALOMEDS::Study_var aStudyDocument = aResult->GetStudyDocument();
744     if (aStudyDocument->GetProperties()->IsLocked()) 
745       return;
746
747
748     QString aComment;
749     VISU::VISUType aType;
750     if (theEntity >= 0)
751       if (theSubMeshName == "")
752         aType = VISU::TENTITY;
753       else
754         aType = VISU::TFAMILY;
755     else
756       aType = VISU::TGROUP;
757
758     VISU::Storable::TRestoringMap aRestoringMap;
759     aRestoringMap["myMeshName"] = theMeshName;
760
761     switch (aType) {
762     case VISU::TENTITY:
763       aRestoringMap["myComment"] = "ENTITY";
764       aRestoringMap["myId"] = QString::number(theEntity);
765       break;
766     case VISU::TFAMILY:
767       aRestoringMap["myComment"] = "FAMILY";
768       aRestoringMap["myEntityId"] = QString::number(theEntity);
769       aRestoringMap["myName"] = theSubMeshName;
770       break;
771     case VISU::TGROUP:
772       aRestoringMap["myComment"] = "GROUP";
773       aRestoringMap["myName"] = theSubMeshName;
774       break;
775     }
776
777     string anEntry = aResult->GetEntry(aRestoringMap);
778     if (anEntry == "") 
779       return;
780
781     SALOMEDS::SObject_ptr aSObject = aStudyDocument->FindObjectID(anEntry.c_str());
782
783     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudyDocument->NewBuilder();
784     aStudyBuilder->NewCommand(); // There is a transaction
785
786     SALOMEDS::GenericAttribute_var anAttr =
787       aStudyBuilder->FindOrCreateAttribute(aSObject,"AttributeName");
788     SALOMEDS::AttributeName_var aNameAttr = SALOMEDS::AttributeName::_narrow(anAttr);
789     aNameAttr->SetValue(theNewName.c_str());
790
791     aStudyBuilder->CommitCommand();
792   }
793
794
795   //----------------------------------------------------------------------------
796   void
797   VISU_Gen_i
798   ::RenameEntityInStudy(Result_ptr   theResult,
799                         const char*  theMeshName,
800                         VISU::Entity theEntity,
801                         const char*  theNewName)
802   {
803     RenameMeshInStudy(theResult, theMeshName, (int)theEntity, "", theNewName);
804   }
805
806
807   //----------------------------------------------------------------------------
808   void
809   VISU_Gen_i
810   ::RenameFamilyInStudy(Result_ptr   theResult,
811                         const char*  theMeshName,
812                         VISU::Entity theEntity,
813                         const char*  theFamilyName,
814                         const char*  theNewName)
815   {
816     RenameMeshInStudy(theResult, theMeshName, (int)theEntity, theFamilyName, theNewName);
817   }
818
819
820   //----------------------------------------------------------------------------
821   void 
822   VISU_Gen_i
823   ::RenameGroupInStudy(Result_ptr  theResult,
824                        const char*  theMeshName,
825                        const char* theGroupName,
826                        const char* theNewName)
827   {
828     RenameMeshInStudy(theResult, theMeshName, -1, theGroupName, theNewName);
829   }
830
831
832   //----------------------------------------------------------------------------
833   Mesh_ptr
834   VISU_Gen_i
835   ::MeshOnEntity(Result_ptr theResult,
836                  const char* theMeshName,
837                  VISU::Entity theEntity)
838   {
839     Result_i* aResult = dynamic_cast<Result_i*>(GetServant(theResult).in());
840     if (!aResult) 
841       return VISU::Mesh::_nil();
842
843     SALOMEDS::Study_var aStudyDocument = aResult->GetStudyDocument();
844     if (aStudyDocument->GetProperties()->IsLocked()) 
845       return VISU::Mesh::_nil();
846
847     Mesh_i* aPresent = new Mesh_i();
848     if(aPresent->Create(aResult, theMeshName, theEntity))
849       return aPresent->_this();
850     else
851       aPresent->_remove_ref();
852
853     return VISU::Mesh::_nil();
854   }
855
856
857   //----------------------------------------------------------------------------
858   Mesh_ptr 
859   VISU_Gen_i
860   ::FamilyMeshOnEntity(Result_ptr theResult,
861                        const char* theMeshName,
862                        VISU::Entity theEntity,
863                        const char* theFamilyName)
864   {
865     Result_i* aResult = dynamic_cast<Result_i*>(GetServant(theResult).in());
866     if (!aResult) 
867       return VISU::Mesh::_nil();
868
869     SALOMEDS::Study_var aStudyDocument = aResult->GetStudyDocument();
870     if (aStudyDocument->GetProperties()->IsLocked()) 
871       return VISU::Mesh::_nil();
872
873     Mesh_i* aPresent = new Mesh_i();
874     if(aPresent->Create(aResult, theMeshName, theEntity, theFamilyName))
875       return aPresent->_this();
876     else
877       aPresent->_remove_ref();
878
879     return VISU::Mesh::_nil();
880   }
881
882
883   //----------------------------------------------------------------------------
884   Mesh_ptr
885   VISU_Gen_i
886   ::GroupMesh(Result_ptr theResult,
887               const char* theMeshName,
888               const char* theGroupName)
889   {
890     Result_i* aResult = dynamic_cast<Result_i*>(GetServant(theResult).in());
891     if (!aResult) 
892       return VISU::Mesh::_nil();
893
894     SALOMEDS::Study_var aStudyDocument = aResult->GetStudyDocument();
895     if (aStudyDocument->GetProperties()->IsLocked()) 
896       return VISU::Mesh::_nil();
897
898     Mesh_i* aPresent = new Mesh_i();
899     if(aPresent->Create(aResult, theMeshName, theGroupName))
900       return aPresent->_this();
901     else
902       aPresent->_remove_ref();
903
904     return VISU::Mesh::_nil();
905   }
906
907
908   //----------------------------------------------------------------------------
909   ScalarMap_ptr 
910   VISU_Gen_i
911   ::ScalarMapOnField(Result_ptr theResult,
912                      const char* theMeshName,
913                      VISU::Entity theEntity,
914                      const char* theFieldName,
915                      CORBA::Long theIteration)
916   {
917     return Prs3dOnField<VISU::ScalarMap_i>(theResult,
918                                            theMeshName,
919                                            theEntity,
920                                            theFieldName,
921                                            theIteration)._retn();
922   }
923
924
925   //----------------------------------------------------------------------------
926   GaussPoints_ptr
927   VISU_Gen_i
928   ::GaussPointsOnField(Result_ptr theResult,
929                        const char* theMeshName,
930                        VISU::Entity theEntity,
931                        const char* theFieldName,
932                        CORBA::Long theIteration)
933   {
934     return Prs3dOnField<VISU::GaussPoints_i>(theResult,
935                                              theMeshName,
936                                              theEntity,
937                                              theFieldName,
938                                              theIteration)._retn();
939   }
940
941
942   //---------------------------------------------------------------
943   DeformedShape_ptr 
944   VISU_Gen_i
945   ::DeformedShapeOnField(Result_ptr theResult,
946                          const char* theMeshName,
947                          VISU::Entity theEntity,
948                          const char* theFieldName,
949                          CORBA::Long theIteration)
950   {
951     return Prs3dOnField<VISU::DeformedShape_i>(theResult,
952                                                theMeshName,
953                                                theEntity,
954                                                theFieldName,
955                                                theIteration)._retn();
956   }
957
958
959   //---------------------------------------------------------------
960   ScalarMapOnDeformedShape_ptr 
961   VISU_Gen_i
962   ::ScalarMapOnDeformedShapeOnField(Result_ptr theResult,
963                                     const char* theMeshName,
964                                     VISU::Entity theEntity,
965                                     const char* theFieldName,
966                                     CORBA::Long theIteration)
967   {
968     return Prs3dOnField<VISU::ScalarMapOnDeformedShape_i>(theResult,
969                                                           theMeshName,
970                                                           theEntity,
971                                                           theFieldName,
972                                                           theIteration)._retn();
973   }
974   
975
976   //---------------------------------------------------------------
977   Vectors_ptr 
978   VISU_Gen_i
979   ::VectorsOnField(Result_ptr theResult,
980                    const char* theMeshName,
981                    VISU::Entity theEntity,
982                    const char* theFieldName,
983                    CORBA::Long theIteration)
984   {
985     return Prs3dOnField<VISU::Vectors_i>(theResult,
986                                          theMeshName,
987                                          theEntity,
988                                          theFieldName,
989                                          theIteration)._retn();
990   }
991
992
993   //---------------------------------------------------------------
994   IsoSurfaces_ptr 
995   VISU_Gen_i
996   ::IsoSurfacesOnField(Result_ptr theResult,
997                        const char* theMeshName,
998                        VISU::Entity theEntity,
999                        const char* theFieldName,
1000                        CORBA::Long theIteration)
1001   {
1002     return Prs3dOnField<VISU::IsoSurfaces_i>(theResult,
1003                                              theMeshName,
1004                                              theEntity,
1005                                              theFieldName,
1006                                              theIteration)._retn();
1007   }
1008
1009
1010   //---------------------------------------------------------------
1011   StreamLines_ptr 
1012   VISU_Gen_i
1013   ::StreamLinesOnField(Result_ptr theResult,
1014                        const char* theMeshName,
1015                        VISU::Entity theEntity,
1016                        const char* theFieldName,
1017                        CORBA::Long theIteration)
1018   {
1019     return Prs3dOnField<VISU::StreamLines_i>(theResult,
1020                                              theMeshName,
1021                                              theEntity,
1022                                              theFieldName,
1023                                              theIteration)._retn();
1024   }
1025
1026
1027   //---------------------------------------------------------------
1028   Plot3D_ptr 
1029   VISU_Gen_i
1030   ::Plot3DOnField(Result_ptr theResult,
1031                   const char* theMeshName,
1032                   VISU::Entity theEntity,
1033                   const char* theFieldName,
1034                   CORBA::Long theIteration)
1035   {
1036     return Prs3dOnField<VISU::Plot3D_i>(theResult,
1037                                         theMeshName,
1038                                         theEntity,
1039                                         theFieldName,
1040                                         theIteration)._retn();
1041   }
1042
1043
1044   //---------------------------------------------------------------
1045   CutPlanes_ptr
1046   VISU_Gen_i
1047   ::CutPlanesOnField(Result_ptr theResult,
1048                      const char* theMeshName,
1049                      VISU::Entity theEntity,
1050                      const char* theFieldName,
1051                      CORBA::Long theIteration)
1052   {
1053     return Prs3dOnField<VISU::CutPlanes_i>(theResult,
1054                                            theMeshName,
1055                                            theEntity,
1056                                            theFieldName,
1057                                            theIteration)._retn();
1058   }
1059
1060
1061   //---------------------------------------------------------------
1062   CutLines_ptr
1063   VISU_Gen_i
1064   ::CutLinesOnField(Result_ptr theResult,
1065                     const char* theMeshName,
1066                     VISU::Entity theEntity,
1067                     const char* theFieldName,
1068                     CORBA::Long theIteration)
1069   {
1070     return Prs3dOnField<VISU::CutLines_i>(theResult,
1071                                           theMeshName,
1072                                           theEntity,
1073                                           theFieldName,
1074                                           theIteration)._retn();
1075   }
1076
1077
1078   //---------------------------------------------------------------
1079   struct CreateTableEvent: public SALOME_Event
1080   {
1081     SALOMEDS::Study_var myStudyDocument;
1082     const char* myTableEntry;
1083     typedef Table_ptr TResult;
1084     TResult myResult;
1085     
1086     CreateTableEvent(const SALOMEDS::Study_var& theStudy, const char* theTableEntry)
1087     {
1088       myStudyDocument = theStudy;
1089       myTableEntry = theTableEntry;
1090       myResult = Table::_nil();
1091     }
1092     
1093     virtual
1094     void
1095     Execute()
1096     {
1097       Table_i* pPresent = new Table_i(myStudyDocument,myTableEntry);
1098       if(pPresent->Create() != NULL)
1099         myResult = pPresent->_this();
1100       else{
1101         pPresent->_remove_ref();
1102         myResult = VISU::Table::_nil();
1103       }
1104     }
1105   };
1106
1107
1108   //---------------------------------------------------------------
1109   Table_ptr
1110   VISU_Gen_i
1111   ::CreateTable(const char* theTableEntry)
1112   {
1113     if(myStudyDocument->GetProperties()->IsLocked())
1114       return Table::_nil();
1115     Mutex mt(myMutex);
1116     return ProcessEvent(new CreateTableEvent(myStudyDocument, theTableEntry));
1117   }
1118
1119
1120   //---------------------------------------------------------------
1121   Curve_ptr
1122   VISU_Gen_i
1123   ::CreateCurve(Table_ptr theTable,
1124                 CORBA::Long theHRow,
1125                 CORBA::Long theVRow)
1126   {
1127     return CreateCurveWithZ( theTable, theHRow, theVRow, 0 );
1128   }
1129
1130
1131   //---------------------------------------------------------------
1132   Curve_ptr
1133   VISU_Gen_i
1134   ::CreateCurveWithZ(Table_ptr theTable,
1135                      CORBA::Long theHRow,
1136                      CORBA::Long theVRow,
1137                      CORBA::Long theZRow)
1138   {
1139     if(myStudyDocument->GetProperties()->IsLocked())
1140       return Curve::_nil();
1141     Mutex mt(myMutex);
1142     PortableServer::POA_ptr aPOA = GetPOA();
1143     Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
1144     Curve_i* pPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow,theZRow);
1145     if(pPresent->Create() != NULL)
1146       return pPresent->_this();
1147     else{
1148       pPresent->_remove_ref();
1149       return VISU::Curve::_nil();
1150     }
1151   }
1152
1153
1154
1155   //---------------------------------------------------------------
1156   Container_ptr
1157   VISU_Gen_i
1158   ::CreateContainer()
1159   {
1160     if(myStudyDocument->GetProperties()->IsLocked())
1161       return Container::_nil();
1162     Mutex mt(myMutex);
1163     Container_i* pPresent = new Container_i(myStudyDocument);
1164     if(pPresent->Create() != NULL)
1165       return pPresent->_this();
1166     else{
1167       pPresent->_remove_ref();
1168       return VISU::Container::_nil();
1169     }
1170   }
1171
1172
1173   //---------------------------------------------------------------
1174   Animation_ptr
1175   VISU_Gen_i
1176   ::CreateAnimation(View3D_ptr theView3D)
1177   {
1178     if(myStudyDocument->GetProperties()->IsLocked())
1179       return Animation::_nil();
1180     Mutex mt(myMutex);
1181     if(VISU_TimeAnimation_i* anAnim = new VISU_TimeAnimation_i(myStudyDocument,theView3D)){
1182       return anAnim->_this();
1183     }else
1184       return VISU::Animation::_nil();
1185   }
1186
1187
1188   //---------------------------------------------------------------
1189   void
1190   VISU_Gen_i
1191   ::DeleteResult (Result_ptr theResult)
1192   {
1193     theResult->RemoveFromStudy();
1194   }
1195
1196
1197   //---------------------------------------------------------------
1198   void
1199   VISU_Gen_i
1200   ::DeletePrs3d(Prs3d_ptr thePrs3d)
1201   {
1202     thePrs3d->RemoveFromStudy();
1203   }
1204
1205
1206   //---------------------------------------------------------------
1207   void
1208   VISU_Gen_i
1209   ::Close(SALOMEDS::SComponent_ptr theComponent)
1210   {}
1211
1212
1213   //---------------------------------------------------------------
1214   char* 
1215   VISU_Gen_i
1216   ::ComponentDataType()
1217   {
1218     return CORBA::string_dup("VISU");
1219   }
1220
1221
1222   //---------------------------------------------------------------
1223   bool
1224   VISU_Gen_i
1225   ::CanPublishInStudy(CORBA::Object_ptr theIOR) 
1226   {
1227     Result_var aResultObj = Result::_narrow(theIOR);
1228     return !(aResultObj->_is_nil());
1229   }
1230
1231
1232   //---------------------------------------------------------------
1233   SALOMEDS::SObject_ptr
1234   VISU_Gen_i
1235   ::PublishInStudy(SALOMEDS::Study_ptr theStudy,
1236                    SALOMEDS::SObject_ptr theSObject,
1237                    CORBA::Object_ptr theObject,
1238                    const char* theName)
1239     throw (SALOME::SALOME_Exception)
1240   {
1241     Unexpect aCatch(SalomeException);
1242     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
1243     Mutex mt(myMutex);
1244     SALOMEDS::SObject_var aResultSO;
1245     Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject).in());
1246     if (!aResultObj) 
1247       return aResultSO._retn();
1248     const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
1249     CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
1250     aResultSO = theStudy->FindObjectID(anEntry);
1251     return aResultSO._retn();
1252   }
1253
1254
1255   //---------------------------------------------------------------
1256   CORBA::Boolean 
1257   VISU_Gen_i
1258   ::CanCopy(SALOMEDS::SObject_ptr theObject) 
1259   {
1260     CORBA::Object_var anObj = SObjectToObject(theObject);
1261     if (Storable* aStorable = dynamic_cast<Storable*>(GetServant(anObj).in()))
1262       return aStorable->CanCopy(theObject);
1263
1264     return false;
1265   }
1266
1267
1268   //---------------------------------------------------------------
1269   SALOMEDS::TMPFile* 
1270   VISU_Gen_i
1271   ::CopyFrom(SALOMEDS::SObject_ptr theObject, 
1272              CORBA::Long& theObjectID) 
1273   {
1274     theObjectID = 0;
1275     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
1276
1277     CORBA::Object_var anObj = SObjectToObject(theObject);
1278     if (CORBA::is_nil(anObj)) {
1279       aStreamFile->length(1);
1280       aStreamFile[0] = CORBA::string_dup("E")[0];
1281     } else if (Storable* aStorable = dynamic_cast<Storable*>(GetServant(anObj).in())) {
1282       std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
1283       Storable::TFileNames aFileNames;
1284       bool anIsDone = aStorable->CopyFrom(theObject, theObjectID, aTmpDir, aFileNames);
1285
1286       SALOMEDS::ListOfFileNames_var aListOfFileNames = new SALOMEDS::ListOfFileNames;
1287       aListOfFileNames->length(aFileNames.size());
1288       for(size_t anId = 0; anId < aFileNames.size(); anId++)
1289         aListOfFileNames[anId] = aFileNames[anId].c_str();
1290
1291       if(anIsDone)
1292         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aListOfFileNames.in(), false);
1293
1294       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aListOfFileNames.in(), true);
1295     }
1296     return aStreamFile._retn();
1297   }
1298
1299
1300   //---------------------------------------------------------------
1301   CORBA::Boolean 
1302   VISU_Gen_i
1303   ::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1304     // The VISU component can paste only objects copied by VISU component
1305     CORBA::String_var aString = ComponentDataType();
1306     if (strcmp(theComponentName, aString.in()) == 0 && theObjectID == 1)
1307       return true;
1308     return false;
1309   }
1310
1311
1312   //---------------------------------------------------------------
1313   SALOMEDS::SObject_ptr
1314   VISU_Gen_i
1315   ::PasteInto(const SALOMEDS::TMPFile& theStream,
1316               CORBA::Long theObjectID,
1317               SALOMEDS::SObject_ptr theSObject)
1318   {
1319     if (theObjectID != 1)
1320       return SALOMEDS::SObject::_nil();
1321
1322     SALOMEDS::SComponent_var aComponent = theSObject->GetFatherComponent();
1323     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
1324     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
1325     CORBA::String_var aComponentID(aComponent->GetID());
1326     CORBA::String_var aSObjectID(theSObject->GetID());
1327     
1328     SALOMEDS::SObject_var aSObject;
1329     if (strcmp(aComponentID, aSObjectID) == 0) //create the new result SObject
1330       aSObject = aStudyBuilder->NewObject(aComponent);
1331     else
1332       aSObject = SALOMEDS::SObject::_duplicate(theSObject);
1333     
1334     std::string aTmpDir = SALOMEDS_Tool::GetTmpDir();
1335     SALOMEDS::ListOfFileNames_var aListOfFileNames = 
1336       SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1337     if(MYDEBUG) MESSAGE("Result_i::PasteInto - aListOfFileNames->length() = "<<aListOfFileNames->length());
1338     
1339     std::string aLocalPersistentID;
1340     {
1341       std::string aCopyPersist =  aTmpDir + "copy_persistent";
1342       std::ifstream anInputFileStream(aCopyPersist.c_str());
1343       anInputFileStream>>aLocalPersistentID;
1344       anInputFileStream.close();
1345     }
1346     
1347     //Just for Result::Restore to find the Comment attribute :(
1348     SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeString");
1349     
1350     std::string aFileName(aTmpDir);
1351     if(aListOfFileNames->length() > 1)
1352       aFileName += aListOfFileNames[1].in();
1353     Storable* aStorable = Storable::Create(aSObject, aLocalPersistentID, aFileName, false);
1354     
1355     SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
1356     aFilesToRemove->length(1);
1357     aFilesToRemove[0] = aListOfFileNames[0];
1358     SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aFilesToRemove.in(), true);
1359     
1360     anAttr = aStudyBuilder->FindOrCreateAttribute(aSObject, "AttributeIOR");
1361     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1362     CORBA::String_var anIORValue(aStorable->GetID());
1363     anIOR->SetValue(anIORValue);
1364     
1365     return aSObject._retn();
1366   }
1367
1368
1369   //---------------------------------------------------------------
1370   VISU::ColoredPrs3dCache_ptr
1371   VISU_Gen_i
1372   ::GetColoredPrs3dCache(SALOMEDS::Study_ptr theStudy)
1373   {
1374     return ColoredPrs3dCache_i::GetInstance(theStudy);
1375   }
1376 }