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