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