1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : VISU_Result_i.cc
6 // Author : Alexey PETROV
10 #include "VISU_Result_i.hh"
11 #include "VISU_Convertor_impl.hxx"
12 #include "VISU_CorbaMedConvertor.hxx"
13 #include "VISU_Extractor.hxx"
16 #include "SALOMEDS_Tool.hxx"
17 #include "HDFascii.hxx"
20 #include <qfileinfo.h>
24 #include <vtkUnstructuredGridReader.h>
25 #include <vtkUnstructuredGridWriter.h>
30 static int MYDEBUG = 0;
31 static int MYDEBUGWITHFILES = 0;
33 static int MYDEBUG = 0;
34 static int MYDEBUGWITHFILES = 0;
37 VISU::Result_var VISU::FindResult(SALOMEDS::SObject_ptr theSObject){
38 SALOMEDS::SComponent_var aSComponent = theSObject->GetFatherComponent();
39 SALOMEDS::SObject_var aFather = theSObject->GetFather();
40 CORBA::String_var aComponentID(aSComponent->GetID());
41 CORBA::String_var aFatherID(aFather->GetID());
42 VISU::Result_var aResult;
43 while(strcmp(aComponentID,aFatherID) != 0){
44 CORBA::Object_var anObject = VISU::SObjectToObject(aFather);
45 if(!CORBA::is_nil(anObject)){
46 aResult = VISU::Result::_narrow(anObject);
47 if(!aResult->_is_nil()) return aResult;
49 aFather = aFather->GetFather();
54 QString VISU::GenerateName(const string& theFmt, int theId){
57 aName.sprintf("%s:%d",theFmt.c_str(),theId);
59 aName.sprintf("%s",theFmt.c_str());
63 void VISU::WriteToFile(vtkUnstructuredGrid* theDataSet, const string& theFileName){
64 vtkUnstructuredGridWriter* aWriter = vtkUnstructuredGridWriter::New();
65 aWriter->SetFileName(theFileName.c_str());
66 aWriter->SetInput(theDataSet);
71 QString GenerateName(const char* theName){
72 typedef map<string,int> TNameMap;
74 TNameMap::const_iterator i = aMap.find(theName);
80 tmp = VISU::GenerateName(theName,++aMap[theName]);
82 if(MYDEBUG) MESSAGE("GenerateName - "<<tmp<<" from - "<<theName<<"; " <<aMap[theName]);
86 QString GenerateFieldName(const string& theName, const string& theUnits){
88 const string tmp(theUnits.size(),' ');
89 if(theUnits == "" || theUnits == tmp)
90 aName.sprintf("%s, -",theName.c_str());
92 aName.sprintf("%s, %s",theName.c_str(),theUnits.c_str());
93 aName = aName.simplifyWhiteSpace();
94 return aName.latin1();
97 void CreateReference(SALOMEDS::Study_ptr theStudyDocument,
98 const string& theFatherEntry, const string& theRefEntry)
100 SALOMEDS::StudyBuilder_var aStudyBuilder = theStudyDocument->NewBuilder();
101 SALOMEDS::SObject_var aFather = theStudyDocument->FindObjectID(theFatherEntry.c_str());
102 SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(aFather);
103 SALOMEDS::SObject_var aRefSObj = theStudyDocument->FindObjectID(theRefEntry.c_str());
104 aStudyBuilder->Addreference(newObj,aRefSObj);
107 //==============================================================================
109 const string VISU::Result_i::myComment = "RESULT";
110 const char* VISU::Result_i::GetComment() const { return myComment.c_str();}
112 VISU::Result_i::Result_i(SALOMEDS::Study_ptr theStudy) {
113 myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
116 VISU::Storable* VISU::Result_i::Build() {
117 if(MYDEBUG) MESSAGE("Result_i::Build");
118 const TMeshMap& aMeshMap = myInput->GetMeshMap();
119 if(!aMeshMap.empty()) {//apo
120 mySComponent = FindOrCreateVisuComponent(myStudyDocument);
121 CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
122 string aRefFatherEntry = GetRefFatherEntry();
124 aComment.sprintf("myComment=%s;myType=%d;myFileName=%s",
125 GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1());
126 string aResultEntry = CreateAttributes(myStudyDocument,aSComponentEntry,aRefFatherEntry.c_str(),
127 anIOR,myName.c_str(),"",aComment.latin1(),true);
128 mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
129 if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
130 if(theSObject != NULL){
131 CORBA::String_var aString = theSObject->GetID();
132 CreateReference(myStudyDocument,aResultEntry,aString.in());
134 TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
135 for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
136 const string& aMeshName = aMeshMapIter->first;
137 const VISU::TMesh& aMesh = aMeshMapIter->second;
138 const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
139 VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
140 aComment.sprintf("myComment=MESH;myName=%s;myDim=%d",
141 aMeshName.c_str(),aMesh.myDim);
142 string aMeshEntry = CreateAttributes(myStudyDocument,aResultEntry.c_str(),aRefFatherEntry.c_str(),
143 "",aMeshName.c_str(),"",aComment.latin1(),true);
144 if(aMeshOnEntityMap.empty()) continue;
145 aComment.sprintf("myComment=FAMILIES;myMeshName=%s",aMeshName.c_str());
146 string aSubMeshesEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
147 "","Families","",aComment.latin1(),true);
148 //Import entities and according families
149 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
150 typedef map<VISU::TEntity,string> TEntity2Entry;
151 TEntity2Entry aEntity2Entry;
152 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
153 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
154 aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
155 VISU::TENTITY,aMeshName.c_str(),anEntity);
158 case VISU::NODE_ENTITY : anEntityName = "onNodes"; break;
159 case VISU::EDGE_ENTITY : anEntityName = "onEdges"; break;
160 case VISU::FACE_ENTITY : anEntityName = "onFaces"; break;
161 case VISU::CELL_ENTITY : anEntityName = "onCells"; break;
163 throw std::runtime_error("Build >> Value of entity is incorrect!");
165 aEntity2Entry[anEntity] = CreateAttributes(myStudyDocument,aSubMeshesEntry.c_str(),aRefFatherEntry.c_str(),
166 "",anEntityName.c_str(),"",aComment.latin1(),true);
167 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
168 const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity.myFamilyMap;
169 VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
170 for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
171 const string& aFamilyName = aFamilyMapIter->first;
172 aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
173 VISU::TFAMILY,aMeshName.c_str(),anEntity,aFamilyName.c_str());
174 CreateAttributes(myStudyDocument,aEntity2Entry[anEntity].c_str(),aRefFatherEntry.c_str(),
175 "",aFamilyName.c_str(),"",aComment.latin1(),true);
178 const VISU::TGroupMap& aGroupMap = aMesh.myGroupMap;
179 if(aGroupMap.size() > 0){
180 aComment.sprintf("myComment=GROUPS;myResultName=%s;myMeshName=%s",myName.c_str(),aMeshName.c_str());
181 string aGroupsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
182 "","Groups","",aComment.latin1(),true);
183 VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
184 for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
185 const string& aGroupName = aGroupMapIter->first;
186 aComment.sprintf("myComment=GROUP;myType=%d;myResultName=%s;myMeshName=%s;myName=%s",
187 VISU::TGROUP,myName.c_str(),aMeshName.c_str(),aGroupName.c_str());
188 string aGroupEntry = CreateAttributes(myStudyDocument,aGroupsEntry.c_str(),aRefFatherEntry.c_str(),
189 "",aGroupName.c_str(),"",aComment.latin1(),true);
190 const VISU::TGroup& aGroup = aGroupMapIter->second;
191 const VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup.myFamilyAndEntitySet;
192 VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = aFamilyAndEntitySet.begin();
193 for(; aFamilyAndEntitySetIter != aFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
194 const VISU::TFamilyAndEntity& aFamilyAndEntity = *aFamilyAndEntitySetIter;
195 const string& aFamilyName = aFamilyAndEntity.first;
196 const VISU::TEntity& anEntity = aFamilyAndEntity.second;
197 aComment.sprintf("myComment=FAMILY;myType=%d;myResultName=%s;myMeshName=%s;myEntityId=%d;myName=%s",
198 VISU::TFAMILY,myName.c_str(),aMeshName.c_str(),anEntity,aFamilyName.c_str());
199 string anEntry = FindEntryWithComment(myStudyDocument,aEntity2Entry[anEntity].c_str(),aComment);
200 CreateReference(myStudyDocument,aGroupEntry,anEntry);
207 bool isFieldEntryCreated = 0;
208 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
209 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
210 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
211 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
212 const VISU::TFieldMap& aFieldMap = aMeshOnEntity.myFieldMap;
213 VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
214 for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
215 if(!isFieldEntryCreated){
216 aComment.sprintf("myComment=FIELDS;myMeshName=%s",aMeshName.c_str());
217 aFieldsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
218 "","Fields","",aComment.latin1(),true);
219 isFieldEntryCreated = true;
221 const string& aFieldName = aFieldMapIter->first;
222 const VISU::TField& aField = aFieldMapIter->second;
223 const VISU::TField::TValField& aValField = aField.myValField;
224 QString aFieldNameWithUnit = ::GenerateFieldName(aFieldName,aField.myUnitNames[0]);
225 aComment.sprintf("myComment=FIELD;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
226 VISU::TFIELD,aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField.myNbComp);
227 string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
228 "",aFieldNameWithUnit.latin1(),"",aComment.latin1(),true);
229 CreateReference(myStudyDocument,aFieldEntry,aEntity2Entry[anEntity]);
230 VISU::TField::TValField::const_iterator aValFieldIter = aValField.begin();
231 for(; aValFieldIter != aValField.end(); aValFieldIter++){
232 int aTimeStamp = aValFieldIter->first;
233 const VISU::TField::TValForTime& aValForTime = aValFieldIter->second;
234 aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
235 VISU::TTIMESTAMP,aMeshName.c_str(),anEntity,aFieldName.c_str(),aTimeStamp,aField.myNbComp);
236 string aTimeStampId = VISU_Convertor::GenerateName(aValForTime.myTime);
237 CreateAttributes(myStudyDocument,aFieldEntry.c_str(),aRefFatherEntry.c_str(),
238 "",aTimeStampId.c_str(),"",aComment.latin1(),true);
247 VISU::Storable* VISU::Result_i::Create(const char* theFileName){
250 myInput = CreateConvertor(theFileName);
251 if(myInput == NULL) return NULL;
252 myFileInfo.setFile(theFileName);
253 myName = (const char*)(::GenerateName(myFileInfo.fileName().latin1()));
254 VISU::Storable* aStorable = Build();
256 }catch(std::runtime_error& exc){
257 MESSAGE("Follow exception was accured :\n"<<exc.what());
259 MESSAGE("Unknown exception was accured!");
264 VISU::Storable* VISU::Result_i::Create(SALOMEDS::SObject_ptr theMedSObject){
265 if(MYDEBUG) MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
267 mySourceId = eComponent;
268 myInput = CreateMEDConvertor(theMedSObject);
269 if(myInput == NULL) return NULL;
270 string aCompDataType = GetComponentDataType(theMedSObject);
271 myFileInfo.setFile(aCompDataType.c_str());
272 myName = (const char*)(::GenerateName("aResult"));
273 VISU::Storable* aStorable = Build(theMedSObject);
275 }catch(std::runtime_error& exc){
276 MESSAGE("Follow exception was accured :\n"<<exc.what());
278 MESSAGE("Unknown exception was accured!");
283 VISU::Storable* VISU::Result_i::Create(SALOME_MED::FIELD_ptr theField){
284 if(MYDEBUG) MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
286 mySourceId = eComponent;
287 myInput = CreateMEDFieldConvertor(theField);
288 if(myInput == NULL) return NULL;
289 string aCompDataType = "Med";
290 myFileInfo.setFile(aCompDataType.c_str());
291 myName = (const char*)(::GenerateName("aResult"));
292 VISU::Storable* aStorable = Build();
294 }catch(std::runtime_error& exc){
295 MESSAGE("Follow exception was accured :\n"<<exc.what());
297 MESSAGE("Unknown exception was accured!");
302 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject,
303 const string& thePrefix, const Storable::TRestoringMap& theMap)
304 throw(std::logic_error&)
306 if(MYDEBUG) MESSAGE("Result_i::Restore - "<<thePrefix);
309 mySObject = SALOMEDS::SObject::_duplicate(theSObject);
310 myStudyDocument = mySObject->GetStudy();
311 mySComponent = mySObject->GetFatherComponent();
312 myName = (const char*)(VISU::Storable::FindValue(theMap,"myName"));
313 SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
314 if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
315 mySourceId = eRestoredComponent;
316 if(MYDEBUG) MESSAGE("Result_i::GetInput - There is some reference.");
317 SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
318 CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
319 myFileInfo.setFile(aDataType.in());
320 if(MYDEBUG) MESSAGE("Result_i::GetInput - aDataType = "<<aDataType);
321 Engines::Component_var aEngComp = Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
322 if (CORBA::is_nil(aEngComp))
323 throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
324 SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
325 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
326 aStudyBuilder->LoadWith(aCompRefSObj,aDriver);
327 if(strcmp(aDataType,"Med") == 0){
328 myInput = CreateMEDConvertor(aTargetRefSObj);
330 throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
332 myFileInfo.setFile(thePrefix.c_str());
334 string aStudyPrefix(SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
335 if(!myFileInfo.isFile()){
336 string aFileName = thePrefix + aStudyPrefix + "_" + myName;
337 myFileInfo.setFile(aFileName.c_str());
340 MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
342 if (HDFascii::isASCII(myFileInfo.filePath().latin1())) {
343 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().latin1());
344 char* aHDFFileName = new char[strlen(aResultPath) + 19];
345 sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
347 if (IsMultifile()) { // set this file as new - temporary
348 static QString aCommand;
349 aCommand.sprintf("mv %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().latin1());
350 if(system(aCommand) == -1){
351 if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
353 } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
354 myFileInfo.setFile(QString(aResultPath)+QString(myFileInfo.baseName().latin1()));
355 } else { // change current temporary file to the new: with hdf-format
356 static QString aCommand;
357 aCommand.sprintf("mv %s %s\0",aHDFFileName, myFileInfo.filePath().latin1());
358 if(system(aCommand.latin1()) == -1) {
359 if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
361 } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
362 SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
363 SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList, true);
365 mySourceId = eRestoredFile;
367 delete(aHDFFileName);
368 } else if (!IsMultifile()) mySourceId = eRestoredFile;
369 else mySourceId = eFile;
371 myInput = CreateMedConvertor(myFileInfo.filePath().latin1());
373 aComment.sprintf("myComment=%s;myType=%d;myName=%s;myFileName=%s",
374 GetComment(),VISU::TRESULT,myName.c_str(),myFileInfo.filePath().latin1());
375 SALOMEDS::GenericAttribute_var anAttr;
376 if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
377 throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
378 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
379 aCmnt->SetValue(aComment.latin1());
382 }catch(std::runtime_error& exc){
383 MESSAGE("Follow exception was accured :\n"<<exc.what());
385 MESSAGE("Unknown exception was accured!");
390 VISU::Result_i::InputType* VISU::Result_i::GetInput() {
394 void VISU::Result_i::ToStream(std::ostringstream& theStr){
395 if(MYDEBUG) MESSAGE(GetComment());
396 Storable::DataToStream(theStr,"myName",myName.c_str());
399 VISU::Storable* VISU::ResultRestore(SALOMEDS::SObject_ptr theSObject,
400 const string& thePrefix, const Storable::TRestoringMap& theMap)
401 throw(std::logic_error&)
403 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
404 VISU::Result_i* pResult = new VISU::Result_i(aStudy);
405 if(pResult == NULL) return NULL;
406 return pResult->Restore(theSObject,thePrefix,theMap);
409 string VISU::Result_i::GetRefFatherEntry() {
410 //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
414 string VISU::Result_i::GetEntry(){
415 CORBA::String_var anEntry = mySObject->GetID();
416 return string(anEntry);
419 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
420 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
421 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
423 VISU::Result_i::~Result_i() {
424 if(MYDEBUG) MESSAGE("Result_i::~Result_i()");
425 if(myInput) delete myInput;