1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <HYDROData_Document.h>
24 #include <HYDROData_Application.h>
25 #include <HYDROData_Iterator.h>
26 #include <HYDROData_Tool.h>
28 #include <TDataStd_Integer.hxx>
29 #include <TDataXtd_Position.hxx>
31 #include <TDF_Delta.hxx>
36 #include <QStringList>
37 #include <QTextStream>
39 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
40 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
42 #define PYTHON_DOC_NAME "hydro_doc"
44 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
46 static const int TAG_PROPS = 1; // general properties tag
47 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
48 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
49 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
50 static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
51 static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
55 typedef QMap<Standard_Integer,Handle_HYDROData_Entity> MapOfOrdered;
56 typedef QMap<QString,Handle_HYDROData_Entity> MapOfUnordered;
58 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
60 Handle(HYDROData_Document) aResult =
61 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
62 if (aResult.IsNull()) {
63 aResult = new HYDROData_Document();
64 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
69 Handle(HYDROData_Document) HYDROData_Document::Document(
70 const TDF_Label& theObjectLabel )
72 Handle(HYDROData_Document) aResDoc;
73 if ( theObjectLabel.IsNull() )
76 Handle(TDocStd_Document) anObjDoc;
79 anObjDoc = TDocStd_Document::Get( theObjectLabel );
85 if ( anObjDoc.IsNull() )
88 HYDROData_Application* anApp = HYDROData_Application::GetApplication();
90 DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
91 for ( ; aMapIt.More(); aMapIt.Next() )
93 Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
94 if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
104 bool HYDROData_Document::HasDocument(const int theStudyID)
106 Handle(HYDROData_Document) aResult =
107 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
108 return !aResult.IsNull();
111 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
114 return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
117 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
119 Handle(TDocStd_Document) aResult;
120 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
121 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
124 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
126 catch (Standard_Failure)
128 if (!aResult.IsNull()) {
129 aResult->SetUndoLimit(UNDO_LIMIT);
130 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
133 Data_DocError anError;
136 anError = DocError_OK;
138 case PCDM_RS_NoDriver:
139 case PCDM_RS_UnknownFileDriver:
140 case PCDM_RS_NoSchema:
141 case PCDM_RS_DriverFailure:
142 case PCDM_RS_WrongResource:
143 anError = DocError_ResourcesProblem;
145 case PCDM_RS_OpenError:
146 case PCDM_RS_NoDocument:
147 case PCDM_RS_WrongStreamMode:
148 case PCDM_RS_PermissionDenied:
149 anError = DocError_CanNotOpen;
151 case PCDM_RS_NoVersion:
152 anError = DocError_InvalidVersion;
154 case PCDM_RS_ExtensionFailure:
155 case PCDM_RS_FormatFailure:
156 case PCDM_RS_TypeFailure:
157 case PCDM_RS_TypeNotFoundInSchema:
158 case PCDM_RS_UnrecognizedFileFormat:
159 anError = DocError_InvalidFormat;
161 case PCDM_RS_MakeFailure:
163 anError = DocError_UnknownProblem;
169 Data_DocError HYDROData_Document::Save(const char* theFileName)
171 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
172 PCDM_StoreStatus aStatus;
174 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
176 catch (Standard_Failure) {}
177 myTransactionsAfterSave = 0;
178 Standard::Purge(); // Release free memory
181 Data_DocError anError;
184 anError = DocError_OK;
186 case PCDM_SS_DriverFailure:
187 anError = DocError_ResourcesProblem;
189 case PCDM_SS_WriteFailure:
190 //case PCDM_SS_DiskWritingFailure:
191 //case PCDM_SS_UserRightsFailure:
192 anError = DocError_CanNotOpen;
195 anError = DocError_UnknownProblem;
201 void HYDROData_Document::Close()
204 HYDROData_Application::GetApplication()->RemoveDocument(this);
207 bool HYDROData_Document::DumpToPython( const QString& theFileName,
208 const bool theIsMultiFile ) const
210 // Try to open the file
211 QFile aFile( theFileName );
212 if ( !aFile.open( QIODevice::WriteOnly ) )
215 MapOfTreatedObjects aTreatedObjects;
217 // Dump header for python script
218 QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
219 if ( aHeaderDump.isEmpty() )
222 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
226 // Dump the local CS data to Python
228 QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX ).arg( myLY );
231 HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
233 // Dump all model objects to Python script
234 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
235 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
236 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
237 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
238 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
239 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
240 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
241 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
242 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
243 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
244 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
246 // Dump code to close python fuction
247 if ( aRes && theIsMultiFile )
249 QStringList aFooterScript;
250 aFooterScript << QString( "" );
251 aFooterScript << QString( " pass" );
252 HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
258 QString HYDROData_Document::GetDocPyName() const
260 QString aDocName = PYTHON_DOC_NAME;
264 if ( DocumentId( this, aDocId ) )
265 aDocName += "_" + QString::number( aDocId );
271 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
272 const bool theIsMultiFile ) const
274 QString aDocName = GetDocPyName();
276 // Append document in to the map of treated objects to prevent names overlaping
277 theTreatedObjects.insert( aDocName, this );
280 if ( !DocumentId( this, aDocId ) )
283 QStringList aResScript;
285 aResScript << QString( "from HYDROPy import *" );
286 aResScript << QString( "from PyQt4.QtCore import *" );
287 aResScript << QString( "from PyQt4.QtGui import *" );
289 if ( theIsMultiFile )
291 aResScript << QString( "import salome" );
292 aResScript << QString( "" );
293 aResScript << QString( "def RebuildData( theStudy ):" );
294 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
298 aResScript << QString( "" );
299 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
305 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
306 const bool theIsMultiFile,
307 MapOfTreatedObjects& theTreatedObjects,
308 const ObjectKind& theObjectKind ) const
310 if ( !theFile.isOpen() )
313 QTextStream anOutStream( &theFile );
317 HYDROData_Iterator anIterator( this, theObjectKind );
318 for( ; anIterator.More(); anIterator.Next() )
320 Handle(HYDROData_Entity) anObject = anIterator.Current();
321 if ( anObject.IsNull() )
324 QString anObjName = anObject->GetName();
325 if ( theTreatedObjects.contains( anObjName ) )
328 theTreatedObjects.insert( anObjName, anObject );
330 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
332 if ( theIsMultiFile )
334 // For multifile dump we use the function, see the document dump header
335 QStringList::iterator anIt = anObjDump.begin();
336 for ( ; anIt != anObjDump.end(); ++anIt )
337 anIt->prepend( " " );
340 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
346 bool takeLastDigits( QString& theStr, int& aRes )
351 for ( int i = theStr.length() - 1; i >= 0; --i )
353 const QChar& aChar = theStr.at( i );
354 if ( !aChar.isDigit() )
357 anStrNum.prepend( aChar );
360 if ( anStrNum.isEmpty() )
363 theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
364 aRes = anStrNum.toInt();
369 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
371 QString aStr1 = theStr1, aStr2 = theStr2;
373 int aNum1 = -1, aNum2 = -1;
374 if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
376 if ( aStr1 == aStr2 )
377 return aNum1 < aNum2;
380 return theStr1 < theStr2;
383 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
384 const Standard_Boolean theIsAll ) const
386 HYDROData_SequenceOfObjects anOrder;
388 MapOfOrdered aMapOfOrdered;
389 MapOfUnordered aMapOfUnordered;
391 HYDROData_Iterator anIter( this );
392 for ( ; anIter.More(); anIter.Next() )
394 Handle(HYDROData_Entity) anObject = anIter.Current();
395 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
398 Standard_Integer anObjZLevel = -1;
399 if ( anObject->GetZLevel( anObjZLevel ) )
401 aMapOfOrdered.insert( anObjZLevel, anObject );
405 QString anObjName = anObject->GetName();
406 if ( anObjName.isEmpty() )
409 aMapOfUnordered.insert( anObjName, anObject );
413 MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
414 for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
416 const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
417 anOrder.Prepend( anObject );
422 QStringList aSortedNames = aMapOfUnordered.keys();
423 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
425 for ( int i = 0; i < aSortedNames.length(); ++i )
427 QString anObjName = aSortedNames.value( i );
429 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
430 anOrder.Append( anObject );
437 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
439 // At first we remove previous model order
440 RemoveObjectsLayerOrder();
442 // Make new objects order
443 Standard_Integer aLevel = 0;
444 for ( int i = theOrder.Length(), n = 1; i >= n; --i )
446 const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
447 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
450 anObject->SetZLevel( aLevel++ );
454 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
456 HYDROData_SequenceOfObjects anOrder;
457 anOrder.Append( theObject );
461 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
463 MapOfUnordered aMapOfUnordered;
465 for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
467 const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
468 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
471 Standard_Integer anObjZLevel = -1;
472 if ( anObject->GetZLevel( anObjZLevel ) )
474 continue; // Skip objects that already have the z-level
478 QString anObjName = anObject->GetName();
479 if ( anObjName.isEmpty() )
482 aMapOfUnordered.insert( anObjName, anObject );
486 if ( aMapOfUnordered.isEmpty() )
487 return; // Nothing to show
489 Standard_Integer aTopId = 0;
491 HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
492 if ( !aModelOrder.IsEmpty() )
494 const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
495 anObject->GetZLevel( aTopId );
499 aTopId += aMapOfUnordered.size() - 1;
501 QStringList aSortedNames = aMapOfUnordered.keys();
502 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
504 for ( int i = 0; i < aSortedNames.length(); ++i )
506 QString anObjName = aSortedNames.value( i );
508 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
509 anObject->SetZLevel( aTopId-- );
513 void HYDROData_Document::RemoveObjectsLayerOrder()
515 HYDROData_Iterator anIter( this );
516 for ( ; anIter.More(); anIter.Next() )
518 Handle(HYDROData_Entity) anObject = anIter.Current();
519 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
522 anObject->RemoveZLevel();
526 void HYDROData_Document::StartOperation()
531 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
533 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
536 NewID(); // workaround: do something just to modify the document
537 myDoc->CommitCommand();
539 myTransactionsAfterSave++;
541 if( theName.Length() != 0 )
543 const TDF_DeltaList& aList = GetUndos();
544 if( !aList.IsEmpty() )
546 Handle(TDF_Delta) aDelta = aList.Last();
547 if( !aDelta.IsNull() )
548 aDelta->SetName( theName );
553 void HYDROData_Document::AbortOperation()
555 myDoc->AbortCommand();
558 bool HYDROData_Document::IsOperation()
560 return myDoc->HasOpenCommand() != 0;
563 bool HYDROData_Document::IsModified()
565 return myTransactionsAfterSave != 0;
568 bool HYDROData_Document::CanUndo()
570 return myDoc->GetAvailableUndos() > 0;
573 const TDF_DeltaList& HYDROData_Document::GetUndos()
575 return myDoc->GetUndos();
578 void HYDROData_Document::ClearUndos()
580 return myDoc->ClearUndos();
583 void HYDROData_Document::Undo()
586 myTransactionsAfterSave--;
589 bool HYDROData_Document::CanRedo()
591 return myDoc->GetAvailableRedos() > 0;
594 const TDF_DeltaList& HYDROData_Document::GetRedos()
596 return myDoc->GetRedos();
599 void HYDROData_Document::ClearRedos()
601 return myDoc->ClearRedos();
604 void HYDROData_Document::Redo()
607 myTransactionsAfterSave++;
610 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
612 return HYDROData_Iterator::CreateObject( this, theKind );
615 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
616 const QString& theName,
617 const ObjectKind theObjectKind ) const
619 Handle(HYDROData_Entity) anObject;
620 if ( theName.isEmpty() )
623 QStringList aNamesList;
624 aNamesList << theName;
626 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
627 if( aSeqOfObjs.IsEmpty() )
630 anObject = aSeqOfObjs.First();
634 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
635 const QStringList& theNames,
636 const ObjectKind theObjectKind ) const
638 HYDROData_SequenceOfObjects aResSeq;
640 QStringList aNamesList = theNames;
642 HYDROData_Iterator anIter( this, theObjectKind );
643 for( ; anIter.More(); anIter.Next() )
645 Handle(HYDROData_Entity) anObject = anIter.Current();
646 if( anObject.IsNull() )
649 QString anObjName = anObject->GetName();
650 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
653 aResSeq.Append( anObject );
655 aNamesList.removeAll( anObjName );
656 if ( aNamesList.isEmpty() )
663 HYDROData_Document::HYDROData_Document()
665 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
666 myDoc->SetUndoLimit(UNDO_LIMIT);
667 NewID(); // needed to have at least one attribute in initial document to avoid errors
668 myTransactionsAfterSave = 0;
673 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
676 myTransactionsAfterSave = 0;
681 HYDROData_Document::~HYDROData_Document()
685 int HYDROData_Document::NewID()
687 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
688 FindChild(TAG_PROPS_NEW_ID);
689 Handle(TDataStd_Integer) anInt;
690 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
691 anInt = TDataStd_Integer::Set(anIDLab, 0);
693 // just increment value and return
694 anInt->Set(anInt->Get() + 1);
698 TDF_Label HYDROData_Document::LabelOfObjects()
700 return myDoc->Main().FindChild(TAG_OBJECTS);
703 TDF_Label HYDROData_Document::LabelOfLocalCS() const
705 return myDoc->Main().FindChild(TAG_LOCAL_CS);
708 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
710 TDF_Label aLocalCSLab = LabelOfLocalCS();
712 Handle( TDataXtd_Position ) aLocalCS;
713 if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
715 gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
716 theLX = aLocalCS3d.X();
717 theLY = aLocalCS3d.Y();
721 theLX = DEFAULT_LOCAL_CS.X();
722 theLY = DEFAULT_LOCAL_CS.Y();
726 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
730 // update the local CS data in attribute
731 TDF_Label aLocalCSLab = LabelOfLocalCS();
732 Handle( TDataXtd_Position ) aLocalCS;
733 if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
734 aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
736 gp_Pnt aLocalCS3d( theLX, theLY, 0 );
737 aLocalCS->SetPosition( aLocalCS3d );
739 // calculate delta for coordinates
740 double aDX = myLX - theLX;
741 double aDY = myLY - theLY;
743 // update the local CS data in internal fields
747 //update all objects in the document
748 HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
749 for( ; anIterator.More(); anIterator.Next() )
750 anIterator.Current()->UpdateLocalCS( aDX, aDY );
753 void HYDROData_Document::UpdateLCSFields() const
755 if( myLX >= 0 && myLY >= 0 )
759 GetLocalCS( aLX, aLY );
760 HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
765 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
780 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
782 double X = thePnt.X();
783 double Y = thePnt.Y();
784 double Z = thePnt.Z();
785 Transform( X, Y, IsToLocalCS );
786 thePnt = gp_Pnt( X, Y, Z );
789 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
791 double X = thePnt.X();
792 double Y = thePnt.Y();
793 double Z = thePnt.Z();
794 Transform( X, Y, IsToLocalCS );
795 thePnt = gp_XYZ( X, Y, Z );
798 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
800 double X = thePnt.X();
801 double Y = thePnt.Y();
802 Transform( X, Y, IsToLocalCS );
803 thePnt = gp_XY( X, Y );