2 #include "HYDROData_Entity.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_Entity,MMgt_TShared)
23 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
25 // is equal function for unique object mapping
26 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
28 if ( !theObj1.IsNull() && !theObj2.IsNull() )
29 return theObj1->ID() == theObj2->ID();
33 QString HYDROData_Entity::GetName() const
35 Handle(TDataStd_Name) aName;
36 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
37 TCollection_AsciiString aStr(aName->Get());
38 return QString(aStr.ToCString());
43 void HYDROData_Entity::SetName(const QString& theName)
45 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
48 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
50 QStringList anEmptyList;
54 void HYDROData_Entity::Update( const bool theIsForce )
58 QVariant HYDROData_Entity::GetDataVariant()
63 bool HYDROData_Entity::IsRemoved() const
65 return !myLab.HasAttribute();
68 void HYDROData_Entity::Remove()
70 return myLab.ForgetAllAttributes( true );
73 HYDROData_Entity::HYDROData_Entity()
77 HYDROData_Entity::~HYDROData_Entity()
81 void HYDROData_Entity::CopyTo(Handle_HYDROData_Entity theDestination) const
83 TDF_CopyLabel aCopy(myLab, theDestination->Label());
87 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
92 void HYDROData_Entity::SaveByteArray(const int theTag,
93 const char* theData, const int theLen)
95 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
96 // array is empty, remove the attribute
98 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
101 // store data of image in byte array
102 Handle(TDataStd_ByteArray) aData;
103 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
104 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
106 // copy bytes one by one
107 if (aData->Length() != theLen) {
108 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
109 for(int a = 0; a < theLen; a++)
110 aNewData->SetValue(a + 1, theData[a]);
111 aData->ChangeArray(aNewData);
113 for(int a = 0; a < theLen; a++)
114 aData->SetValue(a + 1, theData[a]);
118 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
120 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
121 Handle(TDataStd_ByteArray) aData;
122 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
123 return NULL; // return empty image if there is no array
124 theLen = aData->Length();
126 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
130 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
132 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
133 return aRefs.IsNull() ? 0 : aRefs->Extent();
136 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
139 if ( theObj.IsNull() )
142 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
143 aRefs->Append( theObj->Label() );
146 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
150 if ( theObj.IsNull() )
152 RemoveReferenceObject( theTag, theIndex );
156 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
158 if ( theIndex >= aRefs->Extent() )
160 aRefs->Append( theObj->Label() );
162 else if ( theIndex < 0 )
164 aRefs->Prepend( theObj->Label() );
168 RemoveReferenceObject( theTag, theIndex );
170 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
172 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
173 if ( !aBeforeObj.IsNull() )
174 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
176 aRefs->Append( theObj->Label() );
180 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
182 const int theBeforeIndex )
184 if ( theObj.IsNull() )
187 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
189 if ( theBeforeIndex >= aRefs->Extent() )
191 aRefs->Append( theObj->Label() );
193 else if ( theBeforeIndex < 0 )
195 aRefs->Prepend( theObj->Label() );
199 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
200 if ( !aBeforeObj.IsNull() )
201 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
203 aRefs->Append( theObj->Label() );
207 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
210 ClearReferenceObjects( theTag );
211 if ( theObjects.IsEmpty() )
214 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
215 for ( ; anIter.More(); anIter.Next() )
216 AddReferenceObject( anIter.Value(), theTag );
219 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
220 const int theIndex ) const
222 Handle(HYDROData_Entity) aRes;
224 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
225 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
228 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
229 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
231 const TDF_Label& aRefLabel = anIter.Value();
232 aRes = HYDROData_Iterator::Object( aRefLabel );
237 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
239 HYDROData_SequenceOfObjects aRes;
241 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
242 if ( aRefs.IsNull() )
245 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
246 for ( ; anIter.More(); anIter.Next() )
248 const TDF_Label& aRefLabel = anIter.Value();
250 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
251 if ( aRefObject.IsNull() )
254 aRes.Append( aRefObject );
260 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
263 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
264 if ( aRefs.IsNull() )
267 if ( aRefs->Extent() == 1 )
269 // remove all if only one
270 ClearReferenceObjects( theTag );
274 aRefs->Remove( theRefLabel );
277 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
280 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
281 if ( aRefs.IsNull() )
284 if ( aRefs->Extent() == 1 && theIndex == 0 )
286 // remove all if only one
287 ClearReferenceObjects( theTag );
292 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
293 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
295 if ( anIndex != theIndex || !anIter.More() )
298 const TDF_Label& aRefLabel = anIter.Value();
299 aRefs->Remove( aRefLabel );
302 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
304 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
305 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
308 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
309 const bool theIsCreate ) const
311 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
313 Handle(TDataStd_ReferenceList) aRefs;
314 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
315 aRefs = TDataStd_ReferenceList::Set( aLabel );
320 void HYDROData_Entity::SetColor( const QColor& theColor,
323 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
325 Handle(TDataStd_IntegerArray) aColorArray;
326 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
327 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
329 aColorArray->SetValue( 1, theColor.red() );
330 aColorArray->SetValue( 2, theColor.green() );
331 aColorArray->SetValue( 3, theColor.blue() );
332 aColorArray->SetValue( 4, theColor.alpha() );
335 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
336 const int theTag ) const
338 QColor aResColor = theDefColor;
340 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
342 Handle(TDataStd_IntegerArray) aColorArray;
343 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
345 aResColor.setRed( aColorArray->Value( 1 ) );
346 aResColor.setGreen( aColorArray->Value( 2 ) );
347 aResColor.setBlue( aColorArray->Value( 3 ) );
348 aResColor.setAlpha( aColorArray->Value( 4 ) );
354 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
355 QStringList& theScript,
356 const Handle(HYDROData_Entity)& theRefObject,
357 const QString& theMethod ) const
359 if ( theRefObject.IsNull() )
362 QString aRefObjName = theRefObject->GetName();
363 if ( aRefObjName.isEmpty() )
366 bool anIsToSetObject = true;
368 // The definition of reference object must be dumped before this
369 if ( !theTreatedObjects.contains( aRefObjName ) )
371 // Write definition of reference polyline
372 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
373 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
375 QStringList aTmpList = theScript;
376 theScript = aRefObjDump;
378 theScript << QString( "" );
379 theScript << aTmpList;
381 theTreatedObjects.insert( aRefObjName, theRefObject );
385 if ( anIsToSetObject )
387 theScript << QString( "%1.%2( %3 );" )
388 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );