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