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