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