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_CALCULATION );
271
272   // Dump code to close python fuction
273   if ( aRes && theIsMultiFile )
274   {
275     QStringList aFooterScript;
276     aFooterScript << QString( "" );
277     aFooterScript << QString( "  pass" );
278     HYDROData_Tool::WriteStringsToFile( aFile, aFooterScript );
279   }
280
281   return aRes;
282 }
283
284 QString HYDROData_Document::GetDocPyName() const
285 {
286   QString aDocName = PYTHON_DOC_NAME;
287   
288   /*
289   int aDocId = 1;
290   if ( DocumentId( this, aDocId ) )
291     aDocName += "_" + QString::number( aDocId );
292   */
293   
294   return aDocName;
295 }
296
297 QStringList HYDROData_Document::DumpToPython( MapOfTreatedObjects& theTreatedObjects,
298                                               const bool           theIsMultiFile ) const
299 {
300   QString aDocName = GetDocPyName();
301
302   // Append document in to the map of treated objects to prevent names overlaping
303   theTreatedObjects.insert( aDocName, this );
304
305   int aDocId = 1;
306   if ( !DocumentId( this, aDocId ) )
307     aDocId = 1;
308
309   QStringList aResScript;
310
311   aResScript << QString( "from HYDROPy import *" );
312   aResScript << QString( "from PyQt4.QtCore import *" );
313   aResScript << QString( "from PyQt4.QtGui import *" );
314
315   if ( theIsMultiFile )
316   {
317     aResScript << QString( "import salome" );
318     aResScript << QString( "" );
319     aResScript << QString( "def RebuildData( theStudy ):" );
320     aResScript << QString( "  %1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
321   }
322   else
323   {
324     aResScript << QString( "" );
325     aResScript << QString( "%1 = HYDROData_Document.Document( theStudy._get_StudyId() );" ).arg( aDocName );
326   }
327
328   return aResScript;
329 }
330
331 bool HYDROData_Document::dumpPartitionToPython( QFile&               theFile,
332                                                 const bool           theIsMultiFile,
333                                                 MapOfTreatedObjects& theTreatedObjects,
334                                                 const ObjectKind&    theObjectKind ) const
335 {
336   if ( !theFile.isOpen() )
337     return false;
338
339   QTextStream anOutStream( &theFile );
340
341   bool aRes = true;
342
343   HYDROData_Iterator anIterator( this, theObjectKind );
344   for( ; anIterator.More(); anIterator.Next() )
345   {
346     Handle(HYDROData_Entity) anObject = anIterator.Current();
347     if ( anObject.IsNull() )
348       continue;
349
350     QString anObjName = anObject->GetName();
351     if ( theTreatedObjects.contains( anObjName ) )
352       continue;
353
354     theTreatedObjects.insert( anObjName, anObject );
355
356     QStringList anObjDump = anObject->DumpToPython( theTreatedObjects );
357
358     if ( theIsMultiFile )
359     {
360       // For multifile dump we use the function, see the document dump header
361       QStringList::iterator anIt = anObjDump.begin();
362       for ( ; anIt != anObjDump.end(); ++anIt )
363         anIt->prepend( "  " );
364     }
365     
366     HYDROData_Tool::WriteStringsToFile( theFile, anObjDump );
367   }
368   
369   return aRes;
370 }
371
372 bool takeLastDigits( QString& theStr, int& aRes )
373 {
374   aRes = -1;
375
376   QString anStrNum;
377   for ( int i = theStr.length() - 1; i >= 0; --i )
378   {
379     const QChar& aChar = theStr.at( i );
380     if ( !aChar.isDigit() )
381       break;
382
383     anStrNum.prepend( aChar );
384   }
385
386   if ( anStrNum.isEmpty() )
387     return false;
388
389   theStr.remove( theStr.length() - anStrNum.length(), anStrNum.length() );
390   aRes = anStrNum.toInt();
391
392   return true;
393 }
394
395 bool isObjectNameLessThan( const QString& theStr1, const QString& theStr2 )
396 {
397   QString aStr1 = theStr1, aStr2 = theStr2;
398
399   int aNum1 = -1, aNum2 = -1;
400   if ( takeLastDigits( aStr1, aNum1 ) && takeLastDigits( aStr2, aNum2 ) )
401   {
402     if ( aStr1 == aStr2 )
403       return aNum1 < aNum2;
404   }
405
406   return theStr1 < theStr2;
407 }
408
409 HYDROData_SequenceOfObjects HYDROData_Document::GetObjectsLayerOrder(
410   const Standard_Boolean theIsAll ) const
411 {
412   HYDROData_SequenceOfObjects anOrder;
413
414   MapOfOrdered   aMapOfOrdered;
415   MapOfUnordered aMapOfUnordered;
416
417   HYDROData_Iterator anIter( this );
418   for ( ; anIter.More(); anIter.Next() )
419   {
420     Handle(HYDROData_Entity) anObject = anIter.Current();
421     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
422       continue;
423
424     Standard_Integer anObjZLevel = -1;
425     if ( anObject->GetZLevel( anObjZLevel ) )
426     {
427       aMapOfOrdered.insert( anObjZLevel, anObject );
428     }
429     else
430     {
431       QString anObjName = anObject->GetName();
432       if ( anObjName.isEmpty() )
433         continue;
434
435       aMapOfUnordered.insert( anObjName, anObject );
436     }
437   }
438
439   MapOfOrdered::const_iterator anOrderedMapIt = aMapOfOrdered.constBegin();
440   for ( ; anOrderedMapIt != aMapOfOrdered.constEnd(); anOrderedMapIt++ )
441   {
442     const Handle(HYDROData_Entity)& anObject = anOrderedMapIt.value();
443     anOrder.Prepend( anObject );
444   }
445
446   if ( theIsAll )
447   {
448     QStringList aSortedNames = aMapOfUnordered.keys();
449     qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
450
451     for ( int i = 0; i < aSortedNames.length(); ++i )
452     {
453       QString anObjName = aSortedNames.value( i );
454
455       const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
456       anOrder.Append( anObject );
457     }
458   }
459
460   return anOrder;
461 }
462
463 void HYDROData_Document::SetObjectsLayerOrder( const HYDROData_SequenceOfObjects& theOrder )
464 {
465   // At first we remove previous model order
466   RemoveObjectsLayerOrder();
467
468   // Make new objects order
469   Standard_Integer aLevel = 0;
470   for ( int i = theOrder.Length(), n = 1; i >= n; --i )
471   {
472     const Handle(HYDROData_Entity)& anObject = theOrder.Value( i );
473     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
474       continue;
475
476     anObject->SetZLevel( aLevel++ );
477   }
478 }
479
480 void HYDROData_Document::Show( const Handle_HYDROData_Entity& theObject )
481 {
482   HYDROData_SequenceOfObjects anOrder;
483   anOrder.Append( theObject );
484   Show( anOrder );
485 }
486
487 void HYDROData_Document::Show( const HYDROData_SequenceOfObjects& theObjects )
488 {
489   MapOfUnordered aMapOfUnordered;
490
491   for ( int i = 1, n = theObjects.Length(); i <= n; ++i )
492   {
493     const Handle(HYDROData_Entity)& anObject = theObjects.Value( i );
494     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
495       continue;
496
497     Standard_Integer anObjZLevel = -1;
498     if ( anObject->GetZLevel( anObjZLevel ) )
499     {
500       continue; // Skip objects that already have the z-level
501     }
502     else
503     {
504       QString anObjName = anObject->GetName();
505       if ( anObjName.isEmpty() )
506         continue;
507
508       aMapOfUnordered.insert( anObjName, anObject );
509     }
510   }
511
512   if ( aMapOfUnordered.isEmpty() )
513     return; // Nothing to show
514
515   Standard_Integer aTopId = 0;
516
517   HYDROData_SequenceOfObjects aModelOrder = GetObjectsLayerOrder( Standard_False );
518   if ( !aModelOrder.IsEmpty() )
519   {
520     const Handle(HYDROData_Entity)& anObject = aModelOrder.First();
521     anObject->GetZLevel( aTopId );
522     aTopId++;
523   }
524
525   aTopId += aMapOfUnordered.size() - 1;
526
527   QStringList aSortedNames = aMapOfUnordered.keys();
528   qSort( aSortedNames.begin(), aSortedNames.end(), isObjectNameLessThan );
529
530   for ( int i = 0; i < aSortedNames.length(); ++i )
531   {
532     QString anObjName = aSortedNames.value( i );
533
534     const Handle(HYDROData_Entity)& anObject = aMapOfUnordered.value( anObjName );
535     anObject->SetZLevel( aTopId-- );
536   }
537 }
538
539 void HYDROData_Document::RemoveObjectsLayerOrder()
540 {
541   HYDROData_Iterator anIter( this );
542   for ( ; anIter.More(); anIter.Next() )
543   {
544     Handle(HYDROData_Entity) anObject = anIter.Current();
545     if ( anObject.IsNull() || !anObject->IsHas2dPrs() )
546       continue;
547
548     anObject->RemoveZLevel();
549   }
550 }
551
552 void HYDROData_Document::StartOperation()
553 {
554   myDoc->NewCommand();
555 }
556
557 void HYDROData_Document::CommitOperation(const TCollection_ExtendedString& theName)
558 {
559   if( !myDoc->CommitCommand() ) // it means that there were no modifications done
560   {
561     myDoc->NewCommand();
562     NewID(); // workaround: do something just to modify the document
563     myDoc->CommitCommand();
564   }
565   myTransactionsAfterSave++;
566
567   if( theName.Length() != 0 )
568   {
569     const TDF_DeltaList& aList = GetUndos();
570     if( !aList.IsEmpty() )
571     {
572       Handle(TDF_Delta) aDelta = aList.Last();
573       if( !aDelta.IsNull() )
574         aDelta->SetName( theName );
575     }
576   }
577 }
578
579 void HYDROData_Document::AbortOperation()
580 {
581   myDoc->AbortCommand();
582 }
583
584 bool HYDROData_Document::IsOperation()
585 {
586   return myDoc->HasOpenCommand() != 0;
587 }
588
589 bool HYDROData_Document::IsModified()
590 {
591   return myTransactionsAfterSave != 0;
592 }
593
594 bool HYDROData_Document::CanUndo()
595 {
596   return myDoc->GetAvailableUndos() > 0;
597 }
598
599 const TDF_DeltaList& HYDROData_Document::GetUndos()
600 {
601   return myDoc->GetUndos();
602 }
603
604 void HYDROData_Document::ClearUndos()
605 {
606   return myDoc->ClearUndos();
607 }
608
609 void HYDROData_Document::Undo()
610 {
611   myDoc->Undo();
612   myTransactionsAfterSave--;
613 }
614
615 bool HYDROData_Document::CanRedo()
616 {
617   return myDoc->GetAvailableRedos() > 0;
618 }
619
620 const TDF_DeltaList& HYDROData_Document::GetRedos()
621 {
622   return myDoc->GetRedos();
623 }
624
625 void HYDROData_Document::ClearRedos()
626 {
627   return myDoc->ClearRedos();
628 }
629
630 void HYDROData_Document::Redo()
631 {
632   myDoc->Redo();
633   myTransactionsAfterSave++;
634 }
635
636 Handle(HYDROData_Entity) HYDROData_Document::CreateObject( const ObjectKind theKind )
637 {
638   return HYDROData_Iterator::CreateObject( this, theKind );
639 }
640
641 Handle(HYDROData_Entity) HYDROData_Document::FindObjectByName( 
642   const QString&   theName,
643   const ObjectKind theObjectKind ) const
644 {
645   Handle(HYDROData_Entity) anObject;
646   if ( theName.isEmpty() )
647     return anObject;
648
649   QStringList aNamesList;
650   aNamesList << theName;
651
652   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( aNamesList, theObjectKind );
653   if( aSeqOfObjs.IsEmpty() )
654     return anObject;
655   
656   anObject = aSeqOfObjs.First();
657   return anObject;
658 }
659
660 HYDROData_SequenceOfObjects HYDROData_Document::FindObjectsByNames(
661   const QStringList& theNames, 
662   const ObjectKind   theObjectKind ) const
663 {
664   HYDROData_SequenceOfObjects aResSeq;
665
666   QStringList aNamesList = theNames;
667
668   HYDROData_Iterator anIter( this, theObjectKind );
669   for( ; anIter.More(); anIter.Next() )
670   {
671     Handle(HYDROData_Entity) anObject = anIter.Current();
672     if( anObject.IsNull() )
673       continue;
674
675     QString anObjName = anObject->GetName();
676     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
677       continue;
678
679     aResSeq.Append( anObject );
680
681     aNamesList.removeAll( anObjName );
682     if ( aNamesList.isEmpty() )
683       break;
684   }
685
686   return aResSeq;
687 }
688
689 HYDROData_Document::HYDROData_Document()
690 {
691   HYDROData_Application::GetApplication()->NewDocument("BinOcaf", myDoc);
692   myDoc->SetUndoLimit(UNDO_LIMIT);
693   NewID(); // needed to have at least one attribute in initial document to avoid errors
694   myTransactionsAfterSave = 0;
695   myLX = -1;
696   myLY = -1;
697
698   myInterpolatorsFactory = 0;
699 }
700
701 HYDROData_Document::HYDROData_Document(const Handle(TDocStd_Document)& theDoc)
702 {
703   myDoc = theDoc;
704   myTransactionsAfterSave = 0;
705   myLX = -1;
706   myLY = -1;
707
708   myInterpolatorsFactory = 0;
709 }
710
711 HYDROData_Document::~HYDROData_Document()
712 {
713 }
714
715 int HYDROData_Document::NewID()
716 {
717   TDF_Label anIDLab = myDoc->Main().FindChild(TAG_PROPS).
718     FindChild(TAG_PROPS_NEW_ID);
719   Handle(TDataStd_Integer) anInt;
720   if (!anIDLab.FindAttribute(TDataStd_Integer::GetID(), anInt)) {
721     anInt = TDataStd_Integer::Set(anIDLab, 0);
722   }
723   // just increment value and return
724   anInt->Set(anInt->Get() + 1);
725   return anInt->Get();
726 }
727
728 TDF_Label HYDROData_Document::LabelOfObjects()
729 {
730   return myDoc->Main().FindChild(TAG_OBJECTS);
731 }
732
733 TDF_Label HYDROData_Document::LabelOfLocalCS() const
734 {
735   return myDoc->Main().FindChild(TAG_LOCAL_CS);
736 }
737
738 void HYDROData_Document::GetLocalCS( double& theLX, double& theLY ) const
739 {
740   TDF_Label aLocalCSLab = LabelOfLocalCS();
741
742   Handle( TDataXtd_Position ) aLocalCS;
743   if( aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
744   {
745     gp_Pnt aLocalCS3d = aLocalCS->GetPosition();
746     theLX = aLocalCS3d.X();
747     theLY = aLocalCS3d.Y();
748   }
749   else
750   {
751     theLX = DEFAULT_LOCAL_CS.X();
752     theLY = DEFAULT_LOCAL_CS.Y();
753   }
754 }
755
756 void HYDROData_Document::SetLocalCS( double theLX, double theLY )
757 {
758   UpdateLCSFields();
759
760   // update the local CS data in attribute
761   TDF_Label aLocalCSLab = LabelOfLocalCS();
762   Handle( TDataXtd_Position ) aLocalCS;
763   if( !aLocalCSLab.FindAttribute( TDataXtd_Position::GetID(), aLocalCS ) )
764     aLocalCS = TDataXtd_Position::Set( aLocalCSLab );
765
766   gp_Pnt aLocalCS3d( theLX, theLY, 0 );
767   aLocalCS->SetPosition( aLocalCS3d );
768
769   // calculate delta for coordinates
770   double aDX = myLX - theLX;
771   double aDY = myLY - theLY;
772
773   // update the local CS data in internal fields
774   myLX = theLX;
775   myLY = theLY;
776
777   //update all objects in the document
778   HYDROData_Iterator anIterator( this, KIND_UNKNOWN );
779   for( ; anIterator.More(); anIterator.Next() )
780     anIterator.Current()->UpdateLocalCS( aDX, aDY );
781 }
782
783 void HYDROData_Document::UpdateLCSFields() const
784 {
785   if( myLX >= 0 && myLY >= 0 )
786     return;
787
788   double aLX, aLY;
789   GetLocalCS( aLX, aLY );
790   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
791   aThat->myLX = aLX;
792   aThat->myLY = aLY;
793 }
794
795 void HYDROData_Document::Transform( double& X, double& Y, bool IsToLocalCS ) const
796 {
797   UpdateLCSFields();
798   if( IsToLocalCS )
799   {
800     X -= myLX;
801     Y -= myLY;
802   }
803   else
804   {
805     X += myLX;
806     Y += myLY;
807   }
808 }
809
810 void HYDROData_Document::Transform( gp_Pnt& thePnt, bool IsToLocalCS ) const
811 {
812   double X = thePnt.X();
813   double Y = thePnt.Y();
814   double Z = thePnt.Z();
815   Transform( X, Y, IsToLocalCS );
816   thePnt = gp_Pnt( X, Y, Z ); 
817 }
818
819 void HYDROData_Document::Transform( gp_XYZ& thePnt, bool IsToLocalCS ) const
820 {
821   double X = thePnt.X();
822   double Y = thePnt.Y();
823   double Z = thePnt.Z();
824   Transform( X, Y, IsToLocalCS );
825   thePnt = gp_XYZ( X, Y, Z ); 
826 }
827
828 void HYDROData_Document::Transform( gp_XY& thePnt, bool IsToLocalCS ) const
829 {
830   double X = thePnt.X();
831   double Y = thePnt.Y();
832   Transform( X, Y, IsToLocalCS );
833   thePnt = gp_XY( X, Y ); 
834 }
835
836 HYDROData_InterpolatorsFactory* HYDROData_Document::GetInterpolatorsFactory()
837 {
838   if ( !myInterpolatorsFactory ) {
839     myInterpolatorsFactory = new HYDROData_InterpolatorsFactory();
840   }
841
842   return myInterpolatorsFactory;
843 }
844
845 HYDROData_IProfilesInterpolator* HYDROData_Document::GetInterpolator( const TCollection_AsciiString& theName ) const
846 {
847   HYDROData_IProfilesInterpolator* anInterpolator = NULL;
848
849   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
850   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
851   if ( aFactory ) {
852     anInterpolator = aFactory->GetInterpolator( theName );
853   }
854
855   return anInterpolator;
856 }
857  
858 NCollection_Sequence<TCollection_AsciiString> HYDROData_Document::GetInterpolatorNames() const
859 {
860   NCollection_Sequence<TCollection_AsciiString> aNames;
861
862   HYDROData_Document* aThat = const_cast<HYDROData_Document*>( this );
863   HYDROData_InterpolatorsFactory* aFactory = aThat->GetInterpolatorsFactory();
864   if ( aFactory ) {
865     aNames = aFactory->GetInterpolatorNames();
866   }
867
868   return aNames;
869 }