Salome HOME
Merge from V6_4_BR 05/12/2011
[modules/visu.git] / src / VISU_I / VISU_Result_i.cc
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  VISU OBJECT : interactive object for VISU entities implementation
24 //  File   : VISU_Result_i.cc
25 //  Author : Alexey PETROV
26 //  Module : VISU
27
28 #include "VISU_MultiResult_i.hh"
29 #include "VISU_ResultUtils.hh"
30 #include "VISU_Prs3d_i.hh"
31
32 #include "VISU_Convertor_impl.hxx"
33 #include "VISU_CorbaMedConvertor.hxx"
34 #include "VISU_ConvertorUtils.hxx"
35 #include "VISU_PipeLine.hxx"
36
37 #include "SUIT_ResourceMgr.h"
38
39 #include "SALOMEDS_Tool.hxx"
40 #include "HDFascii.hxx"
41 #include "Basics_Utils.hxx"
42
43 #include "SUIT_Session.h"
44 #include "SALOME_Event.h"
45 #include "SalomeApp_Study.h"
46 #include "SalomeApp_Application.h"
47
48 #include "MED_Factory.hxx"
49
50 #include <boost/thread/thread.hpp>
51 #include <boost/bind.hpp>
52
53 // QT Includes
54 #include <QString>
55 #include <QFileInfo>
56 #include <QSemaphore>
57 #include <QThread>
58 #include <QDir>
59 #include <QList>
60
61 // VTK Includes
62 #include <vtkCell.h>
63 #include <vtkUnstructuredGrid.h>
64
65 // OCCT Includes
66 #include <Bnd_Box.hxx>
67
68 #ifdef _DEBUG_
69 static int MYDEBUG = 1;
70 #else
71 static int MYDEBUG = 0;
72 #endif
73
74 using namespace std;
75
76 namespace VISU
77 {
78   //---------------------------------------------------------------
79   Result_var
80   FindResult(SALOMEDS::SObject_ptr theSObject)
81   {
82     SALOMEDS::SComponent_var aSComponent = theSObject->GetFatherComponent();
83     SALOMEDS::SObject_var aFather = theSObject->GetFather();
84     CORBA::String_var aComponentID (aSComponent->GetID());
85     CORBA::String_var aFatherID    (aFather->GetID());
86     Result_var aResult;
87     while (strcmp(aComponentID, aFatherID) != 0) {
88       CORBA::Object_var anObject = SObjectToObject(aFather);
89       if (!CORBA::is_nil(anObject)) {
90         aResult = Result::_narrow(anObject);
91         if (!aResult->_is_nil()) return aResult;
92       }
93       aFather = aFather->GetFather();
94       aFatherID = aFather->GetID();
95     }
96     return aResult;
97   }
98
99
100   //---------------------------------------------------------------
101   Result_i*
102   GetResult(SALOMEDS::Study_ptr theStudy,
103             const std::string& theResultEntry)
104   {
105     Result_i* result = NULL;
106     if (!CORBA::is_nil(theStudy)) {
107       SALOMEDS::SComponent_var aSComponent = theStudy->FindComponent("VISU");
108       if (!CORBA::is_nil(aSComponent)) {
109         std::string compid = aSComponent->GetID();
110         std::string aResultEntry = theResultEntry;
111         if (theResultEntry.substr(0, compid.length()) != compid) aResultEntry = compid + theResultEntry.substr(compid.length());
112         SALOMEDS::SObject_var aSObject = theStudy->FindObjectID(aResultEntry.c_str());
113         CORBA::Object_var anObject = SObjectToObject(aSObject);
114         result = dynamic_cast<VISU::Result_i*>(GetServant(anObject).in());
115       }
116     }
117     return result;
118   }
119
120
121   //---------------------------------------------------------------
122   std::string
123   GetComponentDataType (SALOMEDS::SObject_ptr theSObject)
124   {
125     SALOMEDS::SComponent_var aCompRefSObj = theSObject->GetFatherComponent();
126     CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
127     return aDataType.in();
128   }
129
130
131   //---------------------------------------------------------------
132   void
133   CreateReference (SALOMEDS::Study_ptr theStudyDocument,
134                    const string& theFatherEntry,
135                    const string& theRefEntry)
136   {
137     SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
138     SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry.c_str());
139     SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(aFather);
140     SALOMEDS::SObject_var aRefSObj = theStudyDocument->FindObjectID(theRefEntry.c_str());
141     aStudyBuilder->Addreference(newObj,aRefSObj);
142   }
143
144   //---------------------------------------------------------------
145   // Issue 0021403. Remove a ColoredPrs3dHolder if a Result it refers to is removed
146   //
147   void RemoveHolders(VISU::Result_i* theResult)
148   {
149     CORBA::String_var resultID1 = theResult->GetID();
150     SALOMEDS::SObject_var theResSObject = theResult->GetSObject();
151     SALOMEDS::Study_var aStudyDocument = theResSObject->GetStudy();
152     SALOMEDS::SComponent_var aCompSObj = theResSObject->GetFatherComponent();
153     SALOMEDS::ChildIterator_var aChildIter = aStudyDocument->NewChildIterator(aCompSObj);
154     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next())
155     {
156       SALOMEDS::SObject_var aChildSObject = aChildIter->Value();
157       if ( CORBA::is_nil( aChildSObject )) continue;
158       CORBA::Object_var aChildObj = aChildSObject->GetObject();
159       if ( CORBA::is_nil( aChildObj )) continue;
160       VISU::ColoredPrs3dHolder_var prsHolder = ColoredPrs3dHolder::_narrow( aChildObj );
161       if ( prsHolder->_is_nil() ) continue;
162       // check if the prsHolder refers to theResult
163       VISU::ColoredPrs3dHolder::BasicInput_var input = prsHolder->GetBasicInput();
164       CORBA::String_var resultID2 = input->myResult->GetID();
165       if ( strcmp( resultID1, resultID2 ) == 0 )
166         prsHolder->RemoveFromStudy();
167     }
168   }
169
170 //---------------------------------------------------------------
171 }
172
173
174 //---------------------------------------------------------------
175 const string VISU::Result_i::myComment = "RESULT";
176 const char* VISU::Result_i::GetComment() const { return myComment.c_str();}
177
178 //---------------------------------------------------------------
179 VISU::Result_i
180 ::Result_i(SALOMEDS::Study_ptr theStudy,
181            const ESourceId& theSourceId,
182            const ECreationId& theCreationId,
183            CORBA::Boolean theIsBuildImmediately,
184            CORBA::Boolean theIsBuildFields,
185            CORBA::Boolean theIsBuildMinMax,
186            CORBA::Boolean theIsBuildGroups):
187   myStudyDocument(SALOMEDS::Study::_duplicate(theStudy)),
188   myCreationId(theCreationId),
189   mySourceId(theSourceId),
190   myIsBuildImmediately(theIsBuildImmediately),
191   myIsBuildFields(theIsBuildFields),
192   myIsBuildMinMax(theIsBuildMinMax),
193   myIsBuildGroups(theIsBuildGroups),
194   myIsEntitiesDone(false),
195   myIsFieldsDone(false),
196   myIsGroupsDone(false),
197   myIsMinMaxDone(false),
198   myIsAllDone(false),
199   myIsBuildParts(false),
200   myIsPartsDone(false)
201 {
202   if(MYDEBUG) MESSAGE("Result_i::Result_i - this = "<<this);
203   myStudy = VISU::GetStudy(myStudyDocument);
204 }
205
206
207 //---------------------------------------------------------------
208 VISU::Result_i
209 ::Result_i():
210   myIsEntitiesDone(false),
211   myIsFieldsDone(false),
212   myIsGroupsDone(false),
213   myIsMinMaxDone(false),
214   myIsAllDone(false),
215   myIsBuildParts(false),
216   myIsPartsDone(false)
217 {
218   if(MYDEBUG) MESSAGE("Result_i::Result_i - this = "<<this);
219 }
220
221
222 //---------------------------------------------------------------
223 VISU::Result_i
224 ::~Result_i()
225 {
226   MESSAGE("Result_i::~Result_i() - this = "<<this);
227   if (GetSourceId() == eRestoredFile)
228     VISU::RemoveFile(myFileInfo.filePath().toLatin1().data());
229 }
230
231
232 //---------------------------------------------------------------
233 void
234 VISU::Result_i
235 ::RemoveFromStudy()
236 {
237   struct TRemoveFromStudy: public SALOME_Event
238   {
239     VISU::Result_i* myRemovable;
240     TRemoveFromStudy(VISU::Result_i* theRemovable):
241       myRemovable(theRemovable)
242     {}
243
244     virtual
245     void
246     Execute()
247     {
248       RemoveHolders(myRemovable); 
249       VISU::RemoveFromStudy(myRemovable->GetSObject(),false);
250       myRemovable->UnRegister();
251     }
252   };
253
254   // Remove the result with all presentations and other possible sub-objects
255   ProcessVoidEvent(new TRemoveFromStudy(this));
256 }
257
258
259 //---------------------------------------------------------------
260 void
261 VISU::Result_i
262 ::ConnectObserver(TResultObserver* theObserver,
263                   boost::signalslib::connection& theConnection)
264 {
265   if(theConnection.connected())
266     theConnection.disconnect();
267
268   theConnection =
269     myUpdateObserverSignal.connect(boost::bind(boost::bind(&TResultObserver::UpdateFromResult,
270                                                            theObserver,
271                                                            _1),
272                                                this));
273 }
274
275
276 //---------------------------------------------------------------
277 void
278 VISU::Result_i
279 ::UpdateObservers()
280 {
281   struct TEvent: public SALOME_Event
282   {
283     VISU::Result_i::TUpdateObserverSignal& myUpdateObserverSignal;
284
285     TEvent(VISU::Result_i::TUpdateObserverSignal& theUpdateObserverSignal):
286       myUpdateObserverSignal(theUpdateObserverSignal)
287     {}
288
289     virtual
290     void
291     Execute()
292     {
293       myUpdateObserverSignal();
294     }
295   };
296
297   ProcessVoidEvent(new TEvent(myUpdateObserverSignal));
298 }
299
300
301 //---------------------------------------------------------------
302 size_t
303 VISU::Result_i
304 ::IsPossible()
305 {
306   try{
307     size_t aSize = myInput->GetSize();
308     size_t aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
309     MESSAGE("Result_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<float(aResult));
310     return aResult;
311   }catch(std::exception& exc){
312     INFOS("Follow exception was occured :\n"<<exc.what());
313   }catch(...){
314     INFOS("Unknown exception was occured!");
315   }
316   return 0;
317 }
318
319
320 //---------------------------------------------------------------
321 CORBA::Boolean
322 VISU::Result_i
323 ::BuildAll()
324 {
325   if(MYDEBUG) MESSAGE("Result_i::Build - myIsAllDone = "<<myIsAllDone);
326   if(myIsAllDone)
327     return 1;
328   if(!IsPossible())
329     return 0;
330   try{
331     const VISU::TMeshMap& aMeshMap = myInput->GetMeshMap();
332     VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
333     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
334       const string& aMeshName = aMeshMapIter->first;
335       const VISU::PMesh aMesh = aMeshMapIter->second;
336       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
337       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
338       //Import fields
339       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
340       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
341           const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
342           const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
343           const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
344           VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
345           for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
346             const string& aFieldName = aFieldMapIter->first;
347             const VISU::PField aField = aFieldMapIter->second;
348             const VISU::TValField& aValField = aField->myValField;
349             VISU::TValField::const_iterator aValFieldIter = aValField.begin();
350             for(; aValFieldIter != aValField.end(); aValFieldIter++){
351               int aTimeStamp = aValFieldIter->first;
352             try{
353               myInput->GetTimeStampOnMesh(aMeshName,anEntity,aFieldName,aTimeStamp);
354             }catch(std::exception& exc){
355               INFOS("Follow exception was occured :\n"<<exc.what());
356             }catch(...){
357               INFOS("Unknown exception was occured!!!");
358             }
359             }
360           }
361         //Importing groups
362         const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
363         VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
364         for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
365           const string& aGroupName = aGroupMapIter->first;
366           try{
367             myInput->GetMeshOnGroup(aMeshName,aGroupName);
368           }catch(std::exception& exc){
369             INFOS("Follow exception was occured :\n"<<exc.what());
370           }catch(...){
371             INFOS("Unknown exception was occured!!!");
372           }
373         }
374         //Import families
375         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
376         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
377         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
378           const string& aFamilyName = aFamilyMapIter->first;
379           try{
380             myInput->GetFamilyOnEntity(aMeshName,anEntity,aFamilyName);
381           }catch(std::exception& exc){
382             INFOS("Follow exception was occured :\n"<<exc.what());
383           }catch(...){
384             INFOS("Unknown exception was occured!!!");
385           }
386         }
387         //Import mesh on entity
388         try{
389           myInput->GetMeshOnEntity(aMeshName,anEntity);
390         }catch(std::exception& exc){
391           INFOS("Follow exception was occured :\n"<<exc.what());
392         }catch(...){
393           INFOS("Unknown exception was occured!!!");
394         }
395       }
396     }
397     myIsAllDone = 1;
398   }catch(std::exception& exc){
399     INFOS("Follow exception was occured :\n"<<exc.what());
400   }catch(...){
401     INFOS("Unknown exception was occured!!!");
402   }
403   return myIsAllDone;
404 }
405
406
407 //---------------------------------------------------------------
408 CORBA::Boolean
409 VISU::Result_i
410 ::Build(CORBA::Boolean theIsBuildAll,
411         CORBA::Boolean theIsAtOnce)
412 {
413   if(theIsBuildAll)
414     theIsAtOnce = true;
415
416   if(Build(SALOMEDS::SObject::_nil(), theIsAtOnce)){
417
418     // Set icon
419     SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
420
421     SALOMEDS::GenericAttribute_var anAttr;
422     SALOMEDS::AttributePixMap_var  aPixmap;
423     SALOMEDS::AttributeUserID_var aId;
424
425     anAttr  = aStudyBuilder->FindOrCreateAttribute( mySObject, "AttributePixMap" );
426     aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
427     aPixmap ->SetPixMap("ICON_TREE_RESULT");
428
429     std::string auid = "AttributeUserID";
430     auid += Kernel_Utils::GetGUID(Kernel_Utils::ObjectdID);
431     anAttr  = aStudyBuilder->FindOrCreateAttribute( mySObject, auid.c_str() );
432     aId     = SALOMEDS::AttributeUserID::_narrow( anAttr );
433     aId     ->SetValue("VISU.RESULT");
434
435     if(theIsBuildAll)
436       return BuildAll();
437     return true;
438   }
439
440   return false;
441 }
442
443
444 //---------------------------------------------------------------
445 VISU::Storable* VISU::Result_i::Build(SALOMEDS::SObject_ptr theSObject,
446                                       CORBA::Boolean theIsAtOnce)
447 {
448   if(!myInput)
449     return NULL;
450
451   if(IsDone())
452     return this;
453
454   mySComponent = FindOrCreateVisuComponent(myStudyDocument);
455   CORBA::String_var aSComponentEntry = mySComponent->GetID();
456   CORBA::String_var anIOR(GetID());
457   QString aComment;
458   aComment.sprintf("myComment=%s;myFileName=%s;myInitFileName=%s",
459                    GetComment(),
460                    myFileInfo.filePath().toLatin1().data(),
461                    GetInitFileName().c_str()); // Restoring of Python dump
462   std::string aResultEntry =
463     CreateAttributes(myStudy,
464                      aSComponentEntry.in(),
465                      NO_ICON,
466                      anIOR.in(),
467                      GetName(),
468                      NO_PERFSITENT_REF,
469                      aComment.toLatin1().data(),
470                      true);
471   mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
472   if(!CORBA::is_nil(theSObject)){
473     CORBA::String_var aString = theSObject->GetID();
474     CreateReference(myStudyDocument, aResultEntry, aString.in());
475   }
476
477   if(theIsAtOnce){
478     BuildEntities(this,
479                   myInput,
480                   &myIsEntitiesDone,
481                   aResultEntry,
482                   theIsAtOnce,
483                   myIsBuildGroups,
484                   myIsBuildFields,
485                   myIsBuildParts,
486                   myStudy);
487
488     BuildGroups(this,
489                 myInput,
490                 &myIsGroupsDone,
491                 myIsBuildGroups,
492                 theIsAtOnce,
493                 myStudy);
494
495     BuildFieldDataTree(this,
496                        myInput,
497                        &myIsFieldsDone,
498                        myIsBuildFields,
499                        &myIsMinMaxDone,
500                        myIsBuildMinMax,
501                        myStudy);
502   } else {
503     boost::thread aThread(boost::bind(boost::bind(&Result_i::BuildDataTree, this, _1),
504                                       aResultEntry));
505   }
506
507   return this;
508 }
509
510
511 //---------------------------------------------------------------
512 void
513 VISU::Result_i
514 ::BuildDataTree(const std::string& theResultEntry)
515 {
516   BuildEntities(this,
517                 myInput,
518                 &myIsEntitiesDone,
519                 theResultEntry,
520                 false,
521                 myIsBuildGroups,
522                 myIsBuildFields,
523                 myIsBuildParts,
524                 myStudy);
525
526   {
527     boost::thread aThread(boost::bind(&BuildGroups,
528                                       this,
529                                       myInput,
530                                       &myIsGroupsDone,
531                                       myIsBuildGroups,
532                                       false,
533                                       myStudy));
534   }
535
536   {
537     boost::thread aThread(boost::bind(&BuildFieldDataTree,
538                                       this,
539                                       myInput,
540                                       &myIsFieldsDone,
541                                       myIsBuildFields,
542                                       &myIsMinMaxDone,
543                                       myIsBuildMinMax,
544                                       myStudy));
545   }
546 }
547
548 //---------------------------------------------------------------
549 VISU::Storable*
550 VISU::Result_i
551 ::BuildAll(SALOMEDS::SObject_ptr theSObject)
552 {
553   if(MYDEBUG) MESSAGE("Result_i::Build");
554   try{
555     Build(theSObject);
556     BuildAll();
557   }catch(std::exception& exc){
558     INFOS("Follow exception was occured :\n"<<exc.what());
559     return NULL;
560   }catch(...){
561     INFOS("Unknown exception was occured!!!");
562     return NULL;
563   }
564
565   return this;
566 }
567
568
569 //---------------------------------------------------------------
570 VISU::Storable*
571 VISU::Result_i
572 ::Create(const char* theFileName)
573 {        
574   try {
575     myFileInfo.setFile(theFileName);
576     myFileName = myFileInfo.fileName().toLatin1().data();
577
578     if(!myIsBuildParts){
579       SetInitFileName(myFileInfo.filePath().toLatin1().data());
580       SetName(VISU::GenerateName(myFileInfo.fileName().toLatin1().data()).toLatin1().data(), false);
581     }
582
583     if(mySourceId == eRestoredFile){
584       QString aTmpDir(SALOMEDS_Tool::GetTmpDir().c_str());
585       std::string aFileName = VISU::MakeFileName(myFileInfo.fileName().toLatin1().data(), this);
586       QString aPathToCopy(aTmpDir + aFileName.c_str());
587       if(!VISU::CopyFile(myFileInfo.absoluteFilePath().toLatin1().data(), aPathToCopy.toLatin1().data()))
588         return NULL;
589
590       myFileInfo.setFile(aPathToCopy);
591       myFileName = myFileInfo.fileName().toLatin1().data();
592     }
593
594     myInput.reset(CreateConvertor(myFileInfo.absoluteFilePath().toLatin1().data()));
595
596     if(myInput){
597       if(myIsBuildImmediately)
598         Build(SALOMEDS::SObject::_nil());
599       return this;
600     }
601   }catch(std::exception& exc){
602     MSG(MYDEBUG,"Follow exception was occured in:\n"<<exc.what());
603   }catch(...){
604     MSG(MYDEBUG,"Unknown exception !!!");
605   }
606   return NULL;
607 }
608
609
610 //---------------------------------------------------------------
611 VISU::Storable*
612 VISU::Result_i
613 ::Create(SALOMEDS::SObject_ptr theMedSObject)
614 {
615   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
616   try{
617     myInput.reset(CreateMEDConvertor(theMedSObject));
618     if(!myInput)
619       return NULL;
620
621     myInput->Build();
622
623     string aCompDataType = GetComponentDataType(theMedSObject);
624     myFileInfo.setFile(aCompDataType.c_str());
625     myFileName = VISU::GenerateName("aResult").toLatin1().data();
626
627     SetInitFileName(aCompDataType);
628     SetName(VISU::GenerateName(myFileInfo.fileName().toLatin1().data()).toLatin1().data(), false);
629
630     return Build(theMedSObject);
631   }catch(std::exception& exc){
632     INFOS("Follow exception was occured :\n"<<exc.what());
633   }catch(...){
634     INFOS("Unknown exception was occured!!!");
635   }
636   return NULL;
637 }
638
639
640 //---------------------------------------------------------------
641 VISU::Storable*
642 VISU::Result_i
643 ::Create(SALOME_MED::FIELD_ptr theField)
644 {
645   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
646   try{
647     myInput.reset(CreateMEDFieldConvertor(theField));
648     if(!myInput)
649       return NULL;
650
651     myInput->Build();
652
653     string aCompDataType = "MED";
654     myFileInfo.setFile(aCompDataType.c_str());
655
656     myFileName = VISU::GenerateName("aResult").toLatin1().data();
657     SetInitFileName(aCompDataType);
658     SetName(VISU::GenerateName(myFileInfo.fileName().toLatin1().data()).toLatin1().data(), false);
659
660     CORBA::String_var anIOR = myStudyDocument->ConvertObjectToIOR(theField);
661     SALOMEDS::SObject_var aFieldSObject = myStudyDocument->FindObjectIOR(anIOR);
662
663     return Build(aFieldSObject);
664   }catch(std::exception& exc){
665     INFOS("Follow exception was occured :\n"<<exc.what());
666   }catch(...){
667     INFOS("Unknown exception was occured!!!");
668   }
669   return NULL;
670 }
671
672
673 //---------------------------------------------------------------
674 bool
675 VISU::Result_i
676 ::Save(SALOMEDS::SComponent_ptr theComponent,
677        const std::string& theURL,
678        bool theIsMultiFile,
679        bool theIsASCII,
680        TFileNames& theFileNames,
681        TFileNames& theFiles)
682 {
683   switch(GetCreationId()){
684   case Result_i::eImportFile:
685   case Result_i::eCopyAndImportFile: {
686     INITMSG(MYDEBUG, "Result::Save - this = "<<this<<"\n");
687     INITMSGA(MYDEBUG, 0, "theIsMultiFile = "<<theIsMultiFile<<"; theIsASCII = "<<theIsASCII<<"\n");
688
689     std::string aPrefix;
690     if (theIsMultiFile) {
691       CORBA::String_var anURL = GetStudyDocument()->URL();
692       aPrefix = SALOMEDS_Tool::GetNameFromPath(anURL.in());
693     }
694
695     std::string aFileName = aPrefix + "_" + GetFileName().c_str();
696     BEGMSG(MYDEBUG, "aFileName = '"<<aFileName<<"'\n");
697
698     std::string aFile = GetFileInfo().filePath().toLatin1().data();
699     BEGMSG(MYDEBUG, "aFile = '"<<aFile<<"'\n");
700
701     if(theIsMultiFile || theIsASCII){
702       std::string aPathToCopy(theURL + aFileName);
703       BEGMSG(MYDEBUG, "aPathToCopy = '"<<aPathToCopy<<"'\n");
704
705       if(!VISU::CopyFile(aFile, aPathToCopy))
706         return false;
707
708       if(theIsASCII)
709         HDFascii::ConvertFromHDFToASCII(const_cast<char*>(aPathToCopy.c_str()), true);
710     }
711
712     theFileNames.push_back(aFileName);
713     theFiles.push_back(aFile);
714
715     return true;
716   }}
717
718   return false;
719 }
720
721
722 //---------------------------------------------------------------
723 CORBA::Boolean
724 VISU::Result_i
725 ::CanCopy(SALOMEDS::SObject_ptr theObject)
726 {
727   switch(GetCreationId()){
728   case Result_i::eImportFile:
729   case Result_i::eCopyAndImportFile:
730     return true;
731   }
732
733   return false;
734 }
735
736
737 //---------------------------------------------------------------
738 bool
739 VISU::Result_i
740 ::CopyFrom(SALOMEDS::SObject_ptr theObject,
741            CORBA::Long& theObjectID,
742            const std::string& theTmpDir,
743            TFileNames& theFileNames)
744 {
745   if(!Storable::CopyFrom(theObject, theObjectID, theTmpDir, theFileNames))
746     return false;
747
748   SALOMEDS::Study_var aStudy = theObject->GetStudy();
749   CORBA::String_var anURL = aStudy->URL();
750   std::string aPrefix = SALOMEDS_Tool::GetNameFromPath(anURL.in());
751   std::string aFileName = aPrefix + GetFileInfo().fileName().toLatin1().data();
752
753   static QString aCommand;
754   std::string aFilePath =  theTmpDir + aFileName;
755   aCommand.sprintf(" %s %s", GetFileInfo().filePath().toLatin1().data(), aFilePath.c_str());
756   aCommand = QDir::convertSeparators( aCommand );
757   aCommand.prepend( COPY_COMMAND );
758
759   if(system(aCommand.toLatin1().data()) != 0) {
760     if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand.toLatin1().data());
761     return false;
762   }
763
764   SALOMEDS::SComponent_var aSComponent = aStudy->FindComponent("VISU");
765
766   theFileNames.push_back(aFileName);
767
768   return true;
769 }
770
771
772 //---------------------------------------------------------------
773 VISU::Storable*
774 VISU::Result_i
775 ::Restore(SALOMEDS::SObject_ptr theSObject,
776           const Storable::TRestoringMap& theMap,
777           const string& thePrefix,
778           CORBA::Boolean theIsMultiFile)
779 {
780   if(MYDEBUG) MESSAGE("Result_i::Restore - " << thePrefix);
781   mySObject = SALOMEDS::SObject::_duplicate(theSObject);
782   mySComponent = mySObject->GetFatherComponent();
783   myStudyDocument = theSObject->GetStudy();
784   myStudy = VISU::GetStudy(myStudyDocument);
785
786   myCreationId = ECreationId(Storable::FindValue(theMap, "myCreationId").toInt());
787   mySourceId = eRestoredFile;
788   if(myCreationId == eImportMed || myCreationId == eImportMedField)
789     mySourceId = eRestoredComponent;
790
791   myIsBuildFields = Storable::FindValue(theMap, "myIsBuildFields", "1").toInt();
792   myIsBuildMinMax = Storable::FindValue(theMap, "myIsBuildMinMax", "1").toInt();
793   myIsBuildGroups = Storable::FindValue(theMap, "myIsBuildGroups", "1").toInt();
794
795   myIsBuildImmediately = true;
796
797   try {
798     myFileName = VISU::Storable::FindValue(theMap, "myName").toLatin1().data();
799 #ifdef WIN32
800     for (int i = 0; i < myFileName.length(); i++)
801     {
802       if (myFileName[i] == ':')
803         myFileName[i] = '_';
804     }
805 #endif
806     SetInitFileName(VISU::Storable::FindValue(theMap, "myInitFileName").toLatin1().data());
807
808     SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
809     if (theSObject->FindSubObject(1, aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)) {
810       if(MYDEBUG) MESSAGE("Result_i::GetInput - There is some reference.");
811       SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
812       CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
813       myFileInfo.setFile(aDataType.in());
814       if(MYDEBUG) MESSAGE("Result_i::GetInput - aDataType = " << aDataType);
815       Engines::EngineComponent_var aEngComp =
816         Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
817       if (CORBA::is_nil(aEngComp))
818         throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
819       SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
820       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
821       aStudyBuilder->LoadWith(aCompRefSObj, aDriver);
822       // create field or MED converter
823       CORBA::Object_var aMedObject = VISU::SObjectToObject(aTargetRefSObj);
824       SALOME_MED::FIELD_var aField = SALOME_MED::FIELD::_narrow(aMedObject);
825       if (!CORBA::is_nil(aField)) {
826         // create field converter
827         myInput.reset(CreateMEDFieldConvertor(aField));
828         myInput->Build();
829       }
830       else if (strcmp(aDataType, "MED") == 0) {
831         // create MED converter
832         myInput.reset(CreateMEDConvertor(aTargetRefSObj));
833         myInput->Build();
834       }
835       else {
836         throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
837       }
838     } else {
839       myFileInfo.setFile(thePrefix.c_str());
840
841       std::string aStudyPrefix ("");
842       if (theIsMultiFile) {
843         CORBA::String_var anURL(GetStudyDocument()->URL());
844         aStudyPrefix = SALOMEDS_Tool::GetNameFromPath(anURL.in());
845       }
846       if (!myFileInfo.isFile()) {
847         std::string aFileName = thePrefix + aStudyPrefix + "_" + myFileName;
848         myFileInfo.setFile(aFileName.c_str());
849       }
850       if(MYDEBUG)
851         MESSAGE("Result_i::Restore - aFileName = " << myFileInfo.filePath().toLatin1().data() << "; " << myFileInfo.isFile());
852
853       if (HDFascii::isASCII(myFileInfo.filePath().toLatin1().data())) {
854         MESSAGE("ConvertFromASCIIToHDF(" << myFileInfo.filePath().toLatin1().data() << ")");
855         char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().toLatin1().data());
856         MESSAGE("ConvertFromASCIIToHDF() DONE : " << aResultPath);
857         char* aHDFFileName = new char[strlen(aResultPath) + 19];
858         sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
859         
860         if (theIsMultiFile) { // set this file as new - temporary
861           static QString aCommand;
862           aCommand.sprintf(" %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().toLatin1().data());
863           aCommand = QDir::convertSeparators( aCommand );
864           aCommand.prepend( MOVE_COMMAND );
865         
866           if (system(aCommand.toLatin1().data()) == -1) {
867             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :" << aCommand.toLatin1().data());
868             return NULL;
869           } else {
870             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - " << aCommand.toLatin1().data());
871           }
872           myFileInfo.setFile(QString(aResultPath) + myFileInfo.baseName());
873         } else { // change current temporary file to the new: with hdf-format
874           static QString aCommand;
875           aCommand.sprintf(" %s %s\0",aHDFFileName, myFileInfo.filePath().toLatin1().data());
876           aCommand = QDir::convertSeparators( aCommand );
877           aCommand.prepend( MOVE_COMMAND );
878         
879           if (system(aCommand.toLatin1().data()) == -1) {
880             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :" << aCommand.toLatin1().data());
881             return NULL;
882           } else {
883             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - " << aCommand.toLatin1().data());
884           }
885           SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
886           SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList.in(), true);
887         }
888         mySourceId = eRestoredFile;
889         delete(aResultPath);
890         delete(aHDFFileName);
891       } else if (!theIsMultiFile) {
892         mySourceId = eRestoredFile;
893       } else {
894         mySourceId = eSavedFile;
895       }
896       if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - mySourceId = " << mySourceId);
897       myInput.reset(CreateConvertor(myFileInfo.filePath().toLatin1().data()));
898
899       myInput->BuildEntities();
900       if(myIsBuildFields){
901         myInput->BuildFields();
902         myIsFieldsDone = true;
903         if(myIsBuildMinMax){
904           myInput->BuildMinMax();
905           myIsMinMaxDone = true;
906         }
907       }
908       if(myIsBuildGroups){
909         myInput->BuildGroups();
910         myIsGroupsDone = true;
911       }
912
913       QString aComment;
914       aComment.sprintf("myComment=%s;myFileName=%s;myInitFileName=%s",
915                        GetComment(),
916                        myFileInfo.filePath().toLatin1().data(),
917                        GetInitFileName().c_str()); // Restoring of Python dump
918       SALOMEDS::GenericAttribute_var anAttr;
919       if (!theSObject->FindAttribute(anAttr, "AttributeString"))
920         throw std::runtime_error("Build - There is no AttributeString for the SObject !!!");
921       SALOMEDS::AttributeString_var aCmnt = SALOMEDS::AttributeString::_narrow(anAttr);
922       aCmnt->SetValue(aComment.toLatin1().data());
923     }
924     bool anIsBuildAll = VISU::GetResourceMgr()->booleanValue("VISU", "full_med_loading", false);
925     if(anIsBuildAll)
926       BuildAll();
927     return this;
928   } catch(std::exception& exc) {
929     INFOS("Follow exception was occured :\n"<<exc.what());
930   } catch(...) {
931     INFOS("Unknown exception was occured!!!");
932   }
933   return NULL;
934 }
935
936
937 //---------------------------------------------------------------
938 VISU::Result_i::PInput
939 VISU::Result_i
940 ::GetInput(const std::string& theMeshName,
941            VISU::Entity theEntity,
942            const std::string& theFieldName,
943            CORBA::Long theTimeStampNumber)
944 {
945   return myInput;
946 }
947
948
949 //---------------------------------------------------------------
950 CORBA::Boolean
951 VISU::Result_i
952 ::IsDone()
953 {
954   return
955     myIsEntitiesDone &&
956     (myIsBuildFields? myIsFieldsDone: true) &&
957     (myIsBuildMinMax? myIsMinMaxDone: true) &&
958     (myIsBuildGroups? myIsGroupsDone: true);
959 }
960
961 CORBA::Boolean
962 VISU::Result_i
963 ::IsEntitiesDone()
964 {
965   return myIsEntitiesDone;
966 }
967
968 void
969 VISU::Result_i
970 ::SetBuildFields(CORBA::Boolean theIsBuildFields,
971                  CORBA::Boolean theIsCalculateMinMax)
972 {
973   myIsBuildFields = theIsBuildFields;
974   if(theIsBuildFields)
975     myIsBuildMinMax = theIsCalculateMinMax;
976   else
977     myIsBuildMinMax = false;
978 }
979
980 void
981 VISU::Result_i
982 ::SetBuildGroups(CORBA::Boolean theIsBuildGroups)
983 {
984   myIsBuildGroups = theIsBuildGroups;
985 }
986
987 CORBA::Boolean
988 VISU::Result_i
989 ::IsFieldsDone()
990 {
991   return myIsFieldsDone;
992 }
993
994 CORBA::Boolean
995 VISU::Result_i
996 ::IsGroupsDone()
997 {
998   return myIsGroupsDone;
999 }
1000
1001 CORBA::Boolean
1002 VISU::Result_i
1003 ::IsMinMaxDone()
1004 {
1005   return myIsMinMaxDone;
1006 }
1007
1008 CORBA::Boolean
1009 VISU::Result_i
1010 ::IsPartsDone()
1011 {
1012   return myIsPartsDone;
1013 }
1014
1015
1016 //---------------------------------------------------------------
1017 VISU::Result::EntityNames*
1018 VISU::Result_i
1019 ::GetMeshNames()
1020 {
1021   VISU::Result::EntityNames_var aResult = new VISU::Result::EntityNames();
1022   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1023   if(aMeshMap.empty())
1024     return aResult._retn();
1025
1026   aResult->length(aMeshMap.size());
1027   VISU::TMeshMap::const_iterator anIter = aMeshMap.begin();
1028   for(size_t anId = 0; anIter != aMeshMap.end(); anIter++, anId++){
1029     const std::string& aName = anIter->first;
1030     aResult[anId] = aName.c_str();
1031   }
1032
1033   return aResult._retn();
1034 }
1035
1036
1037 //---------------------------------------------------------------
1038 VISU::Result::Entities*
1039 VISU::Result_i
1040 ::GetEntities(const char* theMeshName)
1041 {
1042   VISU::Result::Entities_var anEntities = new VISU::Result::Entities();
1043   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1044   if(aMeshMap.empty())
1045     return anEntities._retn();
1046
1047   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1048   if(anIter == aMeshMap.end())
1049     return anEntities._retn();
1050
1051   const VISU::PMesh& aMesh = anIter->second;
1052   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1053   if(aMeshOnEntityMap.empty())
1054     return anEntities._retn();
1055
1056   {
1057     anEntities->length(aMeshOnEntityMap.size());
1058     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.begin();
1059     for(size_t anId = 0; anIter != aMeshOnEntityMap.end(); anIter++, anId++){
1060       const VISU::TEntity& anEntity = anIter->first;
1061       anEntities[anId] = VISU::Entity(anEntity);
1062     }
1063   }
1064
1065   return anEntities._retn();
1066 }
1067
1068
1069 //---------------------------------------------------------------
1070 VISU::Result::EntityNames*
1071 VISU::Result_i
1072 ::GetFamilies(const char* theMeshName,
1073               VISU::Entity theEntity)
1074 {
1075   VISU::Result::EntityNames_var aResult = new VISU::Result::EntityNames();
1076   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1077   if(aMeshMap.empty())
1078     return aResult._retn();
1079
1080   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1081   if(anIter == aMeshMap.end())
1082     return aResult._retn();
1083
1084   const VISU::PMesh& aMesh = anIter->second;
1085   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1086   if(aMeshOnEntityMap.empty())
1087     return aResult._retn();
1088
1089   {
1090     VISU::TEntity anEntity = VISU::TEntity(theEntity);
1091     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.find(anEntity);
1092     if(anIter == aMeshOnEntityMap.end())
1093       return aResult._retn();
1094
1095     const VISU::PMeshOnEntity& aMeshOnEntity = anIter->second;
1096     const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
1097     if(aFamilyMap.empty())
1098       return aResult._retn();
1099
1100     {
1101       aResult->length(aFamilyMap.size());
1102       VISU::TFamilyMap::const_iterator anIter = aFamilyMap.begin();
1103       for(size_t anId = 0; anIter != aFamilyMap.end(); anIter++, anId++){
1104         const std::string& aName = anIter->first;
1105         aResult[anId] = aName.c_str();
1106       }
1107     }
1108   }
1109
1110   return aResult._retn();
1111 }
1112
1113
1114 //---------------------------------------------------------------
1115 VISU::Result::EntityNames*
1116 VISU::Result_i
1117 ::GetGroups(const char* theMeshName)
1118 {
1119   VISU::Result::EntityNames_var aResult = new VISU::Result::EntityNames();
1120   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1121   if(aMeshMap.empty())
1122     return aResult._retn();
1123
1124   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1125   if(anIter == aMeshMap.end())
1126     return aResult._retn();
1127
1128   const VISU::PMesh& aMesh = anIter->second;
1129   const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
1130   if(aGroupMap.empty())
1131     return aResult._retn();
1132
1133   {
1134     aResult->length(aGroupMap.size());
1135     VISU::TGroupMap::const_iterator anIter = aGroupMap.begin();
1136     for(size_t anId = 0; anIter != aGroupMap.end(); anIter++, anId++){
1137       const std::string& aName = anIter->first;
1138       aResult[anId] = aName.c_str();
1139     }
1140   }
1141
1142   return aResult._retn();
1143 }
1144
1145
1146 //---------------------------------------------------------------
1147 VISU::Result::EntityNames*
1148 VISU::Result_i
1149 ::GetFields(const char* theMeshName,
1150             VISU::Entity theEntity)
1151 {
1152   VISU::Result::EntityNames_var aResult = new VISU::Result::EntityNames();
1153   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1154   if(aMeshMap.empty())
1155     return aResult._retn();
1156
1157   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1158   if(anIter == aMeshMap.end())
1159     return aResult._retn();
1160
1161   const VISU::PMesh& aMesh = anIter->second;
1162   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1163   if(aMeshOnEntityMap.empty())
1164     return aResult._retn();
1165
1166   {
1167     VISU::TEntity anEntity = VISU::TEntity(theEntity);
1168     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.find(anEntity);
1169     if(anIter == aMeshOnEntityMap.end())
1170       return aResult._retn();
1171
1172     const VISU::PMeshOnEntity& aMeshOnEntity = anIter->second;
1173     const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
1174     if(aFieldMap.empty())
1175       return aResult._retn();
1176
1177     {
1178       aResult->length(aFieldMap.size());
1179       VISU::TFieldMap::const_iterator anIter = aFieldMap.begin();
1180       for(size_t anId = 0; anIter != aFieldMap.end(); anIter++, anId++){
1181         const std::string& aName = anIter->first;
1182         aResult[anId] = aName.c_str();
1183       }
1184     }
1185   }
1186
1187   return aResult._retn();
1188 }
1189
1190
1191 //---------------------------------------------------------------
1192 VISU::Result::TimeStampNumbers*
1193 VISU::Result_i
1194 ::GetTimeStampNumbers(const char* theMeshName,
1195                       VISU::Entity theEntity,
1196                       const char* theFieldName)
1197 {
1198   VISU::Result::TimeStampNumbers_var aResult = new VISU::Result::TimeStampNumbers();
1199   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1200   if(aMeshMap.empty())
1201     return aResult._retn();
1202
1203   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1204   if(anIter == aMeshMap.end())
1205     return aResult._retn();
1206
1207   const VISU::PMesh& aMesh = anIter->second;
1208   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1209   if(aMeshOnEntityMap.empty())
1210     return aResult._retn();
1211
1212   {
1213     VISU::TEntity anEntity = VISU::TEntity(theEntity);
1214     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.find(anEntity);
1215     if(anIter == aMeshOnEntityMap.end())
1216       return aResult._retn();
1217
1218     const VISU::PMeshOnEntity& aMeshOnEntity = anIter->second;
1219     const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
1220     if(aFieldMap.empty())
1221       return aResult._retn();
1222
1223     {
1224       VISU::TFieldMap::const_iterator anIter = aFieldMap.find(theFieldName);
1225       if(anIter == aFieldMap.end())
1226         return aResult._retn();
1227
1228       {
1229         const VISU::PField& aField = anIter->second;
1230         const VISU::TValField& aValField = aField->myValField;
1231         if(aValField.empty())
1232           return aResult._retn();
1233
1234         {
1235           aResult->length(aValField.size());
1236           VISU::TValField::const_iterator anIter = aValField.begin();
1237           for(size_t anId = 0; anIter != aValField.end(); anIter++, anId++){
1238             const vtkIdType& aTimeStampNumber = anIter->first;
1239             aResult[anId] = aTimeStampNumber;
1240           }
1241         }
1242       }
1243     }
1244   }
1245
1246   return aResult._retn();
1247 }
1248
1249
1250 //---------------------------------------------------------------
1251 VISU::double_array*
1252 VISU::Result_i
1253 ::GetTimeStampValues(const char* theMeshName,
1254                      VISU::Entity theEntity,
1255                      const char* theFieldName)
1256 {
1257   VISU::double_array_var aResult = new VISU::double_array();
1258   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1259   if(aMeshMap.empty())
1260     return aResult._retn();
1261
1262   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1263   if(anIter == aMeshMap.end())
1264     return aResult._retn();
1265
1266   const VISU::PMesh& aMesh = anIter->second;
1267   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1268   if(aMeshOnEntityMap.empty())
1269     return aResult._retn();
1270
1271   {
1272     VISU::TEntity anEntity = VISU::TEntity(theEntity);
1273     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.find(anEntity);
1274     if(anIter == aMeshOnEntityMap.end())
1275       return aResult._retn();
1276
1277     const VISU::PMeshOnEntity& aMeshOnEntity = anIter->second;
1278     const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
1279     if(aFieldMap.empty())
1280       return aResult._retn();
1281
1282     {
1283       VISU::TFieldMap::const_iterator anIter = aFieldMap.find(theFieldName);
1284       if(anIter == aFieldMap.end())
1285         return aResult._retn();
1286
1287       {
1288         const VISU::PField& aField = anIter->second;
1289         const VISU::TValField& aValField = aField->myValField;
1290         if(aValField.empty())
1291           return aResult._retn();
1292
1293         {
1294           aResult->length(aValField.size());
1295           VISU::TValField::const_iterator anIter = aValField.begin();
1296           for(size_t anId = 0; anIter != aValField.end(); anIter++, anId++){
1297             const PValForTime& aValForTime = anIter->second;
1298             aResult[anId] = aValForTime->myTime.first;
1299           }
1300         }
1301       }
1302     }
1303   }
1304
1305   return aResult._retn();
1306 }
1307
1308
1309 //---------------------------------------------------------------
1310 CORBA::Long
1311 VISU::Result_i
1312 ::GetNumberOfComponents(const char* theMeshName,
1313                         VISU::Entity theEntity,
1314                         const char* theFieldName)
1315 {
1316   CORBA::Long aResult = 0;
1317   const VISU::TMeshMap& aMeshMap = GetInput()->GetMeshMap();
1318   if(aMeshMap.empty())
1319     return aResult;
1320
1321   VISU::TMeshMap::const_iterator anIter = aMeshMap.find(theMeshName);
1322   if(anIter == aMeshMap.end())
1323     return aResult;
1324
1325   const VISU::PMesh& aMesh = anIter->second;
1326   const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
1327   if(aMeshOnEntityMap.empty())
1328     return aResult;
1329
1330   {
1331     VISU::TEntity anEntity = VISU::TEntity(theEntity);
1332     VISU::TMeshOnEntityMap::const_iterator anIter = aMeshOnEntityMap.find(anEntity);
1333     if(anIter == aMeshOnEntityMap.end())
1334       return aResult;
1335
1336     const VISU::PMeshOnEntity& aMeshOnEntity = anIter->second;
1337     const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
1338     if(aFieldMap.empty())
1339       return aResult;
1340
1341     {
1342       VISU::TFieldMap::const_iterator anIter = aFieldMap.find(theFieldName);
1343       if(anIter == aFieldMap.end())
1344         return aResult;
1345
1346       {
1347         const VISU::PField& aField = anIter->second;
1348         aResult = aField->myNbComp;
1349       }
1350     }
1351   }
1352
1353   return aResult;
1354 }
1355
1356
1357 //---------------------------------------------------------------
1358 VISU::Result::EntityNames*
1359 VISU::Result_i
1360 ::GetPartNames(const char* theMeshName)
1361 {
1362   VISU::Result::EntityNames_var aResult = new VISU::Result::EntityNames();
1363   return aResult._retn();
1364 }
1365
1366
1367 //---------------------------------------------------------------
1368 VISU::Result::Resolutions*
1369 VISU::Result_i
1370 ::GetResolutions(const char* theMeshName,
1371                  const char* thePartName)
1372 {
1373   VISU::Result::Resolutions_var aResult = new VISU::Result::Resolutions();
1374   return aResult._retn();
1375 }
1376
1377
1378 //---------------------------------------------------------------
1379 VISU::Result::Resolution
1380 VISU::Result_i
1381 ::GetResolution(const char* theMeshName,
1382                 const char* thePartName)
1383 {
1384   return VISU::Result::HIDDEN;
1385 }
1386
1387
1388 //---------------------------------------------------------------
1389 void
1390 VISU::Result_i
1391 ::SetResolution(const char* theMeshName,
1392                 const char* thePartName,
1393                 VISU::Result::Resolution theResolution)
1394 {}
1395
1396
1397 //---------------------------------------------------------------
1398 VISU::Result_i*
1399 VISU::Result_i
1400 ::New(SALOMEDS::Study_ptr theStudy,
1401       const ESourceId& theSourceId,
1402       const ECreationId& theCreationId,
1403       CORBA::Boolean theIsBuildImmediately,
1404       CORBA::Boolean theIsBuildFields,
1405       CORBA::Boolean theIsBuildMinMax,
1406       CORBA::Boolean theIsBuildGroups)
1407 {
1408   return new RESULT_CLASS_NAME(theStudy,
1409                                theSourceId,
1410                                theCreationId,
1411                                theIsBuildImmediately,
1412                                theIsBuildFields,
1413                                theIsBuildMinMax,
1414                                theIsBuildGroups);
1415 }
1416
1417
1418 //---------------------------------------------------------------
1419 VISU::Storable*
1420 VISU::Result_i
1421 ::StorableEngine(SALOMEDS::SObject_ptr theSObject,
1422                  const Storable::TRestoringMap& theMap,
1423                  const std::string& thePrefix,
1424                  CORBA::Boolean theIsMultiFile)
1425 {
1426   VISU::Result_i* aResult = new RESULT_CLASS_NAME();
1427   return aResult->Restore(theSObject, theMap, thePrefix, theIsMultiFile);
1428 }
1429
1430
1431 //---------------------------------------------------------------
1432 void
1433 VISU::Result_i
1434 ::ToStream(std::ostringstream& theStr)
1435 {
1436   Storable::DataToStream(theStr,"myName", GetFileName().c_str());
1437   Storable::DataToStream(theStr,"myInitFileName", GetInitFileName().c_str());
1438   Storable::DataToStream(theStr,"myCreationId", myCreationId);
1439   Storable::DataToStream(theStr,"myIsBuildFields", myIsFieldsDone);
1440   Storable::DataToStream(theStr,"myIsBuildMinMax", myIsMinMaxDone);
1441   Storable::DataToStream(theStr,"myIsBuildGroups", myIsGroupsDone);
1442 }
1443
1444 //---------------------------------------------------------------
1445
1446 bool
1447 VISU::Result_i
1448 ::ExportMED(const char* theTargetFileName)
1449 {
1450   static QString aCommand;
1451   const char* aTempFileName = myFileInfo.absoluteFilePath().toLatin1();
1452   aCommand.sprintf(" %s %s", aTempFileName, theTargetFileName);
1453   aCommand = QDir::convertSeparators( aCommand );
1454   aCommand.prepend( COPY_COMMAND );
1455
1456   if(system(aCommand.toLatin1().data()) != 0) {
1457     if(MYDEBUG) MESSAGE("VISU_Gen_i::Copy - Cann't execute the command :"<<aCommand.toLatin1().data());
1458     return false;
1459   }
1460   return true;
1461 }
1462
1463
1464 //---------------------------------------------------------------
1465 std::string
1466 VISU::Result_i
1467 ::GetInitFileName() const
1468 {
1469   return myInitFileName;
1470 }
1471
1472
1473 void
1474 VISU::Result_i
1475 ::SetInitFileName(const std::string& theFileName)
1476 {
1477   myInitFileName = theFileName;
1478 }
1479
1480
1481 //---------------------------------------------------------------
1482 std::string
1483 VISU::Result_i
1484 ::GetFileName() const
1485 {
1486   if(GetSourceId() == eFile)
1487     return VISU::MakeFileName(myFileName, this);
1488
1489   return myFileName;
1490 }
1491
1492
1493 QFileInfo
1494 VISU::Result_i
1495 ::GetFileInfo() const
1496 {
1497   return myFileInfo;
1498 }
1499
1500 VISU::Result_i::ECreationId
1501 VISU::Result_i
1502 ::GetCreationId() const
1503 {
1504   return myCreationId;
1505 }
1506
1507
1508 VISU::Result_i::ESourceId
1509 VISU::Result_i
1510 ::GetSourceId() const
1511 {
1512   return mySourceId;
1513 }
1514
1515
1516 //---------------------------------------------------------------
1517 std::string
1518 VISU::Result_i
1519 ::GetEntry()
1520 {
1521   CORBA::String_var anEntry = mySObject->GetID();
1522   return anEntry.in();
1523 }
1524
1525 SALOMEDS::SObject_var
1526 VISU::Result_i
1527 ::GetSObject() const
1528 {
1529   return mySObject;
1530 }
1531
1532 SALOMEDS::Study_var
1533 VISU::Result_i
1534 ::GetStudyDocument() const
1535 {
1536   return myStudyDocument;
1537 }
1538
1539 _PTR(Study)
1540 VISU::Result_i
1541 ::GetStudy() const
1542 {
1543   return myStudy;
1544 }
1545
1546 SALOMEDS::SComponent_var
1547 VISU::Result_i
1548 ::GetSComponent() const
1549 {
1550   return mySComponent;
1551 }
1552
1553 std::string
1554 VISU::Result_i
1555 ::GetEntry(const Storable::TRestoringMap& theRestoringMap)
1556 {
1557   return Storable::FindEntry(myStudyDocument,
1558                              GetEntry(),
1559                              theRestoringMap);
1560 }
1561
1562
1563 //=======================================================================
1564 //function : GetAxisInfo
1565 //purpose  :
1566 //=======================================================================
1567 const VISU::Result_i::TAxisInfo*
1568 VISU::Result_i
1569 ::GetAxisInfo(const string& theMeshName,
1570               TAxis         theAxis,
1571               gp_Dir&       thePlaneNormal)
1572 {
1573   const TAxisInfo* components = NULL;
1574
1575   if ( theAxis < AXIS_X || theAxis > AXIS_Z ) {
1576     MESSAGE(" Bad axis index " << theAxis );
1577     return components;
1578   }
1579
1580   map< string, TGridInfo >::iterator name_info;
1581   name_info = myMeshName2GridInfoMap.find( theMeshName );
1582   TGridInfo * gInfo = 0;
1583
1584   if ( name_info != myMeshName2GridInfoMap.end() )
1585   {
1586     gInfo = & name_info->second;
1587   }
1588   else if ( myInput && IsPossible() && theAxis >= AXIS_X && theAxis <= AXIS_Z )
1589   {
1590     // check presence of theMeshName
1591     const VISU::TMeshMap& meshMap = myInput->GetMeshMap();
1592     PMesh theMesh;
1593     VISU::TMeshMap::const_iterator aIter = meshMap.find( theMeshName );
1594     if ( aIter == meshMap.end() ) {
1595       MESSAGE("No mesh named " << theMeshName );
1596       return components;
1597     } else
1598       theMesh = aIter->second;
1599
1600     VISU::PUnstructuredGridIDMapper anIDMapper = myInput->GetMeshOnEntity(theMeshName,
1601                                                                           CELL_ENTITY);
1602     vtkUnstructuredGrid* aMesh = anIDMapper->GetUnstructuredGridOutput();
1603
1604     if ( !aMesh || aMesh->GetNumberOfCells() == 0 ) {
1605       MESSAGE( "No cells in the mesh: " << theMeshName );
1606       return components;
1607     }
1608
1609     if ( !theMesh->IsStructured() || theMesh->myIsPolarType ) {
1610       // define axis directions and min cell size in each direction
1611       const int nbAxes = 3;
1612       int iAx;
1613       gp_Vec axDirs[ nbAxes ];
1614       float minSize[3] = { FLT_MAX, FLT_MAX, FLT_MAX };
1615       bool axesComputed = false;
1616       for ( vtkIdType iCell = 0; iCell < aMesh->GetNumberOfCells(); ++iCell ) {
1617         vtkCell* cell = aMesh->GetCell( iCell );
1618         
1619         if (cell->GetCellType() != VTK_HEXAHEDRON &&
1620             cell->GetCellType() != VTK_QUADRATIC_HEXAHEDRON )
1621           continue;
1622         vtkPoints * points = cell->GetPoints();
1623         vtkFloatingPointType coords[ 4 ][3];
1624         points->GetPoint( 0, coords[0] );
1625         points->GetPoint( 1, coords[1] );
1626         points->GetPoint( 3, coords[2] );
1627         points->GetPoint( 4, coords[3] );
1628         gp_Pnt p0( coords[0][0], coords[0][1], coords[0][2] );
1629         for ( iAx = 0; iAx < nbAxes; ++iAx ) {
1630           vtkFloatingPointType* coo = coords[ iAx + 1 ];
1631           gp_Pnt p( coo[0], coo[1], coo[2] );
1632           // min size
1633           vtkFloatingPointType size = p0.SquareDistance( p );
1634           if ( size > FLT_MIN && size < minSize[ iAx ] )
1635             minSize[ iAx ] = size;
1636           // axis direction
1637           if ( !axesComputed ) {
1638             gp_Vec dir( p0, p );
1639             if ( dir.SquareMagnitude() <= gp::Resolution() ) {
1640               break;
1641             }
1642             axDirs[ iAx ] = dir;
1643           }
1644         }
1645         if ( iAx == nbAxes )
1646           axesComputed = true;
1647       }
1648       if ( !axesComputed ) {
1649         MESSAGE("No good hexahedrons in the mesh: " << theMeshName );
1650         return components;
1651       }
1652
1653       // compute axes dirs
1654       gInfo = & myMeshName2GridInfoMap[ theMeshName ];
1655       for ( iAx = 0; iAx < nbAxes; ++iAx ) {
1656         int iPrev = ( iAx == 0 ) ? 2 : iAx - 1;
1657         int iNext = ( iAx == 2 ) ? 0 : iAx + 1;
1658         gInfo->myAxis[ iAx ] = axDirs[ iPrev ] ^ axDirs[ iNext ];
1659       }
1660
1661       // get and sort intermediate component values - projections of nodes
1662       // on axis direction; define bnd box
1663       set< vtkFloatingPointType > comps[ 3 ];
1664       Bnd_Box box;
1665       vtkPoints * points = aMesh->GetPoints();
1666       vtkIdType iP, nbP = aMesh->GetNumberOfPoints();
1667       for ( iP = 0; iP < nbP; ++iP ) {
1668         vtkFloatingPointType coo[3];
1669         points->GetPoint( iP, coo );
1670         gp_Pnt p( coo[0], coo[1], coo[2] );
1671         box.Add( p );
1672         for ( iAx = 0; iAx < nbAxes; ++iAx ) {
1673           const gp_Dir& dir = gInfo->myAxis[ iAx ];
1674           vtkFloatingPointType dot = dir.XYZ() * p.XYZ();
1675           comps[ iAx ].insert( dot );
1676         }
1677       }
1678
1679       // find a range of projections of bnd box corners on each axis
1680       vtkFloatingPointType range[3], firstValue[3];
1681       double x[2],y[2],z[2];
1682       box.Get(x[0],y[0],z[0],x[1],y[1],z[1]);
1683       for ( iAx = 0; iAx < nbAxes; ++iAx ) {
1684         set< vtkFloatingPointType > bndComps;
1685         const gp_Dir& dir = gInfo->myAxis[ iAx ];
1686         for ( int iX = 0; iX < 2; ++iX ) {
1687           for ( int iY = 0; iY < 2; ++iY ) {
1688             for ( int iZ = 0; iZ < 2; ++iZ ) {
1689               gp_Pnt p( x[ iX ], y[ iY ], z[ iZ ] );
1690               vtkFloatingPointType dot = dir.XYZ() * p.XYZ();
1691               bndComps.insert( dot );
1692             }
1693           }
1694         }
1695         firstValue[ iAx ] = *bndComps.begin();
1696         range[ iAx ] = *bndComps.rbegin() - *bndComps.begin();
1697       }
1698
1699       // compute component values
1700       for ( iAx = 0; iAx < nbAxes; ++iAx ) {
1701         list< vtkFloatingPointType > values;
1702         int nbVals = 0;
1703         set< vtkFloatingPointType >& comp = comps[ iAx ];
1704         set< vtkFloatingPointType >::iterator val = comp.begin();
1705         vtkFloatingPointType bnd = -1., rng = range[ iAx ], first = firstValue[ iAx ];
1706         vtkFloatingPointType tol = 0.1 * sqrt( minSize[ iAx ]) / rng;
1707         for ( ; val != comp.end(); ++val ) {
1708           vtkFloatingPointType value = ( *val - first ) / rng;
1709           if ( value > bnd ) {
1710             values.push_back( value );
1711             bnd = value + tol;
1712             nbVals++;
1713           }
1714         }
1715         // store values in gInfo
1716         vector< vtkFloatingPointType >& myComp = gInfo->myComponets[ iAx ];
1717         myComp.resize( nbVals );
1718         list< vtkFloatingPointType >::iterator v = values.begin();
1719         for ( int i = 0; v != values.end(); ++v ){
1720           myComp[ i++ ] = *v;
1721         }
1722       }
1723     }
1724     else {
1725       vtkIdType aMeshDim = theMesh->myDim;
1726       gInfo = & myMeshName2GridInfoMap[ theMeshName ];
1727       switch ( aMeshDim ) {
1728       case 3: {
1729         gp_Dir aDir(0.0,0.0,1.0);
1730         gInfo->myAxis[ 2 ] = aDir;
1731       }
1732       case 2: {
1733         gp_Dir aDir(0.0,1.0,0.0);
1734         gInfo->myAxis[ 1 ] = aDir;
1735       }
1736       case 1: {
1737         gp_Dir aDir(1.0,0.0,0.0);
1738         gInfo->myAxis[ 0 ] = aDir;
1739       }}
1740
1741       TStructuredId aStructuredId = theMesh->GetStructure();
1742       vector<vtkFloatingPointType> PointsCoords[3];
1743       vtkPoints* aPoints = aMesh->GetPoints();
1744       switch ( aMeshDim ) {
1745       case 3: {
1746         TStructuredId aCoordIJK;
1747         vtkIdType nbZ = aStructuredId[2];
1748         for ( int i = 0; i < nbZ; i++ ) {
1749           aCoordIJK[2] = i + 1;
1750           vtkIdType aObjID = anIDMapper->GetObjectIDByIndexes(aCoordIJK);
1751           vtkIdType aVTKID = anIDMapper->GetNodeVTKID(aObjID);
1752           vtkFloatingPointType* aPCoord = aPoints->GetPoint(aVTKID);
1753           PointsCoords[2].push_back(aPCoord[2]);
1754         }
1755       }
1756       case 2: {
1757         TStructuredId aCoordIJK;
1758         vtkIdType nbJ = aStructuredId[1];
1759         for ( int i = 0; i < nbJ; i++ ) {
1760           aCoordIJK[1] = i + 1;
1761           vtkIdType aObjID = anIDMapper->GetObjectIDByIndexes(aCoordIJK);
1762           vtkIdType aVTKID = anIDMapper->GetNodeVTKID(aObjID);
1763           vtkFloatingPointType* aPCoord = aPoints->GetPoint(aVTKID);
1764           PointsCoords[1].push_back(aPCoord[1]);
1765         }
1766       }
1767       case 1: {
1768         TStructuredId aCoordIJK;
1769         vtkIdType nbI = aStructuredId[0];
1770         for ( int i = 0; i < nbI; i++ ) {
1771           aCoordIJK[0] = i + 1;
1772           vtkIdType aObjID = anIDMapper->GetObjectIDByIndexes(aCoordIJK);
1773           vtkIdType aVTKID = anIDMapper->GetNodeVTKID(aObjID);
1774           vtkFloatingPointType* aPCoord = aPoints->GetPoint(aVTKID);
1775           PointsCoords[0].push_back(aPCoord[0]);
1776         }
1777       }}
1778       for ( int i = 0; i < aMeshDim; i++ ) {
1779         vector< vtkFloatingPointType >& myComp = gInfo->myComponets[ i ];
1780         int aSize = PointsCoords[i].size();
1781         if ( aSize > 0 ) {
1782           vtkFloatingPointType aLen = PointsCoords[i][aSize-1] - PointsCoords[i][0];
1783           myComp.resize(aSize);
1784           myComp[0] = 0;
1785           for ( int k = 1; k < aSize; k++ ) {
1786             myComp[k]=myComp[k-1] + (PointsCoords[i][k]-PointsCoords[i][k-1])/aLen;
1787           }
1788         }
1789       }
1790     }
1791   }
1792
1793   // set return values
1794   if ( gInfo ) {
1795     thePlaneNormal = gInfo->myAxis[ theAxis ];
1796     components = & gInfo->myComponets[ theAxis ];
1797   }
1798
1799   return components;
1800 }
1801
1802 SALOME_MED::MedFileInfo* VISU::Result_i::GetMEDFileInfo()
1803 {
1804   SALOME_MED::MedFileInfo_var res = new SALOME_MED::MedFileInfo();
1805
1806   QString fpath = myFileInfo.absoluteFilePath();
1807   res->fileName = fpath.toLatin1().constData();
1808   res->fileSize = myFileInfo.size();
1809   int major, minor, release;
1810   if( !MED::getMEDVersion( fpath.toLatin1().constData(), major, minor, release ) )
1811   {
1812     major = -1;
1813     minor = -1;
1814     release = -1;
1815   }
1816   res->major = major;
1817   res->minor = minor;
1818   res->release = release;
1819   return res._retn();
1820 }