Salome HOME
HYDROOperations: initial development and tests
[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 <TDF_CopyLabel.hxx>
6
7 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared)
8 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared)
9
10 // is equal function for unique object mapping
11 bool IsEqual(const Handle_HYDROData_Object& theObj1, const Handle_HYDROData_Object& theObj2)
12 {
13   return (theObj1->ID() == theObj2->ID());
14 }
15
16 QString HYDROData_Object::GetName() const
17 {
18   Handle(TDataStd_Name) aName;
19   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
20     TCollection_AsciiString aStr(aName->Get());
21     return QString(aStr.ToCString());
22   }
23   return QString();
24 }
25
26 void HYDROData_Object::SetName(const QString& theName)
27 {
28   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
29 }
30
31 bool HYDROData_Object::IsRemoved() const
32 {
33   return !myLab.HasAttribute();
34 }
35
36 void HYDROData_Object::Remove()
37 {
38   return myLab.ForgetAllAttributes(Standard_True);
39 }
40
41 HYDROData_Object::HYDROData_Object()
42 {
43 }
44
45 HYDROData_Object::~HYDROData_Object()
46 {
47 }
48
49 void HYDROData_Object::CopyTo(Handle_HYDROData_Object theDestination) const
50 {
51   TDF_CopyLabel aCopy(myLab, theDestination->Label());
52   aCopy.Perform();
53 }
54
55 void HYDROData_Object::SetLabel(TDF_Label theLabel)
56 {
57   myLab = theLabel;
58 }
59
60 void HYDROData_Object::SaveByteArray(const int theTag, 
61   const char* theData, const int theLen)
62 {
63   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
64   // array is empty, remove the attribute
65   if (theLen <= 0) {
66     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
67     return;
68   }
69   // store data of image in byte array
70   Handle(TDataStd_ByteArray) aData;
71   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
72     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
73   }
74   // copy bytes one by one
75   if (aData->Length() != theLen) {
76     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
77     for(int a = 0; a < theLen; a++)
78       aNewData->SetValue(a + 1, theData[a]);
79     aData->ChangeArray(aNewData);
80   } else {
81     for(int a = 0; a < theLen; a++)
82       aData->SetValue(a + 1, theData[a]);
83   }
84 }
85
86 const char* HYDROData_Object::ByteArray(const int theTag, int& theLen)
87 {
88   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
89   Handle(TDataStd_ByteArray) aData;
90   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
91     return NULL; // return empty image if there is no array
92   theLen = aData->Length();
93   if (theLen)
94     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
95   return NULL;
96 }