Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROData / HYDROData_Document.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROData_Document.h>
20 #include <HYDROData_Application.h>
21 #include <HYDROData_Iterator.h>
22 #include <HYDROData_Tool.h>
23 #include <HYDROData_InterpolatorsFactory.h>
24
25 #include <TDataStd_Real.hxx>
26 #include <TDataStd_Integer.hxx>
27 #include <TDataXtd_Position.hxx>
28
29 #include <TDF_Delta.hxx>
30
31 #include <gp_Pnt.hxx>
32
33 #include <QFile>
34 #include <QStringList>
35 #include <QTextStream>
36
37 IMPLEMENT_STANDARD_HANDLE(HYDROData_Document,MMgt_TShared)
38 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Document,MMgt_TShared)
39
40 #define PYTHON_DOC_NAME "hydro_doc"
41
42 static const int UNDO_LIMIT = 10; // number of possible undo operations in the module
43
44 static const int TAG_PROPS = 1; // general properties tag
45 static const int TAG_PROPS_NEW_ID = 1; // general properties: tag for storage of the new object ID
46 static const int TAG_OBJECTS = 2; // tag of the objects sub-tree
47 static const int TAG_HISTORY = 3; // tag of the history sub-tree (Root for History)
48 static const int TAG_LOCAL_CS = 4; // tag of local coordinate system information
49 static const int TAG_DEF_STRICKLER_COEFF = 5; // tag of default strickler coefficient
50 static const gp_Pnt2d DEFAULT_LOCAL_CS( 0, 0 );
51
52 using namespace std;
53
54 typedef QMap<Standard_Integer,Handle_HYDROData_Entity> MapOfOrdered;
55 typedef QMap<QString,Handle_HYDROData_Entity> MapOfUnordered;
56
57 Handle(HYDROData_Document) HYDROData_Document::Document(const int theStudyID)
58 {
59   Handle(HYDROData_Document) aResult = 
60     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
61   if (aResult.IsNull()) {
62     aResult = new HYDROData_Document();
63     HYDROData_Application::GetApplication()->AddDocument(theStudyID, aResult);
64   }
65   return aResult;
66 }
67
68 Handle(HYDROData_Document) HYDROData_Document::Document(
69   const TDF_Label& theObjectLabel )
70 {
71   Handle(HYDROData_Document) aResDoc;
72   if ( theObjectLabel.IsNull() )
73     return aResDoc;
74
75   Handle(TDocStd_Document) anObjDoc;
76   try
77   {
78     anObjDoc = TDocStd_Document::Get( theObjectLabel );
79   }
80   catch( ... )
81   {
82   }
83
84   if ( anObjDoc.IsNull() )
85     return aResDoc;
86
87   HYDROData_Application* anApp = HYDROData_Application::GetApplication();
88
89   DataMapOfStudyIDDocument::Iterator aMapIt( anApp->myDocuments );
90   for ( ; aMapIt.More(); aMapIt.Next() )
91   {
92     Handle(HYDROData_Document) anAppDoc = aMapIt.Value();
93     if ( anAppDoc.IsNull() || anAppDoc->myDoc != anObjDoc )
94       continue;
95
96     aResDoc = anAppDoc;
97     break;
98   }
99
100   return aResDoc;
101 }
102
103 bool HYDROData_Document::HasDocument(const int theStudyID)
104 {
105   Handle(HYDROData_Document) aResult = 
106     HYDROData_Application::GetApplication()->GetDocument(theStudyID);
107   return !aResult.IsNull();
108 }
109
110 bool HYDROData_Document::DocumentId(const Handle(HYDROData_Document)& theDocument,
111                                     int&                              theDocId )
112 {
113   return HYDROData_Application::GetApplication()->GetDocumentId(theDocument, theDocId);
114 }
115
116 Data_DocError HYDROData_Document::Load(const char* theFileName, const int theStudyID)
117 {
118   Handle(TDocStd_Document) aResult;
119   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
120   PCDM_ReaderStatus aStatus = (PCDM_ReaderStatus) -1;
121   try
122   {
123     aStatus = HYDROData_Application::GetApplication()->Open (aPath, aResult);
124   }
125   catch (Standard_Failure)
126   {}
127   if (!aResult.IsNull()) {
128     aResult->SetUndoLimit(UNDO_LIMIT);
129     HYDROData_Application::GetApplication()->AddDocument(theStudyID, new HYDROData_Document(aResult));
130   }
131   // recognize error
132   Data_DocError anError;
133   switch(aStatus) {
134   case PCDM_RS_OK:
135     anError = DocError_OK;
136     break;
137   case PCDM_RS_NoDriver:
138   case PCDM_RS_UnknownFileDriver:
139   case PCDM_RS_NoSchema:
140   case PCDM_RS_DriverFailure:
141   case PCDM_RS_WrongResource:
142     anError = DocError_ResourcesProblem;
143     break;
144   case PCDM_RS_OpenError:
145   case PCDM_RS_NoDocument:
146   case PCDM_RS_WrongStreamMode:
147   case PCDM_RS_PermissionDenied:
148     anError = DocError_CanNotOpen;
149     break;
150   case PCDM_RS_NoVersion:
151     anError = DocError_InvalidVersion;
152     break;
153   case PCDM_RS_ExtensionFailure:
154   case PCDM_RS_FormatFailure:
155   case PCDM_RS_TypeFailure:
156   case PCDM_RS_TypeNotFoundInSchema:
157   case PCDM_RS_UnrecognizedFileFormat:
158     anError = DocError_InvalidFormat;
159     break;
160   case PCDM_RS_MakeFailure:
161   default:
162     anError = DocError_UnknownProblem;
163     break;
164   }
165   return anError;
166 }
167
168 Data_DocError HYDROData_Document::Save(const char* theFileName)
169 {
170   TCollection_ExtendedString aPath ((const Standard_CString)theFileName);
171   PCDM_StoreStatus aStatus;
172   try {
173     aStatus = HYDROData_Application::GetApplication()->SaveAs (myDoc, aPath);
174   }
175   catch (Standard_Failure) {}
176   myTransactionsAfterSave = 0;
177   Standard::Purge(); // Release free memory
178
179   // recognize error
180   Data_DocError anError;
181   switch(aStatus) {
182   case PCDM_SS_OK:
183     anError = DocError_OK;
184     break;
185   case PCDM_SS_DriverFailure:
186     anError = DocError_ResourcesProblem;
187     break;
188   case PCDM_SS_WriteFailure:
189   //case PCDM_SS_DiskWritingFailure:
190   //case PCDM_SS_UserRightsFailure:
191     anError = DocError_CanNotOpen;
192     break;
193   default:
194     anError = DocError_UnknownProblem;
195     break;
196   }
197   return anError;
198 }
199
200 void HYDROData_Document::Close()
201 {
202   myDoc->Close();
203   HYDROData_Application::GetApplication()->RemoveDocument(this);
204 }
205
206 double HYDROData_Document::GetDefaultStricklerCoefficient() const
207 {
208     double aRes = 0;
209     TDF_Label aLabel = myDoc->Main().FindChild(TAG_DEF_STRICKLER_COEFF, Standard_False);
210     if ( !aLabel.IsNull() )
211     {
212         Handle(TDataStd_Real) anAttr;
213         if ( aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
214             aRes = anAttr->Get();
215     }
216
217     return aRes;
218 }
219
220 void HYDROData_Document::SetDefaultStricklerCoefficient( double theCoeff ) const
221 {
222     TDF_Label aLabel = myDoc->Main().FindChild(TAG_DEF_STRICKLER_COEFF);
223     if ( !aLabel.IsNull() )
224     {
225         Handle(TDataStd_Real) anAttr;
226         if ( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
227             aLabel.AddAttribute( anAttr = new TDataStd_Real() );
228         anAttr->Set( theCoeff );
229     }
230 }
231
232 bool HYDROData_Document::DumpToPython( const QString& theFileName,
233                                        const bool     theIsMultiFile ) const
234 {
235   // Try to open the file
236   QFile aFile( theFileName );
237   if ( !aFile.open( QIODevice::WriteOnly ) )
238     return false;
239
240   MapOfTreatedObjects aTreatedObjects;
241
242   // Dump header for python script
243   QStringList aHeaderDump = DumpToPython( aTreatedObjects, theIsMultiFile );
244   if ( aHeaderDump.isEmpty() )
245     return false;
246
247   HYDROData_Tool::WriteStringsToFile( aFile, aHeaderDump );
248
249   bool aRes = true;
250
251   // Dump the local CS data to Python 
252   UpdateLCSFields();
253   QString aLCS = QString( "%1.SetLocalCS( %2, %3 )" ).arg( GetDocPyName() ).arg( myLX ).arg( myLY );
254   if( theIsMultiFile )
255     aLCS.prepend( "  " );
256   HYDROData_Tool::WriteStringsToFile( aFile, QStringList() << aLCS );
257
258   // Dump all model objects to Python script
259   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMAGE );
260   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STRICKLER_TABLE );
261   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINEXY );
262   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_BATHYMETRY );
263   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_PROFILE );
264   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_POLYLINE );
265   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_IMMERSIBLE_ZONE );
266   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_STREAM );
267   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CHANNEL );
268   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_DIGUE );
269   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_OBSTACLE );
270   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_LAND_COVER_MAP );
271   aRes = aRes && dumpPartitionToPython( aFile, theIsMultiFile, aTreatedObjects, KIND_CALCULATION );
272
273   // Dump code to close python fuction
274   if ( aRes && theIsMultiFile )
275   {
276     QStringList aFooterScript;
277     aFooterScript << QString( "" );
278     aFooterScript << QString( "  pass" );
279     HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
280   }
281
282   return aRes;
283 }
284
285 QString HYDROData_Document::GetDocPyName() const
286 {
287   QString aDocName = PYTHON_DOC_NAME;
288   
289   /*
290   int aDocId = 1;
291   if ( DocumentId( this, aDocId ) )
292     aDocName += "_" + QString::number( aDocId );
293   */
294   
295   return aDocName;
296 }
297
298 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
299                                               const bool           theIsMultiFile ) const
300 {
301   QString aDocName = GetDocPyName();
302
303   // Append document in to the map of treated objects to prevent names overlaping
304   theTreatedObjects.insert( aDocName, this );
305
306   int aDocId = 1;
307   if ( !DocumentId( this, aDocId ) )
308     aDocId = 1;
309
310   QStringList aResScript;
311
312   aResScript << QString( "from HYDROPy import *" );
313   aResScript << QString( "from PyQt4.QtCore import *" );
314   aResScript << QString( "from PyQt4.QtGui import *" );
315
316   if ( theIsMultiFile )
317   {
318     aResScript << QString( "import salome" );
319     aResScript << QString( "" );
320     aResScript << QString( "def RebuildData( theStudy ):" );
321     aResScript << QString( "  %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
322   }
323   else
324   {
325     aResScript << QString( "" );
326     aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
327   }
328
329   return aResScript;
330 }
331
332 bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
333                                                 const bool           theIsMultiFile,
334                                                 MapOfTreatedObjects& theTreatedObjects,
335                                                 const ObjectKind&    theObjectKind ) const
336 {
337   if ( !theFile.isOpen() )
338     return false;
339
340   QTextStream anOutStream( &theFile );
341
342   bool aRes = true;
343
344   HYDROData_Iterator anIterator( this, theObjectKind );
345   for( ; anIterator.More(); anIterator.Next() )
346   {
347     Handle(HYDROData_Entity) anObject = anIterator.Current();
348     if ( anObject.IsNull() )
349       continue;
350
351     QString anObjName = anObject->GetName();
352     if ( theTreatedObjects.contains( anObjName ) )
353       continue;
354
355     theTreatedObjects.insert( anObjName, anObject );
356
357     QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
358
359     if ( theIsMultiFile )
360     {
361       // For multifile dump we use the function, see the document dump header
362       QStringList::iterator anIt = anObjDump.begin();
363       for ( ; anIt != anObjDump.end(); ++anIt )
364         anIt->prepend( "  " );
365     }
366     
367     HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
368   }
369   
370   return aRes;
371 }
372
373 bool takeLastDigits( QString& theStr, int& aRes )
374 {
375   aRes = -1;
376
377   QString anStrNum;
378   for ( int i = theStr.length() - 1; i >= 0; --i )
379   {
380     const QChar& aChar = theStr.at( i );
381     if ( !aChar.isDigit() )
382       break;
383
384     anStrNum.prepend( aChar );
385   }
386
387   if ( anStrNum.isEmpty() )
388     return false;
389
390   theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
391   aRes = anStrNum.toInt();
392
393   return true;
394 }
395
396 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
397 {
398   QString aStr1 = theStr1, aStr2 = theStr2;
399
400   int aNum1 = -1, aNum2 = -1;
401   if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
402   {
403     if ( aStr1 == aStr2 )
404       return aNum1 < aNum2;
405   }
406
407   return theStr1 < theStr2;
408 }
409
410 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
411   const Standard_Boolean theIsAll ) const
412 {
413   HYDROData_SequenceOfObjects anOrder;
414
415   MapOfOrdered   aMapOfOrdered;
416   MapOfUnordered aMapOfUnordered;
417
418   HYDROData_Iterator anIter( this );
419   for ( ; anIter.More(); anIter.Next() )
420   {
421     Handle(HYDROData_Entity) anObject = anIter.Current();
422     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
423       continue;
424
425     Standard_Integer anObjZLevel = -1;
426     if ( anObject->GetZLevel( anObjZLevel ) )
427     {
428       aMapOfOrdered.insert( anObjZLevel, anObject );
429     }
430     else
431     {
432       QString anObjName = anObject->GetName();
433       if ( anObjName.isEmpty() )
434         continue;
435
436       aMapOfUnordered.insert( anObjName, anObject );
437     }
438   }
439
440   MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
441   for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
442   {
443     const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
444     anOrder.Prepend( anObject );
445   }
446
447   if ( theIsAll )
448   {
449     QStringList aSortedNames = aMapOfUnordered.keys();
450     qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
451
452     for ( int i = 0; i < aSortedNames.length(); ++i )
453     {
454       QString anObjName = aSortedNames.value( i );
455
456       const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
457       anOrder.Append( anObject );
458     }
459   }
460
461   return anOrder;
462 }
463
464 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
465 {
466   // At first we remove previous model order
467   RemoveObjectsLayerOrder();
468
469   // Make new objects order
470   Standard_Integer aLevel = 0;
471   for ( int i = theOrder.Length(), n = 1; i >= n; --i )
472   {
473     const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
474     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
475       continue;
476
477     anObject->SetZLevel( aLevel++ );
478   }
479 }
480
481 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
482 {
483   HYDROData_SequenceOfObjects anOrder;
484   anOrder.Append( theObject );
485   Show( anOrder );
486 }
487
488 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
489 {
490   MapOfUnordered aMapOfUnordered;
491
492   for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
493   {
494     const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
495     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
496       continue;
497
498     Standard_Integer anObjZLevel = -1;
499     if ( anObject->GetZLevel( anObjZLevel ) )
500     {
501       continue; // Skip objects that already have the z-level
502     }
503     else
504     {
505       QString anObjName = anObject->GetName();
506       if ( anObjName.isEmpty() )
507         continue;
508
509       aMapOfUnordered.insert( anObjName, anObject );
510     }
511   }
512
513   if ( aMapOfUnordered.isEmpty() )
514     return; // Nothing to show
515
516   Standard_Integer aTopId = 0;
517
518   HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
519   if ( !aModelOrder.IsEmpty() )
520   {
521     const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
522     anObject->GetZLevel( aTopId );
523     aTopId++;
524   }
525
526   aTopId += aMapOfUnordered.size() - 1;
527
528   QStringList aSortedNames = aMapOfUnordered.keys();
529   qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
530
531   for ( int i = 0; i < aSortedNames.length(); ++i )
532   {
533     QString anObjName = aSortedNames.value( i );
534
535     const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
536     anObject->SetZLevel( aTopId-- );
537   }
538 }
539
540 void HYDROData_Document::RemoveObjectsLayerOrder()
541 {
542   HYDROData_Iterator anIter( this );
543   for ( ; anIter.More(); anIter.Next() )
544   {
545     Handle(HYDROData_Entity) anObject = anIter.Current();
546     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
547       continue;
548
549     anObject->RemoveZLevel();
550   }
551 }
552
553 void HYDROData_Document::StartOperation()
554 {
555   myDoc->NewCommand();
556 }
557
558 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
559 {
560   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
561   {
562     myDoc->NewCommand();
563     NewID(); // workaround: do something just to modify the document
564     myDoc->CommitCommand();
565   }
566   myTransactionsAfterSave++;
567
568   if( theName.Length() != 0 )
569   {
570     const TDF_DeltaList& aList = GetUndos();
571     if( !aList.IsEmpty() )
572     {
573       Handle(TDF_Delta) aDelta = aList.Last();
574       if( !aDelta.IsNull() )
575         aDelta->SetName( theName );
576     }
577   }
578 }
579
580 void HYDROData_Document::AbortOperation()
581 {
582   myDoc->AbortCommand();
583 }
584
585 bool HYDROData_Document::IsOperation()
586 {
587   return myDoc->HasOpenCommand() != 0;
588 }
589
590 bool HYDROData_Document::IsModified()
591 {
592   return myTransactionsAfterSave != 0;
593 }
594
595 bool HYDROData_Document::CanUndo()
596 {
597   return myDoc->GetAvailableUndos() > 0;
598 }
599
600 const TDF_DeltaList& HYDROData_Document::GetUndos()
601 {
602   return myDoc->GetUndos();
603 }
604
605 void HYDROData_Document::ClearUndos()
606 {
607   return myDoc->ClearUndos();
608 }
609
610 void HYDROData_Document::Undo()
611 {
612   myDoc->Undo();
613   myTransactionsAfterSave--;
614 }
615
616 bool HYDROData_Document::CanRedo()
617 {
618   return myDoc->GetAvailableRedos() > 0;
619 }
620
621 const TDF_DeltaList& HYDROData_Document::GetRedos()
622 {
623   return myDoc->GetRedos();
624 }
625
626 void HYDROData_Document::ClearRedos()
627 {
628   return myDoc->ClearRedos();
629 }
630
631 void HYDROData_Document::Redo()
632 {
633   myDoc->Redo();
634   myTransactionsAfterSave++;
635 }
636
637 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
638 {
639   return HYDROData_Iterator::CreateObject( this, theKind );
640 }
641
642 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName( 
643   const QString&   theName,
644   const ObjectKind theObjectKind ) const
645 {
646   Handle(HYDROData_Entity) anObject;
647   if ( theName.isEmpty() )
648     return anObject;
649
650   QStringList aNamesList;
651   aNamesList << theName;
652
653   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
654   if( aSeqOfObjs.IsEmpty() )
655     return anObject;
656   
657   anObject = aSeqOfObjs.First();
658   return anObject;
659 }
660
661 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
662   const QStringList& theNames, 
663   const ObjectKind   theObjectKind ) const
664 {
665   HYDROData_SequenceOfObjects aResSeq;
666
667   QStringList aNamesList = theNames;
668
669   HYDROData_Iterator anIter( this, theObjectKind );
670   for( ; anIter.More(); anIter.Next() )
671   {
672     Handle(HYDROData_Entity) anObject = anIter.Current();
673     if( anObject.IsNull() )
674       continue;
675
676     QString anObjName = anObject->GetName();
677     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
678       continue;
679
680     aResSeq.Append( anObject );
681
682     aNamesList.removeAll( anObjName );
683     if ( aNamesList.isEmpty() )
684       break;
685   }
686
687   return aResSeq;
688 }
689
690 HYDROData_Document::HYDROData_Document()
691 {
692   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
693   myDoc->SetUndoLimit(UNDO_LIMIT);
694   NewID(); // needed to have at least one attribute in initial document to avoid errors
695   myTransactionsAfterSave = 0;
696   myLX = -1;
697   myLY = -1;
698
699   myInterpolatorsFactory = 0;
700 }
701
702 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
703 {
704   myDoc = theDoc;
705   myTransactionsAfterSave = 0;
706   myLX = -1;
707   myLY = -1;
708
709   myInterpolatorsFactory = 0;
710 }
711
712 HYDROData_Document::~HYDROData_Document()
713 {
714 }
715
716 int HYDROData_Document::NewID()
717 {
718   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
719     FindChild(TAG_PROPS_NEW_ID);
720   Handle(TDataStd_Integer) anInt;
721   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
722     anInt = TDataStd_Integer::Set(anIDLab, 0);
723   }
724   // just increment value and return
725   anInt->Set(anInt->Get() + 1);
726   return anInt->Get();
727 }
728
729 TDF_Label HYDROData_Document::LabelOfObjects()
730 {
731   return myDoc->Main().FindChild(TAG_OBJECTS);
732 }
733
734 TDF_Label HYDROData_Document::LabelOfLocalCS() const
735 {
736   return myDoc->Main().FindChild(TAG_LOCAL_CS);
737 }
738
739 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
740 {
741   TDF_Label aLocalCSLab = LabelOfLocalCS();
742
743   Handle( TDataXtd_Position ) aLocalCS;
744   if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
745   {
746     gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
747     theLX = aLocalCS3d.X();
748     theLY = aLocalCS3d.Y();
749   }
750   else
751   {
752     theLX = DEFAULT_LOCAL_CS.X();
753     theLY = DEFAULT_LOCAL_CS.Y();
754   }
755 }
756
757 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
758 {
759   UpdateLCSFields();
760
761   // update the local CS data in attribute
762   TDF_Label aLocalCSLab = LabelOfLocalCS();
763   Handle( TDataXtd_Position ) aLocalCS;
764   if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
765     aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
766
767   gp_Pnt aLocalCS3d( theLX, theLY, 0 );
768   aLocalCS->SetPosition( aLocalCS3d );
769
770   // calculate delta for coordinates
771   double aDX = myLX - theLX;
772   double aDY = myLY - theLY;
773
774   // update the local CS data in internal fields
775   myLX = theLX;
776   myLY = theLY;
777
778   //update all objects in the document
779   HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
780   for( ; anIterator.More(); anIterator.Next() )
781     anIterator.Current()->UpdateLocalCS( aDX, aDY );
782 }
783
784 void HYDROData_Document::UpdateLCSFields() const
785 {
786   if( myLX >= 0 && myLY >= 0 )
787     return;
788
789   double aLX, aLY;
790   GetLocalCS( aLX, aLY );
791   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
792   aThat->myLX = aLX;
793   aThat->myLY = aLY;
794 }
795
796 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
797 {
798   UpdateLCSFields();
799   if( IsToLocalCS )
800   {
801     X -= myLX;
802     Y -= myLY;
803   }
804   else
805   {
806     X += myLX;
807     Y += myLY;
808   }
809 }
810
811 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
812 {
813   double X = thePnt.X();
814   double Y = thePnt.Y();
815   double Z = thePnt.Z();
816   Transform( X, Y, IsToLocalCS );
817   thePnt = gp_Pnt( X, Y, Z ); 
818 }
819
820 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
821 {
822   double X = thePnt.X();
823   double Y = thePnt.Y();
824   double Z = thePnt.Z();
825   Transform( X, Y, IsToLocalCS );
826   thePnt = gp_XYZ( X, Y, Z ); 
827 }
828
829 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
830 {
831   double X = thePnt.X();
832   double Y = thePnt.Y();
833   Transform( X, Y, IsToLocalCS );
834   thePnt = gp_XY( X, Y ); 
835 }
836
837 HYDROData_InterpolatorsFactory* HYDROData_Document::GetInterpolatorsFactory()
838 {
839   if ( !myInterpolatorsFactory ) {
840     myInterpolatorsFactory = new HYDROData_InterpolatorsFactory();
841   }
842
843   return myInterpolatorsFactory;
844 }
845
846 HYDROData_IProfilesInterpolator* HYDROData_Document::GetInterpolator( const TCollection_AsciiString& theName ) const
847 {
848   HYDROData_IProfilesInterpolator* anInterpolator = NULL;
849
850   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
851   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
852   if ( aFactory ) {
853     anInterpolator = aFactory->GetInterpolator( theName );
854   }
855
856   return anInterpolator;
857 }
858  
859 NCollection_Sequence<TCollection_AsciiString> HYDROData_Document::GetInterpolatorNames() const
860 {
861   NCollection_Sequence<TCollection_AsciiString> aNames;
862
863   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
864   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
865   if ( aFactory ) {
866     aNames = aFactory->GetInterpolatorNames();
867   }
868
869   return aNames;
870 }