From 43f7b3831e1e5550484571d6db17572e3e9370f1 Mon Sep 17 00:00:00 2001 From: jfa Date: Fri, 9 Dec 2005 11:20:40 +0000 Subject: [PATCH] HDF persistance for light modules --- src/LightApp/LightApp_Application.cxx | 3 +- src/LightApp/LightApp_Driver.h | 10 +- src/LightApp/LightApp_HDFDriver.cxx | 346 ++++++++++++++++++++++++++ src/LightApp/LightApp_HDFDriver.h | 23 ++ src/LightApp/LightApp_Study.cxx | 11 +- src/LightApp/Makefile.in | 6 +- src/Makefile.in | 3 +- 7 files changed, 387 insertions(+), 15 deletions(-) create mode 100644 src/LightApp/LightApp_HDFDriver.cxx create mode 100644 src/LightApp/LightApp_HDFDriver.h diff --git a/src/LightApp/LightApp_Application.cxx b/src/LightApp/LightApp_Application.cxx index 9270ece69..509887161 100644 --- a/src/LightApp/LightApp_Application.cxx +++ b/src/LightApp/LightApp_Application.cxx @@ -1201,7 +1201,8 @@ void LightApp_Application::onDesktopActivated() */ QString LightApp_Application::getFileFilter() const { - return "(*.bin)"; + //return "(*.bin)"; + return "(*.hdf)"; } /*! Gets file name*/ diff --git a/src/LightApp/LightApp_Driver.h b/src/LightApp/LightApp_Driver.h index 022ce16ec..917918e5a 100644 --- a/src/LightApp/LightApp_Driver.h +++ b/src/LightApp/LightApp_Driver.h @@ -40,9 +40,9 @@ public: typedef std::vector 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 MapOfListOfFiles; MapOfListOfFiles myMap; std::string myTmpDir; diff --git a/src/LightApp/LightApp_HDFDriver.cxx b/src/LightApp/LightApp_HDFDriver.cxx new file mode 100644 index 000000000..ec0e6db21 --- /dev/null +++ b/src/LightApp/LightApp_HDFDriver.cxx @@ -0,0 +1,346 @@ +#include "LightApp_HDFDriver.h" + +#include "HDFexplorer.hxx" +#include "HDFOI.hxx" + +// OCCT Includes +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +// QT Includes +#include +#include + +#ifdef WIN32 +#include +#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 mapNameEntry; + + int tag = 1; + std::map::const_iterator it; + for (it = myMap.begin(); it != myMap.end(); ++it, ++tag) { + std::string aName (it->first); + char* aModuleName = const_cast(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(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 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::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 index 000000000..5a978eacf --- /dev/null +++ b/src/LightApp/LightApp_HDFDriver.h @@ -0,0 +1,23 @@ +#ifndef LIGHTAPP_HDFDRIVER_H +#define LIGHTAPP_HDFDRIVER_H + +#include +#include + +#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 diff --git a/src/LightApp/LightApp_Study.cxx b/src/LightApp/LightApp_Study.cxx index e6b6f04a2..551737a64 100644 --- a/src/LightApp/LightApp_Study.cxx +++ b/src/LightApp/LightApp_Study.cxx @@ -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() ); } } - diff --git a/src/LightApp/Makefile.in b/src/LightApp/Makefile.in index 092b071f7..06cc1a897 100755 --- a/src/LightApp/Makefile.in +++ b/src/LightApp/Makefile.in @@ -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@ diff --git a/src/Makefile.in b/src/Makefile.in index 31bf6332d..a482210f4 100755 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -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@ -- 2.39.2