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