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