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