]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Result_i.cc
Salome HOME
Merge with OCC_development_01
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISU_Result_i.cc
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_Result_i.hh"
28 #include "VISU_Convertor_impl.hxx"
29 #include "VISU_CorbaMedConvertor.hxx"
30 #include "VISU_PipeLine.hxx"
31
32 #include "QAD_Config.h"
33 #include "SALOMEDS_Tool.hxx"
34 #include "HDFascii.hxx"
35
36 #include <qstring.h>
37 #include <qfileinfo.h>
38
39 #include <memory>
40 #include <fstream>      
41
42 #include <vtkUnstructuredGridReader.h>
43 #include <vtkUnstructuredGridWriter.h>
44
45 using namespace VISU;
46 using namespace std;
47
48 #ifdef _DEBUG_
49 static int MYDEBUG = 0;
50 #else
51 static int MYDEBUG = 0;
52 #endif
53
54 VISU::Result_var VISU::FindResult(SALOMEDS::SObject_ptr theSObject){
55   SALOMEDS::SComponent_var aSComponent = theSObject->GetFatherComponent();
56   SALOMEDS::SObject_var aFather = theSObject->GetFather();
57   CORBA::String_var aComponentID(aSComponent->GetID());
58   CORBA::String_var aFatherID(aFather->GetID());
59   VISU::Result_var aResult;
60   while(strcmp(aComponentID,aFatherID) != 0){
61     CORBA::Object_var anObject = VISU::SObjectToObject(aFather);
62     if(!CORBA::is_nil(anObject)){
63       aResult = VISU::Result::_narrow(anObject);
64       if(!aResult->_is_nil()) return aResult;
65     }
66     aFather = aFather->GetFather();
67   }
68   return aResult;
69 }
70
71 void VISU::RemoveFromStudy(SALOMEDS::SObject_ptr theSObject, int theIsAttrOnly){
72   if(!theSObject->_is_nil()){
73     SALOMEDS::Study_var aStudy = theSObject->GetStudy();
74     SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
75     if(theIsAttrOnly)
76       aStudyBuilder->RemoveAttribute(theSObject,"AttributeIOR");
77     else
78       aStudyBuilder->RemoveObjectWithChildren(theSObject);
79   }
80 }
81
82 QString GenerateName(const char* theName){
83   typedef map<string,int> TNameMap;
84   static TNameMap aMap;
85   TNameMap::const_iterator i = aMap.find(theName);
86   QString tmp;
87   if(i == aMap.end()) {
88     aMap[theName] = 0;
89     tmp = theName;
90   }else{
91     tmp = VISU::GenerateName(theName,++aMap[theName]);
92   }
93   if(MYDEBUG) MESSAGE("GenerateName - "<<tmp<<" from - "<<theName<<"; " <<aMap[theName]);
94   return tmp;
95 }
96
97 QString GenerateFieldName(const string& theName, const string& theUnits){
98   static QString aName;
99   const string tmp(theUnits.size(),' ');
100   if(theUnits == "" || theUnits == tmp)
101     aName.sprintf("%s, -",theName.c_str());
102   else
103     aName.sprintf("%s, %s",theName.c_str(),theUnits.c_str());
104   aName = aName.simplifyWhiteSpace();
105   return aName.latin1();
106 }
107
108 void CreateReference(SALOMEDS::Study_ptr theStudyDocument, 
109                      const string& theFatherEntry, const string& theRefEntry)
110 {
111   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
112   SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry.c_str());
113   SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(aFather);
114   SALOMEDS::SObject_var aRefSObj = theStudyDocument->FindObjectID(theRefEntry.c_str());
115   aStudyBuilder->Addreference(newObj,aRefSObj);
116 }
117
118 string GetComponentDataType(SALOMEDS::SObject_ptr theSObject){
119   SALOMEDS::SComponent_var aCompRefSObj = theSObject->GetFatherComponent();
120   CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
121   return aDataType.in();
122 }
123
124 //==============================================================================
125
126 const string VISU::Result_i::myComment = "RESULT";
127 const char* VISU::Result_i::GetComment() const { return myComment.c_str();}
128
129 VISU::Result_i::Result_i(SALOMEDS::Study_ptr theStudy, const TSourceId& aSourceId) {
130   myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
131   mySourceId = aSourceId;
132   myInput = NULL;
133   myIsDone = 0;
134   CORBA::String_var aName = theStudy->Name();
135   MESSAGE("Result_i::Result_i - this = "<<this<<"; StudyId = "<<
136         theStudy->StudyId()<<"; Name = '"<<aName.in()<<"'");
137 }
138
139
140 void VISU::Result_i::RemoveFromStudy(){
141   VISU::RemoveFromStudy(mySObject,false);
142 }
143
144
145 int VISU::Result_i::IsPossible(){
146   try{
147     float aSize = myInput->GetSize();
148     bool aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
149     MESSAGE("Result_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<float(aResult));
150     return aResult;
151   }catch(std::exception& exc){
152     INFOS("Follow exception was occured :\n"<<exc.what());
153   }catch(...){
154     INFOS("Unknown exception was occured!");
155   }
156   return 0;
157 }
158
159
160 CORBA::Boolean VISU::Result_i::BuildAll(){
161   if(MYDEBUG) MESSAGE("Result_i::Build - myIsDone = "<<myIsDone);
162   if(myIsDone) return 1;
163   if(!IsPossible()) return 0;
164   try{
165     const VISU::TMeshMap& aMeshMap = myInput->GetMeshMap();
166     VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
167     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
168       const string& aMeshName = aMeshMapIter->first;
169       const VISU::PMesh aMesh = aMeshMapIter->second;
170       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
171       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
172       //Import fields
173       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
174       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
175         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
176         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
177         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
178         VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
179         for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
180           const string& aFieldName = aFieldMapIter->first;
181           const VISU::PField aField = aFieldMapIter->second;
182           const VISU::TValField& aValField = aField->myValField;
183           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
184           for(; aValFieldIter != aValField.end(); aValFieldIter++){
185             int aTimeStamp = aValFieldIter->first;
186             try{
187               myInput->GetTimeStampOnMesh(aMeshName,anEntity,aFieldName,aTimeStamp);
188             }catch(std::exception& exc){
189               INFOS("Follow exception was occured :\n"<<exc.what());
190             }catch(...){
191               INFOS("Unknown exception was occured!!!");
192             }
193           }
194         }
195         //Importing groups
196         const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
197         VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
198         for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
199           const string& aGroupName = aGroupMapIter->first;
200           try{
201             myInput->GetMeshOnGroup(aMeshName,aGroupName);
202           }catch(std::exception& exc){
203             INFOS("Follow exception was occured :\n"<<exc.what());
204           }catch(...){
205             INFOS("Unknown exception was occured!!!");
206           }
207         }
208         //Import families
209         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
210         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
211         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
212           const string& aFamilyName = aFamilyMapIter->first;
213           try{
214             myInput->GetMeshOnEntity(aMeshName,anEntity,aFamilyName);
215           }catch(std::exception& exc){
216             INFOS("Follow exception was occured :\n"<<exc.what());
217           }catch(...){
218             INFOS("Unknown exception was occured!!!");
219           }
220         }
221         //Import mesh on entity
222         try{
223           myInput->GetMeshOnEntity(aMeshName,anEntity);
224         }catch(std::exception& exc){
225           INFOS("Follow exception was occured :\n"<<exc.what());
226         }catch(...){
227           INFOS("Unknown exception was occured!!!");
228         }
229       }
230     }
231     myIsDone = 1;
232   }catch(std::exception& exc){
233     INFOS("Follow exception was occured :\n"<<exc.what());
234   }catch(...){
235     INFOS("Unknown exception was occured!!!");
236   }
237   return myIsDone;
238 }
239
240 VISU::Storable* VISU::Result_i::Build(SALOMEDS::SObject_ptr theSObject) 
241 {
242   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
243   aStudyBuilder->NewCommand();  // There is a transaction
244   if(MYDEBUG) MESSAGE("Result_i::Build");
245   try{
246     const TMeshMap& aMeshMap = myInput->GetMeshMap();
247     if(aMeshMap.empty()) 
248       throw std::runtime_error("Build - There is no any mesh information in the file !!!");
249     mySComponent = FindOrCreateVisuComponent(myStudyDocument);
250     CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
251     string aRefFatherEntry = GetRefFatherEntry();
252     QString aComment;
253     aComment.sprintf("myComment=%s;myType=%d;myFileName=%s;myInitFileName=%s",
254                      GetComment(),
255                      VISU::TRESULT,
256                      myFileInfo.filePath().latin1(),
257                      myInitFileName.c_str()); // Restoring of Python dump 
258     string aResultEntry = 
259       CreateAttributes(myStudyDocument,
260                        aSComponentEntry,
261                        aRefFatherEntry.c_str(),
262                        anIOR,
263                        myName.c_str(),
264                        "",
265                        aComment.latin1(),
266                        true);
267     mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
268     if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
269     if(!CORBA::is_nil(theSObject)){
270       CORBA::String_var aString = theSObject->GetID();
271       CreateReference(myStudyDocument,aResultEntry,aString.in());
272     }
273     TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
274     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
275       const string& aMeshName = aMeshMapIter->first;
276       const VISU::PMesh aMesh = aMeshMapIter->second;
277       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
278       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
279       aComment.sprintf("myComment=MESH;myName=%s;myDim=%d",
280                        aMeshName.c_str(),aMesh->myDim);
281       string aMeshEntry = CreateAttributes(myStudyDocument,aResultEntry.c_str(),aRefFatherEntry.c_str(),
282                                            "",aMeshName.c_str(),"",aComment.latin1(),true);
283       if(aMeshOnEntityMap.empty()) continue;
284       aComment.sprintf("myComment=FAMILIES;myMeshName=%s",aMeshName.c_str());
285       string aSubMeshesEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
286                                                 "","Families","",aComment.latin1(),true);
287       //Import entities and according families
288       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
289       typedef std::map<std::string,std::string> TComment2EntryMap;
290       TComment2EntryMap aComment2EntryMap;
291       typedef std::map<VISU::TEntity,std::string> TEntity2EntryMap;
292       TEntity2EntryMap aEntity2EntryMap;
293       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
294         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
295         aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
296                          VISU::TENTITY,
297                          aMeshName.c_str(),
298                          anEntity);
299         string anEntityName;
300         switch(anEntity){
301         case VISU::NODE_ENTITY : anEntityName = "onNodes"; break;
302         case VISU::EDGE_ENTITY : anEntityName = "onEdges"; break;
303         case VISU::FACE_ENTITY : anEntityName = "onFaces"; break;
304         case VISU::CELL_ENTITY : anEntityName = "onCells"; break;
305         default:
306           throw std::runtime_error("Build >> Value of entity is incorrect!");
307         }
308         aEntity2EntryMap[anEntity] = CreateAttributes(myStudyDocument,aSubMeshesEntry.c_str(),aRefFatherEntry.c_str(),
309                                                    "",anEntityName.c_str(),"",aComment.latin1(),true);
310         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
311         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
312         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
313         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
314           const string& aFamilyName = aFamilyMapIter->first;
315           aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
316                            VISU::TFAMILY,
317                            aMeshName.c_str(),
318                            anEntity,
319                            aFamilyName.c_str());
320           aComment2EntryMap[aComment.latin1()] = 
321             CreateAttributes(myStudyDocument,
322                              aEntity2EntryMap[anEntity].c_str(),
323                              aRefFatherEntry.c_str(),
324                              "",
325                              aFamilyName.c_str(),
326                              "",
327                              aComment.latin1(),
328                              true);
329         }
330       }
331       //Importing groups
332       const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
333       if(aGroupMap.size() > 0){
334         aComment.sprintf("myComment=GROUPS;myMeshName=%s",
335                          aMeshName.c_str());
336         string aGroupsEntry = 
337           CreateAttributes(myStudyDocument,
338                            aMeshEntry.c_str(),
339                            aRefFatherEntry.c_str(),
340                            "",
341                            "Groups",
342                            "",
343                            aComment.latin1(),
344                            true);
345         VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
346         for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
347           const string& aGroupName = aGroupMapIter->first;
348           aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
349                            VISU::TGROUP,aMeshName.c_str(),aGroupName.c_str());
350           string aGroupEntry = CreateAttributes(myStudyDocument,aGroupsEntry.c_str(),aRefFatherEntry.c_str(),
351                                                 "",aGroupName.c_str(),"",aComment.latin1(),true);
352           const VISU::PGroup aGroup = aGroupMapIter->second;
353           const VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup->myFamilyAndEntitySet;
354           VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = aFamilyAndEntitySet.begin();
355           for(; aFamilyAndEntitySetIter != aFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
356             const VISU::TFamilyAndEntity& aFamilyAndEntity = *aFamilyAndEntitySetIter;
357             const string& aFamilyName = aFamilyAndEntity.first;
358             const VISU::TEntity& anEntity = aFamilyAndEntity.second;
359             aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
360                              VISU::TFAMILY,
361                              aMeshName.c_str(),
362                              anEntity,
363                              aFamilyName.c_str());
364             CreateReference(myStudyDocument,aGroupEntry,aComment2EntryMap[aComment.latin1()]);
365           }
366         }
367       }
368       //Import fields
369       string aFieldsEntry;
370       bool isFieldEntryCreated = 0;
371       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
372       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
373         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
374         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
375         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
376         VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
377         for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
378           if(!isFieldEntryCreated){
379             aComment.sprintf("myComment=FIELDS;myMeshName=%s",
380                              aMeshName.c_str());
381             aFieldsEntry = 
382               CreateAttributes(myStudyDocument,
383                                aMeshEntry.c_str(),
384                                aRefFatherEntry.c_str(),
385                                "",
386                                "Fields",
387                                "",
388                                aComment.latin1(),
389                                true);
390             isFieldEntryCreated = true;
391           }
392           const string& aFieldName = aFieldMapIter->first;
393           const VISU::PField aField = aFieldMapIter->second;
394           const VISU::TValField& aValField = aField->myValField;
395           QString aFieldNameWithUnit = ::GenerateFieldName(aFieldName,aField->myUnitNames[0]);
396           aComment.sprintf("myComment=FIELD;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
397                            VISU::TFIELD,aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField->myNbComp);
398           string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
399                                                 "",aFieldNameWithUnit.latin1(),"",aComment.latin1(),true);
400           CreateReference(myStudyDocument,aFieldEntry,aEntity2EntryMap[anEntity]);
401           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
402           for(; aValFieldIter != aValField.end(); aValFieldIter++){
403             int aTimeStamp = aValFieldIter->first;
404             const VISU::PValForTime aValForTime = aValFieldIter->second;
405             aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
406                              VISU::TTIMESTAMP,aMeshName.c_str(),anEntity,aFieldName.c_str(),aTimeStamp,aField->myNbComp);
407             string aTimeStampId = VISU_Convertor::GenerateName(aValForTime->myTime);
408             CreateAttributes(myStudyDocument,aFieldEntry.c_str(),aRefFatherEntry.c_str(),
409                              "",aTimeStampId.c_str(),"",aComment.latin1(),true);
410           }
411         }
412       }
413     }
414     QString aIsBuild = QAD_CONFIG->getSetting("Visu:BuildResult");
415     bool isBuildAll = aIsBuild.isEmpty()? 0 : aIsBuild.toInt();
416     if(isBuildAll) BuildAll();
417   }catch(std::exception& exc){
418     INFOS("Follow exception was occured :\n"<<exc.what());
419     return NULL;
420   }catch(...){
421     INFOS("Unknown exception was occured!!!");
422     return NULL;
423   }
424   aStudyBuilder->CommitCommand();
425   return this;
426 }
427
428 VISU::Storable* VISU::Result_i::Create(const char* theFileName){
429   try{
430     myFileInfo.setFile(theFileName);
431     myInitFileName = myFileInfo.filePath().latin1();
432     myName = ::GenerateName(myFileInfo.fileName()).latin1();
433     if(GetSourceId() == eRestoredFile){
434       auto_ptr<char> aTmpDir((char*)SALOMEDS_Tool::GetTmpDir().c_str());
435       static QString aCommand;
436       aCommand.sprintf("cp %s %s",myFileInfo.absFilePath().latin1(),aTmpDir.get());
437       if(system(aCommand) == -1){
438         MESSAGE("Create - Can't execute the command :"<<aCommand);
439         return NULL;
440       }
441       if(MYDEBUG) MESSAGE("Result_i::Create - aCommand = "<<aCommand);
442       myFileInfo.setFile(QString(aTmpDir.get()) + myFileInfo.fileName());
443     }
444     myInput = CreateConvertor(myFileInfo.absFilePath().latin1());
445     if(!myInput) 
446       throw std::runtime_error("Create - Cannot create a Convertor for this file!!!"); return Build();
447   }catch(std::exception& exc){
448     INFOS("Follow exception was occured :\n"<<exc.what());
449   }catch(...){
450     INFOS("Unknown exception was occured!!!");
451   }
452   return NULL;
453 }
454
455 VISU::Storable* VISU::Result_i::Create(SALOMEDS::SObject_ptr theMedSObject){
456   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
457   try{
458     myInput = CreateMEDConvertor(theMedSObject);
459     if(myInput == NULL) return NULL;
460     mySourceId = eComponent;
461     string aCompDataType = GetComponentDataType(theMedSObject);
462     myFileInfo.setFile(aCompDataType.c_str());
463     myName = ::GenerateName("aResult").latin1();
464     VISU::Storable* aStorable = Build(theMedSObject);
465     return aStorable;
466   }catch(std::exception& exc){
467     INFOS("Follow exception was occured :\n"<<exc.what());
468   }catch(...){
469     INFOS("Unknown exception was occured!!!");
470   }
471   return NULL;
472 }
473
474 VISU::Storable* VISU::Result_i::Create(SALOME_MED::FIELD_ptr theField){
475   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
476   try{
477     myInput = CreateMEDFieldConvertor(theField);
478     if(myInput == NULL) return NULL;
479     mySourceId = eComponent;
480     string aCompDataType = "MED";
481     myFileInfo.setFile(aCompDataType.c_str());
482     myInitFileName = aCompDataType;
483     myName = ::GenerateName("aResult").latin1();
484     CORBA::String_var anIOR = myStudyDocument->ConvertObjectToIOR(theField);
485     SALOMEDS::SObject_var aFieldSObject = myStudyDocument->FindObjectIOR(anIOR);
486     VISU::Storable* aStorable = Build(aFieldSObject);
487     return aStorable;
488   }catch(std::exception& exc){
489     INFOS("Follow exception was occured :\n"<<exc.what());
490   }catch(...){
491     INFOS("Unknown exception was occured!!!");
492   }
493   return NULL;
494 }
495
496 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
497                                         const Storable::TRestoringMap& theMap, const string& thePrefix)
498 {
499   if(MYDEBUG)  MESSAGE("Result_i::Restore - "<<thePrefix);
500   try{
501     mySObject = SALOMEDS::SObject::_duplicate(theSObject);
502     myStudyDocument = mySObject->GetStudy();
503     mySComponent = mySObject->GetFatherComponent();
504     myName = VISU::Storable::FindValue(theMap,"myName").latin1();
505     myInitFileName = VISU::Storable::FindValue(theMap,"myInitFileName").latin1();
506     SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
507     if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
508       mySourceId = eRestoredComponent;
509       if(MYDEBUG)  MESSAGE("Result_i::GetInput - There is some reference.");
510       SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
511       CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
512       myFileInfo.setFile(aDataType.in());
513       if(MYDEBUG)  MESSAGE("Result_i::GetInput - aDataType = "<<aDataType);
514       Engines::Component_var aEngComp = Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
515       if (CORBA::is_nil(aEngComp)) 
516         throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
517       SALOMEDS::StudyBuilder_var  aStudyBuilder = myStudyDocument->NewBuilder();
518       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
519       aStudyBuilder->LoadWith(aCompRefSObj,aDriver);
520       if(strcmp(aDataType,"MED") == 0)
521         myInput = CreateMEDConvertor(aTargetRefSObj);
522       else
523         throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
524     }else{
525       myFileInfo.setFile(thePrefix.c_str());
526
527       string aStudyPrefix("");
528       if (IsMultifile()) aStudyPrefix = (SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
529       if(!myFileInfo.isFile()){
530         string aFileName = thePrefix + aStudyPrefix + "_" + myName;
531         myFileInfo.setFile(aFileName.c_str());
532       }
533       if(MYDEBUG)  
534         MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
535
536       if (HDFascii::isASCII(myFileInfo.filePath().latin1())) {
537         char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().latin1());
538         char* aHDFFileName = new char[strlen(aResultPath) + 19];
539         sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
540
541         if (IsMultifile()) { // set this file as new - temporary
542           static QString aCommand;
543           aCommand.sprintf("mv %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().latin1());
544           if(system(aCommand) == -1){
545             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
546             return NULL;
547           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
548           myFileInfo.setFile(QString(aResultPath)+QString(myFileInfo.baseName().latin1()));
549         } else { // change current temporary file to the new: with hdf-format
550           static QString aCommand;
551           aCommand.sprintf("mv %s %s\0",aHDFFileName, myFileInfo.filePath().latin1());
552           if(system(aCommand.latin1()) == -1) {
553             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
554             return NULL;
555           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
556           SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
557           SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList.in(), true);
558         }
559         mySourceId = eRestoredFile;
560         delete(aResultPath);
561         delete(aHDFFileName);
562       } else if (!IsMultifile()) 
563         mySourceId = eRestoredFile;
564       else 
565         mySourceId = eFile;
566       if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - mySourceId = "<<mySourceId);
567       myInput = CreateConvertor(myFileInfo.filePath().latin1());
568       QString aComment;
569       aComment.sprintf("myComment=%s;myType=%d;myFileName=%s;myInitFileName=%s",
570                        GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1(),
571                        myInitFileName.c_str()); // Restoring of Python dump 
572       SALOMEDS::GenericAttribute_var anAttr;
573       if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
574         throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
575       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
576       aCmnt->SetValue(aComment.latin1());
577     }
578     QString aIsBuild = QAD_CONFIG->getSetting("Visu:BuildResult");
579     if(aIsBuild.isEmpty()? 0 : aIsBuild.toInt()) 
580       BuildAll();
581     return this;
582   }catch(std::exception& exc){
583     INFOS("Follow exception was occured :\n"<<exc.what());
584   }catch(...){
585     INFOS("Unknown exception was occured!!!");
586   }
587   return NULL;
588 }
589
590 VISU::Result_i::TInput* VISU::Result_i::GetInput() { 
591   return myInput;
592 }
593
594 void VISU::Result_i::ToStream(std::ostringstream& theStr){
595   if(MYDEBUG) MESSAGE(GetComment());
596   Storable::DataToStream(theStr,"myName",myName.c_str());
597   Storable::DataToStream(theStr,"myInitFileName",myInitFileName.c_str());
598 }
599
600 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
601                                         const string& thePrefix, const Storable::TRestoringMap& theMap)
602 {
603   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
604   VISU::Result_i* pResult = new VISU::Result_i(aStudy);
605   if(pResult == NULL) return NULL;
606   return pResult->Restore(theSObject,theMap,thePrefix);
607 }
608      
609 string VISU::Result_i::GetRefFatherEntry() { 
610   //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
611   return "";
612 }
613
614 string VISU::Result_i::GetEntry(){ 
615   CORBA::String_var anEntry = mySObject->GetID();
616   return string(anEntry);
617 }
618
619 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
620 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
621 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
622 std::string VISU::Result_i::GetEntry(const std::string& theComment) 
623
624   return FindEntryWithComment(myStudyDocument,GetEntry().c_str(),theComment.c_str());
625 }
626
627 VISU::Result_i::~Result_i() {
628   MESSAGE("Result_i::~Result_i() - this = "<<this);
629   if(GetSourceId() == eRestoredFile){ 
630     static QString aCommand;
631     aCommand.sprintf("rm %s",myFileInfo.filePath().latin1());
632     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
633     aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",myFileInfo.dirPath().latin1());
634     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
635   }
636   if(myInput) delete myInput;
637 }