}
//=======================================================================
-//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);
//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);
}
//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())
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;
+
#include <HYDROData_Document.h>
#include <HYDROData_Application.h>
#include <HYDROData_Iterator.h>
+#include <HYDROData_Tool.h>
#include <TDataStd_Integer.hxx>
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
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;
{
// Try to open the file
QFile aFile( theFileName );
- if ( !aFile.exists() || !aFile.open( QIODevice::WriteOnly ) )
+ if ( !aFile.open( QIODevice::WriteOnly ) )
return false;
- QMap<QString,Handle(HYDROData_Object)> 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<QString,Handle(HYDROData_Object)>& 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;
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;
class QFile;
-
/**
* Errors that could appear on document open/save actions.
* If there is no error, it is "OK".
//! 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
//! 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)
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<QString,Handle(HYDROData_Object)>& 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
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;
DEFINE_STANDARD_HANDLE(HYDROData_Object, MMgt_TShared)
+typedef QMap<QString,Handle(Standard_Transient)> MapOfTreatedObjects;
+
/**\class HYDROData_Object
* \brief Generic class of any object in the data model.
*
* 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.
#include "HYDROData_Tool.h"
+
+#include <QFile>
+#include <QStringList>
+#include <QTextStream>
+
+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;
+}
#include <Precision.hxx>
+#include <QString>
+class QFile;
+class QStringList;
class HYDROData_Tool {
public:
+ static void WriteStringsToFile( QFile& theFile,
+ const QStringList& theStrings,
+ const QString& theSep = "\n" );
};