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 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
24 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
25 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
27 // is equal function for unique object mapping
28 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
30 if ( !theObj1.IsNull() && !theObj2.IsNull() )
31 return theObj1->Label() == theObj2->Label();
35 QString HYDROData_Entity::GetName() const
37 Handle(TDataStd_Name) aName;
38 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
39 TCollection_AsciiString aStr(aName->Get());
40 return QString(aStr.ToCString());
45 void HYDROData_Entity::SetName(const QString& theName)
47 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
50 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
52 QStringList anEmptyList;
56 void HYDROData_Entity::Update()
60 QVariant HYDROData_Entity::GetDataVariant()
65 void HYDROData_Entity::SetToUpdate(bool theFlag)
69 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
73 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
77 bool HYDROData_Entity::IsMustBeUpdated() const
79 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
82 bool HYDROData_Entity::IsRemoved() const
84 return !myLab.HasAttribute();
87 void HYDROData_Entity::Remove()
89 return myLab.ForgetAllAttributes( true );
92 HYDROData_Entity::HYDROData_Entity()
96 HYDROData_Entity::~HYDROData_Entity()
100 void HYDROData_Entity::CopyTo(Handle_HYDROData_Entity theDestination) const
102 TDF_CopyLabel aCopy(myLab, theDestination->Label());
106 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
108 Handle(HYDROData_Entity) aFather;
110 if ( !myLab.IsNull() )
112 TDF_Label aFatherLabel = myLab.Father();
114 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
116 aFather = HYDROData_Iterator::Object( aFatherLabel );
117 aFatherLabel = aFatherLabel.Father();
124 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
129 void HYDROData_Entity::SaveByteArray(const int theTag,
130 const char* theData, const int theLen)
132 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
133 // array is empty, remove the attribute
135 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
138 // store data of image in byte array
139 Handle(TDataStd_ByteArray) aData;
140 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
141 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
143 // copy bytes one by one
144 if (aData->Length() != theLen) {
145 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
146 for(int a = 0; a < theLen; a++)
147 aNewData->SetValue(a + 1, theData[a]);
148 aData->ChangeArray(aNewData);
150 for(int a = 0; a < theLen; a++)
151 aData->SetValue(a + 1, theData[a]);
155 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
157 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
158 Handle(TDataStd_ByteArray) aData;
159 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
160 return NULL; // return empty image if there is no array
161 theLen = aData->Length();
163 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
167 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
169 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
170 return aRefs.IsNull() ? 0 : aRefs->Extent();
173 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
174 const int theTag ) const
176 if ( theObj.IsNull() )
179 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
180 if ( aRefs.IsNull() || aRefs->IsEmpty() )
183 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
184 for ( ; aListIt.More(); aListIt.Next() )
186 const TDF_Label& aRefLabel = aListIt.Value();
187 if ( theObj->Label() == aRefLabel )
194 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
197 if ( theObj.IsNull() )
200 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
201 aRefs->Append( theObj->Label() );
204 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
208 if ( theObj.IsNull() )
210 RemoveReferenceObject( theTag, theIndex );
214 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
216 if ( theIndex >= aRefs->Extent() )
218 aRefs->Append( theObj->Label() );
220 else if ( theIndex < 0 )
222 aRefs->Prepend( theObj->Label() );
226 RemoveReferenceObject( theTag, theIndex );
228 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
230 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
231 if ( !aBeforeObj.IsNull() )
232 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
234 aRefs->Append( theObj->Label() );
238 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
240 const int theBeforeIndex )
242 if ( theObj.IsNull() )
245 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
247 if ( theBeforeIndex >= aRefs->Extent() )
249 aRefs->Append( theObj->Label() );
251 else if ( theBeforeIndex < 0 )
253 aRefs->Prepend( theObj->Label() );
257 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
258 if ( !aBeforeObj.IsNull() )
259 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
261 aRefs->Append( theObj->Label() );
265 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
268 ClearReferenceObjects( theTag );
269 if ( theObjects.IsEmpty() )
272 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
273 for ( ; anIter.More(); anIter.Next() )
274 AddReferenceObject( anIter.Value(), theTag );
277 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
278 const int theIndex ) const
280 Handle(HYDROData_Entity) aRes;
282 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
283 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
286 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
287 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
289 const TDF_Label& aRefLabel = anIter.Value();
290 aRes = HYDROData_Iterator::Object( aRefLabel );
295 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
297 HYDROData_SequenceOfObjects aRes;
299 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
300 if ( aRefs.IsNull() )
303 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
304 for ( ; anIter.More(); anIter.Next() )
306 const TDF_Label& aRefLabel = anIter.Value();
308 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
309 if ( aRefObject.IsNull() )
312 aRes.Append( aRefObject );
318 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
321 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
322 if ( aRefs.IsNull() )
325 if ( aRefs->Extent() == 1 )
327 // remove all if only one
328 ClearReferenceObjects( theTag );
332 aRefs->Remove( theRefLabel );
335 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
338 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
339 if ( aRefs.IsNull() )
342 if ( aRefs->Extent() == 1 && theIndex == 0 )
344 // remove all if only one
345 ClearReferenceObjects( theTag );
350 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
351 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
353 if ( anIndex != theIndex || !anIter.More() )
356 const TDF_Label& aRefLabel = anIter.Value();
357 aRefs->Remove( aRefLabel );
360 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
362 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
363 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
366 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
367 const bool theIsCreate ) const
369 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
371 Handle(TDataStd_ReferenceList) aRefs;
372 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
373 aRefs = TDataStd_ReferenceList::Set( aLabel );
378 void HYDROData_Entity::SetColor( const QColor& theColor,
381 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
383 Handle(TDataStd_IntegerArray) aColorArray;
384 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
385 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
387 aColorArray->SetValue( 1, theColor.red() );
388 aColorArray->SetValue( 2, theColor.green() );
389 aColorArray->SetValue( 3, theColor.blue() );
390 aColorArray->SetValue( 4, theColor.alpha() );
393 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
394 const int theTag ) const
396 QColor aResColor = theDefColor;
398 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
400 Handle(TDataStd_IntegerArray) aColorArray;
401 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
403 aResColor.setRed( aColorArray->Value( 1 ) );
404 aResColor.setGreen( aColorArray->Value( 2 ) );
405 aResColor.setBlue( aColorArray->Value( 3 ) );
406 aResColor.setAlpha( aColorArray->Value( 4 ) );
412 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
413 QStringList& theScript,
414 const Handle(HYDROData_Entity)& theRefObject,
415 const QString& theMethod ) const
417 if ( theRefObject.IsNull() )
420 QString aRefObjName = theRefObject->GetName();
421 if ( aRefObjName.isEmpty() )
424 bool anIsToSetObject = true;
426 // The definition of reference object must be dumped before this
427 if ( !theTreatedObjects.contains( aRefObjName ) )
429 // Write definition of reference polyline
430 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
431 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
433 QStringList aTmpList = theScript;
434 theScript = aRefObjDump;
436 theScript << QString( "" );
437 theScript << aTmpList;
439 theTreatedObjects.insert( aRefObjName, theRefObject );
443 if ( anIsToSetObject )
445 theScript << QString( "%1.%2( %3 );" )
446 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );