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