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