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