Salome HOME
Access to 'Invalid value' of altitude from Bathymetry is added.
[modules/hydro.git] / src / HYDROData / HYDROData_Object.cxx
1 #include <HYDROData_Object.h>
2
3 #include <TDataStd_Name.hxx>
4 #include <TDataStd_ByteArray.hxx>
5 #include <TDataStd_UAttribute.hxx>
6 #include <TDataStd_IntegerArray.hxx>
7 #include <TDataStd_BooleanArray.hxx>
8 #include <TDataStd_RealArray.hxx>
9 #include <TDF_CopyLabel.hxx>
10
11 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared)
12 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared)
13
14 // is equal function for unique object mapping
15 bool IsEqual(const Handle_HYDROData_Object& theObj1, const Handle_HYDROData_Object& theObj2)
16 {
17   return (theObj1->ID() == theObj2->ID());
18 }
19
20 QString HYDROData_Object::GetName() const
21 {
22   Handle(TDataStd_Name) aName;
23   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
24     TCollection_AsciiString aStr(aName->Get());
25     return QString(aStr.ToCString());
26   }
27   return QString();
28 }
29
30 void HYDROData_Object::SetName(const QString& theName)
31 {
32   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
33 }
34
35 bool HYDROData_Object::IsVisible( const int theViewId ) const
36 {
37   ViewId2VisualStateMap aMap;
38   GetViewId2VisualStateMap( aMap );
39   if( aMap.find( theViewId ) != aMap.end() )
40   {
41     const VisualState& aVisualState = aMap[ theViewId ];
42     return aVisualState.Visibility;
43   }
44   return false;
45 }
46
47 void HYDROData_Object::SetVisible( const int theViewId,
48                                    const bool theVal )
49 {
50   if( theViewId == 0 )
51     return;
52
53   ViewId2VisualStateMap aMap;
54   GetViewId2VisualStateMap( aMap );
55
56   VisualState& aVisualState = aMap[ theViewId ];
57   aVisualState.Visibility = theVal;
58
59   SetViewId2VisualStateMap( aMap );
60 }
61
62 bool HYDROData_Object::IsRemoved() const
63 {
64   return !myLab.HasAttribute();
65 }
66
67 void HYDROData_Object::Remove()
68 {
69   return myLab.ForgetAllAttributes(Standard_True);
70 }
71
72 HYDROData_Object::HYDROData_Object()
73 {
74 }
75
76 HYDROData_Object::~HYDROData_Object()
77 {
78 }
79
80 void HYDROData_Object::CopyTo(Handle_HYDROData_Object theDestination) const
81 {
82   TDF_CopyLabel aCopy(myLab, theDestination->Label());
83   aCopy.Perform();
84 }
85
86 void HYDROData_Object::SetLabel(TDF_Label theLabel)
87 {
88   myLab = theLabel;
89 }
90
91 void HYDROData_Object::SaveByteArray(const int theTag, 
92   const char* theData, const int theLen)
93 {
94   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
95   // array is empty, remove the attribute
96   if (theLen <= 0) {
97     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
98     return;
99   }
100   // store data of image in byte array
101   Handle(TDataStd_ByteArray) aData;
102   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
103     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
104   }
105   // copy bytes one by one
106   if (aData->Length() != theLen) {
107     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
108     for(int a = 0; a < theLen; a++)
109       aNewData->SetValue(a + 1, theData[a]);
110     aData->ChangeArray(aNewData);
111   } else {
112     for(int a = 0; a < theLen; a++)
113       aData->SetValue(a + 1, theData[a]);
114   }
115 }
116
117 const char* HYDROData_Object::ByteArray(const int theTag, int& theLen)
118 {
119   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
120   Handle(TDataStd_ByteArray) aData;
121   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
122     return NULL; // return empty image if there is no array
123   theLen = aData->Length();
124   if (theLen)
125     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
126   return NULL;
127 }
128
129 void HYDROData_Object::GetViewId2VisualStateMap( ViewId2VisualStateMap& theMap ) const
130 {
131   theMap.clear();
132
133   TDF_Label aViewIdLab = myLab.FindChild( DataTag_ViewId );
134   TDF_Label aVisibilityLab = myLab.FindChild( DataTag_Visibility );
135   TDF_Label aTransparencyLab = myLab.FindChild( DataTag_Transparency );
136   TDF_Label aZValueLab = myLab.FindChild( DataTag_ZValue );
137
138   Handle(TDataStd_IntegerArray) aViewIdArray;
139   Handle(TDataStd_BooleanArray) aVisibilityArray;
140   Handle(TDataStd_RealArray) aTransparencyArray;
141   Handle(TDataStd_RealArray) aZValueArray;
142
143   if( !aViewIdLab.FindAttribute( TDataStd_IntegerArray::GetID(), aViewIdArray ) ||
144       !aVisibilityLab.FindAttribute( TDataStd_BooleanArray::GetID(), aVisibilityArray ) ||
145       !aTransparencyLab.FindAttribute( TDataStd_RealArray::GetID(), aTransparencyArray ) ||
146       !aZValueLab.FindAttribute( TDataStd_RealArray::GetID(), aZValueArray ) )
147     return;
148
149   int aSize = qMin( qMin( aViewIdArray->Length(), aVisibilityArray->Length() ),
150                     qMin( aTransparencyArray->Length(), aZValueArray->Length() ) );
151   for( int anIndex = 0; anIndex < aSize; anIndex++ )
152   {
153     int aViewId = aViewIdArray->Value( anIndex );
154     VisualState aVisualState;
155     aVisualState.Visibility = aVisibilityArray->Value( anIndex );
156     aVisualState.Transparency = aTransparencyArray->Value( anIndex );
157     aVisualState.ZValue = aZValueArray->Value( anIndex );
158     theMap[ aViewId ] = aVisualState;
159   }
160 }
161
162 void HYDROData_Object::SetViewId2VisualStateMap( const ViewId2VisualStateMap& theMap )
163 {
164   TDF_Label aViewIdLab = myLab.FindChild( DataTag_ViewId );
165   TDF_Label aVisibilityLab = myLab.FindChild( DataTag_Visibility );
166   TDF_Label aTransparencyLab = myLab.FindChild( DataTag_Transparency );
167   TDF_Label aZValueLab = myLab.FindChild( DataTag_ZValue );
168
169   aViewIdLab.ForgetAllAttributes();
170   aVisibilityLab.ForgetAllAttributes();
171   aTransparencyLab.ForgetAllAttributes();
172   aZValueLab.ForgetAllAttributes();
173
174   int aSize = theMap.size();
175
176   Handle(TDataStd_IntegerArray) aViewIdArray =
177     TDataStd_IntegerArray::Set( aViewIdLab, 0, aSize-1 );
178   Handle(TDataStd_BooleanArray) aVisibilityArray =
179     TDataStd_BooleanArray::Set( aVisibilityLab, 0, aSize-1 );
180   Handle(TDataStd_RealArray) aTransparencyArray =
181     TDataStd_RealArray::Set( aTransparencyLab, 0, aSize-1 );
182   Handle(TDataStd_RealArray) aZValueArray =
183     TDataStd_RealArray::Set( aZValueLab, 0, aSize-1 );
184
185   int anIndex = 0;
186   ViewId2VisualStateMapIterator anIter( theMap );
187   while( anIter.hasNext() )
188   {
189     int aViewId = anIter.next().key();
190     const VisualState& aVisualState = anIter.value();
191     aViewIdArray->SetValue( anIndex, aViewId );
192     aVisibilityArray->SetValue( anIndex, aVisualState.Visibility );
193     aTransparencyArray->SetValue( anIndex, aVisualState.Transparency );
194     aZValueArray->SetValue( anIndex, aVisualState.ZValue );
195     anIndex++;
196   }
197 }