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