Salome HOME
NRI : Merge from V1_2.
[modules/visu.git] / src / VISU_I / VISU_Result_i.cc
index dc9f250565e591624090def695edb558e4f479aa..f0a16be6886f7079876be3e31c8e476e13006e81 100644 (file)
@@ -1,9 +1,12 @@
-using namespace std;
-// File:       VISU_Result_i.cc
-// Created:    Fri Jan 10 13:56:05 2003
-// Author:     Alexey PETROV
-//             <apo@ivanox.nnov.matra-dtv.fr>
+//  Copyright (C) 2003  CEA/DEN, EDF R&D
+//
+//
+//
+//  File   : VISU_Result_i.cc
+//  Author : Alexey PETROV
+//  Module : VISU
 
+using namespace std;
 #include "VISU_Result_i.hh"
 #include "VISU_Convertor_impl.hxx"
 #include "VISU_CorbaMedConvertor.hxx"
@@ -11,6 +14,7 @@ using namespace std;
 using namespace VISU;
 
 #include "SALOMEDS_Tool.hxx"
+#include "HDFascii.hxx"
 
 #include <qstring.h>
 #include <qfileinfo.h>
@@ -23,7 +27,7 @@ using namespace VISU;
 using namespace std;
 
 #ifdef DEBUG
-static int MYDEBUG = 1;
+static int MYDEBUG = 0;
 static int MYDEBUGWITHFILES = 0;
 #else
 static int MYDEBUG = 0;
@@ -79,6 +83,17 @@ QString GenerateName(const char* theName){
   return tmp;
 }
 
+QString GenerateFieldName(const string& theName, const string& theUnits){
+  static QString aName;
+  const string tmp(theUnits.size(),' ');
+  if(theUnits == "" || theUnits == tmp)
+    aName.sprintf("%s, -",theName.c_str());
+  else
+    aName.sprintf("%s, %s",theName.c_str(),theUnits.c_str());
+  aName = aName.simplifyWhiteSpace();
+  return aName.latin1();
+}
+
 void CreateReference(SALOMEDS::Study_ptr theStudyDocument, 
                     const string& theFatherEntry, const string& theRefEntry)
 {
@@ -109,22 +124,22 @@ VISU::Storable* VISU::Result_i::Build(SALOMEDS::SObject_ptr theSObject)
      throw (std::runtime_error&)
 {
   if(MYDEBUG) MESSAGE("Result_i::Build");
-  mySComponent = FindOrCreateVisuComponent(myStudyDocument);
-  CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
-  string aRefFatherEntry = GetRefFatherEntry();
-  QString aComment;
-  aComment.sprintf("myComment=%s;myType=%d;myFileName=%s",
-                  GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1());
-  string aResultEntry = CreateAttributes(myStudyDocument,aSComponentEntry,aRefFatherEntry.c_str(),
-                                        anIOR,myName.c_str(),"",aComment.latin1(),true);
-  mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
-  if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
-  if(theSObject != NULL){
-    CORBA::String_var aString = theSObject->GetID();
-    CreateReference(myStudyDocument,aResultEntry,aString.in());
-  }
   const TMeshMap& aMeshMap = myInput->GetMeshMap();
   if(!aMeshMap.empty()) {//apo
+    mySComponent = FindOrCreateVisuComponent(myStudyDocument);
+    CORBA::String_var aSComponentEntry = mySComponent->GetID(), anIOR(GetID());
+    string aRefFatherEntry = GetRefFatherEntry();
+    QString aComment;
+    aComment.sprintf("myComment=%s;myType=%d;myFileName=%s",
+                    GetComment(),VISU::TRESULT,myFileInfo.filePath().latin1());
+    string aResultEntry = CreateAttributes(myStudyDocument,aSComponentEntry,aRefFatherEntry.c_str(),
+                                          anIOR,myName.c_str(),"",aComment.latin1(),true);
+    mySObject = myStudyDocument->FindObjectID(aResultEntry.c_str());
+    if(mySObject->_is_nil()) throw std::runtime_error("Build - There is no SObject for the Result !!!");
+    if(theSObject != NULL){
+      CORBA::String_var aString = theSObject->GetID();
+      CreateReference(myStudyDocument,aResultEntry,aString.in());
+    }
     TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
       const string& aMeshName = aMeshMapIter->first;
@@ -215,10 +230,11 @@ VISU::Storable* VISU::Result_i::Build(SALOMEDS::SObject_ptr theSObject)
          const string& aFieldName = aFieldMapIter->first;
          const VISU::TField& aField = aFieldMapIter->second;
          const VISU::TField::TValField& aValField = aField.myValField;
+         QString aFieldNameWithUnit = ::GenerateFieldName(aFieldName,aField.myUnitNames[0]);
          aComment.sprintf("myComment=FIELD;myType=%d;myMeshName=%s;myEntityId=%d;myName=%s;myNbTimeStamps=%d;myNumComponent=%d",
                           VISU::TFIELD,aMeshName.c_str(),anEntity,aFieldName.c_str(),aValField.size(),aField.myNbComp);
          string aFieldEntry = CreateAttributes(myStudyDocument,aFieldsEntry.c_str(),aRefFatherEntry.c_str(),
-                                               "",aFieldName.c_str(),"",aComment.latin1(),true);
+                                               "",aFieldNameWithUnit.latin1(),"",aComment.latin1(),true);
          CreateReference(myStudyDocument,aFieldEntry,aEntity2Entry[anEntity]);
          VISU::TField::TValField::const_iterator aValFieldIter = aValField.begin();
          for(; aValFieldIter != aValField.end(); aValFieldIter++){
@@ -243,8 +259,9 @@ VISU::Storable* VISU::Result_i::Create(const char* theFileName){
     myInput = CreateConvertor(theFileName);
     if(myInput == NULL) return NULL;
     myFileInfo.setFile(theFileName);
-    myName = ::GenerateName(myFileInfo.fileName().latin1());
-    return Build();
+    myName = (const char*)(::GenerateName(myFileInfo.fileName().latin1()));
+    VISU::Storable* aStorable = Build();
+    return aStorable;
   }catch(std::runtime_error& exc){
     MESSAGE("Follow exception was accured :\n"<<exc.what());
   }catch(...){
@@ -254,15 +271,35 @@ VISU::Storable* VISU::Result_i::Create(const char* theFileName){
 }
 
 VISU::Storable* VISU::Result_i::Create(SALOMEDS::SObject_ptr theMedSObject){
-  if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from theMedSObject");
+  if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOMEDS::SObject_ptr");
   try{
     mySourceId = eComponent;
     myInput = CreateMEDConvertor(theMedSObject);
     if(myInput == NULL) return NULL;
     string aCompDataType = GetComponentDataType(theMedSObject);
     myFileInfo.setFile(aCompDataType.c_str());
-    myName = ::GenerateName("aResult");
-    return Build(theMedSObject);
+    myName = (const char*)(::GenerateName("aResult"));
+    VISU::Storable* aStorable = Build(theMedSObject);
+    return aStorable;
+  }catch(std::runtime_error& exc){
+    MESSAGE("Follow exception was accured :\n"<<exc.what());
+  }catch(...){
+    MESSAGE("Unknown exception was accured!");
+  }
+  return NULL;
+}
+
+VISU::Storable* VISU::Result_i::Create(SALOME_MED::FIELD_ptr theField){
+  if(MYDEBUG)  MESSAGE("Result_i::Create MedObject from SALOME_MED::FIELD_ptr");
+  try{
+    mySourceId = eComponent;
+    myInput = CreateMEDFieldConvertor(theField);
+    if(myInput == NULL) return NULL;
+    string aCompDataType = "Med";
+    myFileInfo.setFile(aCompDataType.c_str());
+    myName = (const char*)(::GenerateName("aResult"));
+    VISU::Storable* aStorable = Build();
+    return aStorable;
   }catch(std::runtime_error& exc){
     MESSAGE("Follow exception was accured :\n"<<exc.what());
   }catch(...){
@@ -280,7 +317,7 @@ VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject,
     mySObject = SALOMEDS::SObject::_duplicate(theSObject);
     myStudyDocument = mySObject->GetStudy();
     mySComponent = mySObject->GetFatherComponent();
-    myName = VISU::Storable::FindValue(theMap,"myName");
+    myName = (const char*)(VISU::Storable::FindValue(theMap,"myName"));
     SALOMEDS::SObject_var aRefSObj, aTargetRefSObj;
     if(mySObject->FindSubObject(1,aRefSObj) && aRefSObj->ReferencedObject(aTargetRefSObj)){
       mySourceId = eRestoredComponent;
@@ -300,16 +337,45 @@ VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject,
       }else
        throw std::runtime_error("GetInput - There is no convertor for the aDataType !!!");
     }else{
-      mySourceId = eRestoredFile;
       myFileInfo.setFile(thePrefix.c_str());
+
       string aStudyPrefix(SALOMEDS_Tool::GetNameFromPath(myStudyDocument->URL()));
-      string aTmpDir(SALOMEDS_Tool::GetDirFromPath(myStudyDocument->URL()));
       if(!myFileInfo.isFile()){
-       string aFileName = thePrefix + aStudyPrefix + "_" + myName;  
+       string aFileName = thePrefix + aStudyPrefix + "_" + myName;
        myFileInfo.setFile(aFileName.c_str());
       }
       if(MYDEBUG)  
        MESSAGE("Result_i::Restore - aFileName = "<<myFileInfo.filePath()<<"; "<<myFileInfo.isFile());
+
+      if (HDFascii::isASCII(myFileInfo.filePath().latin1())) {
+       char* aResultPath = HDFascii::ConvertFromASCIIToHDF(myFileInfo.filePath().latin1());
+       char* aHDFFileName = new char[strlen(aResultPath) + 19];
+       sprintf(aHDFFileName, "%shdf_from_ascii.hdf", aResultPath);
+
+       if (IsMultifile()) { // set this file as new - temporary
+         static QString aCommand;
+         aCommand.sprintf("mv %s %s%s",aHDFFileName, aResultPath, myFileInfo.baseName().latin1());
+         if(system(aCommand) == -1){
+           if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
+           return NULL;
+         } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
+         myFileInfo.setFile(QString(aResultPath)+QString(myFileInfo.baseName().latin1()));
+       } else { // change current temporary file to the new: with hdf-format
+         static QString aCommand;
+         aCommand.sprintf("mv %s %s\0",aHDFFileName, myFileInfo.filePath().latin1());
+         if(system(aCommand.latin1()) == -1) {
+           if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - Can't execute the command :"<<aCommand);
+           return NULL;
+         } else if(MYDEBUG) MESSAGE("VISU::Result_i::Restore - "<<aCommand);
+         SALOMEDS::ListOfFileNames_var anEmptyList = new SALOMEDS::ListOfFileNames;
+         SALOMEDS_Tool::RemoveTemporaryFiles(aResultPath, anEmptyList, true);
+       }
+       mySourceId = eRestoredFile;
+       delete(aResultPath);
+       delete(aHDFFileName);
+      } else if (!IsMultifile()) mySourceId = eRestoredFile;
+      else mySourceId = eFile;
+
       myInput = CreateMedConvertor(myFileInfo.filePath().latin1());
       QString aComment;
       aComment.sprintf("myComment=%s;myType=%d;myName=%s;myFileName=%s",
@@ -319,13 +385,6 @@ VISU::Storable* VISU::Result_i::Restore(SALOMEDS::SObject_ptr theSObject,
        throw std::runtime_error("Build - There is no AttributeComment for the SObject !!!");
       SALOMEDS::AttributeComment_var aCmnt = SALOMEDS::AttributeComment::_narrow(anAttr);
       aCmnt->SetValue(aComment.latin1());
-      // remove temporary file, if it's not multifile mode
-      if (false || !IsMultifile()) {
-       SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
-       aSeq->length(1);
-       aSeq[0]=(aStudyPrefix + myName).c_str();
-       SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
-      }
     }
     return this;
   }catch(std::runtime_error& exc){
@@ -340,7 +399,7 @@ VISU::Result_i::InputType* VISU::Result_i::GetInput() {
   return myInput;
 }
 
-void VISU::Result_i::ToStream(ostrstream& theStr){
+void VISU::Result_i::ToStream(std::ostringstream& theStr){
   if(MYDEBUG) MESSAGE(GetComment());
   Storable::DataToStream(theStr,"myName",myName.c_str());
 }