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