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