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