2 #include "HYDROData_Object.h"
4 #include "HYDROData_Iterator.h"
6 #include <TDataStd_Name.hxx>
7 #include <TDataStd_ByteArray.hxx>
8 #include <TDataStd_UAttribute.hxx>
9 #include <TDataStd_IntegerArray.hxx>
10 #include <TDataStd_BooleanArray.hxx>
11 #include <TDataStd_RealArray.hxx>
12 #include <TDataStd_ReferenceList.hxx>
14 #include <TDF_CopyLabel.hxx>
15 #include <TDF_ListIteratorOfLabelList.hxx>
19 #include <QStringList>
22 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared)
23 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared)
25 // is equal function for unique object mapping
26 bool IsEqual(const Handle_HYDROData_Object& theObj1, const Handle_HYDROData_Object& theObj2)
28 return (theObj1->ID() == theObj2->ID());
31 QString HYDROData_Object::GetName() const
33 Handle(TDataStd_Name) aName;
34 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
35 TCollection_AsciiString aStr(aName->Get());
36 return QString(aStr.ToCString());
41 void HYDROData_Object::SetName(const QString& theName)
43 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
46 QStringList HYDROData_Object::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
48 QStringList anEmptyList;
52 void HYDROData_Object::Update( const bool theIsForce )
56 QVariant HYDROData_Object::GetDataVariant()
61 bool HYDROData_Object::IsRemoved() const
63 return !myLab.HasAttribute();
66 void HYDROData_Object::Remove()
68 return myLab.ForgetAllAttributes(Standard_True);
71 HYDROData_Object::HYDROData_Object()
75 HYDROData_Object::~HYDROData_Object()
79 void HYDROData_Object::CopyTo(Handle_HYDROData_Object theDestination) const
81 TDF_CopyLabel aCopy(myLab, theDestination->Label());
85 void HYDROData_Object::SetLabel(TDF_Label theLabel)
90 void HYDROData_Object::SaveByteArray(const int theTag,
91 const char* theData, const int theLen)
93 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
94 // array is empty, remove the attribute
96 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
99 // store data of image in byte array
100 Handle(TDataStd_ByteArray) aData;
101 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
102 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
104 // copy bytes one by one
105 if (aData->Length() != theLen) {
106 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
107 for(int a = 0; a < theLen; a++)
108 aNewData->SetValue(a + 1, theData[a]);
109 aData->ChangeArray(aNewData);
111 for(int a = 0; a < theLen; a++)
112 aData->SetValue(a + 1, theData[a]);
116 const char* HYDROData_Object::ByteArray(const int theTag, int& theLen) const
118 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
119 Handle(TDataStd_ByteArray) aData;
120 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
121 return NULL; // return empty image if there is no array
122 theLen = aData->Length();
124 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
128 int HYDROData_Object::NbReferenceObjects( const int theTag ) const
130 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
131 return aRefs.IsNull() ? 0 : aRefs->Extent();
134 void HYDROData_Object::AddReferenceObject( const Handle_HYDROData_Object& theObj,
137 if ( theObj.IsNull() )
140 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
141 aRefs->Append( theObj->Label() );
144 void HYDROData_Object::SetReferenceObject( const Handle_HYDROData_Object& theObj,
148 if ( theObj.IsNull() )
150 RemoveReferenceObject( theTag, theIndex );
154 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
156 if ( theIndex >= aRefs->Extent() )
158 aRefs->Append( theObj->Label() );
160 else if ( theIndex < 0 )
162 aRefs->Prepend( theObj->Label() );
166 RemoveReferenceObject( theTag, theIndex );
168 Handle(HYDROData_Object) aBeforeObj = GetReferenceObject( theTag, theIndex );
170 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
171 if ( !aBeforeObj.IsNull() )
172 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
174 aRefs->Append( theObj->Label() );
178 void HYDROData_Object::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
181 ClearReferenceObjects( theTag );
182 if ( theObjects.IsEmpty() )
185 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
186 for ( ; anIter.More(); anIter.Next() )
187 AddReferenceObject( anIter.Value(), theTag );
190 Handle(HYDROData_Object) HYDROData_Object::GetReferenceObject( const int theTag,
191 const int theIndex ) const
193 Handle(HYDROData_Object) aRes;
195 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
196 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
199 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
200 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
202 const TDF_Label& aRefLabel = anIter.Value();
203 aRes = HYDROData_Iterator::Object( aRefLabel );
208 HYDROData_SequenceOfObjects HYDROData_Object::GetReferenceObjects( const int theTag ) const
210 HYDROData_SequenceOfObjects aRes;
212 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
213 if ( aRefs.IsNull() )
216 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
217 for ( ; anIter.More(); anIter.Next() )
219 const TDF_Label& aRefLabel = anIter.Value();
221 Handle(HYDROData_Object) aRefObject = HYDROData_Iterator::Object( aRefLabel );
222 if ( aRefObject.IsNull() )
225 aRes.Append( aRefObject );
231 void HYDROData_Object::RemoveReferenceObject( const int theTag,
234 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
235 if ( aRefs.IsNull() )
238 if ( aRefs->Extent() == 1 && theIndex == 0 )
240 // remove all if only one
241 ClearReferenceObjects( theTag );
246 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
247 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
249 if ( anIndex != theIndex )
252 const TDF_Label& aRefLabel = anIter.Value();
253 aRefs->Remove( aRefLabel );
256 void HYDROData_Object::ClearReferenceObjects( const int theTag )
258 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
259 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
262 Handle(TDataStd_ReferenceList) HYDROData_Object::getReferenceList( const int theTag,
263 const bool theIsCreate ) const
265 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
267 Handle(TDataStd_ReferenceList) aRefs;
268 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
269 aRefs = TDataStd_ReferenceList::Set( aLabel );
274 void HYDROData_Object::SetColor( const QColor& theColor,
277 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
279 Handle(TDataStd_IntegerArray) aColorArray;
280 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
281 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
283 aColorArray->SetValue( 1, theColor.red() );
284 aColorArray->SetValue( 2, theColor.green() );
285 aColorArray->SetValue( 3, theColor.blue() );
286 aColorArray->SetValue( 4, theColor.alpha() );
289 QColor HYDROData_Object::GetColor( const QColor& theDefColor,
290 const int theTag ) const
292 QColor aResColor = theDefColor;
294 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
296 Handle(TDataStd_IntegerArray) aColorArray;
297 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
299 aResColor.setRed( aColorArray->Value( 1 ) );
300 aResColor.setGreen( aColorArray->Value( 2 ) );
301 aResColor.setBlue( aColorArray->Value( 3 ) );
302 aResColor.setAlpha( aColorArray->Value( 4 ) );
308 void HYDROData_Object::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
309 QStringList& theScript,
310 const Handle(HYDROData_Object)& theRefObject,
311 const QString& theMethod ) const
313 if ( theRefObject.IsNull() )
316 QString aRefObjName = theRefObject->GetName();
317 if ( aRefObjName.isEmpty() )
320 bool anIsToSetObject = true;
322 // The definition of reference polyline must be dumped before this
323 if ( !theTreatedObjects.contains( aRefObjName ) )
325 // Write definition of reference polyline
326 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
327 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
329 QStringList aTmpList = theScript;
330 theScript = aRefObjDump;
332 theScript << QString( "" );
333 theScript << aTmpList;
335 theTreatedObjects.insert( aRefObjName, theRefObject );
339 if ( anIsToSetObject )
341 theScript << QString( "%1.%2( %3 );" )
342 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );