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