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