Salome HOME
NRI : Correction 1.1a version.
[modules/visu.git] / src / VISU_I / VISU_Result_i.cc
1 using namespace std;
2 // File:        VISU_Result_i.cc
3 // Created:     Fri Jan 10 13:56:05 2003
4 // Author:      Alexey PETROV
5 //              <apo@ivanox.nnov.matra-dtv.fr>
6
7 #include "VISU_Result_i.hh"
8 #include "VISU_Convertor_impl.hxx"
9 #include "VISU_CorbaMedConvertor.hxx"
10 #include "VISU_Extractor.hxx"
11 using namespace VISU;
12
13 #include "SALOMEDS_Tool.hxx"
14
15 #include <qstring.h>
16 #include <qfileinfo.h>
17
18 #include <fstream>      
19
20 #include <vtkUnstructuredGridReader.h>
21 #include <vtkUnstructuredGridWriter.h>
22
23 using namespace std;
24
25 #ifdef DEBUG
26 static int MYDEBUG = 1;
27 static int MYDEBUGWITHFILES = 0;
28 #else
29 static int MYDEBUG = 0;
30 static int MYDEBUGWITHFILES = 0;
31 #endif
32
33 VISU::Result_var VISU::FindResult(SALOMEDS::SObject_ptr theSObject){
34   SALOMEDS::SComponent_var aSComponent = theSObject->GetFatherComponent();
35   SALOMEDS::SObject_var aFather = theSObject->GetFather();
36   CORBA::String_var aComponentID(aSComponent->GetID());
37   CORBA::String_var aFatherID(aFather->GetID());
38   VISU::Result_var aResult;
39   while(strcmp(aComponentID,aFatherID) != 0){
40     CORBA::Object_var anObject = VISU::SObjectToObject(aFather);
41     if(!CORBA::is_nil(anObject)){
42       aResult = VISU::Result::_narrow(anObject);
43       if(!aResult->_is_nil()) return aResult;
44     }
45     aFather = aFather->GetFather();
46   }
47   return aResult;
48 }
49
50 QString VISU::GenerateName(const string& theFmt, int theId){
51   static QString aName;
52   if(theId > 0)
53     aName.sprintf("%s:%d",theFmt.c_str(),theId);
54   else
55     aName.sprintf("%s",theFmt.c_str());
56   return aName;
57 }
58
59 void VISU::WriteToFile(vtkUnstructuredGrid* theDataSet, const string& theFileName){
60   vtkUnstructuredGridWriter* aWriter = vtkUnstructuredGridWriter::New();
61   aWriter->SetFileName(theFileName.c_str());
62   aWriter->SetInput(theDataSet);
63   aWriter->Write();
64   aWriter->Delete();
65 }
66
67 QString GenerateName(const char* theName){
68   typedef map<string,int> TNameMap;
69   static TNameMap aMap;
70   TNameMap::const_iterator i = aMap.find(theName);
71   QString tmp;
72   if(i == aMap.end()) {
73     aMap[theName] = 0;
74     tmp = theName;
75   }else{
76     tmp = VISU::GenerateName(theName,++aMap[theName]);
77   }
78   if(MYDEBUG) MESSAGE("GenerateName - "<<tmp<<" from - "<<theName<<"; " <<aMap[theName]);
79   return tmp;
80 }
81
82 void CreateReference(SALOMEDS::Study_ptr theStudyDocument, 
83                      const string& theFatherEntry, const string& theRefEntry)
84 {
85   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
86   SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry.c_str());
87   SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(aFather);
88   SALOMEDS::SObject_var aRefSObj = theStudyDocument->FindObjectID(theRefEntry.c_str());
89   aStudyBuilder->Addreference(newObj,aRefSObj);
90 }
91
92 //==============================================================================
93
94 const string VISU::Result_i::myComment = "RESULT";
95 const char* VISU::Result_i::GetComment() const { return myComment.c_str();}
96
97 VISU::Result_i::Result_i(SALOMEDS::Study_ptr theStudy) {
98   myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
99 }
100
101 VISU::Storable* VISU::Result_i::Build() {
102   if(MYDEBUG) MESSAGE("Result_i::Build");
103   try{
104     mySComponent = FindOrCreateVisuComponent(myStudyDocument);
105     CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
106     string aRefFatherEntry = GetRefFatherEntry();
107     QString aComment;
108     aComment.sprintf("myComment=%s;myType=%d;myName=%s;myFileName=%s",
109                      GetComment(),VISU::TRESULT,myName.c_str(),myFileInfo.filePath().latin1());
110     string aResultEntry = CreateAttributes(myStudyDocument,aSComponentEntry,aRefFatherEntry.c_str(),
111                                            anIOR,myName.c_str(),"",aComment.latin1(),true);
112     mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
113     if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for hte Result !!!");
114     const TMeshMap& aMeshMap = *(myInput->GetMeshMap());
115     if( &aMeshMap != NULL) {//apo
116       TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
117       for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
118         const string& aMeshName = aMeshMapIter->first;
119         const VISU::TMesh& aMesh = aMeshMapIter->second;
120         const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
121         VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
122         aComment.sprintf("myComment=MESH;myResultName=%s;myName=%s;myDim=%d",
123                          myName.c_str(),aMeshName.c_str(),aMesh.myDim);
124         string aMeshEntry = CreateAttributes(myStudyDocument,aResultEntry.c_str(),aRefFatherEntry.c_str(),
125                                              "",aMeshName.c_str(),"",aComment.latin1(),true);
126         if(aMeshOnEntityMap.empty()) continue;
127         aComment.sprintf("myComment=FAMILIES;myResultName=%s;myMeshName=%s",myName.c_str(),aMeshName.c_str());
128         string aSubMeshesEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
129                                                   "","Families","",aComment.latin1(),true);
130         //Import entities and according families
131         aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
132         typedef map<VISU::TEntity,string> TEntity2Entry;
133         TEntity2Entry aEntity2Entry;
134         for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
135           const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
136           aComment.sprintf("myComment=ENTITY;myType=%d;myResultName=%s;myMeshName=%s;myId=%d",
137                            VISU::TENTITY,myName.c_str(),aMeshName.c_str(),anEntity);
138           string anEntityName;
139           switch(anEntity){
140           case VISU::NODE_ENTITY : anEntityName = "onNodes"; break;
141           case VISU::EDGE_ENTITY : anEntityName = "onEdges"; break;
142           case VISU::FACE_ENTITY : anEntityName = "onFaces"; break;
143           case VISU::CELL_ENTITY : anEntityName = "onCells"; break;
144           default:
145             throw std::runtime_error("Build >> Value of entity is incorrect!");
146           }
147           aEntity2Entry[anEntity] = CreateAttributes(myStudyDocument,aSubMeshesEntry.c_str(),aRefFatherEntry.c_str(),
148                                                      "",anEntityName.c_str(),"",aComment.latin1(),true);
149           const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
150           const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity.myFamilyMap;
151           VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
152           for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
153             const string& aFamilyName = aFamilyMapIter->first;
154             aComment.sprintf("myComment=FAMILY;myType=%d;myResultName=%s;myMeshName=%s;myEntityId=%d;myName=%s",
155                              VISU::TFAMILY,myName.c_str(),aMeshName.c_str(),anEntity,aFamilyName.c_str());
156             CreateAttributes(myStudyDocument,aEntity2Entry[anEntity].c_str(),aRefFatherEntry.c_str(),
157                              "",aFamilyName.c_str(),"",aComment.latin1(),true);
158           }
159         }
160         //Importing groups
161         const VISU::TGroupMap& aGroupMap = aMesh.myGroupMap;
162         if(aGroupMap.size() > 0){
163           aComment.sprintf("myComment=GROUPS;myResultName=%s;myMeshName=%s",myName.c_str(),aMeshName.c_str());
164           string aGroupsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
165                                                  "","Groups","",aComment.latin1(),true);
166           VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
167           for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
168             const string& aGroupName = aGroupMapIter->first;
169             aComment.sprintf("myComment=GROUP;myType=%d;myResultName=%s;myMeshName=%s;myName=%s",
170                              VISU::TGROUP,myName.c_str(),aMeshName.c_str(),aGroupName.c_str());
171             string aGroupEntry = CreateAttributes(myStudyDocument,aGroupsEntry.c_str(),aRefFatherEntry.c_str(),
172                                                   "",aGroupName.c_str(),"",aComment.latin1(),true);
173             const VISU::TGroup& aGroup = aGroupMapIter->second;
174             const VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup.myFamilyAndEntitySet;
175             VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = aFamilyAndEntitySet.begin();
176             for(; aFamilyAndEntitySetIter != aFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
177               const VISU::TFamilyAndEntity& aFamilyAndEntity = *aFamilyAndEntitySetIter;
178               const string& aFamilyName = aFamilyAndEntity.first;
179               const VISU::TEntity& anEntity = aFamilyAndEntity.second;
180               aComment.sprintf("myComment=FAMILY;myType=%d;myResultName=%s;myMeshName=%s;myEntityId=%d;myName=%s",
181                                VISU::TFAMILY,myName.c_str(),aMeshName.c_str(),anEntity,aFamilyName.c_str());
182               string anEntry = FindEntryWithComment(myStudyDocument,aEntity2Entry[anEntity].c_str(),aComment);
183               CreateReference(myStudyDocument,aGroupEntry,anEntry);
184             }
185           }
186         }
187         //Import fields
188         string aFieldsEntry;
189         bool isFieldEntryCreated = 0;
190         aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
191         for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
192           const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
193           const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
194           const VISU::TFieldMap& aFieldMap = aMeshOnEntity.myFieldMap;
195           VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
196           for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
197             if(!isFieldEntryCreated){
198               aComment.sprintf("myComment=FIELDS;myResultName=%s;myMeshName=%s",myName.c_str(),aMeshName.c_str());
199               aFieldsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
200                                               "","Fields","",aComment.latin1(),true);
201               isFieldEntryCreated = true;
202             }
203             const string& aFieldName = aFieldMapIter->first;
204             const VISU::TField& aField = aFieldMapIter->second;
205             const VISU::TField::TValField& aValField = aField.myValField;
206             aComment.sprintf("myComment=FIELD;myType=%d;myResultName=%s;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
207                              VISU::TFIELD,myName.c_str(),aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField.myNbComp);
208             string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
209                                                   "",aFieldName.c_str(),"",aComment.latin1(),true);
210             CreateReference(myStudyDocument,aFieldEntry,aEntity2Entry[anEntity]);
211             VISU::TField::TValField::const_iterator aValFieldIter = aValField.begin();
212             for(; aValFieldIter != aValField.end(); aValFieldIter++){
213               int aTimeStamp = aValFieldIter->first;
214               const VISU::TField::TValForTime& aValForTime = aValFieldIter->second;
215               aComment.sprintf("myComment=TIMESTAMP;myType=%d;myResultName=%s;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
216                                VISU::TTIMESTAMP,myName.c_str(),aMeshName.c_str(),anEntity,aFieldName.c_str(),aTimeStamp,aField.myNbComp);
217               string aTimeStampId = VISU_Convertor::GenerateName(aValForTime.myTime);
218               CreateAttributes(myStudyDocument,aFieldEntry.c_str(),aRefFatherEntry.c_str(),
219                                "",aTimeStampId.c_str(),"",aComment.latin1(),true);
220             }
221           }
222         }
223       }
224     }
225     return this;
226   }catch(std::runtime_error& exc){
227     MESSAGE("Follow exception was accured :\n"<<exc.what());
228   }catch(...){
229     MESSAGE("Unknown exception was accured!");
230   }
231   return NULL;
232 }
233
234 VISU::Storable* VISU::Result_i::Create(const char* theFileName){
235   myIsRestored = 0;
236   myInput = CreateConvertor(theFileName);
237   if(myInput == NULL) return NULL;
238   myFileInfo.setFile(theFileName);
239   myName = ::GenerateName(myFileInfo.fileName().latin1());
240   return Build();
241 }
242
243 VISU::Storable* VISU::Result_i::Create(SALOME_MED::MESH_ptr theMesh){
244   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from Mesh");
245   VISU_MedMeshConvertor* aInput = new VISU_MedMeshConvertor();
246   aInput->SetMedMesh(theMesh);
247   myInput = aInput;
248   //if(myInput->GetOutput() == NULL) return NULL;
249   TMeshMap::const_iterator aMeshMapIter = myInput->GetMeshMap()->begin();
250   myName = ::GenerateName((aMeshMapIter->first).c_str());
251   return Build();
252 }
253
254 VISU::Storable* VISU::Result_i::Create(SALOME_MED::SUPPORT_ptr theSupport){
255   if(MYDEBUG)  MESSAGE("Result_i::Create Mesh from MED::SUPPORT");
256   VISU_CorbaMedSupportConvertor* aInput = new VISU_CorbaMedSupportConvertor();
257   aInput->SetSupport(theSupport);
258   myInput = aInput;
259   //if(myInput->GetOutput() == NULL) return NULL;
260   TMeshMap::const_iterator aMeshMapIter = myInput->GetMeshMap()->begin();
261   myName = ::GenerateName((aMeshMapIter->first).c_str());
262   return Build();
263 }
264
265 VISU::Storable* VISU::Result_i::Create(SALOME_MED::FIELD_ptr theField){
266   if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from Field");
267   VISU_CorbaMedConvertor* aInput = new VISU_CorbaMedConvertor();
268   aInput->SetMedField(theField);
269   myInput = aInput;
270   //if(myInput->GetOutput() == NULL) return NULL;
271   TMeshMap::const_iterator aMeshMapIter = myInput->GetMeshMap()->begin();
272   myName = ::GenerateName((aMeshMapIter->first).c_str());
273   return Build();
274 }
275
276 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject, 
277                                         const string& thePrefix, const Storable::TRestoringMap& theMap)
278      throw(std::logic_error&)
279 {
280   if(MYDEBUG)  MESSAGE("Result_i::Restore - "<<thePrefix);
281   try{
282     myIsRestored = 1;
283     mySObject = SALOMEDS::SObject::_duplicate(theSObject);
284     myStudyDocument = mySObject->GetStudy();
285     mySComponent = mySObject->GetFatherComponent();
286     myName = VISU::Storable::FindValue(theMap,"myName");
287     string aFileName(thePrefix);
288     string aStudyPrefix = SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL());
289     aFileName += aStudyPrefix + "_" + myName;
290     if(MYDEBUG)  MESSAGE("Result_i::Restore - aFileName = "<<aFileName);
291     myFileInfo.setFile(aFileName.c_str());
292     myInput = CreateMedConvertor(aFileName);
293     QString aComment;
294     aComment.sprintf("myComment=%s;myType=%d;myName=%s;myFileName=%s",
295                      GetComment(),VISU::TRESULT,myName.c_str(),myFileInfo.filePath().latin1());
296     SALOMEDS::GenericAttribute_var anAttr;
297     if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
298        throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
299     SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
300     aCmnt->SetValue(aComment.latin1());
301     // remove temporary file, if it's not multifile mode
302     if (false || !IsMultifile()) {
303       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
304       aSeq->length(1);
305       aSeq[0]=(aStudyPrefix + myName).c_str();
306       SALOMEDS_Tool::RemoveTemporaryFiles(thePrefix.c_str(), aSeq.in(), true);
307     }
308     return this;
309   }catch(std::runtime_error& exc){
310     MESSAGE("Follow exception was accured :\n"<<exc.what());
311   }catch(...){
312     MESSAGE("Unknown exception was accured!");
313   }
314   return NULL;
315 }
316
317 void VISU::Result_i::ToStream(ostrstream& theStr){
318   if(MYDEBUG) MESSAGE(GetComment());
319   Storable::DataToStream(theStr,"myName",myName.c_str());
320 }
321
322 VISU::Storable* VISU::ResultRestore(SALOMEDS::SObject_ptr theSObject, 
323                                     const string& thePrefix, const Storable::TRestoringMap& theMap)
324      throw(std::logic_error&)
325 {
326   SALOMEDS::Study_var aStudy = theSObject->GetStudy();
327   VISU::Result_i* pResult = new VISU::Result_i(aStudy);
328   if(pResult == NULL) return NULL;
329   return pResult->Restore(theSObject,thePrefix,theMap);
330 }
331      
332 string VISU::Result_i::GetRefFatherEntry() { 
333   //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
334   return "";
335 }
336
337 string VISU::Result_i::GetEntry(){ 
338   CORBA::String_var anEntry = mySObject->GetID();
339   return string(anEntry);
340 }
341
342 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
343 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
344 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
345
346 VISU::Result_i::~Result_i() {
347   if(MYDEBUG) MESSAGE("Result_i::~Result_i()");
348   if(myInput) delete myInput;
349 }