]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Gen_i.cc
Salome HOME
Fix on Bug PAL7927
[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_Table_i.hh"
42 #include "VISU_TimeAnimation.h"
43
44 #include "HDFascii.hxx"
45 #include "SALOMEDS_Tool.hxx"
46
47 #include "utilities.h"
48
49 #include <strstream>    
50 #include <TCollection_AsciiString.hxx>
51 #include <TColStd_SequenceOfAsciiString.hxx>
52
53 #include <omnithread.h> 
54 #include CORBA_SERVER_HEADER(SALOME_Session)
55 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
56
57 #include <qstring.h>
58 #include <qfileinfo.h>
59 #include "Utils_ExceptHandlers.hxx"
60
61 using namespace std;
62
63 static QFileInfo aFileInfo;
64
65 #ifdef _DEBUG_
66 static int MYDEBUG = 1;
67 #else
68 static int MYDEBUG = 0;
69 #endif
70
71 UNEXPECT_CATCH(SalomeException, SALOME::SALOME_Exception);
72
73 extern "C" VISU::VISU_Gen_ptr GetImpl(CORBA::ORB_ptr theORB, 
74                                       PortableServer::POA_ptr thePOA, 
75                                       SALOME_NamingService* theNamingService, 
76                                       QMutex* theMutex)
77 {
78   if(MYDEBUG) MESSAGE("extern \"C\" GetImpl");
79   VISU::VISU_Gen_i *aVISU_Gen = new VISU::VISU_Gen_i(theORB,thePOA,theNamingService,theMutex);
80   //return VISU::VISU_Gen::_duplicate(aVISU_Gen->_this());
81   return aVISU_Gen->_this();
82 }
83
84 namespace VISU{
85   static string VisuTmpDir;
86
87   static CORBA::Boolean myIsMultiFile;
88   const CORBA::Boolean IsMultifile() { return myIsMultiFile;}
89
90   //===========================================================================
91   SALOMEDS::SComponent_var FindOrCreateVisuComponent(SALOMEDS::Study_ptr theStudyDocument){
92     SALOMEDS::SComponent_var aSComponent = theStudyDocument->FindComponent("VISU");
93     if (aSComponent->_is_nil()) {
94       SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
95       aStudyBuilder->NewCommand();
96       int aLocked = theStudyDocument->GetProperties()->IsLocked();
97       if (aLocked) theStudyDocument->GetProperties()->SetLocked(false);
98       aSComponent = aStudyBuilder->NewComponent("VISU"); 
99       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributeName");
100       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
101
102       //NRI      aName->SetValue("Visu");
103       CORBA::ORB_var anORB = Base_i::GetORB();
104       SALOME_NamingService *NamingService = new SALOME_NamingService( anORB );
105       CORBA::Object_var objVarN = NamingService->Resolve("/Kernel/ModulCatalog");
106       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
107       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "VISU" );
108       if ( !Comp->_is_nil() ) {
109         aName->SetValue( Comp->componentusername() );
110       }
111
112       anAttr = aStudyBuilder->FindOrCreateAttribute(aSComponent, "AttributePixMap");
113       SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
114       aPixmap->SetPixMap( "ICON_OBJBROWSER_Visu" );
115       
116       VISU_Gen_var aVisuGen = Base_i::GetVisuGenImpl()->_this();
117       aStudyBuilder->DefineComponentInstance(aSComponent,aVisuGen);
118       if (aLocked) theStudyDocument->GetProperties()->SetLocked(true);
119       aStudyBuilder->CommitCommand();
120     }
121     return aSComponent;
122   }    
123
124   void RegistryStorable() {
125     Storable::Registry(Result_i::myComment.c_str(),&(Result_i::Restore));
126     Storable::Registry(Mesh_i::myComment.c_str(),&(Restore<Mesh_i>));
127     Storable::Registry(ScalarMap_i::myComment.c_str(),&(Restore<ScalarMap_i>));
128     Storable::Registry(DeformedShape_i::myComment.c_str(),&(Restore<DeformedShape_i>));
129     Storable::Registry(CutPlanes_i::myComment.c_str(),&(Restore<CutPlanes_i>));
130     Storable::Registry(CutLines_i::myComment.c_str(),&(Restore<CutLines_i>));
131     Storable::Registry(IsoSurfaces_i::myComment.c_str(),&(Restore<IsoSurfaces_i>));
132     Storable::Registry(StreamLines_i::myComment.c_str(),&(Restore<StreamLines_i>));
133     Storable::Registry(Vectors_i::myComment.c_str(),&(Restore<Vectors_i>));
134     Storable::Registry(Table_i::myComment.c_str(),&(Table_i::Restore));
135     Storable::Registry(Curve_i::myComment.c_str(),&(Curve_i::Restore));
136     Storable::Registry(Container_i::myComment.c_str(),&(Container_i::Restore));
137   }
138
139
140   //===========================================================================
141   VISU_Gen_i::VISU_Gen_i(CORBA::ORB_ptr theORB, PortableServer::POA_ptr thePOA, 
142                          SALOME_NamingService* theNamingService, QMutex* theMutex) :
143     Engines_Component_i()
144   {
145     if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : "<<theMutex);
146     Base_i::myMutex = theMutex;  //apo
147     Base_i::myOrb = CORBA::ORB::_duplicate(theORB);
148     Base_i::myPoa = PortableServer::POA::_duplicate(thePOA);
149     Base_i::myNamingService = theNamingService;
150     static SALOME_LifeCycleCORBA aEnginesLifeCycle(theNamingService);
151     Base_i::myEnginesLifeCycle = &aEnginesLifeCycle;
152     Base_i::myVisuGenImpl = this;
153     RegistryStorable();
154
155     CORBA::Object_var anObj = myNamingService->Resolve("/myStudyManager");
156     SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(anObj);
157     SALOMEDS::ListOfOpenStudies_var aListOfOpenStudies = aStudyManager->GetOpenStudies();
158     if(aListOfOpenStudies->length() > 0) {
159       CORBA::String_var aStudyName = aListOfOpenStudies[0];
160       //aFileInfo.setFile(aStudyName.in());
161       myStudyDocument = aStudyManager->GetStudyByName(aStudyName/*aFileInfo.baseName()*/);
162     }else
163       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
164   }
165
166   VISU_Gen_i::~VISU_Gen_i(){
167     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
168   }
169   //===========================================================================
170   CORBA::Boolean VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
171                                   const SALOMEDS::TMPFile & theStream,
172                                   const char* theURL,
173                                   bool isMultiFile)
174   {
175     Mutex mt(myMutex);
176     SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
177     SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder(); 
178     const char* aDir = isMultiFile? theURL: SALOMEDS_Tool::GetTmpDir().c_str();
179     TCollection_AsciiString aTmpDir(const_cast<char*>(aDir));
180     VisuTmpDir = aTmpDir.ToCString();
181     SALOMEDS::ListOfFileNames_var aSeq =
182       SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
183     myIsMultiFile = isMultiFile;
184     return true;
185   }
186
187   CORBA::Boolean VISU_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
188                                        const SALOMEDS::TMPFile & theStream,
189                                        const char* theURL,
190                                        bool isMultiFile) 
191   {
192     return Load(theComponent, theStream, theURL, isMultiFile);
193   }
194
195   char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
196                                            const char* aLocalPersistentID,
197                                            CORBA::Boolean isMultiFile,
198                                            CORBA::Boolean isASCII) 
199   {
200     CORBA::String_var aString("");
201     if(strcmp(aLocalPersistentID,"") != 0) {
202       Mutex mt(myMutex);
203       Storable* aStorable =
204         Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
205       if(aStorable != NULL) aString = aStorable->GetID();
206     }
207     return aString._retn();
208   }
209   //===========================================================================
210   SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
211                                       const char* theURL,
212                                       bool isMultiFile)
213   {
214     Mutex mt(myMutex);
215     const char* aDir = isMultiFile? theURL: SALOMEDS_Tool::GetTmpDir().c_str();
216     TCollection_AsciiString aTmpDir(const_cast<char*>(aDir));
217     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
218     int aCounter = 0;
219     TColStd_SequenceOfAsciiString aFileNames;
220     SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
221     SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
222     for (; itBig->More(); itBig->Next()) {
223       SALOMEDS::SObject_var gotBranch = itBig->Value();
224       CORBA::Object_var anObj = SObjectToObject(gotBranch);
225       if(CORBA::is_nil(anObj)) continue;
226       if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
227         const Result_i::TSourceId& aSourceId = pResult->GetSourceId();
228         if(aSourceId == Result_i::eFile || aSourceId == Result_i::eRestoredFile){
229           const QFileInfo& aFileInfo = pResult->GetFileInfo();
230           QString aPrefix("");
231           if (isMultiFile) aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL()).c_str();
232           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
233           static QString aCommand;
234           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
235
236           int aRes = system(aCommand);
237           if(aRes){
238             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
239             continue;
240           }else
241             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aCommand = "<<aCommand);
242
243           TCollection_AsciiString aString(strdup(aFileName.latin1()));
244           aFileNames.Append(aString);
245         }
246       }
247     }
248     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
249     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
250     if(aFileNames.Length() > 0){
251       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
252       aSeq->length(aFileNames.Length());
253       for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
254         aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
255       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
256       if(!isMultiFile)
257         SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
258     }
259     return aStreamFile._retn();
260   }
261
262   SALOMEDS::TMPFile* VISU_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
263                                            const char* theURL,
264                                            bool isMultiFile) 
265   {
266     Mutex mt(myMutex);
267     const char* aDir = isMultiFile? theURL: SALOMEDS_Tool::GetTmpDir().c_str();
268     TCollection_AsciiString aTmpDir(const_cast<char*>(aDir));
269     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aTmpDir);
270     int aCounter = 0;
271     TColStd_SequenceOfAsciiString aFileNames;
272     SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
273     SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
274     for (; itBig->More(); itBig->Next()) {
275       SALOMEDS::SObject_var gotBranch = itBig->Value();
276       CORBA::Object_var anObj = SObjectToObject(gotBranch);
277       if(CORBA::is_nil(anObj)) continue;
278       if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj).in())){
279         const Result_i::TSourceId& aSourceId = pResult->GetSourceId();
280         if(aSourceId == Result_i::eFile || aSourceId == Result_i::eRestoredFile){
281           const QFileInfo& aFileInfo = pResult->GetFileInfo();
282           QString aPrefix("");
283           if (isMultiFile) aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL()).c_str();
284           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
285           static QString aCommand;
286           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
287
288           int aRes = system(aCommand);
289           if(aRes){       
290             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Can't execute the command :"<<aCommand);
291             continue;
292           }else
293             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
294
295           TCollection_AsciiString aString(strdup(aFileName.latin1()));
296           
297           HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir + aString).ToCString()), true);
298           
299           aFileNames.Append(aString);
300         }
301       }
302     }
303     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
304     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
305     if(aFileNames.Length() > 0){
306       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
307       aSeq->length(aFileNames.Length());
308       for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
309         aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
310       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
311       if(!isMultiFile)
312         SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
313     }
314     return aStreamFile._retn();
315   }
316
317   char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
318                                            const char* IORString,
319                                            CORBA::Boolean isMultiFile,
320                                            CORBA::Boolean isASCII) 
321   {
322     CORBA::String_var aString(IORString);
323     if(strcmp(IORString,"") != 0){ 
324       CORBA::ORB_ptr anORB = GetORB();
325       CORBA::Object_var anObj = anORB->string_to_object(aString);
326       if(!CORBA::is_nil(anObj)){
327         if(Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj).in())){
328           Mutex mt(myMutex);
329           aString = pStorable->ToString().c_str();
330           return aString._retn();
331         }
332       }
333     }
334     return aString._retn();
335   }
336
337   char* VISU_Gen_i::GetID(){
338     return Base_i::GetID();
339   }
340
341   void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
342     if(!CORBA::is_nil(theStudy)){
343       CORBA::String_var aName = theStudy->Name();
344       MESSAGE("VISU_Gen_i::SetCurrentStudy - StudyId = "<<theStudy->StudyId()<<"; Name = '"<<aName.in()<<"'");
345       myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
346     }else{
347       MESSAGE("VISU_Gen_i::SetCurrentStudy : CORBA::is_nil(theStudy)");
348     }
349   }
350
351   SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
352     return SALOMEDS::Study::_duplicate(myStudyDocument);
353   }
354
355   ViewManager_ptr VISU_Gen_i::GetViewManager(){
356     Mutex mt(myMutex);
357     ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
358     return ViewManager::_duplicate(aViewManager->_this());
359   }
360
361   SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
362     if(myStudyDocument->GetProperties()->IsLocked()) 
363       return SALOMEDS::SObject::_nil();
364     Mutex mt(myMutex);
365     SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
366     return aRes._retn();
367   }
368
369   CORBA::Boolean VISU_Gen_i::ExportTableToFile(SALOMEDS::SObject_ptr theTable,
370                                                const char* theFileName)
371   {
372     return VISU::ExportTableToFile(theTable, theFileName);
373   }
374
375   Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
376     if(myStudyDocument->GetProperties()->IsLocked()) 
377       return Result::_nil();
378     Mutex mt(myMutex);
379     aFileInfo.setFile(theFileName);
380     Result_i* pResult = new Result_i(myStudyDocument);
381     if(pResult->Create(theFileName) != NULL)    
382       return pResult->_this();
383     else{
384       pResult->_remove_ref();
385       return VISU::Result::_nil();
386     }
387   }
388
389   Result_ptr VISU_Gen_i::CopyAndImportFile(const char* theFileName){
390     if(myStudyDocument->GetProperties()->IsLocked()) 
391       return Result::_nil();
392     Mutex mt(myMutex);
393     VISU::Result_var aResult;
394     aFileInfo.setFile(theFileName);
395     Result_i* pResult = new Result_i(myStudyDocument,Result_i::eRestoredFile);
396     if(pResult->Create(theFileName) != NULL) 
397       aResult = pResult->_this();
398     return aResult._retn();
399   }
400
401   Result_ptr VISU_Gen_i::ImportMed(SALOMEDS::SObject_ptr theMedSObject){
402     if(myStudyDocument->GetProperties()->IsLocked()) 
403       return Result::_nil();
404     Mutex mt(myMutex);
405     Result_i* pResult = new Result_i(myStudyDocument);
406     if(pResult->Create(theMedSObject) != NULL)
407       return pResult->_this();
408     else{
409       pResult->_remove_ref();
410       return VISU::Result::_nil();
411     }
412   }
413
414   Result_ptr VISU_Gen_i::ImportMedField(SALOME_MED::FIELD_ptr theField){
415     if(myStudyDocument->GetProperties()->IsLocked()) 
416       return Result::_nil();
417     Mutex mt(myMutex);
418     Result_i* pResult = new Result_i(myStudyDocument);
419     if(pResult->Create(theField) != NULL)
420       return pResult->_this();
421     else{
422       pResult->_remove_ref();
423       return VISU::Result::_nil();
424     }
425   }
426
427   Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, 
428                                     const char* theMeshName, 
429                                     VISU::Entity theEntity)
430   {
431     if(myStudyDocument->GetProperties()->IsLocked()) 
432       return Mesh::_nil();
433     Mutex mt(myMutex);
434     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
435       Mesh_i* aPresent = new Mesh_i(pResult);
436       if(aPresent->Create(theMeshName,theEntity) != NULL)
437         return aPresent->_this();
438       else{
439         aPresent->_remove_ref();
440       }
441     }
442     return VISU::Mesh::_nil();
443   }
444
445   Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, 
446                                           const char* theMeshName, 
447                                           VISU::Entity theEntity, 
448                                           const char* theFamilyName)
449   {
450     if(myStudyDocument->GetProperties()->IsLocked()) 
451       return Mesh::_nil();
452     Mutex mt(myMutex);
453     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
454       Mesh_i* aPresent = new Mesh_i(pResult);
455       if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL)
456         return aPresent->_this();
457       else{
458         aPresent->_remove_ref();
459       }
460     }
461     return VISU::Mesh::_nil();
462   }
463
464   Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, 
465                                  const char* theMeshName, 
466                                  const char* theGroupName)
467   {
468     if(myStudyDocument->GetProperties()->IsLocked()) 
469       return Mesh::_nil();
470     Mutex mt(myMutex);
471     if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult).in())){
472       Mesh_i* aPresent = new Mesh_i(pResult);
473       if(aPresent->Create(theMeshName,theGroupName) != NULL)
474         return aPresent->_this();
475       else{
476         aPresent->_remove_ref();
477       }
478     }
479     return VISU::Mesh::_nil();
480   }
481
482   ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, 
483                                              const char* theMeshName, 
484                                              VISU::Entity theEntity, 
485                                              const char* theFieldName, 
486                                              CORBA::Double theIteration)
487   {
488     return Prs3dOnField<VISU::ScalarMap_i>(theResult,theMeshName,theEntity,theFieldName,theIteration,true)._retn();
489   }
490
491   DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, 
492                                                      const char* theMeshName, 
493                                                      VISU::Entity theEntity, 
494                                                      const char* theFieldName, 
495                                                      CORBA::Double theIteration)
496   {
497     return Prs3dOnField<VISU::DeformedShape_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
498   }
499
500   Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, 
501                                          const char* theMeshName, 
502                                          VISU::Entity theEntity, 
503                                          const char* theFieldName, 
504                                          CORBA::Double theIteration)
505   {
506     return Prs3dOnField<VISU::Vectors_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
507   }
508
509   IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, 
510                                                  const char* theMeshName, 
511                                                  VISU::Entity theEntity, 
512                                                  const char* theFieldName, 
513                                                  CORBA::Double theIteration)
514   {
515     return Prs3dOnField<VISU::IsoSurfaces_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
516   }
517
518   StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, 
519                                                  const char* theMeshName, 
520                                                  VISU::Entity theEntity, 
521                                                  const char* theFieldName, 
522                                                  CORBA::Double theIteration)
523   {
524     return Prs3dOnField<VISU::StreamLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
525   }
526
527   CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, 
528                                              const char* theMeshName, 
529                                              VISU::Entity theEntity, 
530                                              const char* theFieldName, 
531                                              CORBA::Double theIteration)
532   {
533     return Prs3dOnField<VISU::CutPlanes_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
534   }
535
536   CutLines_ptr VISU_Gen_i::CutLinesOnField(Result_ptr theResult, 
537                                            const char* theMeshName, 
538                                            VISU::Entity theEntity, 
539                                            const char* theFieldName, 
540                                            CORBA::Double theIteration)
541   {
542     return Prs3dOnField<VISU::CutLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration)._retn();
543   }
544
545   Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
546     if(myStudyDocument->GetProperties()->IsLocked()) 
547       return Table::_nil();
548     Mutex mt(myMutex);
549     Table_i* pPresent = new Table_i(myStudyDocument,theTableEntry);
550     if(pPresent->Create() != NULL)
551       return pPresent->_this();
552     else{
553       pPresent->_remove_ref();
554       return VISU::Table::_nil();
555     }
556   }
557
558   Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, 
559                                     CORBA::Long theHRow, 
560                                     CORBA::Long theVRow)
561   {
562     if(myStudyDocument->GetProperties()->IsLocked()) 
563       return Curve::_nil();
564     Mutex mt(myMutex);
565     PortableServer::POA_ptr aPOA = GetPOA();
566     Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
567     Curve_i* pPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
568     if(pPresent->Create() != NULL)
569       return pPresent->_this();
570     else{
571       pPresent->_remove_ref();
572       return VISU::Curve::_nil();
573     }
574   }
575
576   Container_ptr VISU_Gen_i::CreateContainer(){
577     if(myStudyDocument->GetProperties()->IsLocked()) 
578       return Container::_nil();
579     Mutex mt(myMutex);
580     Container_i* pPresent = new Container_i(myStudyDocument);
581     if(pPresent->Create() != NULL)
582       return pPresent->_this();
583     else{
584       pPresent->_remove_ref();
585       return VISU::Container::_nil();
586     }
587   }
588
589   Animation_ptr VISU_Gen_i::CreateAnimation(View3D_ptr theView3D){
590     if(myStudyDocument->GetProperties()->IsLocked()) 
591       return Animation::_nil();
592     Mutex mt(myMutex);
593     if(VISU_TimeAnimation_i* anAnim = new VISU_TimeAnimation_i(myStudyDocument,theView3D)){
594       return anAnim->_this();
595     }else
596       return VISU::Animation::_nil();
597   }
598
599   void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
600   }
601
602   char* VISU_Gen_i::ComponentDataType(){
603     return CORBA::string_dup("VISU");
604   }
605
606   bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
607     Result_var aResultObj = Result::_narrow(theIOR);
608     return !(aResultObj->_is_nil());
609   }
610
611   SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
612                                                    SALOMEDS::SObject_ptr theSObject,
613                                                    CORBA::Object_ptr theObject,
614                                                    const char* theName) 
615     throw (SALOME::SALOME_Exception) 
616   {
617     Unexpect aCatch(SalomeException);
618     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
619     Mutex mt(myMutex);
620     SALOMEDS::SObject_var aResultSO;
621     Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject).in());
622     if (!aResultObj) return aResultSO._retn();
623     const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
624     CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
625     aResultSO = theStudy->FindObjectID(anEntry);
626     return aResultSO._retn();
627   }
628     
629   CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
630     Mutex mt(myMutex);
631     SALOMEDS::GenericAttribute_var anAttr;
632     if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
633     try {
634       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
635       CORBA::String_var aValue = anIOR->Value();
636       if(strcmp(aValue,"") != 0){
637         CORBA::Object_ptr anObj = GetORB()->string_to_object(aValue);
638         if (!CORBA::is_nil(anObj)){
639           Result_var aResultObj = Result::_narrow(anObj);
640           if(!aResultObj->_is_nil()){
641             if(Result_i* pResult = dynamic_cast<Result_i*>(GetServant(aResultObj).in())){
642               const Result_i::TSourceId& aSourceId = pResult->GetSourceId();
643               if(aSourceId == Result_i::eComponent || aSourceId == Result_i::eRestoredComponent)
644                 if((pResult->GetFileInfo()).filePath() == "MED") 
645                   return false;
646               return true;
647             }
648           }
649         }
650       }
651     }catch(std::exception& exc){
652       INFOS("Follow exception was occured :\n"<<exc.what());
653     }catch (...){
654       INFOS("Unknown exception was occured!");
655     }
656     return false;
657   }
658
659   SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
660     Mutex mt(myMutex);
661     theObjectID = 0;
662     SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
663     SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
664
665     PortableServer::POA_ptr aPOA = GetPOA();
666     
667     SALOMEDS::GenericAttribute_var anAttr;
668     if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
669     SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
670     CORBA::Object_var aCorbaObj;
671     try {
672       aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
673     } catch(...) {
674       aStreamFile->length(1);
675       aStreamFile[0] = strdup("E")[0];
676       return aStreamFile._retn();
677     }
678     if (CORBA::is_nil(aCorbaObj)) {
679       return NULL;
680     }
681     
682     Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj).in());
683     if (!pStorable) {
684       return NULL;
685     }
686
687     string aTmpDir = SALOMEDS_Tool::GetTmpDir();
688     string aCopyPersist =  aTmpDir + "copy_persistent";
689
690     ofstream stmOut2(aCopyPersist.c_str(),ios::out);
691     string aStr = pStorable->ToString().c_str();
692     stmOut2<<aStr<<endl;
693     stmOut2.close();
694     
695     if (Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj))) {
696       string aFileName = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + "_";
697       if(strlen(aFileName.c_str()) == 1) aFileName="";
698       const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
699       aFileName += aFileInfo.fileName().latin1();
700       static QString aCommand;
701       string aFullFileName =  aTmpDir + aFileName;
702       aCommand.sprintf("cp %s %s",
703                        aFileInfo.filePath().latin1(),
704                        aFullFileName.c_str());
705       if(system(aCommand) == -1) {
706         if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
707         return NULL;
708       }
709       aSeq->length(2);
710       aSeq[0] = "copy_persistent";
711       aSeq[1] = aFileName.c_str();
712     } else {
713       aSeq->length(1);
714       aSeq[0] = "copy_persistent";
715     }
716
717     aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), false);
718     SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
719     
720     // Assign an ID = 1 the the type VISU::Result
721     theObjectID = 1;
722     
723     
724     SALOMEDS::SComponent_var aSComponent = theObject->GetStudy()->FindComponent("VISU");
725     return aStreamFile._retn();
726   }
727
728   CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
729     // The VISU component can paste only objects copied by VISU component
730     // and with the object type = 1
731     if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) 
732       return false;
733     return true;
734   }
735
736   SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
737                                               CORBA::Long theObjectID,
738                                               SALOMEDS::SObject_ptr theObject) 
739   {
740     Mutex mt(myMutex);
741     SALOMEDS::SObject_var aResultSO;
742     if (theObjectID != 1) 
743       return aResultSO._retn();
744
745     string aTmpDir = SALOMEDS_Tool::GetTmpDir();
746     SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.c_str(), false);
747     
748     ifstream stmIn((aTmpDir + string("copy_persistent")).c_str());
749     stmIn.seekg(0, ios::end);
750     int aLength = stmIn.tellg();
751     stmIn.seekg(0, ios::beg);
752     char* aString = new char[aLength+1];
753     stmIn.read(aString, aLength);
754     aString[aLength] = 0;
755     myIsMultiFile = false;
756     
757     string aFileName(aTmpDir);
758     string aBasicFileName;
759     if(aSeq->length() > 1) {
760       aBasicFileName = aSeq[1].in();
761       aFileName += aBasicFileName;
762     }
763
764     SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
765     SALOMEDS::Study_var aStudy = theObject->GetStudy();
766     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
767     CORBA::String_var aComponentID(aComponent->GetID()), aSObjID(theObject->GetID());
768     
769     if (strcmp(aComponentID, aSObjID) == 0) //create the new result SObject
770       aResultSO = aStudyBuilder->NewObject(aComponent);
771     else 
772       aResultSO = SALOMEDS::SObject::_duplicate(theObject);
773     
774     //Just for Result::Restore to find the Comment attribute :(
775     SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeComment");
776     
777     Storable* aStorable = Storable::Create(aResultSO,aFileName,aString);
778     
779     SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
780     aSeqToRm->length(1);
781     aSeqToRm[0] = "copy_persistent";
782
783     SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeqToRm.in(), true);
784     
785     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
786     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
787     CORBA::String_var anIORValue(aStorable->GetID());
788     anIOR->SetValue(anIORValue);
789     return aResultSO._retn();
790   }
791
792 }