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>
7 #include "VISU_Result_i.hh"
8 #include "VISU_Convertor_impl.hxx"
9 #include "VISU_CorbaMedConvertor.hxx"
10 #include "VISU_Extractor.hxx"
13 #include "SALOMEDS_Tool.hxx"
16 #include <qfileinfo.h>
20 #include <vtkUnstructuredGridReader.h>
21 #include <vtkUnstructuredGridWriter.h>
26 static int MYDEBUG = 1;
27 static int MYDEBUGWITHFILES = 0;
29 static int MYDEBUG = 0;
30 static int MYDEBUGWITHFILES = 0;
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;
45 aFather = aFather->GetFather();
50 QString VISU::GenerateName(const string& theFmt, int theId){
53 aName.sprintf("%s:%d",theFmt.c_str(),theId);
55 aName.sprintf("%s",theFmt.c_str());
59 void VISU::WriteToFile(vtkUnstructuredGrid* theDataSet, const string& theFileName){
60 vtkUnstructuredGridWriter* aWriter = vtkUnstructuredGridWriter::New();
61 aWriter->SetFileName(theFileName.c_str());
62 aWriter->SetInput(theDataSet);
67 QString GenerateName(const char* theName){
68 typedef map<string,int> TNameMap;
70 TNameMap::const_iterator i = aMap.find(theName);
76 tmp = VISU::GenerateName(theName,++aMap[theName]);
78 if(MYDEBUG) MESSAGE("GenerateName - "<<tmp<<" from - "<<theName<<"; " <<aMap[theName]);
82 void CreateReference(SALOMEDS::Study_ptr theStudyDocument,
83 const string& theFatherEntry, const string& theRefEntry)
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);
92 string GetComponentDataType(SALOMEDS::SObject_ptr theSObject){
93 SALOMEDS::SComponent_var aCompRefSObj = theSObject->GetFatherComponent();
94 CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
95 return aDataType.in();
98 //==============================================================================
100 const string VISU::Result_i::myComment = "RESULT";
101 const char* VISU::Result_i::GetComment() const { return myComment.c_str();}
103 VISU::Result_i::Result_i(SALOMEDS::Study_ptr theStudy) {
104 myStudyDocument = SALOMEDS::Study::_duplicate(theStudy);
108 VISU::Storable* VISU::Result_i::Build(SALOMEDS::SObject_ptr theSObject)
109 throw (std::runtime_error&)
111 if(MYDEBUG) MESSAGE("Result_i::Build");
112 mySComponent = FindOrCreateVisuComponent(myStudyDocument);
113 CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
114 string aRefFatherEntry = GetRefFatherEntry();
116 aComment.sprintf("myComment=%s;myType=%d;myFileName=%s",
117 GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1());
118 string aResultEntry = CreateAttributes(myStudyDocument,aSComponentEntry,aRefFatherEntry.c_str(),
119 anIOR,myName.c_str(),"",aComment.latin1(),true);
120 mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
121 if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
122 if(theSObject != NULL){
123 CORBA::String_var aString = theSObject->GetID();
124 CreateReference(myStudyDocument,aResultEntry,aString.in());
126 const TMeshMap& aMeshMap = myInput->GetMeshMap();
127 if(!aMeshMap.empty()) {//apo
128 TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
129 for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
130 const string& aMeshName = aMeshMapIter->first;
131 const VISU::TMesh& aMesh = aMeshMapIter->second;
132 const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh.myMeshOnEntityMap;
133 VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
134 aComment.sprintf("myComment=MESH;myName=%s;myDim=%d",
135 aMeshName.c_str(),aMesh.myDim);
136 string aMeshEntry = CreateAttributes(myStudyDocument,aResultEntry.c_str(),aRefFatherEntry.c_str(),
137 "",aMeshName.c_str(),"",aComment.latin1(),true);
138 if(aMeshOnEntityMap.empty()) continue;
139 aComment.sprintf("myComment=FAMILIES;myMeshName=%s",aMeshName.c_str());
140 string aSubMeshesEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
141 "","Families","",aComment.latin1(),true);
142 //Import entities and according families
143 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
144 typedef map<VISU::TEntity,string> TEntity2Entry;
145 TEntity2Entry aEntity2Entry;
146 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
147 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
148 aComment.sprintf("myComment=ENTITY;myType=%d;myMeshName=%s;myId=%d",
149 VISU::TENTITY,aMeshName.c_str(),anEntity);
152 case VISU::NODE_ENTITY : anEntityName = "onNodes"; break;
153 case VISU::EDGE_ENTITY : anEntityName = "onEdges"; break;
154 case VISU::FACE_ENTITY : anEntityName = "onFaces"; break;
155 case VISU::CELL_ENTITY : anEntityName = "onCells"; break;
157 throw std::runtime_error("Build >> Value of entity is incorrect!");
159 aEntity2Entry[anEntity] = CreateAttributes(myStudyDocument,aSubMeshesEntry.c_str(),aRefFatherEntry.c_str(),
160 "",anEntityName.c_str(),"",aComment.latin1(),true);
161 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
162 const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity.myFamilyMap;
163 VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
164 for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
165 const string& aFamilyName = aFamilyMapIter->first;
166 aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
167 VISU::TFAMILY,aMeshName.c_str(),anEntity,aFamilyName.c_str());
168 CreateAttributes(myStudyDocument,aEntity2Entry[anEntity].c_str(),aRefFatherEntry.c_str(),
169 "",aFamilyName.c_str(),"",aComment.latin1(),true);
173 const VISU::TGroupMap& aGroupMap = aMesh.myGroupMap;
174 if(aGroupMap.size() > 0){
175 aComment.sprintf("myComment=GROUPS;myMeshName=%s",aMeshName.c_str());
176 string aGroupsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
177 "","Groups","",aComment.latin1(),true);
178 VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
179 for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
180 const string& aGroupName = aGroupMapIter->first;
181 aComment.sprintf("myComment=GROUP;myType=%d;myMeshName=%s;myName=%s",
182 VISU::TGROUP,aMeshName.c_str(),aGroupName.c_str());
183 string aGroupEntry = CreateAttributes(myStudyDocument,aGroupsEntry.c_str(),aRefFatherEntry.c_str(),
184 "",aGroupName.c_str(),"",aComment.latin1(),true);
185 const VISU::TGroup& aGroup = aGroupMapIter->second;
186 const VISU::TFamilyAndEntitySet& aFamilyAndEntitySet = aGroup.myFamilyAndEntitySet;
187 VISU::TFamilyAndEntitySet::const_iterator aFamilyAndEntitySetIter = aFamilyAndEntitySet.begin();
188 for(; aFamilyAndEntitySetIter != aFamilyAndEntitySet.end(); aFamilyAndEntitySetIter++){
189 const VISU::TFamilyAndEntity& aFamilyAndEntity = *aFamilyAndEntitySetIter;
190 const string& aFamilyName = aFamilyAndEntity.first;
191 const VISU::TEntity& anEntity = aFamilyAndEntity.second;
192 aComment.sprintf("myComment=FAMILY;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s",
193 VISU::TFAMILY,aMeshName.c_str(),anEntity,aFamilyName.c_str());
194 string anEntry = FindEntryWithComment(myStudyDocument,aEntity2Entry[anEntity].c_str(),aComment);
195 CreateReference(myStudyDocument,aGroupEntry,anEntry);
201 bool isFieldEntryCreated = 0;
202 aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
203 for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
204 const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
205 const VISU::TMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
206 const VISU::TFieldMap& aFieldMap = aMeshOnEntity.myFieldMap;
207 VISU::TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
208 for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++){
209 if(!isFieldEntryCreated){
210 aComment.sprintf("myComment=FIELDS;myMeshName=%s",aMeshName.c_str());
211 aFieldsEntry = CreateAttributes(myStudyDocument,aMeshEntry.c_str(),aRefFatherEntry.c_str(),
212 "","Fields","",aComment.latin1(),true);
213 isFieldEntryCreated = true;
215 const string& aFieldName = aFieldMapIter->first;
216 const VISU::TField& aField = aFieldMapIter->second;
217 const VISU::TField::TValField& aValField = aField.myValField;
218 aComment.sprintf("myComment=FIELD;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
219 VISU::TFIELD,aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField.myNbComp);
220 string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
221 "",aFieldName.c_str(),"",aComment.latin1(),true);
222 CreateReference(myStudyDocument,aFieldEntry,aEntity2Entry[anEntity]);
223 VISU::TField::TValField::const_iterator aValFieldIter = aValField.begin();
224 for(; aValFieldIter != aValField.end(); aValFieldIter++){
225 int aTimeStamp = aValFieldIter->first;
226 const VISU::TField::TValForTime& aValForTime = aValFieldIter->second;
227 aComment.sprintf("myComment=TIMESTAMP;myType=%d;myMeshName=%s;myEntityId=%d;myFieldName=%s;myTimeStampId=%d;myNumComponent=%d",
228 VISU::TTIMESTAMP,aMeshName.c_str(),anEntity,aFieldName.c_str(),aTimeStamp,aField.myNbComp);
229 string aTimeStampId = VISU_Convertor::GenerateName(aValForTime.myTime);
230 CreateAttributes(myStudyDocument,aFieldEntry.c_str(),aRefFatherEntry.c_str(),
231 "",aTimeStampId.c_str(),"",aComment.latin1(),true);
240 VISU::Storable* VISU::Result_i::Create(const char* theFileName){
243 myInput = CreateConvertor(theFileName);
244 if(myInput == NULL) return NULL;
245 myFileInfo.setFile(theFileName);
246 myName = ::GenerateName(myFileInfo.fileName().latin1());
248 }catch(std::runtime_error& exc){
249 MESSAGE("Follow exception was accured :\n"<<exc.what());
251 MESSAGE("Unknown exception was accured!");
256 VISU::Storable* VISU::Result_i::Create(SALOMEDS::SObject_ptr theMedSObject){
257 if(MYDEBUG) MESSAGE("Result_i::Create MedObject from theMedSObject");
259 mySourceId = eComponent;
260 myInput = CreateMEDConvertor(theMedSObject);
261 if(myInput == NULL) return NULL;
262 string aCompDataType = GetComponentDataType(theMedSObject);
263 myFileInfo.setFile(aCompDataType.c_str());
264 myName = ::GenerateName("aResult");
265 return Build(theMedSObject);
266 }catch(std::runtime_error& exc){
267 MESSAGE("Follow exception was accured :\n"<<exc.what());
269 MESSAGE("Unknown exception was accured!");
274 VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject,
275 const string& thePrefix, const Storable::TRestoringMap& theMap)
276 throw(std::logic_error&)
278 if(MYDEBUG) MESSAGE("Result_i::Restore - "<<thePrefix);
280 mySObject = SALOMEDS::SObject::_duplicate(theSObject);
281 myStudyDocument = mySObject->GetStudy();
282 mySComponent = mySObject->GetFatherComponent();
283 myName = VISU::Storable::FindValue(theMap,"myName");
284 SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
285 if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
286 mySourceId = eRestoredComponent;
287 if(MYDEBUG) MESSAGE("Result_i::GetInput - There is some reference.");
288 SALOMEDS::SComponent_var aCompRefSObj = aTargetRefSObj->GetFatherComponent();
289 CORBA::String_var aDataType = aCompRefSObj->ComponentDataType();
290 myFileInfo.setFile(aDataType.in());
291 if(MYDEBUG) MESSAGE("Result_i::GetInput - aDataType = "<<aDataType);
292 Engines::Component_var aEngComp = Base_i::myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", aDataType.in());
293 if (CORBA::is_nil(aEngComp))
294 throw std::runtime_error("Restore - There is no aEngComp for the aDataType !!!");
295 SALOMEDS::StudyBuilder_var aStudyBuilder = myStudyDocument->NewBuilder();
296 SALOMEDS::Driver_var aDriver = SALOMEDS::Driver::_narrow(aEngComp);
297 aStudyBuilder->LoadWith(aCompRefSObj,aDriver);
298 if(strcmp(aDataType,"Med") == 0){
299 myInput = CreateMEDConvertor(aTargetRefSObj);
301 throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
303 mySourceId = eRestoredFile;
304 myFileInfo.setFile(thePrefix.c_str());
305 string aStudyPrefix(SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
306 string aTmpDir(SALOMEDS_Tool::GetDirFromPath(myStudyDocument->URL()));
307 if(!myFileInfo.isFile()){
308 string aFileName = thePrefix + aStudyPrefix + "_" + myName;
309 myFileInfo.setFile(aFileName.c_str());
312 MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
313 myInput = CreateMedConvertor(myFileInfo.filePath().latin1());
315 aComment.sprintf("myComment=%s;myType=%d;myName=%s;myFileName=%s",
316 GetComment(),VISU::TRESULT,myName.c_str(),myFileInfo.filePath().latin1());
317 SALOMEDS::GenericAttribute_var anAttr;
318 if(!theSObject->FindAttribute(anAttr, "AttributeComment"))
319 throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
320 SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
321 aCmnt->SetValue(aComment.latin1());
322 // remove temporary file, if it's not multifile mode
323 if (false || !IsMultifile()) {
324 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
326 aSeq[0]=(aStudyPrefix + myName).c_str();
327 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
331 }catch(std::runtime_error& exc){
332 MESSAGE("Follow exception was accured :\n"<<exc.what());
334 MESSAGE("Unknown exception was accured!");
339 VISU::Result_i::InputType* VISU::Result_i::GetInput() {
343 void VISU::Result_i::ToStream(ostrstream& theStr){
344 if(MYDEBUG) MESSAGE(GetComment());
345 Storable::DataToStream(theStr,"myName",myName.c_str());
348 VISU::Storable* VISU::ResultRestore(SALOMEDS::SObject_ptr theSObject,
349 const string& thePrefix, const Storable::TRestoringMap& theMap)
350 throw(std::logic_error&)
352 SALOMEDS::Study_var aStudy = theSObject->GetStudy();
353 VISU::Result_i* pResult = new VISU::Result_i(aStudy);
354 if(pResult == NULL) return NULL;
355 return pResult->Restore(theSObject,thePrefix,theMap);
358 string VISU::Result_i::GetRefFatherEntry() {
359 //return QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry();
363 string VISU::Result_i::GetEntry(){
364 CORBA::String_var anEntry = mySObject->GetID();
365 return string(anEntry);
368 const SALOMEDS::SObject_var& VISU::Result_i::GetSObject() const { return mySObject;}
369 const SALOMEDS::Study_var& VISU::Result_i::GetStudyDocument() const { return myStudyDocument;}
370 const SALOMEDS::SComponent_var& VISU::Result_i::GetSComponent() const { return mySComponent;}
372 VISU::Result_i::~Result_i() {
373 if(MYDEBUG) MESSAGE("Result_i::~Result_i()");
374 if(myInput) delete myInput;