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>
27 #include <HYDROData_InterpolatorsFactory.h>
29 #include <TDataStd_Integer.hxx>
30 #include <TDataXtd_Position.hxx>
32 #include <TDF_Delta.hxx>
37 #include <QStringList>
38 #include <QTextStream>
40 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
41 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
43 #define PYTHON_DOC_NAME "hydro_doc"
45 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
47 static const int TAG_PROPS = 1; // general properties tag
48 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
49 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
50 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
51 static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
52 static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
56 typedef QMap<Standard_Integer,Handle_HYDROData_Entity> MapOfOrdered;
57 typedef QMap<QString,Handle_HYDROData_Entity> MapOfUnordered;
59 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
61 Handle(HYDROData_Document) aResult =
62 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
63 if (aResult.IsNull()) {
64 aResult = new HYDROData_Document();
65 HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
70 Handle(HYDROData_Document) HYDROData_Document::Document(
71 const TDF_Label& theObjectLabel )
73 Handle(HYDROData_Document) aResDoc;
74 if ( theObjectLabel.IsNull() )
77 Handle(TDocStd_Document) anObjDoc;
80 anObjDoc = TDocStd_Document::Get( theObjectLabel );
86 if ( anObjDoc.IsNull() )
89 HYDROData_Application* anApp = HYDROData_Application::GetApplication();
91 DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
92 for ( ; aMapIt.More(); aMapIt.Next() )
94 Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
95 if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
105 bool HYDROData_Document::HasDocument(const int theStudyID)
107 Handle(HYDROData_Document) aResult =
108 HYDROData_Application::GetApplication()->GetDocument(theStudyID);
109 return !aResult.IsNull();
112 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
115 return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
118 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
120 Handle(TDocStd_Document) aResult;
121 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
122 PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
125 aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
127 catch (Standard_Failure)
129 if (!aResult.IsNull()) {
130 aResult->SetUndoLimit(UNDO_LIMIT);
131 HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
134 Data_DocError anError;
137 anError = DocError_OK;
139 case PCDM_RS_NoDriver:
140 case PCDM_RS_UnknownFileDriver:
141 case PCDM_RS_NoSchema:
142 case PCDM_RS_DriverFailure:
143 case PCDM_RS_WrongResource:
144 anError = DocError_ResourcesProblem;
146 case PCDM_RS_OpenError:
147 case PCDM_RS_NoDocument:
148 case PCDM_RS_WrongStreamMode:
149 case PCDM_RS_PermissionDenied:
150 anError = DocError_CanNotOpen;
152 case PCDM_RS_NoVersion:
153 anError = DocError_InvalidVersion;
155 case PCDM_RS_ExtensionFailure:
156 case PCDM_RS_FormatFailure:
157 case PCDM_RS_TypeFailure:
158 case PCDM_RS_TypeNotFoundInSchema:
159 case PCDM_RS_UnrecognizedFileFormat:
160 anError = DocError_InvalidFormat;
162 case PCDM_RS_MakeFailure:
164 anError = DocError_UnknownProblem;
170 Data_DocError HYDROData_Document::Save(const char* theFileName)
172 TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
173 PCDM_StoreStatus aStatus;
175 aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
177 catch (Standard_Failure) {}
178 myTransactionsAfterSave = 0;
179 Standard::Purge(); // Release free memory
182 Data_DocError anError;
185 anError = DocError_OK;
187 case PCDM_SS_DriverFailure:
188 anError = DocError_ResourcesProblem;
190 case PCDM_SS_WriteFailure:
191 //case PCDM_SS_DiskWritingFailure:
192 //case PCDM_SS_UserRightsFailure:
193 anError = DocError_CanNotOpen;
196 anError = DocError_UnknownProblem;
202 void HYDROData_Document::Close()
205 HYDROData_Application::GetApplication()->RemoveDocument(this);
208 bool HYDROData_Document::DumpToPython( const QString& theFileName,
209 const bool theIsMultiFile ) const
211 // Try to open the file
212 QFile aFile( theFileName );
213 if ( !aFile.open( QIODevice::WriteOnly ) )
216 MapOfTreatedObjects aTreatedObjects;
218 // Dump header for python script
219 QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
220 if ( aHeaderDump.isEmpty() )
223 HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
227 // Dump the local CS data to Python
229 QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX ).arg( myLY );
232 HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
234 // Dump all model objects to Python script
235 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
236 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
237 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
238 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
239 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
240 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
241 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
242 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
243 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
244 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
245 aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
247 // Dump code to close python fuction
248 if ( aRes && theIsMultiFile )
250 QStringList aFooterScript;
251 aFooterScript << QString( "" );
252 aFooterScript << QString( " pass" );
253 HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
259 QString HYDROData_Document::GetDocPyName() const
261 QString aDocName = PYTHON_DOC_NAME;
265 if ( DocumentId( this, aDocId ) )
266 aDocName += "_" + QString::number( aDocId );
272 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
273 const bool theIsMultiFile ) const
275 QString aDocName = GetDocPyName();
277 // Append document in to the map of treated objects to prevent names overlaping
278 theTreatedObjects.insert( aDocName, this );
281 if ( !DocumentId( this, aDocId ) )
284 QStringList aResScript;
286 aResScript << QString( "from HYDROPy import *" );
287 aResScript << QString( "from PyQt4.QtCore import *" );
288 aResScript << QString( "from PyQt4.QtGui import *" );
290 if ( theIsMultiFile )
292 aResScript << QString( "import salome" );
293 aResScript << QString( "" );
294 aResScript << QString( "def RebuildData( theStudy ):" );
295 aResScript << QString( " %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
299 aResScript << QString( "" );
300 aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
306 bool HYDROData_Document::dumpPartitionToPython( QFile& theFile,
307 const bool theIsMultiFile,
308 MapOfTreatedObjects& theTreatedObjects,
309 const ObjectKind& theObjectKind ) const
311 if ( !theFile.isOpen() )
314 QTextStream anOutStream( &theFile );
318 HYDROData_Iterator anIterator( this, theObjectKind );
319 for( ; anIterator.More(); anIterator.Next() )
321 Handle(HYDROData_Entity) anObject = anIterator.Current();
322 if ( anObject.IsNull() )
325 QString anObjName = anObject->GetName();
326 if ( theTreatedObjects.contains( anObjName ) )
329 theTreatedObjects.insert( anObjName, anObject );
331 QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
333 if ( theIsMultiFile )
335 // For multifile dump we use the function, see the document dump header
336 QStringList::iterator anIt = anObjDump.begin();
337 for ( ; anIt != anObjDump.end(); ++anIt )
338 anIt->prepend( " " );
341 HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
347 bool takeLastDigits( QString& theStr, int& aRes )
352 for ( int i = theStr.length() - 1; i >= 0; --i )
354 const QChar& aChar = theStr.at( i );
355 if ( !aChar.isDigit() )
358 anStrNum.prepend( aChar );
361 if ( anStrNum.isEmpty() )
364 theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
365 aRes = anStrNum.toInt();
370 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
372 QString aStr1 = theStr1, aStr2 = theStr2;
374 int aNum1 = -1, aNum2 = -1;
375 if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
377 if ( aStr1 == aStr2 )
378 return aNum1 < aNum2;
381 return theStr1 < theStr2;
384 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
385 const Standard_Boolean theIsAll ) const
387 HYDROData_SequenceOfObjects anOrder;
389 MapOfOrdered aMapOfOrdered;
390 MapOfUnordered aMapOfUnordered;
392 HYDROData_Iterator anIter( this );
393 for ( ; anIter.More(); anIter.Next() )
395 Handle(HYDROData_Entity) anObject = anIter.Current();
396 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
399 Standard_Integer anObjZLevel = -1;
400 if ( anObject->GetZLevel( anObjZLevel ) )
402 aMapOfOrdered.insert( anObjZLevel, anObject );
406 QString anObjName = anObject->GetName();
407 if ( anObjName.isEmpty() )
410 aMapOfUnordered.insert( anObjName, anObject );
414 MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
415 for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
417 const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
418 anOrder.Prepend( anObject );
423 QStringList aSortedNames = aMapOfUnordered.keys();
424 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
426 for ( int i = 0; i < aSortedNames.length(); ++i )
428 QString anObjName = aSortedNames.value( i );
430 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
431 anOrder.Append( anObject );
438 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
440 // At first we remove previous model order
441 RemoveObjectsLayerOrder();
443 // Make new objects order
444 Standard_Integer aLevel = 0;
445 for ( int i = theOrder.Length(), n = 1; i >= n; --i )
447 const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
448 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
451 anObject->SetZLevel( aLevel++ );
455 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
457 HYDROData_SequenceOfObjects anOrder;
458 anOrder.Append( theObject );
462 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
464 MapOfUnordered aMapOfUnordered;
466 for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
468 const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
469 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
472 Standard_Integer anObjZLevel = -1;
473 if ( anObject->GetZLevel( anObjZLevel ) )
475 continue; // Skip objects that already have the z-level
479 QString anObjName = anObject->GetName();
480 if ( anObjName.isEmpty() )
483 aMapOfUnordered.insert( anObjName, anObject );
487 if ( aMapOfUnordered.isEmpty() )
488 return; // Nothing to show
490 Standard_Integer aTopId = 0;
492 HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
493 if ( !aModelOrder.IsEmpty() )
495 const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
496 anObject->GetZLevel( aTopId );
500 aTopId += aMapOfUnordered.size() - 1;
502 QStringList aSortedNames = aMapOfUnordered.keys();
503 qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
505 for ( int i = 0; i < aSortedNames.length(); ++i )
507 QString anObjName = aSortedNames.value( i );
509 const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
510 anObject->SetZLevel( aTopId-- );
514 void HYDROData_Document::RemoveObjectsLayerOrder()
516 HYDROData_Iterator anIter( this );
517 for ( ; anIter.More(); anIter.Next() )
519 Handle(HYDROData_Entity) anObject = anIter.Current();
520 if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
523 anObject->RemoveZLevel();
527 void HYDROData_Document::StartOperation()
532 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
534 if( !myDoc->CommitCommand() ) // it means that there were no modifications done
537 NewID(); // workaround: do something just to modify the document
538 myDoc->CommitCommand();
540 myTransactionsAfterSave++;
542 if( theName.Length() != 0 )
544 const TDF_DeltaList& aList = GetUndos();
545 if( !aList.IsEmpty() )
547 Handle(TDF_Delta) aDelta = aList.Last();
548 if( !aDelta.IsNull() )
549 aDelta->SetName( theName );
554 void HYDROData_Document::AbortOperation()
556 myDoc->AbortCommand();
559 bool HYDROData_Document::IsOperation()
561 return myDoc->HasOpenCommand() != 0;
564 bool HYDROData_Document::IsModified()
566 return myTransactionsAfterSave != 0;
569 bool HYDROData_Document::CanUndo()
571 return myDoc->GetAvailableUndos() > 0;
574 const TDF_DeltaList& HYDROData_Document::GetUndos()
576 return myDoc->GetUndos();
579 void HYDROData_Document::ClearUndos()
581 return myDoc->ClearUndos();
584 void HYDROData_Document::Undo()
587 myTransactionsAfterSave--;
590 bool HYDROData_Document::CanRedo()
592 return myDoc->GetAvailableRedos() > 0;
595 const TDF_DeltaList& HYDROData_Document::GetRedos()
597 return myDoc->GetRedos();
600 void HYDROData_Document::ClearRedos()
602 return myDoc->ClearRedos();
605 void HYDROData_Document::Redo()
608 myTransactionsAfterSave++;
611 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
613 return HYDROData_Iterator::CreateObject( this, theKind );
616 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName(
617 const QString& theName,
618 const ObjectKind theObjectKind ) const
620 Handle(HYDROData_Entity) anObject;
621 if ( theName.isEmpty() )
624 QStringList aNamesList;
625 aNamesList << theName;
627 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
628 if( aSeqOfObjs.IsEmpty() )
631 anObject = aSeqOfObjs.First();
635 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
636 const QStringList& theNames,
637 const ObjectKind theObjectKind ) const
639 HYDROData_SequenceOfObjects aResSeq;
641 QStringList aNamesList = theNames;
643 HYDROData_Iterator anIter( this, theObjectKind );
644 for( ; anIter.More(); anIter.Next() )
646 Handle(HYDROData_Entity) anObject = anIter.Current();
647 if( anObject.IsNull() )
650 QString anObjName = anObject->GetName();
651 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
654 aResSeq.Append( anObject );
656 aNamesList.removeAll( anObjName );
657 if ( aNamesList.isEmpty() )
664 HYDROData_Document::HYDROData_Document()
666 HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
667 myDoc->SetUndoLimit(UNDO_LIMIT);
668 NewID(); // needed to have at least one attribute in initial document to avoid errors
669 myTransactionsAfterSave = 0;
673 myInterpolatorsFactory = 0;
676 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
679 myTransactionsAfterSave = 0;
683 myInterpolatorsFactory = 0;
686 HYDROData_Document::~HYDROData_Document()
690 int HYDROData_Document::NewID()
692 TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
693 FindChild(TAG_PROPS_NEW_ID);
694 Handle(TDataStd_Integer) anInt;
695 if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
696 anInt = TDataStd_Integer::Set(anIDLab, 0);
698 // just increment value and return
699 anInt->Set(anInt->Get() + 1);
703 TDF_Label HYDROData_Document::LabelOfObjects()
705 return myDoc->Main().FindChild(TAG_OBJECTS);
708 TDF_Label HYDROData_Document::LabelOfLocalCS() const
710 return myDoc->Main().FindChild(TAG_LOCAL_CS);
713 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
715 TDF_Label aLocalCSLab = LabelOfLocalCS();
717 Handle( TDataXtd_Position ) aLocalCS;
718 if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
720 gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
721 theLX = aLocalCS3d.X();
722 theLY = aLocalCS3d.Y();
726 theLX = DEFAULT_LOCAL_CS.X();
727 theLY = DEFAULT_LOCAL_CS.Y();
731 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
735 // update the local CS data in attribute
736 TDF_Label aLocalCSLab = LabelOfLocalCS();
737 Handle( TDataXtd_Position ) aLocalCS;
738 if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
739 aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
741 gp_Pnt aLocalCS3d( theLX, theLY, 0 );
742 aLocalCS->SetPosition( aLocalCS3d );
744 // calculate delta for coordinates
745 double aDX = myLX - theLX;
746 double aDY = myLY - theLY;
748 // update the local CS data in internal fields
752 //update all objects in the document
753 HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
754 for( ; anIterator.More(); anIterator.Next() )
755 anIterator.Current()->UpdateLocalCS( aDX, aDY );
758 void HYDROData_Document::UpdateLCSFields() const
760 if( myLX >= 0 && myLY >= 0 )
764 GetLocalCS( aLX, aLY );
765 HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
770 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
785 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
787 double X = thePnt.X();
788 double Y = thePnt.Y();
789 double Z = thePnt.Z();
790 Transform( X, Y, IsToLocalCS );
791 thePnt = gp_Pnt( X, Y, Z );
794 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
796 double X = thePnt.X();
797 double Y = thePnt.Y();
798 double Z = thePnt.Z();
799 Transform( X, Y, IsToLocalCS );
800 thePnt = gp_XYZ( X, Y, Z );
803 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
805 double X = thePnt.X();
806 double Y = thePnt.Y();
807 Transform( X, Y, IsToLocalCS );
808 thePnt = gp_XY( X, Y );
811 HYDROData_InterpolatorsFactory* HYDROData_Document::GetInterpolatorsFactory()
813 if ( !myInterpolatorsFactory ) {
814 myInterpolatorsFactory = new HYDROData_InterpolatorsFactory();
817 return myInterpolatorsFactory;