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