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