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