]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Entity.cxx
Salome HOME
Merge branch 'master' into BR_quadtree
[modules/hydro.git] / src / HYDROData / HYDROData_Entity.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_Entity.h>
20 #include <HYDROData_Document.h>
21 #include <HYDROData_Iterator.h>
22 #include <HYDROData_Tool.h>
23 #include <Standard_GUID.hxx>
24 #include <TDataStd_Name.hxx>
25 #include <TDataStd_ByteArray.hxx>
26 #include <TDataStd_UAttribute.hxx>
27 #include <TDataStd_Integer.hxx>
28 #include <TDataStd_IntegerArray.hxx>
29 #include <TDataStd_ReferenceList.hxx>
30 #include <TDataStd_Real.hxx>
31 #include <TDF_CopyLabel.hxx>
32 #include <TDF_ListIteratorOfLabelList.hxx>
33 #include <TNaming_Builder.hxx>
34 #include <TNaming_NamedShape.hxx>
35 #include <TopoDS_Shape.hxx>
36 #include <QColor>
37 #include <QRegExp>
38 #include <QStringList>
39 #include <QVariant>
40
41 #define _DEVDEBUG_
42 #include "HYDRO_trace.hxx"
43
44 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects()
45   : NCollection_Sequence<Handle_HYDROData_Entity>()
46 {
47 }
48
49 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const HYDROData_SequenceOfObjects& theSequence )
50   : NCollection_Sequence<Handle_HYDROData_Entity>( theSequence )
51 {
52 }
53
54 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const NCollection_Sequence<Handle_HYDROData_Entity>& theSequence )
55   : NCollection_Sequence<Handle_HYDROData_Entity>( theSequence )
56 {
57 }
58
59 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
60 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
61
62 // is equal function for unique object mapping
63 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
64 {
65   if ( !theObj1.IsNull() && !theObj2.IsNull() )
66     return theObj1->Label() == theObj2->Label();
67   return false;
68 }
69
70 QString HYDROData_Entity::GetName() const
71 {
72   Handle(TDataStd_Name) aName;
73   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
74     TCollection_AsciiString aStr(aName->Get());
75     return QString(aStr.ToCString());
76   }
77   return QString();
78 }
79
80 QString HYDROData_Entity::GetObjPyName() const
81 {
82   QString aName = GetName();
83   aName.replace(QRegExp("[\\W]"), "_");
84
85   return aName;
86 }
87
88 void HYDROData_Entity::SetName(const QString& theName)
89 {
90   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
91 }
92
93 QStringList HYDROData_Entity::DumpToPython( const QString& thePyScriptPath,
94                                             MapOfTreatedObjects& theTreatedObjects ) const
95 {
96   QStringList anEmptyList;
97   return anEmptyList;
98 }
99
100 void HYDROData_Entity::Update()
101 {
102   ClearChanged();
103 }
104
105 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
106 {
107   //On the base level no actions are necessary
108 }
109
110 bool HYDROData_Entity::IsHas2dPrs() const
111 {
112   return false;
113 }
114
115 void HYDROData_Entity::Show()
116 {
117   if ( !IsHas2dPrs() )
118     return;
119
120   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
121   if ( aDocument.IsNull() )
122     return;
123
124   aDocument->Show( this );
125 }
126
127 QVariant HYDROData_Entity::GetDataVariant()
128 {
129   return QVariant();
130 }
131
132 void HYDROData_Entity::ClearChanged()
133 {
134   TDataStd_Integer::Set( myLab.FindChild( DataTag_GeomChange ), 0 );
135 }
136
137 int HYDROData_Entity::GetGeomChangeFlag() const
138 {
139   int aGeomChangeFlag = 0;
140   Handle(TDataStd_Integer) aGeomChangeAttr;
141   TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
142   aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
143   if ( !aGeomChangeAttr.IsNull() )
144     aGeomChangeFlag = aGeomChangeAttr->Get();
145   return aGeomChangeFlag;
146 }
147
148 void HYDROData_Entity::Changed( Geometry theChangedGeometry )
149 {
150   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
151   if( aDocument.IsNull() )
152     return;
153
154   int aGeomChangeFlag = 0;
155   Handle(TDataStd_Integer) aGeomChangeAttr;
156   TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
157   aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
158   if ( !aGeomChangeAttr.IsNull() )
159     aGeomChangeFlag = aGeomChangeAttr->Get();
160
161   int aBitsToChange = ( myGeom & theChangedGeometry );
162   if( aBitsToChange == 0 )
163     return;
164
165   aGeomChangeFlag = ( aGeomChangeFlag | aBitsToChange );
166   TDataStd_Integer::Set( aGeomChangeLab, aGeomChangeFlag );
167
168   HYDROData_Iterator anIter( aDocument );
169   for ( ; anIter.More(); anIter.Next() )
170   {
171     Handle(HYDROData_Entity) anObject = anIter.Current();
172     if( anObject.IsNull() )
173       continue;
174     HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
175     for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
176     {
177       Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
178       if( aRefObject->Label()==myLab )
179         anObject->Changed( theChangedGeometry );
180     }
181   }
182 }
183
184 bool HYDROData_Entity::IsMustBeUpdated( Geometry theGeom ) const
185 {
186   return ( ( GetGeomChangeFlag() & theGeom ) != 0 );
187 }
188
189 bool HYDROData_Entity::CanBeUpdated() const
190 {
191   return true;
192 }
193
194 bool HYDROData_Entity::IsRemoved() const
195 {
196   return !myLab.HasAttribute();
197 }
198
199 void HYDROData_Entity::Remove()
200 {
201   return myLab.ForgetAllAttributes( true );
202 }
203
204 bool HYDROData_Entity::CanRemove()
205 {
206   return true;
207 }
208
209 HYDROData_Entity::HYDROData_Entity( Geometry theGeom )
210   : myGeom( theGeom )
211 {
212 }
213
214 HYDROData_Entity::~HYDROData_Entity()
215 {
216 }
217
218 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination,
219                                bool isGenerateNewName ) const
220 {
221   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
222   if ( aDocument.IsNull() ) {
223     return;
224   }
225
226   TDF_CopyLabel aCopy(myLab, theDestination->Label());
227   aCopy.Perform();
228          
229   if( isGenerateNewName )
230   {
231     // generate a new unique name for the clone object:
232     // case 1: Image_1 -> Image_2
233     // case 2: ImageObj -> ImageObj_1
234     QString aName = theDestination->GetName();
235     QString aPrefix = aName;
236     if( aName.contains( '_' ) ) { // case 1
237       QString aSuffix = aName.section( '_', -1 );
238       bool anIsInteger = false;
239       aSuffix.toInt( &anIsInteger );
240       if( anIsInteger )
241         aPrefix = aName.section( '_', 0, -2 );
242     } else { // case 2
243       aPrefix = aName;
244     }
245
246     aName = HYDROData_Tool::GenerateObjectName( aDocument, aPrefix );
247     theDestination->SetName( aName );
248   }
249 }
250
251 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
252 {
253   Handle(HYDROData_Entity) aFather;
254
255   if ( !myLab.IsNull() )
256   {
257     TDF_Label aFatherLabel = myLab.Father();
258
259     while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
260     {
261       aFather = HYDROData_Iterator::Object( aFatherLabel );
262       aFatherLabel = aFatherLabel.Father();
263     }
264   }
265
266   return aFather;
267 }
268
269 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
270 {
271   return HYDROData_SequenceOfObjects();
272 }
273
274 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
275 {
276   theLevel = -1;
277
278   TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
279   if ( !aLabel.IsNull() )
280   {
281     Handle(TDataStd_Integer) anIntVal;
282     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
283     {
284       theLevel = anIntVal->Get();
285       return Standard_True;
286     }
287   }
288
289   return Standard_False;
290 }
291
292 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
293 {
294   TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
295 }
296
297 void HYDROData_Entity::RemoveZLevel()
298 {
299   TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
300   if ( !aLabel.IsNull() )
301     aLabel.ForgetAllAttributes();
302 }
303
304 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
305 {
306   myLab = theLabel;
307 }
308
309 void HYDROData_Entity::SaveByteArray( const int   theTag, 
310                                       const char* theData,
311                                       const int   theLen )
312 {
313   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
314   // array is empty, remove the attribute
315   if (theLen <= 0) {
316     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
317     return;
318   }
319   // store data of image in byte array
320   Handle(TDataStd_ByteArray) aData;
321   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
322     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
323   }
324   // copy bytes one by one
325   if (aData->Length() != theLen) {
326     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
327     for(int a = 0; a < theLen; a++)
328       aNewData->SetValue(a + 1, theData[a]);
329     aData->ChangeArray(aNewData);
330   } else {
331     for(int a = 0; a < theLen; a++)
332       aData->SetValue(a + 1, theData[a]);
333   }
334 }
335
336 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
337 {
338   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
339   Handle(TDataStd_ByteArray) aData;
340   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
341     return NULL; // return empty image if there is no array
342   theLen = aData->Length();
343   if (theLen)
344     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
345   return NULL;
346 }
347
348 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
349 {
350   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
351   return aRefs.IsNull() ? 0 : aRefs->Extent();
352 }
353
354 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
355                                      const int                      theTag ) const
356 {
357   if ( theObj.IsNull() )
358     return false;
359
360   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
361   if ( aRefs.IsNull() || aRefs->IsEmpty() )
362     return false;
363
364   TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
365   for ( ; aListIt.More(); aListIt.Next() )
366   {
367     const TDF_Label& aRefLabel = aListIt.Value();
368     if  ( theObj->Label() == aRefLabel )
369       return true;
370   }
371
372   return false;
373 }
374
375 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
376                                            const int                      theTag )
377 {
378   if ( theObj.IsNull() )
379     return;
380
381   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
382   aRefs->Append( theObj->Label() );
383 }
384
385 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
386                                            const int                      theTag,
387                                            const int                      theIndex )
388 {
389   if ( theObj.IsNull() )
390   {
391     RemoveReferenceObject( theTag, theIndex );
392     return;
393   }
394
395   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
396
397   if ( theIndex >= aRefs->Extent() )
398   {
399     aRefs->Append( theObj->Label() );
400   }
401   else if ( theIndex < 0 )
402   {
403     aRefs->Prepend( theObj->Label() );
404   }
405   else
406   {
407     RemoveReferenceObject( theTag, theIndex );
408
409     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
410
411     aRefs = getReferenceList( theTag, true ); // because reference list can be removed
412     if ( !aBeforeObj.IsNull() )
413       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
414     else 
415       aRefs->Append( theObj->Label() );
416   }
417 }
418
419 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
420                                               const int                      theTag,
421                                               const int                      theBeforeIndex )
422 {
423   if ( theObj.IsNull() )
424     return;
425
426   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
427
428   if ( theBeforeIndex >= aRefs->Extent() )
429   {
430     aRefs->Append( theObj->Label() );
431   }
432   else if ( theBeforeIndex < 0 )
433   {
434     aRefs->Prepend( theObj->Label() );
435   }
436   else
437   {
438     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
439     if ( !aBeforeObj.IsNull() )
440       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
441     else 
442       aRefs->Append( theObj->Label() );
443   }
444 }
445
446 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
447                                             const int                          theTag )
448 {
449   ClearReferenceObjects( theTag );
450   if ( theObjects.IsEmpty() )
451     return;
452
453   HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
454   for ( ; anIter.More(); anIter.Next() )
455     AddReferenceObject( anIter.Value(), theTag );
456 }
457
458 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
459                                                                const int theIndex ) const
460 {
461   Handle(HYDROData_Entity) aRes;
462
463   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
464   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
465     return aRes;
466
467   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
468   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
469
470   const TDF_Label& aRefLabel = anIter.Value();
471   aRes = HYDROData_Iterator::Object( aRefLabel );
472
473   return aRes;
474 }
475
476 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
477 {
478   HYDROData_SequenceOfObjects aRes;
479
480   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
481   if ( aRefs.IsNull() )
482     return aRes;
483
484   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
485   for ( ; anIter.More(); anIter.Next() )
486   {
487     const TDF_Label& aRefLabel = anIter.Value();
488
489     Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
490     if ( aRefObject.IsNull() )
491       continue;
492
493     aRes.Append( aRefObject );
494   }
495
496   return aRes;
497 }
498
499 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
500                                               const int        theTag )
501 {
502   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
503   if ( aRefs.IsNull() )
504     return;
505
506   if ( aRefs->Extent() == 1 )
507   { 
508     // remove all if only one
509     ClearReferenceObjects( theTag );
510     return;
511   }
512
513   aRefs->Remove( theRefLabel );
514 }
515
516 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
517                                               const int theIndex )
518 {
519   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
520   if ( aRefs.IsNull() )
521     return;
522
523   if ( aRefs->Extent() == 1 && theIndex == 0 )
524   { 
525     // remove all if only one
526     ClearReferenceObjects( theTag );
527     return;
528   }
529
530   int anIndex = 0;
531   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
532   for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
533
534   if ( anIndex != theIndex || !anIter.More() )
535     return;
536
537   const TDF_Label& aRefLabel = anIter.Value();
538   aRefs->Remove( aRefLabel );
539 }
540
541 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
542 {
543   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
544   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
545 }
546
547 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
548                                                                    const bool theIsCreate ) const
549 {
550   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
551
552   Handle(TDataStd_ReferenceList) aRefs;
553   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
554     aRefs = TDataStd_ReferenceList::Set( aLabel );
555
556   return aRefs;
557 }
558
559 void HYDROData_Entity::SetColor( const QColor& theColor,
560                                  const int     theTag )
561 {
562   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
563
564   Handle(TDataStd_IntegerArray) aColorArray;
565   if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
566     aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
567
568   aColorArray->SetValue( 1, theColor.red()   );
569   aColorArray->SetValue( 2, theColor.green() );
570   aColorArray->SetValue( 3, theColor.blue()  );
571   aColorArray->SetValue( 4, theColor.alpha() );
572 }
573
574 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
575                                    const int     theTag ) const
576 {
577   QColor aResColor = theDefColor;
578
579   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
580
581   Handle(TDataStd_IntegerArray) aColorArray;
582   if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
583   {
584     aResColor.setRed(   aColorArray->Value( 1 ) );
585     aResColor.setGreen( aColorArray->Value( 2 ) );
586     aResColor.setBlue(  aColorArray->Value( 3 ) );
587     aResColor.setAlpha( aColorArray->Value( 4 ) );
588   }
589
590   return aResColor;
591 }
592
593 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
594 {
595   QStringList aResList;
596
597   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
598   if ( aDocument.IsNull() )
599     return aResList;
600
601   QString aDocName = aDocument->GetDocPyName();
602   QString aName = GetObjPyName();
603
604   aResList << QString( "%1 = %2.CreateObject( %3 )" )
605               .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
606   aResList << QString( "%1.SetName( \"%2\" )" )
607               .arg( aName ).arg( GetName() );
608   aResList << QString( "" );
609
610   if ( IsHas2dPrs() )
611   {
612     // Dump object z-level in viewer
613     Standard_Integer anObjZLevel = -1;
614     if ( GetZLevel( anObjZLevel ) )
615     {
616       aResList << QString( "%1.SetZLevel( %2 )" )
617                   .arg( aName ).arg( anObjZLevel );
618       aResList << QString( "" );
619     }
620   }
621
622   return aResList;
623 }
624
625 QString HYDROData_Entity::getPyTypeID() const
626 {
627   DEBTRACE("HYDROData_Entity::getPyTypeID " << GetKind());
628   switch( GetKind() )
629   {
630     case KIND_IMAGE:             return "KIND_IMAGE";
631     case KIND_POLYLINE:          return "KIND_POLYLINE";
632     case KIND_BATHYMETRY:        return "KIND_BATHYMETRY";
633     case KIND_ALTITUDE:          return "KIND_ALTITUDE";
634     case KIND_IMMERSIBLE_ZONE:   return "KIND_IMMERSIBLE_ZONE";
635     case KIND_RIVER:             return "KIND_RIVER";
636     case KIND_STREAM:            return "KIND_STREAM";
637     case KIND_CONFLUENCE:        return "KIND_CONFLUENCE";
638     case KIND_CHANNEL:           return "KIND_CHANNEL";
639     case KIND_OBSTACLE:          return "KIND_OBSTACLE";
640     case KIND_DIGUE:             return "KIND_DIGUE";
641     case KIND_PROFILE:           return "KIND_PROFILE";
642     case KIND_PROFILEUZ:         return "KIND_PROFILEUZ";
643     case KIND_POLYLINEXY:        return "KIND_POLYLINEXY";
644     case KIND_CALCULATION:       return "KIND_CALCULATION";
645     case KIND_ZONE:              return "KIND_ZONE";
646     case KIND_REGION:            return "KIND_REGION";
647     case KIND_VISUAL_STATE:      return "KIND_VISUAL_STATE";
648     case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
649     case KIND_NATURAL_OBJECT:    return "KIND_NATURAL_OBJECT";
650     case KIND_DUMMY_3D:          return "KIND_DUMMY_3D";
651     case KIND_SHAPES_GROUP:      return "KIND_SHAPES_GROUP";
652     case KIND_SPLIT_GROUP:       return "KIND_SPLIT_GROUP";
653     case KIND_STREAM_ALTITUDE:   return "KIND_STREAM_ALTITUDE";
654     case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
655     case KIND_STRICKLER_TABLE:   return "KIND_STRICKLER_TABLE";
656     case KIND_LAND_COVER_OBSOLETE: return "";
657     case KIND_CHANNEL_ALTITUDE:  return "KIND_CHANNEL_ALTITUDE";
658     case KIND_LAND_COVER_MAP:    return "KIND_LAND_COVER_MAP";
659     default:                     return "KIND_UNKNOWN"; ///! Unrecognized object
660   }
661 }
662
663 void HYDROData_Entity::setPythonReferenceObject( const QString&                  thePyScriptPath,
664                                                  MapOfTreatedObjects&            theTreatedObjects,
665                                                  QStringList&                    theScript,
666                                                  const Handle(HYDROData_Entity)& theRefObject,
667                                                  const QString&                  theMethod ) const
668 {
669   if ( !checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, theScript, theRefObject ) )
670     return;
671
672   QString aRefObjName = theRefObject->GetObjPyName();
673
674   QString anObjName = GetObjPyName();
675   theScript << QString( "%1.%2( %3 )" )
676                .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
677 }
678
679 bool HYDROData_Entity::checkObjectPythonDefinition( const QString&                  thePyScriptPath,
680                                                     MapOfTreatedObjects&            theTreatedObjects,
681                                                     QStringList&                    theScript,
682                                                     const Handle(HYDROData_Entity)& theRefObject ) const
683 {
684   if ( theRefObject.IsNull() )
685     return false;
686
687   QString aRefObjName = theRefObject->GetName();
688   if ( aRefObjName.isEmpty() )
689     return false;
690
691   if ( theTreatedObjects.contains( aRefObjName ) )
692     return true;
693
694   // The definition of reference object must be dumped before this
695   QStringList aRefObjDump = theRefObject->DumpToPython( thePyScriptPath, theTreatedObjects );
696   if ( aRefObjDump.isEmpty() )
697     return false;
698
699   QStringList aTmpList = theScript;
700   theScript = aRefObjDump;
701
702   theScript << QString( "" );
703   theScript << aTmpList;
704
705   theTreatedObjects.insert( aRefObjName, theRefObject );
706
707   return true;
708 }
709
710 void HYDROData_Entity::setPythonObjectColor( QStringList&         theScript,
711                                              const QColor&        theColor,
712                                              const QColor&        theDefaultColor,
713                                              const QString&       theMethod ) const
714 {
715   if ( theColor == theDefaultColor )
716     return; //Do not set the color for object if it like default
717
718   QString anObjName = GetObjPyName();
719   theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) )" )
720               .arg( anObjName ).arg( theMethod )
721               .arg( theColor.red()  ).arg( theColor.green() )
722               .arg( theColor.blue() ).arg( theColor.alpha() );
723 }
724
725 void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
726                                                   QStringList& theScript ) const
727 {
728   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
729   if ( aDocument.IsNull() )
730     return;
731     
732   theScript << QString( "%1 = %2.FindObjectByName( \"%3\" )" ).arg( GetObjPyName() )
733                                                                .arg( aDocument->GetDocPyName() )
734                                                                .arg( GetName() );
735 }
736
737 void HYDROData_Entity::SetShape( int theTag, const TopoDS_Shape& theShape )
738 {
739   TNaming_Builder aBuilder( myLab.FindChild( theTag ) );
740   aBuilder.Generated( theShape );
741 }
742
743 TopoDS_Shape HYDROData_Entity::GetShape( int theTag ) const
744 {
745   TDF_Label aShapeLabel = myLab.FindChild( theTag, false );
746   if ( !aShapeLabel.IsNull() )
747   {
748     Handle(TNaming_NamedShape) aNamedShape;
749     if ( aShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
750       return aNamedShape->Get();
751   }
752   return TopoDS_Shape();
753 }
754
755 void HYDROData_Entity::SetDouble( int theTag, double theValue )
756 {
757   Handle(TDataStd_Real) anAttr;
758   TDF_Label aLabel = myLab.FindChild( theTag );
759   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
760     aLabel.AddAttribute( anAttr = new TDataStd_Real() );
761   anAttr->Set( theValue );
762 }
763
764 double HYDROData_Entity::GetDouble( int theTag, double theDefValue ) const
765 {
766   Handle(TDataStd_Real) anAttr;
767   TDF_Label aLabel = myLab.FindChild( theTag );
768   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
769     return theDefValue;
770
771   return anAttr->Get();
772 }