]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Gen_i.cc
Salome HOME
f64209251c350ff67f205c47db7f03dbdbbf5100
[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       //NRI      aName->SetValue("Visu");
108
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(),&(Mesh_i::Restore));
134     Storable::Registry(ScalarMap_i::myComment.c_str(),&(ScalarMap_i::Restore));
135     Storable::Registry(DeformedShape_i::myComment.c_str(),&(DeformedShape_i::Restore));
136     Storable::Registry(CutPlanes_i::myComment.c_str(),&(CutPlanes_i::Restore));
137     Storable::Registry(CutLines_i::myComment.c_str(),&(CutLines_i::Restore));
138     Storable::Registry(IsoSurfaces_i::myComment.c_str(),&(IsoSurfaces_i::Restore));
139     Storable::Registry(StreamLines_i::myComment.c_str(),&(StreamLines_i::Restore));
140     Storable::Registry(Vectors_i::myComment.c_str(),&(Vectors_i::Restore));
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(aFileInfo.baseName());
188     }else{
189       if(MYDEBUG) MESSAGE("VISU_Gen_i::VISU_Gen_i : there is no opened study in StudyManager !!!");
190     }
191
192     Base_i::myPOA->activate_object(this);
193   }
194
195   VISU_Gen_i::~VISU_Gen_i(){
196     if(MYDEBUG) MESSAGE("VISU_Gen_i::~VISU_Gen_i");
197   }
198   //===========================================================================
199   bool VISU_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
200                         const SALOMEDS::TMPFile & theStream,
201                         const char* theURL,
202                         bool isMultiFile)
203   {
204     if(MYDEBUG) MESSAGE("VISU_Gen_i::Load - myMutex = "<<myMutex);
205     if(myMutex){
206       Mutex mt(myMutex,qApp);
207       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
208       SALOMEDS::StudyBuilder_var  aStudyBuilder = aStudy->NewBuilder(); 
209       TCollection_AsciiString aTmpDir =
210         isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
211       VisuTmpDir = aTmpDir.ToCString();
212       SALOMEDS::ListOfFileNames_var aSeq =
213         SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(),isMultiFile);
214       myIsMultiFile = isMultiFile;
215       return true;
216     }
217     return myVisuGen->Load(theComponent,theStream,theURL,isMultiFile);
218   }
219
220   bool VISU_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
221                              const SALOMEDS::TMPFile & theStream,
222                              const char* theURL,
223                              bool isMultiFile) 
224   {
225     return Load(theComponent, theStream, theURL, isMultiFile);
226   }
227
228   char* VISU_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
229                                            const char* aLocalPersistentID,
230                                            CORBA::Boolean isMultiFile,
231                                            CORBA::Boolean isASCII) 
232   {
233     if(myMutex){
234       CORBA::String_var aString("");
235       if(strcmp(aLocalPersistentID,"") != 0) {
236         Mutex mt(myMutex,qApp);
237         Storable* aStorable =
238           Storable::Create(theSObject,VisuTmpDir.c_str(),aLocalPersistentID);
239         if(aStorable != NULL) aString = aStorable->GetID();
240       }
241       return aString._retn();
242     }
243     return myVisuGen->LocalPersistentIDToIOR(theSObject, aLocalPersistentID, isMultiFile, isASCII);
244   }
245   //===========================================================================
246   SALOMEDS::TMPFile* VISU_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
247                                       const char* theURL,
248                                       bool isMultiFile)
249   {
250     if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - myMutex = "<<myMutex);
251     if(myMutex){
252       Mutex mt(myMutex,qApp);
253       CORBA::String_var aString = SALOMEDS_Tool::GetTmpDir();
254       TCollection_AsciiString aTmpDir = isMultiFile? (const Standard_CString)theURL: (const Standard_CString)aString.in();
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       SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
260       for (; itBig->More(); itBig->Next()) {
261         SALOMEDS::SObject_var gotBranch = itBig->Value();
262         CORBA::Object_var anObj = SObjectToObject(gotBranch);
263         if(CORBA::is_nil(anObj)) continue;
264         Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
265         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
266           const QFileInfo& aFileInfo = pResult->GetFileInfo();
267           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
268           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
269           static QString aCommand;
270           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
271           if(system(aCommand) == -1){
272             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
273             continue;
274           }else
275             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
276           TCollection_AsciiString aString(strdup(aFileName.latin1()));
277           aFileNames.Append(aString);
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));
314         if(pResult && abs(pResult->GetSourceId()) == Result_i::eFile){
315           const QFileInfo& aFileInfo = pResult->GetFileInfo();
316           QString aPrefix = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
317           QString aFileName = aPrefix + "_" + (pResult->GetName()).c_str();
318           static QString aCommand;
319           aCommand.sprintf("cp %s %s%s",aFileInfo.filePath().latin1(),aTmpDir.ToCString(),aFileName.latin1());
320           
321           if(system(aCommand) == -1){
322             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - Cann't execute the command :"<<aCommand);
323             continue;
324           }else
325             if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - "<<aCommand);
326           TCollection_AsciiString aString(strdup(aFileName.latin1()));
327
328           HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir + aString).ToCString()), true);
329
330           aFileNames.Append(aString);
331         }
332       }
333       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
334       if(MYDEBUG) MESSAGE("VISU_Gen_i::Save - aFileNames.Length() - "<<aFileNames.Length());
335       if(aFileNames.Length() > 0){
336         SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
337         aSeq->length(aFileNames.Length());
338         for(aCounter = aFileNames.Length(); aCounter > 0; aCounter--)
339           aSeq[aCounter-1] = CORBA::string_dup(aFileNames.Value(aCounter).ToCString());
340         aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
341         if(!isMultiFile)
342           SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
343       }
344       return aStreamFile._retn();
345     }
346     return myVisuGen->Save(theComponent,theURL,isMultiFile);
347   }
348
349   char* VISU_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
350                                            const char* IORString,
351                                            CORBA::Boolean isMultiFile,
352                                            CORBA::Boolean isASCII) {
353     if(MYDEBUG) MESSAGE("VISU_Gen_i::IORToLocalPersistentID - myMutex = "<<myMutex);
354     if(myMutex){
355       CORBA::String_var aString(IORString);
356       if(strcmp(IORString,"") != 0){ 
357         CORBA::ORB_ptr anORB = GetORB();
358         CORBA::Object_var anObj = anORB->string_to_object(aString);
359         if(!CORBA::is_nil(anObj)){
360           Storable* pStorable = dynamic_cast<Storable*>(GetServant(anObj));
361           if(pStorable != NULL){
362             Mutex mt(myMutex,qApp);
363             aString = pStorable->ToString().c_str();
364             return aString._retn();
365           }
366         }
367       }
368       return aString._retn();
369     }
370     return myVisuGen->IORToLocalPersistentID(theSObject, IORString, isMultiFile, isASCII);
371   }
372
373   char* VISU_Gen_i::GetID(){
374     if(myMutex)
375       return Base_i::GetID();
376     return myVisuGen->GetID();
377   }
378
379   void VISU_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy){
380     if(myMutex){
381       if(!CORBA::is_nil(theStudy))
382         myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
383     }else
384       myVisuGen->SetCurrentStudy(SALOMEDS::Study::_duplicate(theStudy));
385   }
386   SALOMEDS::Study_ptr VISU_Gen_i::GetCurrentStudy(){
387     if(myMutex)
388       return SALOMEDS::Study::_duplicate(myStudyDocument);
389     return myVisuGen->GetCurrentStudy();
390   }
391   ViewManager_ptr VISU_Gen_i::GetViewManager(){
392     //omni_mutex aMutex;
393     //omni_mutex_lock aMutexLock(aMutex);
394     if(MYDEBUG) MESSAGE("VISU_Gen_i::GetViewManager : "<<myMutex);
395     if(myMutex){
396       Mutex mt(myMutex,qApp);
397       ViewManager_i * aViewManager = new ViewManager_i(myStudyDocument);
398       return ViewManager::_duplicate(aViewManager->_this());
399     }
400     return myVisuGen->GetViewManager();
401   }
402
403   SALOMEDS::SObject_ptr VISU_Gen_i::ImportTables(const char* theFileName){
404     if(myMutex){
405       if(myStudyDocument->GetProperties()->IsLocked()) return SALOMEDS::SObject::_nil();
406       Mutex mt(myMutex,qApp);
407       SALOMEDS::SObject_var aRes = VISU::ImportTables(theFileName,myStudyDocument);
408       return aRes._retn();
409     }
410     return myVisuGen->ImportTables(theFileName);
411   }
412
413   Result_ptr VISU_Gen_i::ImportFile(const char* theFileName){
414     if(myMutex){
415       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
416       Mutex mt(myMutex,qApp);
417       VISU::Result_var aResult;
418       aFileInfo.setFile(theFileName);
419       Result_i* pResult = new Result_i(myStudyDocument);
420       if(pResult->Create(theFileName) != NULL) 
421         aResult = pResult->_this();
422       return aResult._retn();
423     }
424     return myVisuGen->ImportFile(theFileName);
425   }
426
427   Result_ptr VISU_Gen_i::ImportMed(SALOMEDS::SObject_ptr theMedSObject){
428     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMed : "<<myMutex);
429     if(myMutex){
430       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
431       Mutex mt(myMutex,qApp);
432       Result_i* pResult = new Result_i(myStudyDocument);
433       if(pResult->Create(theMedSObject) != NULL) 
434         return Result::_duplicate(pResult->_this());
435       return Result::_nil();
436     }
437     return myVisuGen->ImportMed(theMedSObject);
438   }
439
440   Result_ptr VISU_Gen_i::ImportMedField(SALOME_MED::FIELD_ptr theField){
441     if(MYDEBUG) MESSAGE("VISU_Gen_i::ImportMedField : "<<myMutex);
442     if(myMutex){
443       if(myStudyDocument->GetProperties()->IsLocked()) return Result::_nil();
444       Mutex mt(myMutex,qApp);
445       Result_i* pResult = new Result_i(myStudyDocument);
446       if(pResult->Create(theField) != NULL) 
447         return Result::_duplicate(pResult->_this());
448       return Result::_nil();
449     }
450     return myVisuGen->ImportMedField(theField);
451   }
452
453   Mesh_ptr VISU_Gen_i::MeshOnEntity(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity){
454     if(MYDEBUG) MESSAGE("VISU_Gen_i::MeshOnEntity : "<<myMutex);
455     if(myMutex){
456       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
457       Mutex mt(myMutex,qApp);
458       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
459       Mesh_i* aPresent = new Mesh_i(pResult);
460       if(aPresent->Create(theMeshName,theEntity) != NULL) 
461         return Mesh::_duplicate(aPresent->_this());
462       return Mesh::_nil();
463     }
464     return myVisuGen->MeshOnEntity(theResult,theMeshName,theEntity);
465   }
466
467   Mesh_ptr VISU_Gen_i::FamilyMeshOnEntity(Result_ptr theResult, const char* theMeshName, 
468                                           VISU::Entity theEntity, 
469                                           const char* theFamilyName){
470     if(MYDEBUG) MESSAGE("VISU_Gen_i::FamilyMeshOnEntity : "<<myMutex);
471     if(myMutex){
472       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
473       Mutex mt(myMutex,qApp);
474       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
475       Mesh_i* aPresent = new Mesh_i(pResult);
476       if(aPresent->Create(theMeshName,theEntity,theFamilyName) != NULL) 
477         return Mesh::_duplicate(aPresent->_this());
478       return Mesh::_nil();
479     }
480     return myVisuGen->FamilyMeshOnEntity(theResult,theMeshName,theEntity,theFamilyName);
481   }
482
483   Mesh_ptr VISU_Gen_i::GroupMesh(Result_ptr theResult, const char* theMeshName, 
484                                  const char* theGroupName){
485     if(MYDEBUG) MESSAGE("VISU_Gen_i::GroupMesh : "<<myMutex);
486     if(myMutex){
487       if(myStudyDocument->GetProperties()->IsLocked()) return Mesh::_nil();
488       Mutex mt(myMutex,qApp);
489       Result_i* pResult = dynamic_cast<Result_i*>(GetServant(theResult));
490       Mesh_i* aPresent = new Mesh_i(pResult);
491       if(aPresent->Create(theMeshName,theGroupName) != NULL) 
492         return Mesh::_duplicate(aPresent->_this());
493       return Mesh::_nil();
494     }
495     return myVisuGen->GroupMesh(theResult,theMeshName,theGroupName);
496   }
497
498   ScalarMap_ptr VISU_Gen_i::ScalarMapOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
499                                              const char* theFieldName, CORBA::Double theIteration)
500   {
501     if(myMutex) return Prs3dOnField<VISU::ScalarMap_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
502     return myVisuGen->ScalarMapOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
503   }
504
505   DeformedShape_ptr VISU_Gen_i::DeformedShapeOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
506                                                      const char* theFieldName, CORBA::Double theIteration)
507   {
508     if(myMutex) return Prs3dOnField<VISU::DeformedShape_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
509     return myVisuGen->DeformedShapeOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
510   }
511
512   Vectors_ptr VISU_Gen_i::VectorsOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
513                                                      const char* theFieldName, CORBA::Double theIteration)
514   {
515     if(myMutex) return Prs3dOnField<VISU::Vectors_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
516     return myVisuGen->VectorsOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
517   }
518
519   IsoSurfaces_ptr VISU_Gen_i::IsoSurfacesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
520                                                      const char* theFieldName, CORBA::Double theIteration)
521   {
522     if(myMutex) return Prs3dOnField<VISU::IsoSurfaces_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
523     return myVisuGen->IsoSurfacesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
524   }
525
526   StreamLines_ptr VISU_Gen_i::StreamLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
527                                                      const char* theFieldName, CORBA::Double theIteration)
528   {
529     if(myMutex) return Prs3dOnField<VISU::StreamLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
530     return myVisuGen->StreamLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
531   }
532
533   CutPlanes_ptr VISU_Gen_i::CutPlanesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
534                                                      const char* theFieldName, CORBA::Double theIteration)
535   {
536     if(myMutex) return Prs3dOnField<VISU::CutPlanes_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
537     return myVisuGen->CutPlanesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
538   }
539
540   CutLines_ptr VISU_Gen_i::CutLinesOnField(Result_ptr theResult, const char* theMeshName, VISU::Entity theEntity, 
541                                             const char* theFieldName, CORBA::Double theIteration)
542   {
543     if(myMutex) return Prs3dOnField<VISU::CutLines_i>(theResult,theMeshName,theEntity,theFieldName,theIteration);
544     return myVisuGen->CutLinesOnField(theResult,theMeshName,theEntity,theFieldName,theIteration);
545   }
546
547   Table_ptr VISU_Gen_i::CreateTable(const char* theTableEntry){
548     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateTable : "<<myMutex);
549     if(myMutex){
550       if(myStudyDocument->GetProperties()->IsLocked()) return Table::_nil();
551       Mutex mt(myMutex,qApp);
552       Table_i* aPresent = new Table_i(myStudyDocument,theTableEntry);
553       if(aPresent->Create() != NULL) 
554         return Table::_duplicate(aPresent->_this());
555       return Table::_nil();
556     }
557     return myVisuGen->CreateTable(theTableEntry);
558   }
559
560   Curve_ptr VISU_Gen_i::CreateCurve(Table_ptr theTable, CORBA::Long theHRow, CORBA::Long theVRow){
561     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateCurve : "<<myMutex);
562     if(myMutex){
563       if(myStudyDocument->GetProperties()->IsLocked()) return Curve::_nil();
564       Mutex mt(myMutex,qApp);
565       PortableServer::POA_ptr aPOA = GetPOA();
566       Table_i* pTable = dynamic_cast<Table_i*>(aPOA->reference_to_servant(theTable));
567       Curve_i* aPresent = new Curve_i(myStudyDocument,pTable,theHRow,theVRow);
568       if(aPresent->Create() != NULL) { 
569         return Curve::_duplicate(aPresent->_this());
570       }
571       return Curve::_nil();
572     }
573     return myVisuGen->CreateCurve(theTable,theHRow,theVRow);
574   }
575   Container_ptr VISU_Gen_i::CreateContainer(){
576     if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateContainer : "<<myMutex);
577     if(myMutex){
578       if(myStudyDocument->GetProperties()->IsLocked()) return Container::_nil();
579       Mutex mt(myMutex,qApp);
580       Container_i* aPresent = new Container_i(myStudyDocument);
581       if(aPresent->Create() != NULL) {
582         return Container::_duplicate(aPresent->_this());
583       }
584       return Container::_nil();
585     }
586     return myVisuGen->CreateContainer();
587   }
588
589   Animation_ptr VISU_Gen_i::CreateAnimation(View3D_ptr theView3D){
590     if(myMutex){
591       if(myStudyDocument->GetProperties()->IsLocked()) return Animation::_nil();
592       Mutex mt(myMutex,qApp);
593       if(MYDEBUG) MESSAGE("VISU_Gen_i::CreateAnimation : "<<myMutex);
594       VISU_TimeAnimation* anAnim = new VISU_TimeAnimation(myStudyDocument,theView3D);
595       return Animation::_duplicate(anAnim->_this());
596     }
597     return myVisuGen->CreateAnimation(theView3D);
598   }
599
600   void VISU_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent){
601     if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : "<<myMutex);
602     if(myMutex){
603       Mutex mt(myMutex,qApp);
604       SALOMEDS::Study_var aStudy = theComponent->GetStudy(); 
605       if(!aStudy->_is_nil()){
606         SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
607         for (int i = 0; itBig->More(); itBig->Next(),i++) {
608           SALOMEDS::SObject_var gotBranch = itBig->Value();
609           if(MYDEBUG) MESSAGE("VISU_Gen_i::Close : itBig->Next() = "<<i);
610           CORBA::Object_var anObj = SObjectToObject(gotBranch);
611           if(CORBA::is_nil(anObj)) continue;
612           Result_i* pResult = dynamic_cast<Result_i*>(GetServant(anObj));
613           if(!pResult) continue;
614           if(pResult->GetSourceId() == Result_i::eRestoredFile){ //Try remove its file and directory
615             const QFileInfo& aFileInfo = pResult->GetFileInfo();
616             static QString aCommand;
617             aCommand.sprintf("rm %s",aFileInfo.filePath().latin1());
618             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
619             aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",aFileInfo.dirPath().latin1());
620             if(system(aCommand) != -1 && MYDEBUG) MESSAGE("VISU_Gen_i::Close - "<<aCommand);
621           }
622         }
623       }
624       return;
625     }
626     myVisuGen->Close(theComponent);
627   }
628
629   char* VISU_Gen_i::ComponentDataType(){
630     return CORBA::string_dup("VISU");
631   }
632
633   bool VISU_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
634     Result_var aResultObj = Result::_narrow(theIOR);
635     return !(aResultObj->_is_nil());
636   }
637
638   SALOMEDS::SObject_ptr VISU_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
639                                                    SALOMEDS::SObject_ptr theSObject,
640                                                    CORBA::Object_ptr theObject,
641                                                    const char* theName) 
642   throw (SALOME::SALOME_Exception) 
643   {
644     if(MYDEBUG) MESSAGE("VISU_Gen_i::PublishInStudy : "<<myMutex);
645     if(myMutex){
646       Mutex mt(myMutex,qApp);
647       SALOMEDS::SObject_var aResultSO;
648       Result_i* aResultObj = dynamic_cast<Result_i*>(GetServant(theObject));
649       if (!aResultObj) return aResultSO._retn();
650       const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
651       CORBA::String_var anEntry = aResultObj->Create(aFileInfo.filePath().latin1())->GetID();
652       aResultSO = theStudy->FindObjectID(anEntry);
653       return aResultSO._retn();
654     }
655     return myVisuGen->PublishInStudy(theStudy, theSObject, theObject, theName);
656   }
657     
658   CORBA::Boolean VISU_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
659     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanCopy : "<<myMutex);
660     if(myMutex){
661       //Mutex mt(myMutex,qApp);
662       SALOMEDS::GenericAttribute_var anAttr;
663       if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
664       try {
665         SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
666         CORBA::String_var aValue = anIOR->Value();
667         if(strcmp(aValue,"") != 0){
668           CORBA::Object_ptr anObj = GetORB()->string_to_object(aValue);
669           if (!CORBA::is_nil(anObj)){
670             Result_var aResultObj = Result::_narrow(anObj);
671             if(!aResultObj->_is_nil()){
672               Result_i* pResult = dynamic_cast<Result_i*>(GetServant(aResultObj));
673               if(pResult != NULL){
674                 if(abs(pResult->GetSourceId()) > Result_i::eFile) 
675                   if((pResult->GetFileInfo()).filePath() == "MED") return false;
676                 return true;
677               }
678             }
679           }
680         }
681       } catch (...) {
682         INFOS("Unknown exception was accured!");
683       }
684       return false;
685     }
686     return myVisuGen->CanCopy(theObject);
687   }
688
689   SALOMEDS::TMPFile* VISU_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
690     if(MYDEBUG) MESSAGE("VISU_Gen_i::CopyFrom : "<<myMutex);
691     if(myMutex){
692       Mutex mt(myMutex,qApp);
693
694       theObjectID = 0;
695       SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile;
696       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
697
698       PortableServer::POA_ptr aPOA = GetPOA();
699       
700       SALOMEDS::GenericAttribute_var anAttr;
701       if (!theObject->FindAttribute(anAttr,"AttributeIOR")) return NULL;
702       SALOMEDS::AttributeIOR_var anIORAttr = SALOMEDS::AttributeIOR::_narrow(anAttr);
703       CORBA::Object_var aCorbaObj;
704       try {
705         aCorbaObj = GetORB()->string_to_object(anIORAttr->Value());
706       } catch(...) {
707         aStreamFile->length(1);
708         aStreamFile[0] = strdup("E")[0];
709         return aStreamFile._retn();
710       }
711       if (CORBA::is_nil(aCorbaObj)) {
712         return NULL;
713       }
714
715       Storable* pStorable = dynamic_cast<Storable*>(GetServant(aCorbaObj));
716       if (!pStorable) {
717         return NULL;
718       }
719       CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
720
721       string aStr = pStorable->ToString().c_str();
722       ofstream stmOut2((string(aTmpDir) + string("copy_persistent")).c_str(),ios::out);
723       stmOut2<<aStr<<endl;
724       stmOut2.close();
725
726       Result_i* aResultObj = dynamic_cast<Result_i*>(aPOA->reference_to_servant(aCorbaObj));
727       if (aResultObj) {
728         string aFileName = string(SALOMEDS_Tool::GetNameFromPath(theObject->GetStudy()->URL())) + "_";
729         const QFileInfo& aFileInfo = aResultObj->GetFileInfo();
730         aFileName += aFileInfo.fileName().latin1();
731         static QString aCommand;
732         aCommand.sprintf("cp %s %s",
733                          aFileInfo.filePath().latin1(),
734                          (string(aTmpDir) + aFileName).c_str());
735         if(system(aCommand) == -1) {
736           if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand);
737           return NULL;
738         }
739
740         aSeq->length(2);
741         aSeq[0] = "copy_persistent";
742         aSeq[1] = aFileName.c_str();
743       } else {
744         aSeq->length(1);
745         aSeq[0] = "copy_persistent";
746       }
747       aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
748       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
749
750       // Assign an ID = 1 the the type VISU::Result
751       theObjectID = 1;
752
753
754       SALOMEDS::SComponent_var aSComponent = theObject->GetStudy()->FindComponent("VISU");
755       return aStreamFile._retn();
756     }
757     return myVisuGen->CopyFrom(theObject, theObjectID);
758   }
759
760   CORBA::Boolean VISU_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
761     // The VISU component can paste only objects copied by VISU component
762     // and with the object type = 1
763     if(0 && MYDEBUG) MESSAGE("VISU_Gen_i::CanPaste : "<<myMutex);
764     if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
765     return true;
766   }
767
768   SALOMEDS::SObject_ptr VISU_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
769                                               CORBA::Long theObjectID,
770                                               SALOMEDS::SObject_ptr theObject) {
771     if(MYDEBUG) MESSAGE("VISU_Gen_i::PasteInto : "<<myMutex);
772     if(myMutex){
773       Mutex mt(myMutex,qApp);
774       SALOMEDS::SObject_var aResultSO;
775       if (theObjectID != 1) return aResultSO._retn();
776
777       CORBA::String_var aTmpDir = (const char*)SALOMEDS_Tool::GetTmpDir();
778       SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
779
780       ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str());
781 //        ifstream stmIn((string(aTmpDir) + string("copy_persistent")).c_str(),ios::in);
782       stmIn.seekg(0, ios::end);
783       int aLength = stmIn.tellg();
784       stmIn.seekg(0, ios::beg);
785       char* aString = new char[aLength+1];
786       stmIn.read(aString, aLength);
787       aString[aLength] = 0;
788       myIsMultiFile = true;
789
790       string aFileName(aTmpDir.in());
791       if(aSeq->length() > 1) aFileName += aSeq[1].in();
792
793       SALOMEDS::SComponent_var aComponent = theObject->GetFatherComponent();
794       SALOMEDS::Study_var aStudy = theObject->GetStudy();
795       SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
796       CORBA::String_var aComponentID(aComponent->GetID()), aSObjID(theObject->GetID());
797
798       if (strcmp(aComponentID, aSObjID) == 0) //create the new result SObject
799         aResultSO = aStudyBuilder->NewObject(aComponent);
800       else 
801         aResultSO = SALOMEDS::SObject::_duplicate(theObject);
802
803       //Just for Result::Restore to find the Comment attribute :(
804       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeComment");
805
806       Storable* aStorable = Storable::Create(aResultSO,aFileName,aString);
807
808       SALOMEDS::ListOfFileNames_var aSeqToRm = new SALOMEDS::ListOfFileNames;
809       aSeqToRm->length(1);
810       aSeqToRm[0] = "copy_persistent";
811       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeqToRm.in(), true);
812
813       anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
814       SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
815       CORBA::String_var anIORValue(aStorable->GetID());
816       anIOR->SetValue(anIORValue);
817       return aResultSO._retn();
818     }
819     return myVisuGen->PasteInto(theStream,theObjectID,theObject);
820   }
821
822 };