]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
HDF persistance for light modules
authorjfa <jfa@opencascade.com>
Fri, 9 Dec 2005 11:20:40 +0000 (11:20 +0000)
committerjfa <jfa@opencascade.com>
Fri, 9 Dec 2005 11:20:40 +0000 (11:20 +0000)
src/LightApp/LightApp_Application.cxx
src/LightApp/LightApp_Driver.h
src/LightApp/LightApp_HDFDriver.cxx [new file with mode: 0644]
src/LightApp/LightApp_HDFDriver.h [new file with mode: 0644]
src/LightApp/LightApp_Study.cxx
src/LightApp/Makefile.in
src/Makefile.in

index 9270ece694f87c5caa31f233c42f631e758f95af..50988716107d10c18c9e66b41668059e57e89087 100644 (file)
@@ -1201,7 +1201,8 @@ void LightApp_Application::onDesktopActivated()
  */
 QString LightApp_Application::getFileFilter() const
 {
-  return "(*.bin)";
+  //return "(*.bin)";
+  return "(*.hdf)";
 }
 
 /*! Gets file name*/
index 022ce16ec81cc47895d160e17a276c0a55853ad8..917918e5ad6de53d21977f32a318b874a30f29ef 100644 (file)
@@ -40,9 +40,9 @@ public:
 
   typedef std::vector<std::string> ListOfFiles;
 
-  bool                SaveDatasInFile   (const char* theFileName, bool isMultiFile );
-  bool                ReadDatasFromFile (const char* theFileName,  bool isMultiFile );
-  virtual std::string GetTmpDir      (const char* theURL, const bool  isMultiFile);
+  virtual bool        SaveDatasInFile   (const char* theFileName, bool isMultiFile);
+  virtual bool        ReadDatasFromFile (const char* theFileName, bool isMultiFile);
+  virtual std::string GetTmpDir      (const char* theURL, const bool isMultiFile);
 
   ListOfFiles         GetListOfFiles (const char* theModuleName);
   virtual void        SetListOfFiles (const char* theModuleName, const ListOfFiles theListOfFiles);
@@ -51,7 +51,7 @@ public:
 
   virtual void        ClearDriverContents();
 
-private:
+protected:
   void                PutFilesToStream(const std::string& theModuleName, unsigned char*& theBuffer,
                                        long& theBufferSize, bool theNamesOnly = false);
   ListOfFiles         PutStreamToFiles(const unsigned char* theBuffer,
@@ -60,7 +60,7 @@ private:
   std::string GetTmpDir();
   std::string GetDirFromPath(const std::string& thePath);
 
-private:
+protected:
   typedef std::map<std::string, ListOfFiles> MapOfListOfFiles;
   MapOfListOfFiles                           myMap;
   std::string                                myTmpDir;
diff --git a/src/LightApp/LightApp_HDFDriver.cxx b/src/LightApp/LightApp_HDFDriver.cxx
new file mode 100644 (file)
index 0000000..ec0e6db
--- /dev/null
@@ -0,0 +1,346 @@
+#include "LightApp_HDFDriver.h"
+
+#include "HDFexplorer.hxx"
+#include "HDFOI.hxx"
+
+// OCCT Includes
+#include <TCollection_AsciiString.hxx>
+
+#include <OSD_Path.hxx>
+#include <OSD_File.hxx>
+#include <OSD_Directory.hxx>
+#include <OSD_Process.hxx>
+#include <OSD_Directory.hxx>
+#include <OSD_Protection.hxx>
+#include <OSD_SingleProtection.hxx>
+#include <OSD_FileIterator.hxx>
+
+// QT Includes
+#include <qfileinfo.h>
+#include <qdir.h>
+
+#ifdef WIN32
+#include <time.h>
+#endif
+
+/*! Constructor.*/
+LightApp_HDFDriver::LightApp_HDFDriver()
+{
+}
+
+/*! Destructor.*/
+LightApp_HDFDriver::~LightApp_HDFDriver()
+{
+}
+
+using namespace std;
+
+//================================================================
+//  Function : SaveDatasInFile
+/*! Purpose  : save in file 'theFileName' datas from this driver*/
+//================================================================
+bool LightApp_HDFDriver::SaveDatasInFile( const char* theFileName, bool isMultiFile )
+{
+  bool isASCII = false;
+  bool isError = false;
+
+  HDFfile *hdf_file = 0;
+  HDFgroup *hdf_group_datacomponent = 0;
+  HDFgroup *hdf_group_study_structure = 0;
+  HDFgroup *hdf_sco_group  = 0;
+  HDFgroup *hdf_sco_group2 = 0;
+  HDFdataset *hdf_dataset = 0;
+  hdf_size aHDFSize[1];
+
+  try {
+    hdf_file = new HDFfile ((char*)theFileName);
+    hdf_file->CreateOnDisk();
+
+    //-----------------------------------------------------------------------
+    // 1 - Create a groupe for each SComponent and Update the PersistanceRef
+    //-----------------------------------------------------------------------
+    hdf_group_datacomponent = new HDFgroup ("DATACOMPONENT", hdf_file);
+    hdf_group_datacomponent->CreateOnDisk();
+
+    std::map<std::string, std::string> mapNameEntry;
+
+    int tag = 1;
+    std::map<std::string, ListOfFiles>::const_iterator it;
+    for (it = myMap.begin(); it != myMap.end(); ++it, ++tag) {
+      std::string aName (it->first);
+      char* aModuleName = const_cast<char*>(aName.c_str());
+      unsigned char* aBuffer;
+      long           aBufferSize;
+      PutFilesToStream(aName, aBuffer, aBufferSize, isMultiFile);
+
+      //Handle(SALOMEDSImpl_SComponent) sco = itcomponent.Value();
+      //TCollection_AsciiString scoid = sco->GetID();
+      //hdf_sco_group = new HDFgroup(scoid.ToCString(), hdf_group_datacomponent);
+
+      TCollection_AsciiString entry ("0:1:");
+      entry += TCollection_AsciiString(tag);
+      mapNameEntry[aModuleName] = entry.ToCString();
+
+      //hdf_sco_group = new HDFgroup (aModuleName, hdf_group_datacomponent);
+      hdf_sco_group = new HDFgroup (entry.ToCString(), hdf_group_datacomponent);
+      hdf_sco_group->CreateOnDisk();
+
+      aHDFSize[0] = aBufferSize;
+
+      hdf_dataset = new HDFdataset ("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
+      hdf_dataset->CreateOnDisk();
+      hdf_dataset->WriteOnDisk(aBuffer); //Save the stream in the HDF file
+      hdf_dataset->CloseOnDisk();
+      hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
+
+      // store multifile state
+      aHDFSize[0] = 2;
+      hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
+      hdf_dataset->CreateOnDisk();
+      hdf_dataset->WriteOnDisk((void*)(isMultiFile ? "M" : "S")); // save: multi or single
+      hdf_dataset->CloseOnDisk();
+      hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
+
+      // store ASCII state
+      aHDFSize[0] = 2;
+      hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
+      hdf_dataset->CreateOnDisk();
+      hdf_dataset->WriteOnDisk((void*)(isASCII ? "A" : "B")); // save: ASCII or BINARY
+      hdf_dataset->CloseOnDisk();
+      hdf_dataset = 0; //will be deleted by hdf_sco_group destructor
+
+      hdf_sco_group->CloseOnDisk();
+      hdf_sco_group = 0; // will be deleted by hdf_group_datacomponent destructor
+
+      delete [] aBuffer;
+    }
+
+    hdf_group_datacomponent->CloseOnDisk();
+    hdf_group_datacomponent = 0; // will be deleted by hdf_file destructor
+
+    //-----------------------------------------------------------------------
+    // 3 - Write the Study Structure
+    //-----------------------------------------------------------------------
+    hdf_group_study_structure = new HDFgroup ("STUDY_STRUCTURE", hdf_file);
+    hdf_group_study_structure->CreateOnDisk();
+
+    for (it = myMap.begin(); it != myMap.end(); ++it) {
+      std::string aName (it->first);
+      char* aModuleName = const_cast<char*>(aName.c_str());
+
+      //hdf_sco_group2 = new HDFgroup(scid.ToCString(), hdf_group_study_structure);
+      char* entry = (char*)(mapNameEntry[aModuleName].c_str());
+      hdf_sco_group2 = new HDFgroup (entry, hdf_group_study_structure);
+      hdf_sco_group2->CreateOnDisk();
+
+      // ComponentDataType treatment
+      hdf_int32 name_len = (hdf_int32)strlen(aModuleName);
+      aHDFSize[0] = name_len + 1;
+      hdf_dataset = new HDFdataset ("COMPONENTDATATYPE", hdf_sco_group2, HDF_STRING, aHDFSize, 1);
+      hdf_dataset->CreateOnDisk();
+      hdf_dataset->WriteOnDisk(aModuleName);
+      hdf_dataset->CloseOnDisk();
+      hdf_dataset = 0; //will be deleted by hdf_sco_group2 destructor
+
+      hdf_sco_group2->CloseOnDisk();
+      hdf_sco_group2 = 0; // will be deleted by hdf_group_study_structure destructor
+    }
+
+    hdf_group_study_structure->CloseOnDisk();
+    hdf_group_study_structure = 0; // will be deleted by hdf_file destructor
+
+    hdf_file->CloseOnDisk();
+    delete hdf_file; // recursively deletes all hdf objects...
+
+  } catch (HDFexception) {
+    isError = true;
+  }
+  if (isASCII && !isError) { // save file in ASCII format
+    HDFascii::ConvertFromHDFToASCII(theFileName, true);
+  }
+
+  return !isError;
+}
+
+//=======================================================================
+//  Function : ReadDatasFromFile
+/*! Purpose  : filling current driver from file 'theFileName'*/
+//=======================================================================
+bool LightApp_HDFDriver::ReadDatasFromFile( const char* theFileName, bool isMultiFile )
+{
+  bool isASCII = false;
+  bool isError = false;
+  TCollection_AsciiString aHDFUrl;
+
+  HDFfile *hdf_file = 0;
+  HDFgroup *hdf_group_datacomponent = 0;
+  HDFgroup *hdf_group_study_structure = 0;
+  HDFgroup *hdf_sco_group  = 0;
+  HDFgroup *hdf_sco_group2 = 0;
+
+  std::map<std::string, std::string> mapEntryName;
+
+  if (HDFascii::isASCII(theFileName)) {
+    isASCII = true;
+    char* aResultPath = HDFascii::ConvertFromASCIIToHDF(theFileName);
+    aHDFUrl = aResultPath;
+    aHDFUrl += "hdf_from_ascii.hdf";
+    delete(aResultPath);
+  } else {
+    aHDFUrl = (char*)theFileName;
+  }
+
+  hdf_file = new HDFfile((char*)aHDFUrl.ToCString());
+
+  char aMultifileState[2];
+  char ASCIIfileState[2];
+
+  try {
+    hdf_file->OpenOnDisk(HDF_RDONLY);
+
+  } catch (HDFexception) {
+    //char *eStr = new char[strlen(aUrl.ToCString()) + 17];
+    //sprintf(eStr,"Can't open file %s", aUrl.ToCString());
+    //_errorCode = TCollection_AsciiString(eStr);
+    //delete [] eStr;
+    return false;
+  }
+
+  try {
+    if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
+      //_errorCode = "Study is empty";
+      isError = true;
+    } else {
+      hdf_group_study_structure = new HDFgroup ("STUDY_STRUCTURE", hdf_file);
+      hdf_group_study_structure->OpenOnDisk();
+
+      char name[HDF_NAME_MAX_LEN + 1];
+      Standard_Integer nbsons = hdf_group_study_structure->nInternalObjects();
+      for (Standard_Integer i = 0; i < nbsons; i++) {
+        hdf_group_study_structure->InternalObjectIndentify(i, name);
+        if (strncmp(name, "INTERNAL_COMPLEX", 16) == 0) continue;
+        hdf_object_type type = hdf_group_study_structure->InternalObjectType(name);
+        if (type == HDF_GROUP) {
+          hdf_sco_group2 = new HDFgroup (name, hdf_group_study_structure);
+          hdf_sco_group2->OpenOnDisk();
+
+          // Read component data
+          char* aCompDataType = NULL;
+          int aDataSize = 0;
+
+          if (hdf_sco_group2->ExistInternalObject("COMPONENTDATATYPE")) {
+            HDFdataset *hdf_dataset = new HDFdataset("COMPONENTDATATYPE", hdf_sco_group2);
+            hdf_dataset->OpenOnDisk();
+            aDataSize = hdf_dataset->GetSize();
+            aCompDataType = new char[aDataSize];
+            if (aCompDataType == NULL) {
+              isError = true;
+            } else {
+              hdf_dataset->ReadFromDisk(aCompDataType);
+
+              mapEntryName[name] = aCompDataType;
+
+              delete [] aCompDataType;
+            }
+
+            hdf_dataset->CloseOnDisk();
+            hdf_dataset = 0;
+          }
+
+          hdf_sco_group2->CloseOnDisk();
+        }
+      }
+
+      hdf_group_study_structure->CloseOnDisk();
+    }
+
+    if (!hdf_file->ExistInternalObject("DATACOMPONENT")) {
+      //_errorCode = "No components stored";
+      isError = true;
+    } else {
+      hdf_group_datacomponent = new HDFgroup ("DATACOMPONENT", hdf_file);
+      hdf_group_datacomponent->OpenOnDisk();
+
+      char name[HDF_NAME_MAX_LEN + 1];
+      Standard_Integer nbsons = hdf_group_datacomponent->nInternalObjects();
+      for (Standard_Integer i = 0; i < nbsons; i++) {
+        hdf_group_datacomponent->InternalObjectIndentify(i, name);
+        if (strncmp(name, "INTERNAL_COMPLEX", 16) == 0) continue;
+        hdf_object_type type = hdf_group_datacomponent->InternalObjectType(name);
+        if (type == HDF_GROUP) {
+          hdf_sco_group = new HDFgroup (name, hdf_group_datacomponent);
+          hdf_sco_group->OpenOnDisk();
+
+          // Read component data
+          unsigned char* aStreamFile = NULL;
+          int aStreamSize = 0;
+
+          if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
+            HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
+            hdf_dataset->OpenOnDisk();
+            aStreamSize = hdf_dataset->GetSize();
+            aStreamFile = new unsigned char[aStreamSize];
+            if (aStreamFile == NULL) {
+              isError = true;
+            } else {
+              hdf_dataset->ReadFromDisk(aStreamFile);
+            }
+
+            hdf_dataset->CloseOnDisk();
+            hdf_dataset = 0;
+          }
+
+          HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
+          multifile_hdf_dataset->OpenOnDisk();
+          multifile_hdf_dataset->ReadFromDisk(aMultifileState);
+          multifile_hdf_dataset->CloseOnDisk();
+          multifile_hdf_dataset = 0;
+
+          HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
+          ascii_hdf_dataset->OpenOnDisk();
+          ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
+          ascii_hdf_dataset->CloseOnDisk();
+          ascii_hdf_dataset = 0;
+
+          isASCII = (ASCIIfileState[0] == 'A') ? true : false;
+
+          if (aStreamFile != NULL) {
+            // Put buffer to aListOfFiles and set to myMap
+            ListOfFiles aListOfFiles = PutStreamToFiles(aStreamFile, aStreamSize, isMultiFile);
+            char* aCompDataType = (char*)(mapEntryName[name].c_str());
+            SetListOfFiles(aCompDataType, aListOfFiles);
+
+            delete [] aStreamFile;
+          }
+
+          hdf_sco_group->CloseOnDisk();
+        }
+      }
+
+      hdf_group_datacomponent->CloseOnDisk();
+    }
+  } catch (HDFexception) {
+    isError = true;
+
+    //Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
+    //aFilesToRemove->Append(aHDFUrl);
+    //RemoveFiles(aFilesToRemove, true);
+  }
+
+  hdf_file->CloseOnDisk();
+  delete hdf_file; // all related hdf objects will be deleted
+
+  if (isASCII && !isError) {
+    //Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
+    //aFilesToRemove->Append(aHDFUrl);
+    //RemoveFiles(aFilesToRemove, true);
+  }
+
+  std::map<std::string, std::string>::const_iterator it;
+  for (it = mapEntryName.begin(); it != mapEntryName.end(); ++it) {
+    cout << "Read Component: entry = " << it->first
+         << ", Component data type = " << it->second << endl;
+  }
+
+  return !isError;
+}
diff --git a/src/LightApp/LightApp_HDFDriver.h b/src/LightApp/LightApp_HDFDriver.h
new file mode 100644 (file)
index 0000000..5a978ea
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef LIGHTAPP_HDFDRIVER_H
+#define LIGHTAPP_HDFDRIVER_H
+
+#include <LightApp.h>
+#include <LightApp_Driver.h>
+
+#ifdef WIN32
+#pragma warning( disable:4251 )
+#endif
+
+/*!Description : Driver can save to file and read from file list of files for light modules*/
+
+class LIGHTAPP_EXPORT LightApp_HDFDriver : public LightApp_Driver
+{
+public:
+  LightApp_HDFDriver();
+  virtual ~LightApp_HDFDriver();
+
+  virtual bool        SaveDatasInFile   (const char* theFileName, bool isMultiFile);
+  virtual bool        ReadDatasFromFile (const char* theFileName, bool isMultiFile);
+};
+
+#endif
index e6b6f04a28c29440b4287d508002f6b1f6d65d07..551737a648da2b250f755ac992b05fc2b93053d1 100644 (file)
@@ -23,7 +23,7 @@
 #include "LightApp_DataModel.h"
 #include "LightApp_DataObject.h"
 #include "LightApp_RootObject.h"
-#include "LightApp_Driver.h"
+#include "LightApp_HDFDriver.h"
 
 #include "SUIT_ResourceMgr.h"
 #include "SUIT_DataObjectIterator.h"
@@ -50,7 +50,7 @@
 LightApp_Study::LightApp_Study( SUIT_Application* app )
 : CAM_Study( app )
 {
-  myDriver = new LightApp_Driver();
+  myDriver = new LightApp_HDFDriver();
 }
  
 /*!
@@ -311,8 +311,8 @@ bool LightApp_Study::isSaved() const
 }
 
 //=======================================================================
-// name    : saveModuleData
-/*! Purpose :  Create SComponent for module, necessary for SalomeApp study */
+// name    : addComponent
+/*! Purpose : Create SComponent for module, necessary for SalomeApp study */
 //=======================================================================
 void LightApp_Study::addComponent(const CAM_DataModel* dm)
 {
@@ -451,7 +451,7 @@ void LightApp_Study::RemoveTemporaryFiles (const char* theModuleName, const bool
 }
 
 //================================================================
-// Function : RemoveTemporaryFiles
+// Function : components
 /*! Purpose  : to be used by modules*/
 //================================================================
 void LightApp_Study::components( QStringList& comp ) const
@@ -465,4 +465,3 @@ void LightApp_Study::components( QStringList& comp ) const
       comp.append( obj->entry() );
   }
 }
-
index 092b071f780a6b4e74d8708cbf2668685cd1d044..06cc1a897220036612510d266793c06e81e7bc65 100755 (executable)
@@ -22,6 +22,7 @@ EXPORT_HEADERS= LightApp.h \
                LightApp_Dialog.h \
                LightApp_Displayer.h \
                LightApp_Driver.h \
+               LightApp_HDFDriver.h \
                LightApp_GLSelector.h \
                LightApp_Module.h \
                LightApp_ModuleDlg.h \
@@ -58,6 +59,7 @@ LIB_SRC= LightApp_AboutDlg.cxx \
         LightApp_Dialog.cxx \
         LightApp_Displayer.cxx \
         LightApp_Driver.cxx \
+        LightApp_HDFDriver.cxx \
         LightApp_GLSelector.cxx \
         LightApp_Module.cxx \
         LightApp_ModuleDlg.cxx \
@@ -105,9 +107,9 @@ RESOURCES_FILES = icon_about.png \
                  LightApp.ini \
                  LightApp.xml
 
-CPPFLAGS+=$(PYTHON_INCLUDES) $(QT_INCLUDES) $(QWT_INCLUDES) $(OCC_INCLUDES) $(VTK_INCLUDES)
+CPPFLAGS+=$(PYTHON_INCLUDES) $(QT_INCLUDES) $(QWT_INCLUDES) $(OCC_INCLUDES) $(VTK_INCLUDES) $(HDF5_INCLUDES)
 
 LDFLAGS+=$(PYTHON_LIBS) $(QT_MT_LIBS)
-LIBS+= -lsuit -lstd -lCAM -lObjBrowser -lLogWindow $(CAS_KERNEL) -lGLViewer -lOCCViewer -lVTKViewer -lSalomeObject -lSOCC -lSVTK -lSPlot2d -lSUPERVGraph -lPyInterp -lPythonConsole
+LIBS+= -lsuit -lstd -lCAM -lObjBrowser -lLogWindow $(CAS_KERNEL) -lGLViewer -lOCCViewer -lVTKViewer -lSalomeObject -lSOCC -lSVTK -lSPlot2d -lSUPERVGraph -lPyInterp -lPythonConsole $(HDF5_LIBS) -lSalomeHDFPersistCopy
 
 @CONCLUDE@
index 31bf6332d3f2a4672ed6d6aad3b7b559ca7b77cc..a482210f49ab74721c2515153f4f8cce477a2276 100755 (executable)
@@ -34,6 +34,7 @@ VPATH=.:@srcdir@
 
 SUBDIRS = Qtx SUIT STD CAF CAM SUITApp VTKViewer OCCViewer GLViewer \
           LogWindow Event OBJECT Prs PyInterp PythonConsole ObjBrowser \
-          RegistryDisplay Plot2d TOOLSGUI SOCC SVTK SPlot2d SUPERVGraph LightApp Session SalomeApp SALOME_SWIG SALOME_PY SALOME_PYQT Style ResExporter
+          RegistryDisplay Plot2d TOOLSGUI SOCC SVTK SPlot2d SUPERVGraph HDFPersistCopy LightApp \
+          Session SalomeApp SALOME_SWIG SALOME_PY SALOME_PYQT Style ResExporter
 
 @MODULE@