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