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