]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Entity.cxx
Salome HOME
refs #1456: correct Python name on dump ECW images
[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_AsciiString.hxx>
28 #include <TDataStd_Integer.hxx>
29 #include <TDataStd_IntegerArray.hxx>
30 #include <TDataStd_ReferenceList.hxx>
31 #include <TDataStd_Real.hxx>
32 #include <TDF_CopyLabel.hxx>
33 #include <TDF_ListIteratorOfLabelList.hxx>
34 #include <TNaming_Builder.hxx>
35 #include <TNaming_NamedShape.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <QColor>
38 #include <QRegExp>
39 #include <QStringList>
40 #include <QVariant>
41
42 #define _DEVDEBUG_
43 #include "HYDRO_trace.hxx"
44
45 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects()
46   : NCollection_Sequence<Handle(HYDROData_Entity)>()
47 {
48 }
49
50 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const HYDROData_SequenceOfObjects& theSequence )
51   : NCollection_Sequence<Handle(HYDROData_Entity)>( theSequence )
52 {
53 }
54
55 HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const NCollection_Sequence<Handle(HYDROData_Entity)>& theSequence )
56   : NCollection_Sequence<Handle(HYDROData_Entity)>( theSequence )
57 {
58 }
59
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::GetType() const
81 {
82   return Type( GetKind() );
83 }
84
85 QString HYDROData_Entity::Type( ObjectKind theKind )
86 {
87   switch( theKind )
88   {
89   case KIND_UNKNOWN:
90     return "Unknown";
91   case KIND_IMAGE:
92     return "Image";
93   case KIND_POLYLINE:
94     return "Polyline";
95   case KIND_BATHYMETRY:
96     return "Bathymetry";
97   case KIND_ALTITUDE:
98     return "Altitude";
99   case KIND_IMMERSIBLE_ZONE:
100     return "Immersible_zone";
101   case KIND_RIVER:
102     return "River";
103   case KIND_STREAM:
104     return "Stream";
105   case KIND_CONFLUENCE:
106     return "Confluence";
107   case KIND_CHANNEL:
108     return "Channel";
109   case KIND_OBSTACLE:
110     return "Obstacle";
111   case KIND_DIGUE:
112     return "Digue";
113   case KIND_PROFILE:
114     return "Profile";
115   case KIND_PROFILEUZ:
116     return "Profile_UZ";
117   case KIND_POLYLINEXY:
118     return "Polyline_XY";
119   case KIND_CALCULATION:
120     return "Calculation_case";
121   case KIND_ZONE:
122     return "Zone";
123   case KIND_REGION:
124     return "Region";
125   case KIND_VISUAL_STATE:
126     return "Visual_state";
127   case KIND_ARTIFICIAL_OBJECT:
128     return "Artificial_object";
129   case KIND_NATURAL_OBJECT:
130     return "Natural_object";
131   case KIND_DUMMY_3D:
132     return "Dummy_3d";
133   case KIND_SHAPES_GROUP:
134     return "Shapes_group";
135   case KIND_SPLIT_GROUP:
136     return "Split_group";
137   case KIND_STREAM_ALTITUDE:
138     return "Stream_altitude";
139   case KIND_OBSTACLE_ALTITUDE:
140     return "Obstacle_altitude";
141   case KIND_STRICKLER_TABLE:
142     return "Strickler_table";
143   case KIND_LAND_COVER_OBSOLETE:
144     return "Land_cover";
145   case KIND_CHANNEL_ALTITUDE:
146     return "Channel_altitude";
147   case KIND_LAND_COVER_MAP:
148     return "Land_cover_map";
149   case KIND_DTM:
150     return "DTM";
151   }
152   return "";
153 }
154
155 QString HYDROData_Entity::GetObjPyName() const
156 {
157   QString aName = GetName();
158   aName.replace(QRegExp("[\\W]"), "_");
159
160   if( aName[0].isDigit() )
161     aName = GetType() + "_" + aName;
162
163   return aName;
164 }
165
166 QString HYDROData_Entity::GetDefaultName() const
167 {
168   QString aName;
169
170   TDF_Label aLabel = myLab.FindChild(DataTag_DefaultName, false);
171   if (!aLabel.IsNull())
172     {
173       Handle(TDataStd_AsciiString) anAsciiStr;
174       if (aLabel.FindAttribute(TDataStd_AsciiString::GetID(), anAsciiStr))
175         aName = QString(anAsciiStr->Get().ToCString());
176     }
177   else
178     aName = GetName();
179
180   return aName;
181
182 }
183
184 void HYDROData_Entity::SetName(const QString& theName, bool isDefault)
185 {
186   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
187   if (isDefault)
188     {
189       TDF_Label aDefaultNameLabel = myLab.FindChild( DataTag_DefaultName, true );
190       if ( aDefaultNameLabel.IsNull() )
191         return;
192       Handle(TDataStd_AsciiString) theDefaultName;
193
194       if ( !aDefaultNameLabel.FindAttribute( TDataStd_AsciiString::GetID(), theDefaultName ))
195         {
196           TCollection_AsciiString aName = theName.toStdString().c_str();
197           theDefaultName = TDataStd_AsciiString::Set(myLab.FindChild( DataTag_DefaultName), aName );
198         }
199     }
200 }
201
202 QStringList HYDROData_Entity::DumpToPython( const QString& thePyScriptPath,
203                                             MapOfTreatedObjects& theTreatedObjects ) const
204 {
205   QStringList anEmptyList;
206   return anEmptyList;
207 }
208
209 void HYDROData_Entity::Update()
210 {
211   ClearChanged();
212 }
213
214 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
215 {
216   //On the base level no actions are necessary
217 }
218
219 bool HYDROData_Entity::IsHas2dPrs() const
220 {
221   return false;
222 }
223
224 void HYDROData_Entity::Show()
225 {
226   if ( !IsHas2dPrs() )
227     return;
228
229   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
230   if ( aDocument.IsNull() )
231     return;
232
233   aDocument->Show( this );
234 }
235
236 QVariant HYDROData_Entity::GetDataVariant()
237 {
238   return QVariant();
239 }
240
241 void HYDROData_Entity::ClearChanged()
242 {
243   TDataStd_Integer::Set( myLab.FindChild( DataTag_GeomChange ), 0 );
244 }
245
246 int HYDROData_Entity::GetGeomChangeFlag() const
247 {
248   int aGeomChangeFlag = 0;
249   Handle(TDataStd_Integer) aGeomChangeAttr;
250   TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
251   aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
252   if ( !aGeomChangeAttr.IsNull() )
253     aGeomChangeFlag = aGeomChangeAttr->Get();
254   return aGeomChangeFlag;
255 }
256
257 void HYDROData_Entity::Changed( Geometry theChangedGeometry )
258 {
259   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
260   if( aDocument.IsNull() )
261     return;
262
263   int aGeomChangeFlag = 0;
264   Handle(TDataStd_Integer) aGeomChangeAttr;
265   TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
266   aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
267   if ( !aGeomChangeAttr.IsNull() )
268     aGeomChangeFlag = aGeomChangeAttr->Get();
269
270   int aBitsToChange = ( myGeom & theChangedGeometry );
271   if( aBitsToChange == 0 )
272     return;
273
274   aGeomChangeFlag = ( aGeomChangeFlag | aBitsToChange );
275   TDataStd_Integer::Set( aGeomChangeLab, aGeomChangeFlag );
276
277   HYDROData_Iterator anIter( aDocument );
278   for ( ; anIter.More(); anIter.Next() )
279   {
280     Handle(HYDROData_Entity) anObject = anIter.Current();
281     if( anObject.IsNull() )
282       continue;
283     HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
284     for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
285     {
286       Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
287       if( aRefObject->Label()==myLab )
288         anObject->Changed( theChangedGeometry );
289     }
290   }
291 }
292
293 bool HYDROData_Entity::IsMustBeUpdated( Geometry theGeom ) const
294 {
295   return ( ( GetGeomChangeFlag() & theGeom ) != 0 );
296 }
297
298 bool HYDROData_Entity::CanBeUpdated() const
299 {
300   return true;
301 }
302
303 bool HYDROData_Entity::IsRemoved() const
304 {
305   return !myLab.HasAttribute();
306 }
307
308 void HYDROData_Entity::Remove()
309 {
310   return myLab.ForgetAllAttributes( true );
311 }
312
313 bool HYDROData_Entity::CanRemove()
314 {
315   return true;
316 }
317
318 HYDROData_Entity::HYDROData_Entity( Geometry theGeom )
319   : myGeom( theGeom )
320 {
321 }
322
323 HYDROData_Entity::~HYDROData_Entity()
324 {
325 }
326
327 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination,
328                                bool isGenerateNewName ) const
329 {
330   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
331   if ( aDocument.IsNull() ) {
332     return;
333   }
334
335   TDF_CopyLabel aCopy(myLab, theDestination->Label());
336   aCopy.Perform();
337          
338   if( isGenerateNewName )
339   {
340     // generate a new unique name for the clone object:
341     // case 1: Image_1 -> Image_2
342     // case 2: ImageObj -> ImageObj_1
343     QString aName = theDestination->GetName();
344     QString aPrefix = aName;
345     if( aName.contains( '_' ) ) { // case 1
346       QString aSuffix = aName.section( '_', -1 );
347       bool anIsInteger = false;
348       aSuffix.toInt( &anIsInteger );
349       if( anIsInteger )
350         aPrefix = aName.section( '_', 0, -2 );
351     } else { // case 2
352       aPrefix = aName;
353     }
354
355     aName = HYDROData_Tool::GenerateObjectName( aDocument, aPrefix );
356     theDestination->SetName( aName );
357   }
358 }
359
360 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
361 {
362   Handle(HYDROData_Entity) aFather;
363
364   if ( !myLab.IsNull() )
365   {
366     TDF_Label aFatherLabel = myLab.Father();
367
368     while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
369     {
370       aFather = HYDROData_Iterator::Object( aFatherLabel );
371       aFatherLabel = aFatherLabel.Father();
372     }
373   }
374
375   return aFather;
376 }
377
378 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
379 {
380   return HYDROData_SequenceOfObjects();
381 }
382
383 bool HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
384 {
385   theLevel = -1;
386
387   TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
388   if ( !aLabel.IsNull() )
389   {
390     Handle(TDataStd_Integer) anIntVal;
391     if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
392     {
393       theLevel = anIntVal->Get();
394       return true;
395     }
396   }
397
398   return false;
399 }
400
401 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
402 {
403   TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
404 }
405
406 void HYDROData_Entity::RemoveZLevel()
407 {
408   TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
409   if ( !aLabel.IsNull() )
410     aLabel.ForgetAllAttributes();
411 }
412
413 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
414 {
415   myLab = theLabel; 
416 }
417
418 void HYDROData_Entity::SaveByteArray( const int   theTag, 
419                                       const char* theData,
420                                       const int   theLen )
421 {
422   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
423   // array is empty, remove the attribute
424   if (theLen <= 0) {
425     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
426     return;
427   }
428   // store data of image in byte array
429   Handle(TDataStd_ByteArray) aData;
430   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
431     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
432   }
433   Standard_Byte* Byte0 = &(aData->InternalArray()->ChangeArray1().ChangeFirst());
434   memcpy(Byte0, theData, theLen * sizeof (char));
435   // copy bytes one by one
436   if (aData->Length() != theLen) {
437     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
438     for(int a = 0; a < theLen; a++)
439       aNewData->SetValue(a + 1, theData[a]);
440     aData->ChangeArray(aNewData);
441   } 
442   else
443   {
444   //  for(int a = 0; a < theLen; a++)
445   //    aData->SetValue(a + 1, theData[a]);
446   }
447 }
448
449 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
450 {
451   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
452   Handle(TDataStd_ByteArray) aData;
453   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
454     return NULL; // return empty image if there is no array
455   theLen = aData->Length();
456   if (theLen)
457     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
458   return NULL;
459 }
460
461 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
462 {
463   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
464   return aRefs.IsNull() ? 0 : aRefs->Extent();
465 }
466
467 bool HYDROData_Entity::HasReference( const Handle(HYDROData_Entity)& theObj,
468                                      const int                      theTag ) const
469 {
470   if ( theObj.IsNull() )
471     return false;
472
473   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
474   if ( aRefs.IsNull() || aRefs->IsEmpty() )
475     return false;
476
477   TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
478   for ( ; aListIt.More(); aListIt.Next() )
479   {
480     const TDF_Label& aRefLabel = aListIt.Value();
481     if  ( theObj->Label() == aRefLabel )
482       return true;
483   }
484
485   return false;
486 }
487
488 void HYDROData_Entity::AddReferenceObject( const Handle(HYDROData_Entity)& theObj,
489                                            const int                      theTag )
490 {
491   if ( theObj.IsNull() )
492     return;
493
494   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
495   aRefs->Append( theObj->Label() );
496 }
497
498 void HYDROData_Entity::SetReferenceObject( const Handle(HYDROData_Entity)& theObj,
499                                            const int                      theTag,
500                                            const int                      theIndex )
501 {
502   if ( theObj.IsNull() )
503   {
504     RemoveReferenceObject( theTag, theIndex );
505     return;
506   }
507
508   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
509
510   if ( theIndex >= aRefs->Extent() )
511   {
512     aRefs->Append( theObj->Label() );
513   }
514   else if ( theIndex < 0 )
515   {
516     aRefs->Prepend( theObj->Label() );
517   }
518   else
519   {
520     RemoveReferenceObject( theTag, theIndex );
521
522     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
523
524     aRefs = getReferenceList( theTag, true ); // because reference list can be removed
525     if ( !aBeforeObj.IsNull() )
526       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
527     else 
528       aRefs->Append( theObj->Label() );
529   }
530 }
531
532 void HYDROData_Entity::InsertReferenceObject( const Handle(HYDROData_Entity)& theObj,
533                                               const int                      theTag,
534                                               const int                      theBeforeIndex )
535 {
536   if ( theObj.IsNull() )
537     return;
538
539   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
540
541   if ( theBeforeIndex >= aRefs->Extent() )
542   {
543     aRefs->Append( theObj->Label() );
544   }
545   else if ( theBeforeIndex < 0 )
546   {
547     aRefs->Prepend( theObj->Label() );
548   }
549   else
550   {
551     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
552     if ( !aBeforeObj.IsNull() )
553       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
554     else 
555       aRefs->Append( theObj->Label() );
556   }
557 }
558
559 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
560                                             const int                          theTag )
561 {
562   ClearReferenceObjects( theTag );
563   if ( theObjects.IsEmpty() )
564     return;
565
566   HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
567   for ( ; anIter.More(); anIter.Next() )
568     AddReferenceObject( anIter.Value(), theTag );
569 }
570
571 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
572                                                                const int theIndex ) const
573 {
574   //DEBTRACE("GetReferenceObject " << theTag << " " << theIndex);
575   Handle(HYDROData_Entity) aRes;
576
577   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
578   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
579     return aRes;
580
581   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
582   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
583
584   const TDF_Label& aRefLabel = anIter.Value();
585   aRes = HYDROData_Iterator::Object( aRefLabel );
586
587   return aRes;
588 }
589
590 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
591 {
592   HYDROData_SequenceOfObjects aRes;
593
594   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
595   if ( aRefs.IsNull() )
596     return aRes;
597
598   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
599   for ( ; anIter.More(); anIter.Next() )
600   {
601     const TDF_Label& aRefLabel = anIter.Value();
602
603     Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
604     if ( aRefObject.IsNull() )
605       continue;
606
607     aRes.Append( aRefObject );
608   }
609
610   return aRes;
611 }
612
613 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
614                                               const int        theTag )
615 {
616   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
617   if ( aRefs.IsNull() )
618     return;
619
620   if ( aRefs->Extent() == 1 )
621   { 
622     // remove all if only one
623     ClearReferenceObjects( theTag );
624     return;
625   }
626
627   aRefs->Remove( theRefLabel );
628 }
629
630 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
631                                               const int theIndex )
632 {
633   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
634   if ( aRefs.IsNull() )
635     return;
636
637   if ( aRefs->Extent() == 1 && theIndex == 0 )
638   { 
639     // remove all if only one
640     ClearReferenceObjects( theTag );
641     return;
642   }
643
644   int anIndex = 0;
645   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
646   for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
647
648   if ( anIndex != theIndex || !anIter.More() )
649     return;
650
651   const TDF_Label& aRefLabel = anIter.Value();
652   aRefs->Remove( aRefLabel );
653 }
654
655 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
656 {
657   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
658   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
659 }
660
661 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
662                                                                    const bool theIsCreate ) const
663 {
664   //DEBTRACE("getReferenceList " << theTag << " " << theIsCreate);
665   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
666
667   Handle(TDataStd_ReferenceList) aRefs;
668   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
669     aRefs = TDataStd_ReferenceList::Set( aLabel );
670   return aRefs;
671 }
672
673 void HYDROData_Entity::SetColor( const QColor& theColor,
674                                  const int     theTag )
675 {
676   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
677
678   Handle(TDataStd_IntegerArray) aColorArray;
679   if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
680     aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
681
682   aColorArray->SetValue( 1, theColor.red()   );
683   aColorArray->SetValue( 2, theColor.green() );
684   aColorArray->SetValue( 3, theColor.blue()  );
685   aColorArray->SetValue( 4, theColor.alpha() );
686 }
687
688 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
689                                    const int     theTag ) const
690 {
691   QColor aResColor = theDefColor;
692
693   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
694
695   Handle(TDataStd_IntegerArray) aColorArray;
696   if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
697   {
698     aResColor.setRed(   aColorArray->Value( 1 ) );
699     aResColor.setGreen( aColorArray->Value( 2 ) );
700     aResColor.setBlue(  aColorArray->Value( 3 ) );
701     aResColor.setAlpha( aColorArray->Value( 4 ) );
702   }
703
704   return aResColor;
705 }
706
707 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
708 {
709   QStringList aResList;
710
711   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
712   if ( aDocument.IsNull() )
713     return aResList;
714
715   QString aDocName = aDocument->GetDocPyName();
716   QString aName = GetObjPyName();
717
718   aResList << QString( "%1 = %2.CreateObject( %3 )" )
719               .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
720   aResList << QString( "%1.SetName( \"%2\" )" )
721               .arg( aName ).arg( GetName() );
722   aResList << QString( "" );
723
724   if ( IsHas2dPrs() )
725   {
726     // Dump object z-level in viewer
727     Standard_Integer anObjZLevel = -1;
728     if ( GetZLevel( anObjZLevel ) )
729     {
730       aResList << QString( "%1.SetZLevel( %2 )" )
731                   .arg( aName ).arg( anObjZLevel );
732       aResList << QString( "" );
733     }
734   }
735
736   return aResList;
737 }
738
739 QString HYDROData_Entity::getPyTypeID() const
740 {
741   //DEBTRACE("HYDROData_Entity::getPyTypeID " << GetKind());
742   switch( GetKind() )
743   {
744     case KIND_IMAGE:             return "KIND_IMAGE";
745     case KIND_POLYLINE:          return "KIND_POLYLINE";
746     case KIND_BATHYMETRY:        return "KIND_BATHYMETRY";
747     case KIND_ALTITUDE:          return "KIND_ALTITUDE";
748     case KIND_IMMERSIBLE_ZONE:   return "KIND_IMMERSIBLE_ZONE";
749     case KIND_RIVER:             return "KIND_RIVER";
750     case KIND_STREAM:            return "KIND_STREAM";
751     case KIND_CONFLUENCE:        return "KIND_CONFLUENCE";
752     case KIND_CHANNEL:           return "KIND_CHANNEL";
753     case KIND_OBSTACLE:          return "KIND_OBSTACLE";
754     case KIND_DIGUE:             return "KIND_DIGUE";
755     case KIND_PROFILE:           return "KIND_PROFILE";
756     case KIND_PROFILEUZ:         return "KIND_PROFILEUZ";
757     case KIND_POLYLINEXY:        return "KIND_POLYLINEXY";
758     case KIND_CALCULATION:       return "KIND_CALCULATION";
759     case KIND_ZONE:              return "KIND_ZONE";
760     case KIND_REGION:            return "KIND_REGION";
761     case KIND_VISUAL_STATE:      return "KIND_VISUAL_STATE";
762     case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
763     case KIND_NATURAL_OBJECT:    return "KIND_NATURAL_OBJECT";
764     case KIND_DUMMY_3D:          return "KIND_DUMMY_3D";
765     case KIND_SHAPES_GROUP:      return "KIND_SHAPES_GROUP";
766     case KIND_SPLIT_GROUP:       return "KIND_SPLIT_GROUP";
767     case KIND_STREAM_ALTITUDE:   return "KIND_STREAM_ALTITUDE";
768     case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
769     case KIND_STRICKLER_TABLE:   return "KIND_STRICKLER_TABLE";
770     case KIND_LAND_COVER_OBSOLETE: return "";
771     case KIND_CHANNEL_ALTITUDE:  return "KIND_CHANNEL_ALTITUDE";
772     case KIND_LAND_COVER_MAP:    return "KIND_LAND_COVER_MAP";
773     default:                     return "KIND_UNKNOWN"; ///! Unrecognized object
774   }
775 }
776
777 void HYDROData_Entity::setPythonReferenceObject( const QString&                  thePyScriptPath,
778                                                  MapOfTreatedObjects&            theTreatedObjects,
779                                                  QStringList&                    theScript,
780                                                  const Handle(HYDROData_Entity)& theRefObject,
781                                                  const QString&                  theMethod ) const
782 {
783   if ( !checkObjectPythonDefinition( thePyScriptPath, theTreatedObjects, theScript, theRefObject ) )
784     return;
785
786   QString aRefObjName = theRefObject->GetObjPyName();
787
788   QString anObjName = GetObjPyName();
789   theScript << QString( "%1.%2( %3 )" )
790                .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
791 }
792
793 bool HYDROData_Entity::checkObjectPythonDefinition( const QString&                  thePyScriptPath,
794                                                     MapOfTreatedObjects&            theTreatedObjects,
795                                                     QStringList&                    theScript,
796                                                     const Handle(HYDROData_Entity)& theRefObject ) const
797 {
798   if ( theRefObject.IsNull() )
799     return false;
800
801   QString aRefObjName = theRefObject->GetName();
802   if ( aRefObjName.isEmpty() )
803     return false;
804
805   if ( theTreatedObjects.contains( aRefObjName ) )
806     return true;
807
808   // The definition of reference object must be dumped before this
809   QStringList aRefObjDump = theRefObject->DumpToPython( thePyScriptPath, theTreatedObjects );
810   if ( aRefObjDump.isEmpty() )
811     return false;
812
813   QStringList aTmpList = theScript;
814   theScript = aRefObjDump;
815
816   theScript << QString( "" );
817   theScript << aTmpList;
818
819   theTreatedObjects.insert( aRefObjName, theRefObject );
820
821   return true;
822 }
823
824 void HYDROData_Entity::setPythonObjectColor( QStringList&         theScript,
825                                              const QColor&        theColor,
826                                              const QColor&        theDefaultColor,
827                                              const QString&       theMethod ) const
828 {
829   if ( theColor == theDefaultColor )
830     return; //Do not set the color for object if it like default
831
832   QString anObjName = GetObjPyName();
833   theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) )" )
834               .arg( anObjName ).arg( theMethod )
835               .arg( theColor.red()  ).arg( theColor.green() )
836               .arg( theColor.blue() ).arg( theColor.alpha() );
837 }
838
839 void HYDROData_Entity::findPythonReferenceObject( QStringList& theScript,
840                                                   QString      defName) const
841 {
842   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
843   if ( aDocument.IsNull() )
844     return;
845
846   if (defName.isEmpty())
847     theScript << QString( "%1 = %2.FindObjectByName( \"%3\" )" ).arg( GetObjPyName() )
848                                                                 .arg( aDocument->GetDocPyName() )
849                                                                 .arg( GetDefaultName() );
850   else
851     theScript << QString( "%1 = %2.FindObjectByName( \"%3\" )" ).arg( GetObjPyName() )
852                                                                 .arg( aDocument->GetDocPyName() )
853                                                                 .arg( defName );
854 }
855
856 void HYDROData_Entity::SetNameInDumpPython(QStringList&  theScript,
857                                            QString       theName) const
858 {
859   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
860   if ( aDocument.IsNull() )
861     return;
862
863   if (theName.isEmpty())
864       theScript << QString( "%1.SetName( \"%2\" )" ).arg( GetObjPyName() )
865                                                     .arg( GetName() );
866   else
867     theScript << QString( "%1.SetName( \"%2\" )" ).arg( GetObjPyName() )
868                                                   .arg( theName );
869 }
870
871 void HYDROData_Entity::SetShape( int theTag, const TopoDS_Shape& theShape )
872 {
873   TNaming_Builder aBuilder( myLab.FindChild( theTag ) );
874   aBuilder.Generated( theShape );
875 }
876
877 TopoDS_Shape HYDROData_Entity::GetShape( int theTag ) const
878 {
879   TDF_Label aShapeLabel = myLab.FindChild( theTag, false );
880   if ( !aShapeLabel.IsNull() )
881   {
882     Handle(TNaming_NamedShape) aNamedShape;
883     if ( aShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
884       return aNamedShape->Get();
885   }
886   return TopoDS_Shape();
887 }
888
889 void HYDROData_Entity::SetDouble( int theTag, double theValue )
890 {
891   Handle(TDataStd_Real) anAttr;
892   TDF_Label aLabel = myLab.FindChild( theTag );
893   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
894     aLabel.AddAttribute( anAttr = new TDataStd_Real() );
895   anAttr->Set( theValue );
896 }
897
898 double HYDROData_Entity::GetDouble( int theTag, double theDefValue ) const
899 {
900   Handle(TDataStd_Real) anAttr;
901   TDF_Label aLabel = myLab.FindChild( theTag );
902   if( !aLabel.FindAttribute( TDataStd_Real::GetID(), anAttr ) )
903     return theDefValue;
904
905   return anAttr->Get();
906 }
907
908 void HYDROData_Entity::SetInteger( int theTag, int theValue )
909 {
910   Handle(TDataStd_Integer) anAttr;
911   TDF_Label aLabel = myLab.FindChild( theTag );
912   if( !aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
913     aLabel.AddAttribute( anAttr = new TDataStd_Integer() );
914   anAttr->Set( theValue );
915 }
916
917 int HYDROData_Entity::GetInteger( int theTag, int theDefValue ) const
918 {
919   Handle(TDataStd_Integer) anAttr;
920   TDF_Label aLabel = myLab.FindChild( theTag );
921   if( !aLabel.FindAttribute( TDataStd_Integer::GetID(), anAttr ) )
922     return 0;
923
924   return anAttr->Get();
925 }
926
927 bool HYDROData_Entity::CompareLabels(const Handle(HYDROData_Entity)& theOtherObj) 
928 {
929   if ( !theOtherObj.IsNull() )
930     return this->Label() == theOtherObj->Label();
931   return false;
932
933