2 #include <HYDROData_Document.h>
3 #include <HYDROData_Application.h>
4 #include <HYDROData_Iterator.h>
5 #include <HYDROData_Tool.h>
7 #include <TDataStd_Integer.hxx>
8 #include <TDataXtd_Position.hxx>
10 #include <TDF_Delta.hxx>
15 #include <QStringList>
16 #include <QTextStream>
18 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
19 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
21 #define PYTHON_DOC_NAME "hydro_doc"
23 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
25 static const int TAG_PROPS = 1; // general properties tag
26 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
27 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
28 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
29 static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
30 static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
34 typedef QMap<Standard_Integer,Handle_HYDROData_Entity> MapOfOrdered;
35 typedef QMap<QString,Handle_HYDROData_Entity> MapOfUnordered;
37 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
39 Handle(HYDROData_Document) aResult =
40 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
41 if (aResult.IsNull()) {
42 aResult = new HYDROData_Document();
43 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
48 Handle(HYDROData_Document) HYDROData_Document::Document(
49 const TDF_Label& theObjectLabel )
51 Handle(HYDROData_Document) aResDoc;
52 if ( theObjectLabel.IsNull() )
55 Handle(TDocStd_Document) anObjDoc;
58 anObjDoc = TDocStd_Document::Get( theObjectLabel );
64 if ( anObjDoc.IsNull() )
67 HYDROData_Application* anApp = HYDROData_Application::GetApplication();
69 DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
70 for ( ; aMapIt.More(); aMapIt.Next() )
72 Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
73 if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
83 bool HYDROData_Document::HasDocument(const int theStudyID)
85 Handle(HYDROData_Document) aResult =
86 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
87 return !aResult.IsNull();
90 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
93 return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
96 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
98 Handle(TDocStd_Document) aResult;
99 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
100 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
103 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
105 catch (Standard_Failure)
107 if (!aResult.IsNull()) {
108 aResult->SetUndoLimit(UNDO_LIMIT);
109 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
112 Data_DocError anError;
115 anError = DocError_OK;
117 case PCDM_RS_NoDriver:
118 case PCDM_RS_UnknownFileDriver:
119 case PCDM_RS_NoSchema:
120 case PCDM_RS_DriverFailure:
121 case PCDM_RS_WrongResource:
122 anError = DocError_ResourcesProblem;
124 case PCDM_RS_OpenError:
125 case PCDM_RS_NoDocument:
126 case PCDM_RS_WrongStreamMode:
127 case PCDM_RS_PermissionDenied:
128 anError = DocError_CanNotOpen;
130 case PCDM_RS_NoVersion:
131 anError = DocError_InvalidVersion;
133 case PCDM_RS_ExtensionFailure:
134 case PCDM_RS_FormatFailure:
135 case PCDM_RS_TypeFailure:
136 case PCDM_RS_TypeNotFoundInSchema:
137 case PCDM_RS_UnrecognizedFileFormat:
138 anError = DocError_InvalidFormat;
140 case PCDM_RS_MakeFailure:
142 anError = DocError_UnknownProblem;
148 Data_DocError HYDROData_Document::Save(const char* theFileName)
150 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
151 PCDM_StoreStatus aStatus;
153 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
155 catch (Standard_Failure) {}
156 myTransactionsAfterSave = 0;
157 Standard::Purge(); // Release free memory
160 Data_DocError anError;
163 anError = DocError_OK;
165 case PCDM_SS_DriverFailure:
166 anError = DocError_ResourcesProblem;
168 case PCDM_SS_WriteFailure:
169 //case PCDM_SS_DiskWritingFailure:
170 //case PCDM_SS_UserRightsFailure:
171 anError = DocError_CanNotOpen;
174 anError = DocError_UnknownProblem;
180 void HYDROData_Document::Close()
183 HYDROData_Application::GetApplication()->RemoveDocument(this);
186 bool HYDROData_Document::DumpToPython( const QString& theFileName,
187 const bool theIsMultiFile ) const
189 // Try to open the file
190 QFile aFile( theFileName );
191 if ( !aFile.open( QIODevice::WriteOnly ) )
194 MapOfTreatedObjects aTreatedObjects;
196 // Dump header for python script
197 QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
198 if ( aHeaderDump.isEmpty() )
201 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
205 // Dump the local CS data to Python
207 QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX ).arg( myLY );
210 HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
212 // Dump all model objects to Python script
213 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
214 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
215 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
216 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
217 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
218 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
219 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
220 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
221 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
222 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
223 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
225 // Dump code to close python fuction
226 if ( aRes && theIsMultiFile )
228 QStringList aFooterScript;
229 aFooterScript << QString( "" );
230 aFooterScript << QString( " pass" );
231 HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
237 QString HYDROData_Document::GetDocPyName() const
239 QString aDocName = PYTHON_DOC_NAME;
243 if ( DocumentId( this, aDocId ) )
244 aDocName += "_" + QString::number( aDocId );
250 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
251 const bool theIsMultiFile ) const
253 QString aDocName = GetDocPyName();
255 // Append document in to the map of treated objects to prevent names overlaping
256 theTreatedObjects.insert( aDocName, this );
259 if ( !DocumentId( this, aDocId ) )
262 QStringList aResScript;
264 aResScript << QString( "from HYDROPy import *" );
265 aResScript << QString( "from PyQt4.QtCore import *" );
266 aResScript << QString( "from PyQt4.QtGui import *" );
268 if ( theIsMultiFile )
270 aResScript << QString( "import salome" );
271 aResScript << QString( "" );
272 aResScript << QString( "def RebuildData( theStudy ):" );
273 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
277 aResScript << QString( "" );
278 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
284 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
285 const bool theIsMultiFile,
286 MapOfTreatedObjects& theTreatedObjects,
287 const ObjectKind& theObjectKind ) const
289 if ( !theFile.isOpen() )
292 QTextStream anOutStream( &theFile );
296 HYDROData_Iterator anIterator( this, theObjectKind );
297 for( ; anIterator.More(); anIterator.Next() )
299 Handle(HYDROData_Entity) anObject = anIterator.Current();
300 if ( anObject.IsNull() )
303 QString anObjName = anObject->GetName();
304 if ( theTreatedObjects.contains( anObjName ) )
307 theTreatedObjects.insert( anObjName, anObject );
309 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
311 if ( theIsMultiFile )
313 // For multifile dump we use the function, see the document dump header
314 QStringList::iterator anIt = anObjDump.begin();
315 for ( ; anIt != anObjDump.end(); ++anIt )
316 anIt->prepend( " " );
319 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
325 bool takeLastDigits( QString& theStr, int& aRes )
330 for ( int i = theStr.length() - 1; i >= 0; --i )
332 const QChar& aChar = theStr.at( i );
333 if ( !aChar.isDigit() )
336 anStrNum.prepend( aChar );
339 if ( anStrNum.isEmpty() )
342 theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
343 aRes = anStrNum.toInt();
348 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
350 QString aStr1 = theStr1, aStr2 = theStr2;
352 int aNum1 = -1, aNum2 = -1;
353 if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
355 if ( aStr1 == aStr2 )
356 return aNum1 < aNum2;
359 return theStr1 < theStr2;
362 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
363 const Standard_Boolean theIsAll ) const
365 HYDROData_SequenceOfObjects anOrder;
367 MapOfOrdered aMapOfOrdered;
368 MapOfUnordered aMapOfUnordered;
370 HYDROData_Iterator anIter( this );
371 for ( ; anIter.More(); anIter.Next() )
373 Handle(HYDROData_Entity) anObject = anIter.Current();
374 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
377 Standard_Integer anObjZLevel = -1;
378 if ( anObject->GetZLevel( anObjZLevel ) )
380 aMapOfOrdered.insert( anObjZLevel, anObject );
384 QString anObjName = anObject->GetName();
385 if ( anObjName.isEmpty() )
388 aMapOfUnordered.insert( anObjName, anObject );
392 MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
393 for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
395 const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
396 anOrder.Prepend( anObject );
401 QStringList aSortedNames = aMapOfUnordered.keys();
402 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
404 for ( int i = 0; i < aSortedNames.length(); ++i )
406 QString anObjName = aSortedNames.value( i );
408 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
409 anOrder.Append( anObject );
416 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
418 // At first we remove previous model order
419 RemoveObjectsLayerOrder();
421 // Make new objects order
422 Standard_Integer aLevel = 0;
423 for ( int i = theOrder.Length(), n = 1; i >= n; --i )
425 const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
426 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
429 anObject->SetZLevel( aLevel++ );
433 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
435 HYDROData_SequenceOfObjects anOrder;
436 anOrder.Append( theObject );
440 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
442 MapOfUnordered aMapOfUnordered;
444 for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
446 const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
447 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
450 Standard_Integer anObjZLevel = -1;
451 if ( anObject->GetZLevel( anObjZLevel ) )
453 continue; // Skip objects that already have the z-level
457 QString anObjName = anObject->GetName();
458 if ( anObjName.isEmpty() )
461 aMapOfUnordered.insert( anObjName, anObject );
465 if ( aMapOfUnordered.isEmpty() )
466 return; // Nothing to show
468 Standard_Integer aTopId = 0;
470 HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
471 if ( !aModelOrder.IsEmpty() )
473 const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
474 anObject->GetZLevel( aTopId );
478 aTopId += aMapOfUnordered.size() - 1;
480 QStringList aSortedNames = aMapOfUnordered.keys();
481 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
483 for ( int i = 0; i < aSortedNames.length(); ++i )
485 QString anObjName = aSortedNames.value( i );
487 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
488 anObject->SetZLevel( aTopId-- );
492 void HYDROData_Document::RemoveObjectsLayerOrder()
494 HYDROData_Iterator anIter( this );
495 for ( ; anIter.More(); anIter.Next() )
497 Handle(HYDROData_Entity) anObject = anIter.Current();
498 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
501 anObject->RemoveZLevel();
505 void HYDROData_Document::StartOperation()
510 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
512 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
515 NewID(); // workaround: do something just to modify the document
516 myDoc->CommitCommand();
518 myTransactionsAfterSave++;
520 if( theName.Length() != 0 )
522 const TDF_DeltaList& aList = GetUndos();
523 if( !aList.IsEmpty() )
525 Handle(TDF_Delta) aDelta = aList.Last();
526 if( !aDelta.IsNull() )
527 aDelta->SetName( theName );
532 void HYDROData_Document::AbortOperation()
534 myDoc->AbortCommand();
537 bool HYDROData_Document::IsOperation()
539 return myDoc->HasOpenCommand() != 0;
542 bool HYDROData_Document::IsModified()
544 return myTransactionsAfterSave != 0;
547 bool HYDROData_Document::CanUndo()
549 return myDoc->GetAvailableUndos() > 0;
552 const TDF_DeltaList& HYDROData_Document::GetUndos()
554 return myDoc->GetUndos();
557 void HYDROData_Document::ClearUndos()
559 return myDoc->ClearUndos();
562 void HYDROData_Document::Undo()
565 myTransactionsAfterSave--;
568 bool HYDROData_Document::CanRedo()
570 return myDoc->GetAvailableRedos() > 0;
573 const TDF_DeltaList& HYDROData_Document::GetRedos()
575 return myDoc->GetRedos();
578 void HYDROData_Document::ClearRedos()
580 return myDoc->ClearRedos();
583 void HYDROData_Document::Redo()
586 myTransactionsAfterSave++;
589 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
591 return HYDROData_Iterator::CreateObject( this, theKind );
594 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
595 const QString& theName,
596 const ObjectKind theObjectKind ) const
598 Handle(HYDROData_Entity) anObject;
599 if ( theName.isEmpty() )
602 QStringList aNamesList;
603 aNamesList << theName;
605 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
606 if( aSeqOfObjs.IsEmpty() )
609 anObject = aSeqOfObjs.First();
613 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
614 const QStringList& theNames,
615 const ObjectKind theObjectKind ) const
617 HYDROData_SequenceOfObjects aResSeq;
619 QStringList aNamesList = theNames;
621 HYDROData_Iterator anIter( this, theObjectKind );
622 for( ; anIter.More(); anIter.Next() )
624 Handle(HYDROData_Entity) anObject = anIter.Current();
625 if( anObject.IsNull() )
628 QString anObjName = anObject->GetName();
629 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
632 aResSeq.Append( anObject );
634 aNamesList.removeAll( anObjName );
635 if ( aNamesList.isEmpty() )
642 HYDROData_Document::HYDROData_Document()
644 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
645 myDoc->SetUndoLimit(UNDO_LIMIT);
646 NewID(); // needed to have at least one attribute in initial document to avoid errors
647 myTransactionsAfterSave = 0;
652 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
655 myTransactionsAfterSave = 0;
660 HYDROData_Document::~HYDROData_Document()
664 int HYDROData_Document::NewID()
666 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
667 FindChild(TAG_PROPS_NEW_ID);
668 Handle(TDataStd_Integer) anInt;
669 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
670 anInt = TDataStd_Integer::Set(anIDLab, 0);
672 // just increment value and return
673 anInt->Set(anInt->Get() + 1);
677 TDF_Label HYDROData_Document::LabelOfObjects()
679 return myDoc->Main().FindChild(TAG_OBJECTS);
682 TDF_Label HYDROData_Document::LabelOfLocalCS() const
684 return myDoc->Main().FindChild(TAG_LOCAL_CS);
687 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
689 TDF_Label aLocalCSLab = LabelOfLocalCS();
691 Handle( TDataXtd_Position ) aLocalCS;
692 if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
694 gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
695 theLX = aLocalCS3d.X();
696 theLY = aLocalCS3d.Y();
700 theLX = DEFAULT_LOCAL_CS.X();
701 theLY = DEFAULT_LOCAL_CS.Y();
705 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
709 // update the local CS data in attribute
710 TDF_Label aLocalCSLab = LabelOfLocalCS();
711 Handle( TDataXtd_Position ) aLocalCS;
712 if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
713 aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
715 gp_Pnt aLocalCS3d( theLX, theLY, 0 );
716 aLocalCS->SetPosition( aLocalCS3d );
718 // calculate delta for coordinates
719 double aDX = myLX - theLX;
720 double aDY = myLY - theLY;
722 // update the local CS data in internal fields
726 //update all objects in the document
727 HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
728 for( ; anIterator.More(); anIterator.Next() )
729 anIterator.Current()->UpdateLocalCS( aDX, aDY );
732 void HYDROData_Document::UpdateLCSFields() const
734 if( myLX >= 0 && myLY >= 0 )
738 GetLocalCS( aLX, aLY );
739 HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
744 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
759 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
761 double X = thePnt.X();
762 double Y = thePnt.Y();
763 double Z = thePnt.Z();
764 Transform( X, Y, IsToLocalCS );
765 thePnt = gp_Pnt( X, Y, Z );
768 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
770 double X = thePnt.X();
771 double Y = thePnt.Y();
772 double Z = thePnt.Z();
773 Transform( X, Y, IsToLocalCS );
774 thePnt = gp_XYZ( X, Y, Z );
777 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
779 double X = thePnt.X();
780 double Y = thePnt.Y();
781 Transform( X, Y, IsToLocalCS );
782 thePnt = gp_XY( X, Y );