]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Gen_i.cc
Salome HOME
Fix for Bug IPAL8945
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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_ScalarMap_i.hh"
35 #include "VISU_IsoSurfaces_i.hh"
36 #include "VISU_DeformedShape_i.hh"
37 #include "VISU_CutPlanes_i.hh"
38 #include "VISU_CutLines_i.hh"
39 #include "VISU_Vectors_i.hh"
40 #include "VISU_StreamLines_i.hh"
41 #include "VISU_GaussPoints_i.hh"
42 #include "VISU_Plot3D_i.hh"
43 #include "VISU_Table_i.hh"
44 #include "VISU_TimeAnimation.h"
45 #include "VISU_ScalarMapOnDeformedShape_i.hh"
46
47 #include "VISU_Actor.h"
48
49 #include "HDFascii.hxx"
50 #include "SALOMEDS_Tool.hxx"
51
52 #include "SALOMEDSClient_AttributeName.hxx"
53 #include "SALOMEDSClient_AttributePixMap.hxx"
54
55 #include "SUIT_Session.h"
56 #include "SalomeApp_Study.h"
57 #include "SalomeApp_Application.h"
58 #include "LightApp_SelectionMgr.h"
59 #include "SVTK_ViewModel.h"
60 #include "SVTK_ViewWindow.h"
61 #include "SALOME_Event.hxx"
62 #include "SALOME_ListIO.hxx"
63 #include "SALOME_ListIteratorOfListIO.hxx"
64
65 #include "utilities.h"
66
67 // IDL Headers
68 #include <omnithread.h>
69 #include CORBA_SERVER_HEADER(SALOME_Session)
70 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
71
72 // QT Includes
73 #include <qstring.h>
74 #include <qfileinfo.h>
75
76 // VTK Includes
77 #include <vtkRenderer.h>
78 #include <vtkActorCollection.h>
79
80 // OCCT Includes
81 #include <TCollection_AsciiString.hxx>
82 #include <TColStd_SequenceOfAsciiString.hxx>
83
84 // STL Includes
85 #include <strstream>
86
87 #include "Utils_ExceptHandlers.hxx"
88
89 using namespace std;
90
91 static QFileInfo aFileInfo;
92
93 #ifdef _DEBUG_
94 static int MYDEBUG = 0;
95 //#define _DEXCEPT_
96 #else
97 static int MYDEBUG = 0;
98 #endif
99
100 UNEXPECT_CATCH(SalomeException, SALOME::SALOME_Exception);
101
102 extern "C" VISU::VISU_Gen_ptr GetImpl(CORBA::ORB_ptr theORB,
103                                       PortableServer::POA_ptr thePOA,
104                                       SALOME_NamingService* theNamingService,
105                                       QMutex* theMutex)
106 {
107   if(MYDEBUG) MESSAGE("extern \"C\" GetImpl");
108   VISU::VISU_Gen_i *aVISU_Gen = new VISU::VISU_Gen_i(theORB,thePOA,theNamingService,theMutex);
109   //return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
110   return aVISU_Gen->_this();
111 }
112
113 namespace VISU
114 {
115   static string VisuTmpDir;
116
117   static CORBA::Boolean myIsMultiFile;
118   const CORBA::Boolean IsMultifile() { return myIsMultiFile;}
119
120   //===========================================================================
121   _PTR(SComponent) ClientFindOrCreateVisuComponent (_PTR(Study) theStudyDocument)
122   {
123     _PTR(SComponent) aSComponent = theStudyDocument->FindComponent("VISU");
124     if (!aSComponent) {
125       _PTR(StudyBuilder) aStudyBuilder = theStudyDocument->NewBuilder();
126       aStudyBuilder->NewCommand();
127       int aLocked = theStudyDocument->GetProperties()->IsLocked();
128       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
129       aSComponent = aStudyBuilder->NewComponent("VISU");
130       _PTR(GenericAttribute) anAttr =
131         aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
132       _PTR(AttributeName) aName (anAttr);
133
134       CORBA::ORB_var anORB = Base_i::GetORB();
135       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
136       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
137       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue =
138         SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
139       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
140       if (!Comp->_is_nil()) {
141         aName->SetValue(Comp->componentusername());
142       }
143
144       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
145       _PTR(AttributePixMap) aPixmap (anAttr);
146       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
147
148       VISU_Gen_var aVisuGen = Base_i::GetVisuGenImpl()->_this();
149       aStudyBuilder->DefineComponentInstance(aSComponent, aVisuGen->GetID());
150       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
151       aStudyBuilder->CommitCommand();
152     }
153     return aSComponent;
154   }
155
156   SALOMEDS::SComponent_var FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument){
157     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
158     if (aSComponent->_is_nil()) {
159       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
160       aStudyBuilder->NewCommand();
161       int aLocked = theStudyDocument->GetProperties()->IsLocked();
162       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
163       aSComponent = aStudyBuilder->NewComponent("VISU");
164       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
165       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
166
167       //NRI      aName->SetValue("Visu");
168       CORBA::ORB_var anORB = Base_i::GetORB();
169       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
170       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
171       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
172       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
173       if ( !Comp->_is_nil() ) {
174         aName->SetValue( Comp->componentusername() );
175       }
176
177       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
178       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
179       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
180
181       VISU_Gen_var aVisuGen = Base_i::GetVisuGenImpl()->_this();
182       aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
183       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
184       aStudyBuilder->CommitCommand();
185     }
186     return aSComponent;
187   }
188
189   void RegistryStorable() {
190     Storable::Registry(Result_i::myComment.c_str(),&(Result_i::Restore));
191     Storable::Registry(Mesh_i::myComment.c_str(),&(Restore<Mesh_i>));
192     Storable::Registry(ScalarMap_i::myComment.c_str(),&(Restore<ScalarMap_i>));
193     Storable::Registry(GaussPoints_i::myComment.c_str(),&(Restore<GaussPoints_i>));
194     Storable::Registry(DeformedShape_i::myComment.c_str(),&(Restore<DeformedShape_i>));
195     Storable::Registry(CutPlanes_i::myComment.c_str(),&(Restore<CutPlanes_i>));
196     Storable::Registry(CutLines_i::myComment.c_str(),&(Restore<CutLines_i>));
197     Storable::Registry(IsoSurfaces_i::myComment.c_str(),&(Restore<IsoSurfaces_i>));
198     Storable::Registry(StreamLines_i::myComment.c_str(),&(Restore<StreamLines_i>));
199     Storable::Registry(Plot3D_i::myComment.c_str(),&(Restore<Plot3D_i>));
200     Storable::Registry(Vectors_i::myComment.c_str(),&(Restore<Vectors_i>));
201     Storable::Registry(Table_i::myComment.c_str(),&(Table_i::Restore));
202     Storable::Registry(Curve_i::myComment.c_str(),&(Curve_i::Restore));
203     Storable::Registry(Container_i::myComment.c_str(),&(Container_i::Restore));
204     Storable::Registry(ScalarMapOnDeformedShape_i::myComment.c_str(),&(Restore<ScalarMapOnDeformedShape_i>));
205   }
206
207
208   //===========================================================================
209   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA,
210                          SALOME_NamingService* theNamingService, QMutex* theMutex) :
211     Engines_Component_i()
212   {
213     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
214     Base_i::myMutex = theMutex;  //apo
215     Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
216     Base_i::myPoa = PortableServer::POA::_duplicate(thePOA);
217     Base_i::myNamingService = theNamingService;
218     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
219     Base_i::myEnginesLifeCycle = &aEnginesLifeCycle;
220     Base_i::myVisuGenImpl = this;
221     RegistryStorable();
222
223     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
224     SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
225     SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
226     if(aListOfOpenStudies->length() > 0) {
227       CORBA::String_var aStudyName = aListOfOpenStudies[0];
228       //aFileInfo.setFile(aStudyName.in());
229       myStudyDocument = aStudyManager->GetStudyByName(aStudyName/*aFileInfo.baseName()*/);
230     }else
231       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
232   }
233
234   bool
235   CreatColoredPrs3d(ColoredPrs3d_i* theColoredPrs3d,
236                     const char* theMeshName, 
237                     VISU::Entity theEntity,
238                     const char* theFieldName, 
239                     CORBA::Double theIteration)
240   {
241 #ifndef _DEXCEPT_
242     try{
243 #endif
244       if(theColoredPrs3d->Create(theMeshName,theEntity,theFieldName,int(theIteration)))
245         return true;
246 #ifndef _DEXCEPT_
247     }catch(std::exception& exc){
248       INFOS("Follow exception was occured :\n"<<exc.what());
249     }catch(...){
250       INFOS("Unknown exception was occured!");
251     }
252 #endif
253     return false;
254   }
255
256   VISU_Gen_i::~VISU_Gen_i(){
257     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
258   }
259   //===========================================================================
260   CORBA::Boolean VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
261                                   const SALOMEDS::TMPFile & theStream,
262                                   const char* theURL,
263                                   bool isMultiFile)
264   {
265     Mutex mt(myMutex);
266     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
267     SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder();
268     string aDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
269     TCollection_AsciiString aTmpDir (const_cast<char*>(aDir.c_str()));
270     VisuTmpDir = aTmpDir.ToCString();
271     SALOMEDS::ListOfFileNames_var aSeq =
272       SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
273     myIsMultiFile = isMultiFile;
274     return true;
275   }
276
277   CORBA::Boolean VISU_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
278                                        const SALOMEDS::TMPFile & theStream,
279                                        const char* theURL,
280                                        bool isMultiFile)
281   {
282     return Load(theComponent, theStream, theURL, isMultiFile);
283   }
284
285   char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
286                                            const char* aLocalPersistentID,
287                                            CORBA::Boolean isMultiFile,
288                                            CORBA::Boolean isASCII)
289   {
290     CORBA::String_var aString("");
291     if(strcmp(aLocalPersistentID,"") != 0) {
292       Mutex mt(myMutex);
293       Storable* aStorable =
294         Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
295       if(aStorable != NULL) aString = aStorable->GetID();
296     }
297     return aString._retn();
298   }
299   //===========================================================================
300   SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
301                                       const char* theURL,
302                                       bool isMultiFile)
303   {
304     Mutex mt(myMutex);
305     string aDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
306     TCollection_AsciiString aTmpDir (const_cast<char*>(aDir.c_str()));
307     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
308     int aCounter = 0;
309     TColStd_SequenceOfAsciiString aFileNames;
310     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
311
312     //CORBA::Boolean anIsValidScript;
313     //Engines::TMPFile_var aDump = DumpPython(aStudy,false,anIsValidScript);
314
315     SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
316     for (; itBig->More(); itBig->Next()) {
317       SALOMEDS::SObject_var gotBranch = itBig->Value();
318       CORBA::Object_var anObj = SObjectToObject(gotBranch);
319       if(CORBA::is_nil(anObj)) continue;
320       if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
321         switch(pResult->GetCreationId()){
322         case Result_i::eImportFile:
323         case Result_i::eCopyAndImportFile: {
324           const QFileInfo& aFileInfo = pResult->GetFileInfo();
325           QString aPrefix("");
326           if (isMultiFile) aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL()).c_str();
327           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
328           static QString aCommand;
329           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
330
331           int aRes = system(aCommand);
332           if(aRes){
333             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
334             continue;
335           }else
336             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aCommand = "<<aCommand);
337
338           TCollection_AsciiString aString(strdup(aFileName.latin1()));
339           aFileNames.Append(aString);
340         }}
341       }
342     }
343     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
344     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
345     if(aFileNames.Length() > 0){
346       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
347       aSeq->length(aFileNames.Length());
348       for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
349         aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
350       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
351       if(!isMultiFile)
352         SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
353     }
354     return aStreamFile._retn();
355   }
356
357   SALOMEDS::TMPFile* VISU_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
358                                            const char* theURL,
359                                            bool isMultiFile)
360   {
361     Mutex mt(myMutex);
362     string aDir = isMultiFile ? theURL : SALOMEDS_Tool::GetTmpDir();
363     TCollection_AsciiString aTmpDir (const_cast<char*>(aDir.c_str()));
364     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
365     int aCounter = 0;
366     TColStd_SequenceOfAsciiString aFileNames;
367     SALOMEDS::Study_var aStudy = theComponent->GetStudy();
368     SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
369     for (; itBig->More(); itBig->Next()) {
370       SALOMEDS::SObject_var gotBranch = itBig->Value();
371       CORBA::Object_var anObj = SObjectToObject(gotBranch);
372       if(CORBA::is_nil(anObj)) continue;
373       if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
374         switch(pResult->GetCreationId()){
375         case Result_i::eImportFile:
376         case Result_i::eCopyAndImportFile: {
377           const QFileInfo& aFileInfo = pResult->GetFileInfo();
378           QString aPrefix("");
379           if (isMultiFile) aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL()).c_str();
380           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
381           static QString aCommand;
382           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
383
384           int aRes = system(aCommand);
385           if(aRes){
386             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Can't execute the command :"<<aCommand);
387             continue;
388           }else
389             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
390
391           TCollection_AsciiString aString(strdup(aFileName.latin1()));
392
393           HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir + aString).ToCString()), true);
394
395           aFileNames.Append(aString);
396         }}
397       }
398     }
399     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
400     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
401     if(aFileNames.Length() > 0){
402       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
403       aSeq->length(aFileNames.Length());
404       for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
405         aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
406       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
407       if(!isMultiFile)
408         SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
409     }
410     return aStreamFile._retn();
411   }
412
413   char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
414                                            const char* IORString,
415                                            CORBA::Boolean isMultiFile,
416                                            CORBA::Boolean isASCII)
417   {
418     CORBA::String_var aString(IORString);
419     if(strcmp(IORString,"") != 0){
420       CORBA::ORB_ptr anORB = GetORB();
421       CORBA::Object_var anObj = anORB->string_to_object(aString);
422       if(!CORBA::is_nil(anObj)){
423         if(Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj).in())){
424           Mutex mt(myMutex);
425           aString = pStorable->ToString().c_str();
426           return aString._retn();
427         }
428       }
429     }
430     return aString._retn();
431   }
432
433   char* VISU_Gen_i::GetID(){
434     return Base_i::GetID();
435   }
436
437   void VISU_Gen_i::SetCurrentStudy (SALOMEDS::Study_ptr theStudy)
438   {
439     class TEvent: public SALOME_Event {
440       std::string myStudyName;
441     public:
442       TEvent(const std::string theStudyName):myStudyName(theStudyName)
443         {}
444       virtual void Execute()
445         {
446           bool isActive = false;
447           SUIT_Session* aSession = SUIT_Session::session();
448           QPtrList<SUIT_Application> anApplications = aSession->applications();
449           QPtrListIterator<SUIT_Application> anIter (anApplications);
450           SUIT_Application* aFirstApp = anIter.current();
451           while (SUIT_Application* anApp = anIter.current()) {
452             ++anIter;
453             if (SUIT_Study* aSStudy = anApp->activeStudy()) {
454               if (SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(aSStudy)) {
455                 if (_PTR(Study) aCStudy = aStudy->studyDS()) {
456                   if(MYDEBUG) MESSAGE("There is an application with active study : StudyId = "
457                                       << aCStudy->StudyId() << "; Name = '" << aCStudy->Name() << "'");
458                   if (myStudyName == aCStudy->Name()) {
459                     isActive = true;
460                     break;
461                   }
462                 }
463               }
464             }
465           }
466           if (!isActive) {
467             MESSAGE("!!! anApp->onLoadDoc(myStudyName) !!!");
468             // Has to be loaded in an empty or in a new application
469             SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(aFirstApp);
470             anApp->onLoadDoc(myStudyName.c_str());
471           }
472         }
473     };
474
475     if (!CORBA::is_nil(theStudy))
476     {
477       CORBA::String_var aName = theStudy->Name();
478       std::string aStudyName (aName.in());
479       if(MYDEBUG) MESSAGE("StudyId = " << theStudy->StudyId() << "; Name = '" << aName.in() << "'");
480       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
481
482       ProcessVoidEvent(new TEvent(aStudyName));
483     } else {
484       INFOS("CORBA::is_nil(theStudy)");
485     }
486   }
487
488   SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
489     return SALOMEDS::Study::_duplicate(myStudyDocument);
490   }
491
492   ViewManager_ptr VISU_Gen_i::GetViewManager(){
493     Mutex mt(myMutex);
494     ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
495     return ViewManager::_duplicate(aViewManager->_this());
496   }
497
498   SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
499     if(myStudyDocument->GetProperties()->IsLocked())
500       return SALOMEDS::SObject::_nil();
501     Mutex mt(myMutex);
502     SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
503     return aRes._retn();
504   }
505
506   CORBA::Boolean VISU_Gen_i::ExportTableToFile(SALOMEDS::SObject_ptr theTable,
507                                                const char* theFileName)
508   {
509     return VISU::ExportTableToFile(theTable, theFileName);
510   }
511
512   Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
513     if(myStudyDocument->GetProperties()->IsLocked())
514       return Result::_nil();
515     Mutex mt(myMutex);
516     aFileInfo.setFile(theFileName);
517     Result_i* pResult = new Result_i(myStudyDocument,
518                                      Result_i::eFile,
519                                      Result_i::eImportFile);
520     if(pResult->Create(theFileName) != NULL)
521       return pResult->_this();
522     else{
523       pResult->_remove_ref();
524       return VISU::Result::_nil();
525     }
526   }
527
528   Result_ptr VISU_Gen_i::CreateResult(const char* theFileName){
529     if(myStudyDocument->GetProperties()->IsLocked())
530       return Result::_nil();
531     Mutex mt(myMutex);
532     aFileInfo.setFile(theFileName);
533     Result_i* pResult = new Result_i(myStudyDocument,
534                                      Result_i::eFile,
535                                      Result_i::eImportFile,
536                                      false);
537     if(pResult->Create(theFileName) != NULL)
538       return pResult->_this();
539     else{
540       pResult->_remove_ref();
541       return VISU::Result::_nil();
542     }
543   }
544
545   Result_ptr VISU_Gen_i::CopyAndImportFile(const char* theFileName){
546     if(myStudyDocument->GetProperties()->IsLocked())
547       return Result::_nil();
548     Mutex mt(myMutex);
549     VISU::Result_var aResult;
550     aFileInfo.setFile(theFileName);
551     Result_i* pResult = new Result_i(myStudyDocument,
552                                      Result_i::eRestoredFile,
553                                      Result_i::eCopyAndImportFile);
554     if(pResult->Create(theFileName) != NULL)
555       aResult = pResult->_this();
556     return aResult._retn();
557   }
558
559   Result_ptr VISU_Gen_i::ImportMed (SALOMEDS::SObject_ptr theMedSObject)
560   {
561     if (myStudyDocument->GetProperties()->IsLocked())
562       return Result::_nil();
563     Mutex mt(myMutex);
564     Result_i* pResult = new Result_i(myStudyDocument,
565                                      Result_i::eComponent,
566                                      Result_i::eImportMed);
567     if (pResult->Create(theMedSObject) != NULL) {
568       return pResult->_this();
569     } else {
570       pResult->_remove_ref();
571       return VISU::Result::_nil();
572     }
573   }
574
575   Result_ptr VISU_Gen_i::ImportMedField (SALOME_MED::FIELD_ptr theField)
576   {
577     if (myStudyDocument->GetProperties()->IsLocked())
578       return Result::_nil();
579     Mutex mt(myMutex);
580     Result_i* pResult = new Result_i(myStudyDocument,
581                                      Result_i::eComponent,
582                                      Result_i::eImportMedField);
583     if (pResult->Create(theField) != NULL) {
584       return pResult->_this();
585     } else {
586       pResult->_remove_ref();
587       return VISU::Result::_nil();
588     }
589   }
590
591   Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult,
592                                     const char* theMeshName,
593                                     VISU::Entity theEntity)
594   {
595     if(myStudyDocument->GetProperties()->IsLocked())
596       return Mesh::_nil();
597     Mutex mt(myMutex);
598     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
599       Mesh_i* aPresent = new Mesh_i(pResult);
600       if(aPresent->Create(theMeshName,theEntity) != NULL)
601         return aPresent->_this();
602       else{
603         aPresent->_remove_ref();
604       }
605     }
606     return VISU::Mesh::_nil();
607   }
608
609   Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult,
610                                           const char* theMeshName,
611                                           VISU::Entity theEntity,
612                                           const char* theFamilyName)
613   {
614     if(myStudyDocument->GetProperties()->IsLocked())
615       return Mesh::_nil();
616     Mutex mt(myMutex);
617     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
618       Mesh_i* aPresent = new Mesh_i(pResult);
619       if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL)
620         return aPresent->_this();
621       else{
622         aPresent->_remove_ref();
623       }
624     }
625     return VISU::Mesh::_nil();
626   }
627
628   Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult,
629                                  const char* theMeshName,
630                                  const char* theGroupName)
631   {
632     if(myStudyDocument->GetProperties()->IsLocked())
633       return Mesh::_nil();
634     Mutex mt(myMutex);
635     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
636       Mesh_i* aPresent = new Mesh_i(pResult);
637       if(aPresent->Create(theMeshName,theGroupName) != NULL)
638         return aPresent->_this();
639       else{
640         aPresent->_remove_ref();
641       }
642     }
643     return VISU::Mesh::_nil();
644   }
645
646   ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult,
647                                              const char* theMeshName,
648                                              VISU::Entity theEntity,
649                                              const char* theFieldName,
650                                              CORBA::Double theIteration)
651   {
652     return Prs3dOnField<VISU::ScalarMap_i>(theResult,theMeshName,theEntity,theFieldName,theIteration,true)._retn();
653   }
654
655   GaussPoints_ptr VISU_Gen_i::GaussPointsOnField(Result_ptr theResult,
656                                                  const char* theMeshName,
657                                                  VISU::Entity theEntity,
658                                                  const char* theFieldName,
659                                                  CORBA::Double theIteration)
660   {
661     return Prs3dOnField<VISU::GaussPoints_i>(theResult,theMeshName,theEntity,theFieldName,theIteration,true)._retn();
662   }
663
664   DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult,
665                                                      const char* theMeshName,
666                                                      VISU::Entity theEntity,
667                                                      const char* theFieldName,
668                                                      CORBA::Double theIteration)
669   {
670     return Prs3dOnField<VISU::DeformedShape_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
671   }
672
673   ScalarMapOnDeformedShape_ptr VISU_Gen_i::ScalarMapOnDeformedShapeOnField(Result_ptr theResult,
674                                                                            const char* theMeshName,
675                                                                            VISU::Entity theEntity,
676                                                                            const char* theFieldName,
677                                                                            CORBA::Double theIteration)
678   {
679     return Prs3dOnField<VISU::ScalarMapOnDeformedShape_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
680   }
681   
682   Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult,
683                                          const char* theMeshName,
684                                          VISU::Entity theEntity,
685                                          const char* theFieldName,
686                                          CORBA::Double theIteration)
687   {
688     return Prs3dOnField<VISU::Vectors_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
689   }
690
691   IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult,
692                                                  const char* theMeshName,
693                                                  VISU::Entity theEntity,
694                                                  const char* theFieldName,
695                                                  CORBA::Double theIteration)
696   {
697     return Prs3dOnField<VISU::IsoSurfaces_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
698   }
699
700   StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult,
701                                                  const char* theMeshName,
702                                                  VISU::Entity theEntity,
703                                                  const char* theFieldName,
704                                                  CORBA::Double theIteration)
705   {
706     return Prs3dOnField<VISU::StreamLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
707   }
708
709   Plot3D_ptr VISU_Gen_i::Plot3DOnField(Result_ptr theResult,
710                                        const char* theMeshName,
711                                        VISU::Entity theEntity,
712                                        const char* theFieldName,
713                                        CORBA::Double theIteration)
714   {
715     return Prs3dOnField<VISU::Plot3D_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
716   }
717
718   CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult,
719                                              const char* theMeshName,
720                                              VISU::Entity theEntity,
721                                              const char* theFieldName,
722                                              CORBA::Double theIteration)
723   {
724     return Prs3dOnField<VISU::CutPlanes_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
725   }
726
727   CutLines_ptr VISU_Gen_i::CutLinesOnField(Result_ptr theResult,
728                                            const char* theMeshName,
729                                            VISU::Entity theEntity,
730                                            const char* theFieldName,
731                                            CORBA::Double theIteration)
732   {
733     return Prs3dOnField<VISU::CutLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
734   }
735
736   Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
737     if(myStudyDocument->GetProperties()->IsLocked())
738       return Table::_nil();
739     Mutex mt(myMutex);
740     Table_i* pPresent = new Table_i(myStudyDocument,theTableEntry);
741     if(pPresent->Create() != NULL)
742       return pPresent->_this();
743     else{
744       pPresent->_remove_ref();
745       return VISU::Table::_nil();
746     }
747   }
748
749   Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable,
750                                     CORBA::Long theHRow,
751                                     CORBA::Long theVRow)
752   {
753     if(myStudyDocument->GetProperties()->IsLocked())
754       return Curve::_nil();
755     Mutex mt(myMutex);
756     PortableServer::POA_ptr aPOA = GetPOA();
757     Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
758     Curve_i* pPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
759     if(pPresent->Create() != NULL)
760       return pPresent->_this();
761     else{
762       pPresent->_remove_ref();
763       return VISU::Curve::_nil();
764     }
765   }
766
767   Container_ptr VISU_Gen_i::CreateContainer(){
768     if(myStudyDocument->GetProperties()->IsLocked())
769       return Container::_nil();
770     Mutex mt(myMutex);
771     Container_i* pPresent = new Container_i(myStudyDocument);
772     if(pPresent->Create() != NULL)
773       return pPresent->_this();
774     else{
775       pPresent->_remove_ref();
776       return VISU::Container::_nil();
777     }
778   }
779
780   Animation_ptr VISU_Gen_i::CreateAnimation(View3D_ptr theView3D){
781     if(myStudyDocument->GetProperties()->IsLocked())
782       return Animation::_nil();
783     Mutex mt(myMutex);
784     if(VISU_TimeAnimation_i* anAnim = new VISU_TimeAnimation_i(myStudyDocument,theView3D)){
785       return anAnim->_this();
786     }else
787       return VISU::Animation::_nil();
788   }
789
790   void VISU_Gen_i::DeleteResult (Result_ptr theResult)
791   {
792     theResult->RemoveFromStudy();
793   }
794
795   void VISU_Gen_i::DeletePrs3d (Prs3d_ptr thePrs3d)
796   {
797     thePrs3d->RemoveFromStudy();
798   }
799
800   void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
801   }
802
803   char* VISU_Gen_i::ComponentDataType(){
804     return CORBA::string_dup("VISU");
805   }
806
807   bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
808     Result_var aResultObj = Result::_narrow(theIOR);
809     return !(aResultObj->_is_nil());
810   }
811
812   SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
813                                                    SALOMEDS::SObject_ptr theSObject,
814                                                    CORBA::Object_ptr theObject,
815                                                    const char* theName)
816     throw (SALOME::SALOME_Exception)
817   {
818     Unexpect aCatch(SalomeException);
819     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
820     Mutex mt(myMutex);
821     SALOMEDS::SObject_var aResultSO;
822     Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject).in());
823     if (!aResultObj) return aResultSO._retn();
824     const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
825     CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
826     aResultSO = theStudy->FindObjectID(anEntry);
827     return aResultSO._retn();
828   }
829
830   CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
831     Mutex mt(myMutex);
832     SALOMEDS::GenericAttribute_var anAttr;
833     if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
834     try {
835       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
836       CORBA::String_var aValue = anIOR->Value();
837       if(strcmp(aValue,"") != 0){
838         CORBA::Object_ptr anObj = GetORB()->string_to_object(aValue);
839         if (!CORBA::is_nil(anObj)){
840           Result_var aResultObj = Result::_narrow(anObj);
841           if(!aResultObj->_is_nil()){
842             if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(aResultObj).in())){
843               switch(pResult->GetCreationId()){
844               case Result_i::eImportFile:
845               case Result_i::eCopyAndImportFile:
846                 return true;
847               }
848             }
849           }
850         }
851       }
852     }catch(std::exception& exc){
853       INFOS("Follow exception was occured :\n"<<exc.what());
854     }catch (...){
855       INFOS("Unknown exception was occured!");
856     }
857     return false;
858   }
859
860   SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
861     Mutex mt(myMutex);
862     theObjectID = 0;
863     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
864     SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
865
866     PortableServer::POA_ptr aPOA = GetPOA();
867
868     SALOMEDS::GenericAttribute_var anAttr;
869     if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
870     SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
871     CORBA::Object_var aCorbaObj;
872     try {
873       aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
874     } catch(...) {
875       aStreamFile->length(1);
876       aStreamFile[0] = strdup("E")[0];
877       return aStreamFile._retn();
878     }
879     if (CORBA::is_nil(aCorbaObj)) {
880       return NULL;
881     }
882
883     Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj).in());
884     if (!pStorable) {
885       return NULL;
886     }
887
888     string aTmpDir = SALOMEDS_Tool::GetTmpDir();
889     string aCopyPersist =  aTmpDir + "copy_persistent";
890
891     ofstream stmOut2(aCopyPersist.c_str(),ios::out);
892     string aStr = pStorable->ToString().c_str();
893     stmOut2<<aStr<<endl;
894     stmOut2.close();
895
896     if (Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj))) {
897       string aFileName = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + "_";
898       if(strlen(aFileName.c_str()) == 1) aFileName="";
899       const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
900       aFileName += aFileInfo.fileName().latin1();
901       static QString aCommand;
902       string aFullFileName =  aTmpDir + aFileName;
903       aCommand.sprintf("cp %s %s",
904                        aFileInfo.filePath().latin1(),
905                        aFullFileName.c_str());
906       if(system(aCommand) == -1) {
907         if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
908         return NULL;
909       }
910       aSeq->length(2);
911       aSeq[0] = "copy_persistent";
912       aSeq[1] = aFileName.c_str();
913     } else {
914       aSeq->length(1);
915       aSeq[0] = "copy_persistent";
916     }
917
918     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), false);
919     SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
920
921     // Assign an ID = 1 the the type VISU::Result
922     theObjectID = 1;
923
924
925     SALOMEDS::SComponent_var aSComponent = theObject->GetStudy()->FindComponent("VISU");
926     return aStreamFile._retn();
927   }
928
929   CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
930     // The VISU component can paste only objects copied by VISU component
931     // and with the object type = 1
932     if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1)
933       return false;
934     return true;
935   }
936
937   SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
938                                               CORBA::Long theObjectID,
939                                               SALOMEDS::SObject_ptr theObject)
940   {
941     Mutex mt(myMutex);
942     SALOMEDS::SObject_var aResultSO;
943     if (theObjectID != 1)
944       return aResultSO._retn();
945
946     string aTmpDir = SALOMEDS_Tool::GetTmpDir();
947     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), false);
948
949     ifstream stmIn((aTmpDir + string("copy_persistent")).c_str());
950     stmIn.seekg(0, ios::end);
951     int aLength = stmIn.tellg();
952     stmIn.seekg(0, ios::beg);
953     char* aString = new char[aLength+1];
954     stmIn.read(aString, aLength);
955     aString[aLength] = 0;
956     myIsMultiFile = false;
957
958     string aFileName(aTmpDir);
959     string aBasicFileName;
960     if(aSeq->length() > 1) {
961       aBasicFileName = aSeq[1].in();
962       aFileName += aBasicFileName;
963     }
964
965     SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
966     SALOMEDS::Study_var aStudy = theObject->GetStudy();
967     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
968     CORBA::String_var aComponentID(aComponent->GetID()), aSObjID(theObject->GetID());
969
970     if (strcmp(aComponentID, aSObjID) == 0) //create the new result SObject
971       aResultSO = aStudyBuilder->NewObject(aComponent);
972     else
973       aResultSO = SALOMEDS::SObject::_duplicate(theObject);
974
975     //Just for Result::Restore to find the Comment attribute :(
976     SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeComment");
977
978     Storable* aStorable = Storable::Create(aResultSO,aFileName,aString);
979
980     SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
981     aSeqToRm->length(1);
982     aSeqToRm[0] = "copy_persistent";
983
984     SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeqToRm.in(), true);
985
986     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
987     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
988     CORBA::String_var anIORValue(aStorable->GetID());
989     anIOR->SetValue(anIORValue);
990     return aResultSO._retn();
991   }
992 }