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( "" );
271 aResScript << QString( "def RebuildData( theStudy ):" );
272 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
276 aResScript << QString( "" );
277 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
283 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
284 const bool theIsMultiFile,
285 MapOfTreatedObjects& theTreatedObjects,
286 const ObjectKind& theObjectKind ) const
288 if ( !theFile.isOpen() )
291 QTextStream anOutStream( &theFile );
295 HYDROData_Iterator anIterator( this, theObjectKind );
296 for( ; anIterator.More(); anIterator.Next() )
298 Handle(HYDROData_Entity) anObject = anIterator.Current();
299 if ( anObject.IsNull() )
302 QString anObjName = anObject->GetName();
303 if ( theTreatedObjects.contains( anObjName ) )
306 theTreatedObjects.insert( anObjName, anObject );
308 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
310 if ( theIsMultiFile )
312 // For multifile dump we use the function, see the document dump header
313 QStringList::iterator anIt = anObjDump.begin();
314 for ( ; anIt != anObjDump.end(); ++anIt )
315 anIt->prepend( " " );
318 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
324 bool takeLastDigits( QString& theStr, int& aRes )
329 for ( int i = theStr.length() - 1; i >= 0; --i )
331 const QChar& aChar = theStr.at( i );
332 if ( !aChar.isDigit() )
335 anStrNum.prepend( aChar );
338 if ( anStrNum.isEmpty() )
341 theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
342 aRes = anStrNum.toInt();
347 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
349 QString aStr1 = theStr1, aStr2 = theStr2;
351 int aNum1 = -1, aNum2 = -1;
352 if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
354 if ( aStr1 == aStr2 )
355 return aNum1 < aNum2;
358 return theStr1 < theStr2;
361 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
362 const Standard_Boolean theIsAll ) const
364 HYDROData_SequenceOfObjects anOrder;
366 MapOfOrdered aMapOfOrdered;
367 MapOfUnordered aMapOfUnordered;
369 HYDROData_Iterator anIter( this );
370 for ( ; anIter.More(); anIter.Next() )
372 Handle(HYDROData_Entity) anObject = anIter.Current();
373 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
376 Standard_Integer anObjZLevel = -1;
377 if ( anObject->GetZLevel( anObjZLevel ) )
379 aMapOfOrdered.insert( anObjZLevel, anObject );
383 QString anObjName = anObject->GetName();
384 if ( anObjName.isEmpty() )
387 aMapOfUnordered.insert( anObjName, anObject );
391 MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
392 for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
394 const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
395 anOrder.Prepend( anObject );
400 QStringList aSortedNames = aMapOfUnordered.keys();
401 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
403 for ( int i = 0; i < aSortedNames.length(); ++i )
405 QString anObjName = aSortedNames.value( i );
407 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
408 anOrder.Append( anObject );
415 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
417 // At first we remove previous model order
418 RemoveObjectsLayerOrder();
420 // Make new objects order
421 Standard_Integer aLevel = 0;
422 for ( int i = theOrder.Length(), n = 1; i >= n; --i )
424 const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
425 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
428 anObject->SetZLevel( aLevel++ );
432 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
434 HYDROData_SequenceOfObjects anOrder;
435 anOrder.Append( theObject );
439 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
441 MapOfUnordered aMapOfUnordered;
443 for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
445 const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
446 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
449 Standard_Integer anObjZLevel = -1;
450 if ( anObject->GetZLevel( anObjZLevel ) )
452 continue; // Skip objects that already have the z-level
456 QString anObjName = anObject->GetName();
457 if ( anObjName.isEmpty() )
460 aMapOfUnordered.insert( anObjName, anObject );
464 if ( aMapOfUnordered.isEmpty() )
465 return; // Nothing to show
467 Standard_Integer aTopId = 0;
469 HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
470 if ( !aModelOrder.IsEmpty() )
472 const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
473 anObject->GetZLevel( aTopId );
477 aTopId += aMapOfUnordered.size() - 1;
479 QStringList aSortedNames = aMapOfUnordered.keys();
480 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
482 for ( int i = 0; i < aSortedNames.length(); ++i )
484 QString anObjName = aSortedNames.value( i );
486 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
487 anObject->SetZLevel( aTopId-- );
491 void HYDROData_Document::RemoveObjectsLayerOrder()
493 HYDROData_Iterator anIter( this );
494 for ( ; anIter.More(); anIter.Next() )
496 Handle(HYDROData_Entity) anObject = anIter.Current();
497 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
500 anObject->RemoveZLevel();
504 void HYDROData_Document::StartOperation()
509 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
511 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
514 NewID(); // workaround: do something just to modify the document
515 myDoc->CommitCommand();
517 myTransactionsAfterSave++;
519 if( theName.Length() != 0 )
521 const TDF_DeltaList& aList = GetUndos();
522 if( !aList.IsEmpty() )
524 Handle(TDF_Delta) aDelta = aList.Last();
525 if( !aDelta.IsNull() )
526 aDelta->SetName( theName );
531 void HYDROData_Document::AbortOperation()
533 myDoc->AbortCommand();
536 bool HYDROData_Document::IsOperation()
538 return myDoc->HasOpenCommand() != 0;
541 bool HYDROData_Document::IsModified()
543 return myTransactionsAfterSave != 0;
546 bool HYDROData_Document::CanUndo()
548 return myDoc->GetAvailableUndos() > 0;
551 const TDF_DeltaList& HYDROData_Document::GetUndos()
553 return myDoc->GetUndos();
556 void HYDROData_Document::ClearUndos()
558 return myDoc->ClearUndos();
561 void HYDROData_Document::Undo()
564 myTransactionsAfterSave--;
567 bool HYDROData_Document::CanRedo()
569 return myDoc->GetAvailableRedos() > 0;
572 const TDF_DeltaList& HYDROData_Document::GetRedos()
574 return myDoc->GetRedos();
577 void HYDROData_Document::ClearRedos()
579 return myDoc->ClearRedos();
582 void HYDROData_Document::Redo()
585 myTransactionsAfterSave++;
588 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
590 return HYDROData_Iterator::CreateObject( this, theKind );
593 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
594 const QString& theName,
595 const ObjectKind theObjectKind ) const
597 Handle(HYDROData_Entity) anObject;
598 if ( theName.isEmpty() )
601 QStringList aNamesList;
602 aNamesList << theName;
604 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
605 if( aSeqOfObjs.IsEmpty() )
608 anObject = aSeqOfObjs.First();
612 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
613 const QStringList& theNames,
614 const ObjectKind theObjectKind ) const
616 HYDROData_SequenceOfObjects aResSeq;
618 QStringList aNamesList = theNames;
620 HYDROData_Iterator anIter( this, theObjectKind );
621 for( ; anIter.More(); anIter.Next() )
623 Handle(HYDROData_Entity) anObject = anIter.Current();
624 if( anObject.IsNull() )
627 QString anObjName = anObject->GetName();
628 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
631 aResSeq.Append( anObject );
633 aNamesList.removeAll( anObjName );
634 if ( aNamesList.isEmpty() )
641 HYDROData_Document::HYDROData_Document()
643 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
644 myDoc->SetUndoLimit(UNDO_LIMIT);
645 NewID(); // needed to have at least one attribute in initial document to avoid errors
646 myTransactionsAfterSave = 0;
651 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
654 myTransactionsAfterSave = 0;
659 HYDROData_Document::~HYDROData_Document()
663 int HYDROData_Document::NewID()
665 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
666 FindChild(TAG_PROPS_NEW_ID);
667 Handle(TDataStd_Integer) anInt;
668 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
669 anInt = TDataStd_Integer::Set(anIDLab, 0);
671 // just increment value and return
672 anInt->Set(anInt->Get() + 1);
676 TDF_Label HYDROData_Document::LabelOfObjects()
678 return myDoc->Main().FindChild(TAG_OBJECTS);
681 TDF_Label HYDROData_Document::LabelOfLocalCS() const
683 return myDoc->Main().FindChild(TAG_LOCAL_CS);
686 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
688 TDF_Label aLocalCSLab = LabelOfLocalCS();
690 Handle( TDataXtd_Position ) aLocalCS;
691 if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
693 gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
694 theLX = aLocalCS3d.X();
695 theLY = aLocalCS3d.Y();
699 theLX = DEFAULT_LOCAL_CS.X();
700 theLY = DEFAULT_LOCAL_CS.Y();
704 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
708 // update the local CS data in attribute
709 TDF_Label aLocalCSLab = LabelOfLocalCS();
710 Handle( TDataXtd_Position ) aLocalCS;
711 if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
712 aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
714 gp_Pnt aLocalCS3d( theLX, theLY, 0 );
715 aLocalCS->SetPosition( aLocalCS3d );
717 // calculate delta for coordinates
718 double aDX = myLX - theLX;
719 double aDY = myLY - theLY;
721 // update the local CS data in internal fields
725 //update all objects in the document
726 HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
727 for( ; anIterator.More(); anIterator.Next() )
728 anIterator.Current()->UpdateLocalCS( aDX, aDY );
731 void HYDROData_Document::UpdateLCSFields() const
733 if( myLX >= 0 && myLY >= 0 )
737 GetLocalCS( aLX, aLY );
738 HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
743 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
758 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
760 double X = thePnt.X();
761 double Y = thePnt.Y();
762 double Z = thePnt.Z();
763 Transform( X, Y, IsToLocalCS );
764 thePnt = gp_Pnt( X, Y, Z );
767 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
769 double X = thePnt.X();
770 double Y = thePnt.Y();
771 double Z = thePnt.Z();
772 Transform( X, Y, IsToLocalCS );
773 thePnt = gp_XYZ( X, Y, Z );
776 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
778 double X = thePnt.X();
779 double Y = thePnt.Y();
780 Transform( X, Y, IsToLocalCS );
781 thePnt = gp_XY( X, Y );