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 );
208 HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
210 // Dump all model objects to Python script
211 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
212 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
213 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
214 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
215 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
216 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
217 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
218 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
219 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
220 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
221 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
223 // Dump code to close python fuction
224 if ( aRes && theIsMultiFile )
226 QStringList aFooterScript;
227 aFooterScript << QString( "" );
228 aFooterScript << QString( " pass" );
229 HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
235 QString HYDROData_Document::GetDocPyName() const
237 QString aDocName = PYTHON_DOC_NAME;
241 if ( DocumentId( this, aDocId ) )
242 aDocName += "_" + QString::number( aDocId );
248 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
249 const bool theIsMultiFile ) const
251 QString aDocName = GetDocPyName();
253 // Append document in to the map of treated objects to prevent names overlaping
254 theTreatedObjects.insert( aDocName, this );
257 if ( !DocumentId( this, aDocId ) )
260 QStringList aResScript;
262 aResScript << QString( "from HYDROPy import *" );
263 aResScript << QString( "from PyQt4.QtCore import *" );
264 aResScript << QString( "from PyQt4.QtGui import *" );
266 if ( theIsMultiFile )
268 aResScript << QString( "" );
269 aResScript << QString( "def RebuildData( theStudy ):" );
270 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
274 aResScript << QString( "" );
275 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
281 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
282 const bool theIsMultiFile,
283 MapOfTreatedObjects& theTreatedObjects,
284 const ObjectKind& theObjectKind ) const
286 if ( !theFile.isOpen() )
289 QTextStream anOutStream( &theFile );
293 HYDROData_Iterator anIterator( this, theObjectKind );
294 for( ; anIterator.More(); anIterator.Next() )
296 Handle(HYDROData_Entity) anObject = anIterator.Current();
297 if ( anObject.IsNull() )
300 QString anObjName = anObject->GetName();
301 if ( theTreatedObjects.contains( anObjName ) )
304 theTreatedObjects.insert( anObjName, anObject );
306 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
308 if ( theIsMultiFile )
310 // For multifile dump we use the function, see the document dump header
311 QStringList::iterator anIt = anObjDump.begin();
312 for ( ; anIt != anObjDump.end(); ++anIt )
313 anIt->prepend( " " );
316 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
322 bool takeLastDigits( QString& theStr, int& aRes )
327 for ( int i = theStr.length() - 1; i >= 0; --i )
329 const QChar& aChar = theStr.at( i );
330 if ( !aChar.isDigit() )
333 anStrNum.prepend( aChar );
336 if ( anStrNum.isEmpty() )
339 theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
340 aRes = anStrNum.toInt();
345 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
347 QString aStr1 = theStr1, aStr2 = theStr2;
349 int aNum1 = -1, aNum2 = -1;
350 if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
352 if ( aStr1 == aStr2 )
353 return aNum1 < aNum2;
356 return theStr1 < theStr2;
359 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
360 const Standard_Boolean theIsAll ) const
362 HYDROData_SequenceOfObjects anOrder;
364 MapOfOrdered aMapOfOrdered;
365 MapOfUnordered aMapOfUnordered;
367 HYDROData_Iterator anIter( this );
368 for ( ; anIter.More(); anIter.Next() )
370 Handle(HYDROData_Entity) anObject = anIter.Current();
371 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
374 Standard_Integer anObjZLevel = -1;
375 if ( anObject->GetZLevel( anObjZLevel ) )
377 aMapOfOrdered.insert( anObjZLevel, anObject );
381 QString anObjName = anObject->GetName();
382 if ( anObjName.isEmpty() )
385 aMapOfUnordered.insert( anObjName, anObject );
389 MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
390 for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
392 const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
393 anOrder.Prepend( anObject );
398 QStringList aSortedNames = aMapOfUnordered.keys();
399 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
401 for ( int i = 0; i < aSortedNames.length(); ++i )
403 QString anObjName = aSortedNames.value( i );
405 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
406 anOrder.Append( anObject );
413 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
415 // At first we remove previous model order
416 RemoveObjectsLayerOrder();
418 // Make new objects order
419 Standard_Integer aLevel = 0;
420 for ( int i = theOrder.Length(), n = 1; i >= n; --i )
422 const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
423 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
426 anObject->SetZLevel( aLevel++ );
430 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
432 HYDROData_SequenceOfObjects anOrder;
433 anOrder.Append( theObject );
437 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
439 MapOfUnordered aMapOfUnordered;
441 for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
443 const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
444 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
447 Standard_Integer anObjZLevel = -1;
448 if ( anObject->GetZLevel( anObjZLevel ) )
450 continue; // Skip objects that already have the z-level
454 QString anObjName = anObject->GetName();
455 if ( anObjName.isEmpty() )
458 aMapOfUnordered.insert( anObjName, anObject );
462 if ( aMapOfUnordered.isEmpty() )
463 return; // Nothing to show
465 Standard_Integer aTopId = 0;
467 HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
468 if ( !aModelOrder.IsEmpty() )
470 const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
471 anObject->GetZLevel( aTopId );
475 aTopId += aMapOfUnordered.size() - 1;
477 QStringList aSortedNames = aMapOfUnordered.keys();
478 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
480 for ( int i = 0; i < aSortedNames.length(); ++i )
482 QString anObjName = aSortedNames.value( i );
484 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
485 anObject->SetZLevel( aTopId-- );
489 void HYDROData_Document::RemoveObjectsLayerOrder()
491 HYDROData_Iterator anIter( this );
492 for ( ; anIter.More(); anIter.Next() )
494 Handle(HYDROData_Entity) anObject = anIter.Current();
495 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
498 anObject->RemoveZLevel();
502 void HYDROData_Document::StartOperation()
507 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
509 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
512 NewID(); // workaround: do something just to modify the document
513 myDoc->CommitCommand();
515 myTransactionsAfterSave++;
517 if( theName.Length() != 0 )
519 const TDF_DeltaList& aList = GetUndos();
520 if( !aList.IsEmpty() )
522 Handle(TDF_Delta) aDelta = aList.Last();
523 if( !aDelta.IsNull() )
524 aDelta->SetName( theName );
529 void HYDROData_Document::AbortOperation()
531 myDoc->AbortCommand();
534 bool HYDROData_Document::IsOperation()
536 return myDoc->HasOpenCommand() != 0;
539 bool HYDROData_Document::IsModified()
541 return myTransactionsAfterSave != 0;
544 bool HYDROData_Document::CanUndo()
546 return myDoc->GetAvailableUndos() > 0;
549 const TDF_DeltaList& HYDROData_Document::GetUndos()
551 return myDoc->GetUndos();
554 void HYDROData_Document::ClearUndos()
556 return myDoc->ClearUndos();
559 void HYDROData_Document::Undo()
562 myTransactionsAfterSave--;
565 bool HYDROData_Document::CanRedo()
567 return myDoc->GetAvailableRedos() > 0;
570 const TDF_DeltaList& HYDROData_Document::GetRedos()
572 return myDoc->GetRedos();
575 void HYDROData_Document::ClearRedos()
577 return myDoc->ClearRedos();
580 void HYDROData_Document::Redo()
583 myTransactionsAfterSave++;
586 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
588 return HYDROData_Iterator::CreateObject( this, theKind );
591 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
592 const QString& theName,
593 const ObjectKind theObjectKind ) const
595 Handle(HYDROData_Entity) anObject;
596 if ( theName.isEmpty() )
599 QStringList aNamesList;
600 aNamesList << theName;
602 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
603 if( aSeqOfObjs.IsEmpty() )
606 anObject = aSeqOfObjs.First();
610 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
611 const QStringList& theNames,
612 const ObjectKind theObjectKind ) const
614 HYDROData_SequenceOfObjects aResSeq;
616 QStringList aNamesList = theNames;
618 HYDROData_Iterator anIter( this, theObjectKind );
619 for( ; anIter.More(); anIter.Next() )
621 Handle(HYDROData_Entity) anObject = anIter.Current();
622 if( anObject.IsNull() )
625 QString anObjName = anObject->GetName();
626 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
629 aResSeq.Append( anObject );
631 aNamesList.removeAll( anObjName );
632 if ( aNamesList.isEmpty() )
639 HYDROData_Document::HYDROData_Document()
641 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
642 myDoc->SetUndoLimit(UNDO_LIMIT);
643 NewID(); // needed to have at least one attribute in initial document to avoid errors
644 myTransactionsAfterSave = 0;
649 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
652 myTransactionsAfterSave = 0;
657 HYDROData_Document::~HYDROData_Document()
661 int HYDROData_Document::NewID()
663 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
664 FindChild(TAG_PROPS_NEW_ID);
665 Handle(TDataStd_Integer) anInt;
666 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
667 anInt = TDataStd_Integer::Set(anIDLab, 0);
669 // just increment value and return
670 anInt->Set(anInt->Get() + 1);
674 TDF_Label HYDROData_Document::LabelOfObjects()
676 return myDoc->Main().FindChild(TAG_OBJECTS);
679 TDF_Label HYDROData_Document::LabelOfLocalCS() const
681 return myDoc->Main().FindChild(TAG_LOCAL_CS);
684 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
686 TDF_Label aLocalCSLab = LabelOfLocalCS();
688 Handle( TDataXtd_Position ) aLocalCS;
689 if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
691 gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
692 theLX = aLocalCS3d.X();
693 theLY = aLocalCS3d.Y();
697 theLX = DEFAULT_LOCAL_CS.X();
698 theLY = DEFAULT_LOCAL_CS.Y();
702 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
706 // update the local CS data in attribute
707 TDF_Label aLocalCSLab = LabelOfLocalCS();
708 Handle( TDataXtd_Position ) aLocalCS;
709 if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
710 aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
712 gp_Pnt aLocalCS3d( theLX, theLY, 0 );
713 aLocalCS->SetPosition( aLocalCS3d );
715 // calculate delta for coordinates
716 double aDX = myLX - theLX;
717 double aDY = myLY - theLY;
719 // update the local CS data in internal fields
723 //update all objects in the document
724 HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
725 for( ; anIterator.More(); anIterator.Next() )
726 anIterator.Current()->UpdateLocalCS( aDX, aDY );
729 void HYDROData_Document::UpdateLCSFields() const
731 if( myLX >= 0 && myLY >= 0 )
735 GetLocalCS( aLX, aLY );
736 HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
741 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
756 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
758 double X = thePnt.X();
759 double Y = thePnt.Y();
760 double Z = thePnt.Z();
761 Transform( X, Y, IsToLocalCS );
762 thePnt = gp_Pnt( X, Y, Z );
765 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
767 double X = thePnt.X();
768 double Y = thePnt.Y();
769 double Z = thePnt.Z();
770 Transform( X, Y, IsToLocalCS );
771 thePnt = gp_XYZ( X, Y, Z );
774 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
776 double X = thePnt.X();
777 double Y = thePnt.Y();
778 Transform( X, Y, IsToLocalCS );
779 thePnt = gp_XY( X, Y );