]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Result_i.cc
Salome HOME
DCQ : Merge with Ecole Ete a6.
[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::runtime_error& exc){
154     INFOS("Follow exception was accured :\n"<<exc.what());
155   }catch(...){
156     INFOS("Unknown exception was accured!");
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::runtime_error& exc){
191               INFOS("Follow exception was accured :\n"<<exc.what());
192             }catch(...){
193               INFOS("Unknown exception was accured!!!");
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::runtime_error& exc){
205             INFOS("Follow exception was accured :\n"<<exc.what());
206           }catch(...){
207             INFOS("Unknown exception was accured!!!");
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::runtime_error& exc){
218             INFOS("Follow exception was accured :\n"<<exc.what());
219           }catch(...){
220             INFOS("Unknown exception was accured!!!");
221           }
222         }
223         //Import mesh on entity
224         try{
225           myInput->GetMeshOnEntity(aMeshName,anEntity);
226         }catch(std::runtime_error& exc){
227           INFOS("Follow exception was accured :\n"<<exc.what());
228         }catch(...){
229           INFOS("Unknown exception was accured!!!");
230         }
231       }
232     }
233     myIsDone = 1;
234   }catch(std::runtime_error& exc){
235     INFOS("Follow exception was accured :\n"<<exc.what());
236   }catch(...){
237     INFOS("Unknown exception was accured!!!");
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::runtime_error& exc){
380     INFOS("Follow exception was accured :\n"<<exc.what());
381     return NULL;
382   }catch(...){
383     INFOS("Unknown exception was accured!!!");
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(SALOMEDS_Tool::GetTmpDir());
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!!!");
409     return Build();
410   }catch(std::runtime_error& exc){
411     INFOS("Follow exception was accured :\n"<<exc.what());
412   }catch(...){
413     INFOS("Unknown exception was accured!!!");
414   }
415   return NULL;
416 }
417
418 VISU::Storable* VISU::Result_i::Create(SALOMEDS::SObject_ptr theMedSObject){
419   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
420   try{
421     myInput = CreateMEDConvertor(theMedSObject);
422     if(myInput == NULL) return NULL;
423     mySourceId = eComponent;
424     string aCompDataType = GetComponentDataType(theMedSObject);
425     myFileInfo.setFile(aCompDataType.c_str());
426     myName = ::GenerateName("aResult").latin1();
427     VISU::Storable* aStorable = Build(theMedSObject);
428     return aStorable;
429   }catch(std::runtime_error& exc){
430     INFOS("Follow exception was accured :\n"<<exc.what());
431   }catch(...){
432     INFOS("Unknown exception was accured!!!");
433   }
434   return NULL;
435 }
436
437 VISU::Storable* VISU::Result_i::Create(SALOME_MED::FIELD_ptr theField){
438   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
439   try{
440     myInput = CreateMEDFieldConvertor(theField);
441     if(myInput == NULL) return NULL;
442     mySourceId = eComponent;
443     string aCompDataType = "MED";
444     myFileInfo.setFile(aCompDataType.c_str());
445     myInitFileName = aCompDataType;
446     myName = ::GenerateName("aResult").latin1();
447     VISU::Storable* aStorable = Build();
448     return aStorable;
449   }catch(std::runtime_error& exc){
450     INFOS("Follow exception was accured :\n"<<exc.what());
451   }catch(...){
452     INFOS("Unknown exception was accured!!!");
453   }
454   return NULL;
455 }
456
457 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
458                                         const Storable::TRestoringMap& theMap, const string& thePrefix)
459 {
460   if(MYDEBUG)  MESSAGE("Result_i::Restore - "<<thePrefix);
461   try{
462     mySObject = SALOMEDS::SObject::_duplicate(theSObject);
463     myStudyDocument = mySObject->GetStudy();
464     mySComponent = mySObject->GetFatherComponent();
465     myName = VISU::Storable::FindValue(theMap,"myName").latin1();
466     myInitFileName = VISU::Storable::FindValue(theMap,"myInitFileName").latin1();
467     SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
468     if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
469       mySourceId = eRestoredComponent;
470       if(MYDEBUG)  MESSAGE("Result_i::GetInput - There is some reference.");
471       SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
472       CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
473       myFileInfo.setFile(aDataType.in());
474       if(MYDEBUG)  MESSAGE("Result_i::GetInput - aDataType = "<<aDataType);
475       Engines::Component_var aEngComp = Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
476       if (CORBA::is_nil(aEngComp)) 
477         throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
478       SALOMEDS::StudyBuilder_var  aStudyBuilder = myStudyDocument->NewBuilder();
479       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
480       aStudyBuilder->LoadWith(aCompRefSObj,aDriver);
481       if(strcmp(aDataType,"MED") == 0)
482         myInput = CreateMEDConvertor(aTargetRefSObj);
483       else
484         throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
485     }else{
486       myFileInfo.setFile(thePrefix.c_str());
487
488       string aStudyPrefix("");
489       if (IsMultifile()) aStudyPrefix = (SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
490       if(!myFileInfo.isFile()){
491         string aFileName = thePrefix + aStudyPrefix + "_" + myName;
492         myFileInfo.setFile(aFileName.c_str());
493       }
494       if(MYDEBUG)  
495         MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
496
497       if (HDFascii::isASCII(myFileInfo.filePath().latin1())) {
498         char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().latin1());
499         char* aHDFFileName = new char[strlen(aResultPath) + 19];
500         sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
501
502         if (IsMultifile()) { // set this file as new - temporary
503           static QString aCommand;
504           aCommand.sprintf("mv %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().latin1());
505           if(system(aCommand) == -1){
506             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
507             return NULL;
508           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
509           myFileInfo.setFile(QString(aResultPath)+QString(myFileInfo.baseName().latin1()));
510         } else { // change current temporary file to the new: with hdf-format
511           static QString aCommand;
512           aCommand.sprintf("mv %s %s\0",aHDFFileName, myFileInfo.filePath().latin1());
513           if(system(aCommand.latin1()) == -1) {
514             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
515             return NULL;
516           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
517           SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
518           SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList.in(), true);
519         }
520         mySourceId = eRestoredFile;
521         delete(aResultPath);
522         delete(aHDFFileName);
523       } else if (!IsMultifile()) 
524         mySourceId = eRestoredFile;
525       else 
526         mySourceId = eFile;
527       if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - mySourceId = "<<mySourceId);
528       myInput = CreateMedConvertor(myFileInfo.filePath().latin1());
529       QString aComment;
530       aComment.sprintf("myComment=%s;myType=%d;myFileName=%s;myInitFileName=%s",
531                        GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1(),
532                        myInitFileName.c_str()); // Restoring of Python dump 
533       SALOMEDS::GenericAttribute_var anAttr;
534       if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
535         throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
536       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
537       aCmnt->SetValue(aComment.latin1());
538     }
539     QString aIsBuild = QAD_CONFIG->getSetting("Visu:BuildResult");
540     if(aIsBuild.isEmpty()? 0 : aIsBuild.toInt()) 
541       BuildAll();
542     return this;
543   }catch(std::runtime_error& exc){
544     INFOS("Follow exception was accured :\n"<<exc.what());
545   }catch(...){
546     INFOS("Unknown exception was accured!!!");
547   }
548   return NULL;
549 }
550
551 VISU::Result_i::TInput* VISU::Result_i::GetInput() { 
552   return myInput;
553 }
554
555 void VISU::Result_i::ToStream(std::ostringstream& theStr){
556   if(MYDEBUG) MESSAGE(GetComment());
557   Storable::DataToStream(theStr,"myName",myName.c_str());
558   Storable::DataToStream(theStr,"myInitFileName",myInitFileName.c_str());
559 }
560
561 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
562                                         const string& thePrefix, const Storable::TRestoringMap& theMap)
563 {
564   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
565   VISU::Result_i* pResult = new VISU::Result_i(aStudy);
566   if(pResult == NULL) return NULL;
567   return pResult->Restore(theSObject,theMap,thePrefix);
568 }
569      
570 string VISU::Result_i::GetRefFatherEntry() { 
571   //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
572   return "";
573 }
574
575 string VISU::Result_i::GetEntry(){ 
576   CORBA::String_var anEntry = mySObject->GetID();
577   return string(anEntry);
578 }
579
580 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
581 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
582 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
583
584 VISU::Result_i::~Result_i() {
585   MESSAGE("Result_i::~Result_i() - this = "<<this);
586   if(GetSourceId() == eRestoredFile){ 
587     static QString aCommand;
588     aCommand.sprintf("rm %s",myFileInfo.filePath().latin1());
589     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
590     aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",myFileInfo.dirPath().latin1());
591     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
592   }
593   if(myInput) delete myInput;
594 }