From: adv Date: Fri, 6 Sep 2013 13:11:32 +0000 (+0000) Subject: Dump of document data to Python sript. X-Git-Tag: BR_hydro_v_0_1~62 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=ac7145bc4d05b5df24b7bb28e968dfdc2930aed6;p=modules%2Fhydro.git Dump of document data to Python sript. --- diff --git a/src/HYDROData/HYDROData_Application.cxx b/src/HYDROData/HYDROData_Application.cxx index 3c03259b..bd65e98f 100644 --- a/src/HYDROData/HYDROData_Application.cxx +++ b/src/HYDROData/HYDROData_Application.cxx @@ -17,10 +17,30 @@ HYDROData_Application* HYDROData_Application::GetApplication() } //======================================================================= -//function : getDocument +//function : GetDocument //purpose : //======================================================================= -Handle(HYDROData_Document) HYDROData_Application::GetDocument(int theStudyID) +bool HYDROData_Application::GetDocumentId(const Handle(HYDROData_Document)& theDocument, + int& theDocId) const +{ + DataMapOfStudyIDDocument::Iterator aMapit( myDocuments ); + for ( ; aMapit.More(); aMapit.Next() ) + { + if ( aMapit.Value() != theDocument ) + continue; + + theDocId = aMapit.Key(); + return true; + } + + return false; +} + +//======================================================================= +//function : GetDocument +//purpose : +//======================================================================= +Handle(HYDROData_Document) HYDROData_Application::GetDocument(int theStudyID) const { if (myDocuments.IsBound(theStudyID)) { return myDocuments.Find(theStudyID); @@ -44,7 +64,7 @@ HYDROData_Application::HYDROData_Application () //function : addDocument //purpose : //======================================================================= -void HYDROData_Application::AddDocument(int theStudyID, Handle(HYDROData_Document) theDocument) +void HYDROData_Application::AddDocument(int theStudyID, const Handle(HYDROData_Document)& theDocument) { myDocuments.Bind(theStudyID, theDocument); } @@ -53,7 +73,7 @@ void HYDROData_Application::AddDocument(int theStudyID, Handle(HYDROData_Documen //function : removeDocument //purpose : //======================================================================= -void HYDROData_Application::RemoveDocument(Handle(HYDROData_Document) theDocument) +void HYDROData_Application::RemoveDocument(const Handle(HYDROData_Document)& theDocument) { DataMapOfStudyIDDocument::Iterator anIter(myDocuments); for(; anIter.More(); anIter.Next()) diff --git a/src/HYDROData/HYDROData_Application.h b/src/HYDROData/HYDROData_Application.h index b0fa53db..b20eaefc 100644 --- a/src/HYDROData/HYDROData_Application.h +++ b/src/HYDROData/HYDROData_Application.h @@ -38,13 +38,17 @@ public: // Redefined OCAF methods private: //! Returns document by its study ID, if document doesn't exists return null - Handle(HYDROData_Document) GetDocument(int theStudyID); + Handle(HYDROData_Document) GetDocument(int theStudyID) const; + + //! Get study id by document instance, if document doesn't exists return false + bool GetDocumentId(const Handle(HYDROData_Document)& theDocument, + int& theDocId) const; //! Appends document to the application - void AddDocument(int theStudyID, Handle(HYDROData_Document) theDocument); + void AddDocument(int theStudyID, const Handle(HYDROData_Document)& theDocument); //! Removes document from the application - void RemoveDocument(Handle(HYDROData_Document) theDocument); + void RemoveDocument( const Handle(HYDROData_Document)& theDocument ); //! map from SALOME study ID to the document DataMapOfStudyIDDocument myDocuments; diff --git a/src/HYDROData/HYDROData_Document.cxx b/src/HYDROData/HYDROData_Document.cxx index 722a39e8..da06a2c8 100644 --- a/src/HYDROData/HYDROData_Document.cxx +++ b/src/HYDROData/HYDROData_Document.cxx @@ -1,6 +1,8 @@ + #include #include #include +#include #include @@ -13,6 +15,8 @@ IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared) IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared) +#define PYTHON_DOC_NAME "doc" + static const int UNDO_LIMIT = 10; // number of possible undo operations in the module static const int TAG_PROPS = 1; // general properties tag @@ -40,6 +44,12 @@ bool HYDROData_Document::HasDocument(const int theStudyID) return !aResult.IsNull(); } +bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument, + int& theDocId ) +{ + return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId); +} + Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID) { Handle(TDocStd_Document) aResult; @@ -134,22 +144,62 @@ bool HYDROData_Document::DumpToPython( const QString& theFileName ) const { // Try to open the file QFile aFile( theFileName ); - if ( !aFile.exists() || !aFile.open( QIODevice::WriteOnly ) ) + if ( !aFile.open( QIODevice::WriteOnly ) ) return false; - QMap aDumpedObjects; + MapOfTreatedObjects aTreatedObjects; + + // Dump header for python script + QStringList aHeaderDump = DumpToPython( aTreatedObjects ); + if ( aHeaderDump.isEmpty() ) + return false; - bool aRes = dumpPartitionToPython( aFile, aDumpedObjects, KIND_IMAGE ); - aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_POLYLINE ); - aRes = aRes && dumpPartitionToPython( aFile, aDumpedObjects, KIND_BATHYMETRY ); + HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump ); + + bool aRes = true; + + // Dump all model objects to Python script + aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_IMAGE ); + aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_POLYLINE ); + aRes = aRes && dumpPartitionToPython( aFile, aTreatedObjects, KIND_BATHYMETRY ); return aRes; } -bool HYDROData_Document::dumpPartitionToPython( - QFile& theFile, - QMap& theDumpedObjects, - const ObjectKind& theObjectKind ) const +QString HYDROData_Document::GetDocPyName() const +{ + QString aDocName = PYTHON_DOC_NAME; + + int aDocId = 1; + if ( DocumentId( this, aDocId ) ) + aDocName += "_" + QString::number( aDocId ); + + return aDocName; +} + +QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const +{ + QString aDocName = GetDocPyName(); + + // Append document in to the map of treated objects to prevent names overlaping + theTreatedObjects.insert( aDocName, this ); + + int aDocId = 1; + if ( !DocumentId( this, aDocId ) ) + aDocId = 1; + + QStringList aResScript; + + aResScript << QString( "from HYDROData import *" ); + aResScript << QString( "" ); + aResScript << QString( "%1 = HYDROData_Document.Document( %2 );" ).arg( aDocName ).arg( aDocId ); + + return aResScript; +} + +bool HYDROData_Document::dumpPartitionToPython( QFile& theFile, + MapOfTreatedObjects& theTreatedObjects, + const ObjectKind& theObjectKind ) const { if ( !theFile.isOpen() ) return false; @@ -166,20 +216,14 @@ bool HYDROData_Document::dumpPartitionToPython( continue; QString anObjName = anObject->GetName(); - if ( theDumpedObjects.contains( anObjName ) ) + if ( theTreatedObjects.contains( anObjName ) ) continue; - theDumpedObjects.insert( anObjName, anObject ); + theTreatedObjects.insert( anObjName, anObject ); - QStringList anObjDump = anObject->DumpToPython(); - if ( anObjDump.isEmpty() ) - continue; + QStringList anObjDump = anObject->DumpToPython( theTreatedObjects ); - QString anObjDumpStr = anObjDump.join( "\n" ); - if ( anObjDumpStr.isEmpty() ) - continue; - - anOutStream << anObjDumpStr << "\n"; + HYDROData_Tool::WriteStringsToFile( theFile, anObjDump ); } return aRes; diff --git a/src/HYDROData/HYDROData_Document.h b/src/HYDROData/HYDROData_Document.h index 1ec24ee7..28ac1f41 100644 --- a/src/HYDROData/HYDROData_Document.h +++ b/src/HYDROData/HYDROData_Document.h @@ -8,7 +8,6 @@ class QFile; - /** * Errors that could appear on document open/save actions. * If there is no error, it is "OK". @@ -45,6 +44,10 @@ public: //! Returns true if data model contains document for this study HYDRODATA_EXPORT static bool HasDocument(const int theStudyID); + //! Get study id by document instance, if document doesn't exists return false + HYDRODATA_EXPORT static bool DocumentId( const Handle(HYDROData_Document)& theDocument, + int& theDocId ); + //! Loads the OCAF document from the file. //! \param theFileName full name of the file to load //! \param theStudyID identifier of the SALOME study to associate with loaded file @@ -59,11 +62,17 @@ public: //! Removes document data HYDRODATA_EXPORT void Close(); + // Returns name of document instance in python dump script + HYDRODATA_EXPORT QString GetDocPyName() const; + //! Dump study document to Python script representation. //! \param theFileName full name of the file to store //! \returns true if document has been successfuly dumped HYDRODATA_EXPORT bool DumpToPython( const QString& theFileName ) const; + //! Dump model data to Python script representation. + HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + //! Starts a new operation (opens a tansaction) HYDRODATA_EXPORT void StartOperation(); //! Finishes the previously started operation (closes the transaction) @@ -119,10 +128,14 @@ protected: private: + // Dump header Python part in to file \c theFile + bool DumpToPython( QFile& theFile, + MapOfTreatedObjects& theTreatedObjects ) const; + // Dump objects of type \c theObjectKind to file \c theFile - bool dumpPartitionToPython( QFile& theFile, - QMap& theDumpedObjects, - const ObjectKind& theObjectKind ) const; + bool dumpPartitionToPython( QFile& theFile, + MapOfTreatedObjects& theDumpedObjects, + const ObjectKind& theObjectKind ) const; private: Handle(TDocStd_Document) myDoc; ///< OCAF document instance corresponding for keeping all persistent data diff --git a/src/HYDROData/HYDROData_Object.cxx b/src/HYDROData/HYDROData_Object.cxx index eb9d0f72..c2bad239 100644 --- a/src/HYDROData/HYDROData_Object.cxx +++ b/src/HYDROData/HYDROData_Object.cxx @@ -35,7 +35,7 @@ void HYDROData_Object::SetName(const QString& theName) TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData())); } -QStringList HYDROData_Object::DumpToPython() const +QStringList HYDROData_Object::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const { QStringList anEmptyList; return anEmptyList; diff --git a/src/HYDROData/HYDROData_Object.h b/src/HYDROData/HYDROData_Object.h index 4f2ae435..84d60156 100644 --- a/src/HYDROData/HYDROData_Object.h +++ b/src/HYDROData/HYDROData_Object.h @@ -21,6 +21,8 @@ const ObjectKind KIND_BATHYMETRY = 4; DEFINE_STANDARD_HANDLE(HYDROData_Object, MMgt_TShared) +typedef QMap MapOfTreatedObjects; + /**\class HYDROData_Object * \brief Generic class of any object in the data model. * @@ -64,7 +66,7 @@ public: * You should reimplement this function in your derived class if it * has Python API and can be imported/exported from/to Python script. */ - HYDRODATA_EXPORT virtual QStringList DumpToPython() const; + HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; /** * Checks is object exists in the data structure. diff --git a/src/HYDROData/HYDROData_Tool.cxx b/src/HYDROData/HYDROData_Tool.cxx index 9ed8f0be..0cd14427 100644 --- a/src/HYDROData/HYDROData_Tool.cxx +++ b/src/HYDROData/HYDROData_Tool.cxx @@ -1,2 +1,21 @@ #include "HYDROData_Tool.h" + +#include +#include +#include + +void HYDROData_Tool::WriteStringsToFile( QFile& theFile, + const QStringList& theStrings, + const QString& theSep ) +{ + if ( !theFile.isOpen() || theStrings.isEmpty() ) + return; + + QString aWriteStr = theStrings.join( theSep ); + if ( aWriteStr.isEmpty() ) + return; + + QTextStream anOutStream( &theFile ); + anOutStream << aWriteStr << theSep; +} diff --git a/src/HYDROData/HYDROData_Tool.h b/src/HYDROData/HYDROData_Tool.h index e2306013..5be4ba3a 100644 --- a/src/HYDROData/HYDROData_Tool.h +++ b/src/HYDROData/HYDROData_Tool.h @@ -4,12 +4,18 @@ #include +#include +class QFile; +class QStringList; class HYDROData_Tool { public: + static void WriteStringsToFile( QFile& theFile, + const QStringList& theStrings, + const QString& theSep = "\n" ); };