]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_Result_i.cc
Salome HOME
SMH: 3.0.0 preparation - merged and adopted version (POLYWORK+HEAD)
[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 "SUIT_ResourceMgr.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, 
130                          const ESourceId& theSourceId,
131                          const ECreationId& theCreationId):
132   myStudyDocument(SALOMEDS::Study::_duplicate(theStudy)),
133   myCreationId(theCreationId),
134   mySourceId(theSourceId),
135   myInput(NULL),
136   myIsDone(0)
137 {
138 }
139
140
141 void VISU::Result_i::RemoveFromStudy(){
142   VISU::RemoveFromStudy(mySObject,false);
143 }
144
145
146 int 
147 VISU::Result_i::
148 IsPossible()
149 {
150   try{
151     float aSize = myInput->GetSize();
152     bool aResult = VISU_PipeLine::CheckAvailableMemory(aSize);
153     MESSAGE("Result_i::IsPossible - CheckAvailableMemory = "<<float(aSize)<<"; aResult = "<<float(aResult));
154     return aResult;
155   }catch(std::exception& exc){
156     INFOS("Follow exception was occured :\n"<<exc.what());
157   }catch(...){
158     INFOS("Unknown exception was occured!");
159   }
160   return 0;
161 }
162
163
164 CORBA::Boolean 
165 VISU::Result_i::
166 BuildAll()
167 {
168   if(MYDEBUG) MESSAGE("Result_i::Build - myIsDone = "<<myIsDone);
169   if(myIsDone) return 1;
170   if(!IsPossible()) return 0;
171   try{
172     const VISU::TMeshMap& aMeshMap = myInput->GetMeshMap();
173     VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
174     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
175       const string& aMeshName = aMeshMapIter->first;
176       const VISU::PMesh aMesh = aMeshMapIter->second;
177       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
178       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
179       //Import fields
180       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
181       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
182         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
183         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
184         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
185         VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
186         for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
187           const string& aFieldName = aFieldMapIter->first;
188           const VISU::PField aField = aFieldMapIter->second;
189           const VISU::TValField& aValField = aField->myValField;
190           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
191           for(; aValFieldIter != aValField.end(); aValFieldIter++){
192             int aTimeStamp = aValFieldIter->first;
193             try{
194               myInput->GetTimeStampOnMesh(aMeshName,anEntity,aFieldName,aTimeStamp);
195             }catch(std::exception& exc){
196               INFOS("Follow exception was occured :\n"<<exc.what());
197             }catch(...){
198               INFOS("Unknown exception was occured!!!");
199             }
200           }
201         }
202         //Importing groups
203         const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
204         VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
205         for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
206           const string& aGroupName = aGroupMapIter->first;
207           try{
208             myInput->GetMeshOnGroup(aMeshName,aGroupName);
209           }catch(std::exception& exc){
210             INFOS("Follow exception was occured :\n"<<exc.what());
211           }catch(...){
212             INFOS("Unknown exception was occured!!!");
213           }
214         }
215         //Import families
216         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
217         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
218         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
219           const string& aFamilyName = aFamilyMapIter->first;
220           try{
221             myInput->GetMeshOnEntity(aMeshName,anEntity,aFamilyName);
222           }catch(std::exception& exc){
223             INFOS("Follow exception was occured :\n"<<exc.what());
224           }catch(...){
225             INFOS("Unknown exception was occured!!!");
226           }
227         }
228         //Import mesh on entity
229         try{
230           myInput->GetMeshOnEntity(aMeshName,anEntity);
231         }catch(std::exception& exc){
232           INFOS("Follow exception was occured :\n"<<exc.what());
233         }catch(...){
234           INFOS("Unknown exception was occured!!!");
235         }
236       }
237     }
238     myIsDone = 1;
239   }catch(std::exception& exc){
240     INFOS("Follow exception was occured :\n"<<exc.what());
241   }catch(...){
242     INFOS("Unknown exception was occured!!!");
243   }
244   return myIsDone;
245 }
246
247
248 VISU::Storable* 
249 VISU::Result_i::
250 Build(SALOMEDS::SObject_ptr theSObject) 
251 {
252   SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
253   aStudyBuilder->NewCommand();  // There is a transaction
254   if(MYDEBUG) MESSAGE("Result_i::Build");
255   try{
256     const TMeshMap& aMeshMap = myInput->GetMeshMap();
257     if(aMeshMap.empty()) 
258       throw std::runtime_error("Build - There is no any mesh information in the file !!!");
259     mySComponent = FindOrCreateVisuComponent(myStudyDocument);
260     CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
261     string aRefFatherEntry = GetRefFatherEntry();
262     QString aComment;
263     aComment.sprintf("myComment=%s;myType=%d;myFileName=%s;myInitFileName=%s",
264                      GetComment(),
265                      VISU::TRESULT,
266                      myFileInfo.filePath().latin1(),
267                      myInitFileName.c_str()); // Restoring of Python dump 
268     string aResultEntry = 
269       CreateAttributes(myStudyDocument,
270                        aSComponentEntry,
271                        aRefFatherEntry.c_str(),
272                        anIOR,
273                        myName.c_str(),
274                        "",
275                        aComment.latin1(),
276                        true);
277     mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
278     if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
279     if(!CORBA::is_nil(theSObject)){
280       CORBA::String_var aString = theSObject->GetID();
281       CreateReference(myStudyDocument,aResultEntry,aString.in());
282     }
283     TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
284     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
285       const string& aMeshName = aMeshMapIter->first;
286       const VISU::PMesh aMesh = aMeshMapIter->second;
287       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
288       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
289       aComment.sprintf("myComment=MESH;myName=%s;myDim=%d",
290                        aMeshName.c_str(),aMesh->myDim);
291       string aMeshEntry = CreateAttributes(myStudyDocument,aResultEntry.c_str(),aRefFatherEntry.c_str(),
292                                            "",aMeshName.c_str(),"",aComment.latin1(),true);
293       if(aMeshOnEntityMap.empty()) continue;
294       aComment.sprintf("myComment=FAMILIES;myMeshName=%s",aMeshName.c_str());
295       string aSubMeshesEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
296                                                 "","Families","",aComment.latin1(),true);
297       //Import entities and according families
298       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
299       typedef std::map<std::string,std::string> TComment2EntryMap;
300       TComment2EntryMap aComment2EntryMap;
301       typedef std::map<VISU::TEntity,std::string> TEntity2EntryMap;
302       TEntity2EntryMap aEntity2EntryMap;
303       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
304         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
305         aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
306                          VISU::TENTITY,
307                          aMeshName.c_str(),
308                          anEntity);
309         string anEntityName;
310         switch(anEntity){
311         case VISU::NODE_ENTITY : anEntityName = "onNodes"; break;
312         case VISU::EDGE_ENTITY : anEntityName = "onEdges"; break;
313         case VISU::FACE_ENTITY : anEntityName = "onFaces"; break;
314         case VISU::CELL_ENTITY : anEntityName = "onCells"; break;
315         default:
316           throw std::runtime_error("Build >> Value of entity is incorrect!");
317         }
318         aEntity2EntryMap[anEntity] = CreateAttributes(myStudyDocument,aSubMeshesEntry.c_str(),aRefFatherEntry.c_str(),
319                                                    "",anEntityName.c_str(),"",aComment.latin1(),true);
320         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
321         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
322         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
323         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
324           const string& aFamilyName = aFamilyMapIter->first;
325           aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
326                            VISU::TFAMILY,
327                            aMeshName.c_str(),
328                            anEntity,
329                            aFamilyName.c_str());
330           aComment2EntryMap[aComment.latin1()] = 
331             CreateAttributes(myStudyDocument,
332                              aEntity2EntryMap[anEntity].c_str(),
333                              aRefFatherEntry.c_str(),
334                              "",
335                              aFamilyName.c_str(),
336                              "",
337                              aComment.latin1(),
338                              true);
339         }
340       }
341       //Importing groups
342       const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
343       if(aGroupMap.size() > 0){
344         aComment.sprintf("myComment=GROUPS;myMeshName=%s",
345                          aMeshName.c_str());
346         string aGroupsEntry = 
347           CreateAttributes(myStudyDocument,
348                            aMeshEntry.c_str(),
349                            aRefFatherEntry.c_str(),
350                            "",
351                            "Groups",
352                            "",
353                            aComment.latin1(),
354                            true);
355         VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
356         for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
357           const string& aGroupName = aGroupMapIter->first;
358           aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
359                            VISU::TGROUP,aMeshName.c_str(),aGroupName.c_str());
360           string aGroupEntry = CreateAttributes(myStudyDocument,aGroupsEntry.c_str(),aRefFatherEntry.c_str(),
361                                                 "",aGroupName.c_str(),"",aComment.latin1(),true);
362           const VISU::PGroup aGroup = aGroupMapIter->second;
363           const VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup->myFamilyAndEntitySet;
364           VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = aFamilyAndEntitySet.begin();
365           for(; aFamilyAndEntitySetIter != aFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
366             const VISU::TFamilyAndEntity& aFamilyAndEntity = *aFamilyAndEntitySetIter;
367             const string& aFamilyName = aFamilyAndEntity.first;
368             const VISU::TEntity& anEntity = aFamilyAndEntity.second;
369             aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
370                              VISU::TFAMILY,
371                              aMeshName.c_str(),
372                              anEntity,
373                              aFamilyName.c_str());
374             CreateReference(myStudyDocument,aGroupEntry,aComment2EntryMap[aComment.latin1()]);
375           }
376         }
377       }
378       //Import fields
379       string aFieldsEntry;
380       bool isFieldEntryCreated = 0;
381       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
382       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
383         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
384         const VISU::PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
385         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
386         VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
387         for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
388           if(!isFieldEntryCreated){
389             aComment.sprintf("myComment=FIELDS;myMeshName=%s",
390                              aMeshName.c_str());
391             aFieldsEntry = 
392               CreateAttributes(myStudyDocument,
393                                aMeshEntry.c_str(),
394                                aRefFatherEntry.c_str(),
395                                "",
396                                "Fields",
397                                "",
398                                aComment.latin1(),
399                                true);
400             isFieldEntryCreated = true;
401           }
402           const string& aFieldName = aFieldMapIter->first;
403           const VISU::PField aField = aFieldMapIter->second;
404           const VISU::TValField& aValField = aField->myValField;
405           QString aFieldNameWithUnit = ::GenerateFieldName(aFieldName,aField->myUnitNames[0]);
406           aComment.sprintf("myComment=FIELD;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
407                            VISU::TFIELD,aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField->myNbComp);
408           string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
409                                                 "",aFieldNameWithUnit.latin1(),"",aComment.latin1(),true);
410           CreateReference(myStudyDocument,aFieldEntry,aEntity2EntryMap[anEntity]);
411           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
412           for(; aValFieldIter != aValField.end(); aValFieldIter++){
413             int aTimeStamp = aValFieldIter->first;
414             const VISU::PValForTime aValForTime = aValFieldIter->second;
415             aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
416                              VISU::TTIMESTAMP,aMeshName.c_str(),anEntity,aFieldName.c_str(),aTimeStamp,aField->myNbComp);
417             string aTimeStampId = VISU_Convertor::GenerateName(aValForTime->myTime);
418             CreateAttributes(myStudyDocument,aFieldEntry.c_str(),aRefFatherEntry.c_str(),
419                              "",aTimeStampId.c_str(),"",aComment.latin1(),true);
420           }
421         }
422       }
423     }
424     QString aIsBuild = VISU::GetResourceMgr()->stringValue("Visu:BuildResult");
425     bool isBuildAll = aIsBuild.isEmpty()? 0 : aIsBuild.toInt();
426     if(isBuildAll) BuildAll();
427   }catch(std::exception& exc){
428     INFOS("Follow exception was occured :\n"<<exc.what());
429     return NULL;
430   }catch(...){
431     INFOS("Unknown exception was occured!!!");
432     return NULL;
433   }
434   aStudyBuilder->CommitCommand();
435   return this;
436 }
437
438
439 VISU::Storable* 
440 VISU::Result_i::
441 Create(const char* theFileName)
442 {
443   try{
444     myFileInfo.setFile(theFileName);
445     myInitFileName = myFileInfo.filePath().latin1();
446     myName = ::GenerateName(myFileInfo.fileName()).latin1();
447     if(mySourceId == eRestoredFile){
448       std::string aTmpDir(SALOMEDS_Tool::GetTmpDir());
449       static QString aCommand;
450       aCommand.sprintf("cp %s %s",myFileInfo.absFilePath().latin1(),aTmpDir.c_str());
451       if(system(aCommand) == -1){
452         MESSAGE("Create - Can't execute the command :"<<aCommand);
453         return NULL;
454       }
455       if(MYDEBUG) MESSAGE("Result_i::Create - aCommand = "<<aCommand);
456       myFileInfo.setFile(QString(aTmpDir.c_str()) + myFileInfo.fileName());
457     }
458     myInput = CreateConvertor(myFileInfo.absFilePath().latin1());
459     if(!myInput) 
460       throw std::runtime_error("Create - Cannot create a Convertor for this file!!!"); return Build();
461   }catch(std::exception& exc){
462     INFOS("Follow exception was occured :\n"<<exc.what());
463   }catch(...){
464     INFOS("Unknown exception was occured!!!");
465   }
466   return NULL;
467 }
468
469
470 VISU::Storable* 
471 VISU::Result_i::
472 Create(SALOMEDS::SObject_ptr theMedSObject)
473 {
474   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
475   try{
476     myInput = CreateMEDConvertor(theMedSObject);
477     if(myInput == NULL) 
478       return NULL;
479
480     string aCompDataType = GetComponentDataType(theMedSObject);
481     myFileInfo.setFile(aCompDataType.c_str());
482     myInitFileName = aCompDataType;
483
484     myName = ::GenerateName("aResult").latin1();
485
486     VISU::Storable* aStorable = Build(theMedSObject);
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* 
497 VISU::Result_i::
498 Create(SALOME_MED::FIELD_ptr theField)
499 {
500   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
501   try{
502     myInput = CreateMEDFieldConvertor(theField);
503     if(myInput == NULL) 
504       return NULL;
505
506     string aCompDataType = "MED";
507     myFileInfo.setFile(aCompDataType.c_str());
508     myInitFileName = aCompDataType;
509
510     myName = ::GenerateName("aResult").latin1();
511
512     CORBA::String_var anIOR = myStudyDocument->ConvertObjectToIOR(theField);
513     SALOMEDS::SObject_var aFieldSObject = myStudyDocument->FindObjectIOR(anIOR);
514
515     VISU::Storable* aStorable = Build(aFieldSObject);
516     return aStorable;
517   }catch(std::exception& exc){
518     INFOS("Follow exception was occured :\n"<<exc.what());
519   }catch(...){
520     INFOS("Unknown exception was occured!!!");
521   }
522   return NULL;
523 }
524
525
526 VISU::Storable* 
527 VISU::Result_i::
528 Restore(SALOMEDS::SObject_ptr theSObject, 
529         const Storable::TRestoringMap& theMap, 
530         const string& thePrefix)
531 {
532   if(MYDEBUG)  MESSAGE("Result_i::Restore - "<<thePrefix);
533   try{
534     mySObject = SALOMEDS::SObject::_duplicate(theSObject);
535     myStudyDocument = mySObject->GetStudy();
536     mySComponent = mySObject->GetFatherComponent();
537     myName = VISU::Storable::FindValue(theMap,"myName").latin1();
538     myInitFileName = VISU::Storable::FindValue(theMap,"myInitFileName").latin1();
539     SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
540     if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
541       if(MYDEBUG)  MESSAGE("Result_i::GetInput - There is some reference.");
542       SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
543       CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
544       myFileInfo.setFile(aDataType.in());
545       if(MYDEBUG)  MESSAGE("Result_i::GetInput - aDataType = "<<aDataType);
546       Engines::Component_var aEngComp = Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
547       if (CORBA::is_nil(aEngComp)) 
548         throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
549       SALOMEDS::StudyBuilder_var  aStudyBuilder = myStudyDocument->NewBuilder();
550       SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
551       aStudyBuilder->LoadWith(aCompRefSObj,aDriver);
552       if(strcmp(aDataType,"MED") == 0)
553         myInput = CreateMEDConvertor(aTargetRefSObj);
554       else
555         throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
556     }else{
557       myFileInfo.setFile(thePrefix.c_str());
558
559       string aStudyPrefix("");
560       if (IsMultifile()) aStudyPrefix = (SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
561       if(!myFileInfo.isFile()){
562         string aFileName = thePrefix + aStudyPrefix + "_" + myName;
563         myFileInfo.setFile(aFileName.c_str());
564       }
565       if(MYDEBUG)  
566         MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
567
568       if (HDFascii::isASCII(myFileInfo.filePath().latin1())) {
569         char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().latin1());
570         char* aHDFFileName = new char[strlen(aResultPath) + 19];
571         sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
572
573         if (IsMultifile()) { // set this file as new - temporary
574           static QString aCommand;
575           aCommand.sprintf("mv %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().latin1());
576           if(system(aCommand) == -1){
577             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
578             return NULL;
579           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
580           myFileInfo.setFile(QString(aResultPath)+QString(myFileInfo.baseName().latin1()));
581         } else { // change current temporary file to the new: with hdf-format
582           static QString aCommand;
583           aCommand.sprintf("mv %s %s\0",aHDFFileName, myFileInfo.filePath().latin1());
584           if(system(aCommand.latin1()) == -1) {
585             if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
586             return NULL;
587           } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
588           SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
589           SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList.in(), true);
590         }
591         mySourceId = eRestoredFile;
592         delete(aResultPath);
593         delete(aHDFFileName);
594       } else if (!IsMultifile()) 
595         mySourceId = eRestoredFile;
596       else 
597         mySourceId = eFile;
598       if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - mySourceId = "<<mySourceId);
599       myInput = CreateConvertor(myFileInfo.filePath().latin1());
600       QString aComment;
601       aComment.sprintf("myComment=%s;myType=%d;myFileName=%s;myInitFileName=%s",
602                        GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1(),
603                        myInitFileName.c_str()); // Restoring of Python dump 
604       SALOMEDS::GenericAttribute_var anAttr;
605       if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
606         throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
607       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
608       aCmnt->SetValue(aComment.latin1());
609     }
610     QString aIsBuild = VISU::GetResourceMgr()->stringValue("Visu:BuildResult");
611     if(aIsBuild.isEmpty()? 0 : aIsBuild.toInt()) 
612       BuildAll();
613     return this;
614   }catch(std::exception& exc){
615     INFOS("Follow exception was occured :\n"<<exc.what());
616   }catch(...){
617     INFOS("Unknown exception was occured!!!");
618   }
619   return NULL;
620 }
621
622 VISU::Result_i::TInput* VISU::Result_i::GetInput() { 
623   return myInput;
624 }
625
626 void VISU::Result_i::ToStream(std::ostringstream& theStr){
627   if(MYDEBUG) MESSAGE(GetComment());
628   Storable::DataToStream(theStr,"myName",myName.c_str());
629   Storable::DataToStream(theStr,"myInitFileName",myInitFileName.c_str());
630   Storable::DataToStream(theStr,"myCreationId",myCreationId);
631 }
632
633 VISU::Storable* 
634 VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
635                         const string& thePrefix, 
636                         const Storable::TRestoringMap& theMap)
637 {
638   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
639
640   ECreationId aCreationId = ECreationId(Storable::FindValue(theMap,"myCreationId").toInt());
641   ESourceId aSourceId = eRestoredFile;
642   if(aCreationId == eImportMed || aCreationId == eImportMedField)
643     aSourceId = eRestoredComponent;
644
645   VISU::Result_i* pResult = new VISU::Result_i(aStudy,aSourceId,aCreationId);
646   if(pResult == NULL) 
647     return NULL;
648
649   return pResult->Restore(theSObject,theMap,thePrefix);
650 }
651      
652 string VISU::Result_i::GetRefFatherEntry() { 
653   //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
654   return "";
655 }
656
657 string VISU::Result_i::GetEntry(){ 
658   CORBA::String_var anEntry = mySObject->GetID();
659   return string(anEntry);
660 }
661
662 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
663 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
664 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
665 std::string VISU::Result_i::GetEntry(const std::string& theComment) 
666
667   return FindEntryWithComment(myStudyDocument,GetEntry().c_str(),theComment.c_str());
668 }
669
670 VISU::Result_i::~Result_i() {
671   MESSAGE("Result_i::~Result_i() - this = "<<this);
672   if(mySourceId == eRestoredFile){ 
673     static QString aCommand;
674     aCommand.sprintf("rm %s",myFileInfo.filePath().latin1());
675     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
676     aCommand.sprintf("rmdir --ignore-fail-on-non-empty %s",myFileInfo.dirPath().latin1());
677     MESSAGE("Result_i::~Result_i - system("<<aCommand<<") = "<<system(aCommand));
678   }
679   if(myInput) delete myInput;
680 }