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 bool HYDROData_Entity::CanRemove()
97 HYDROData_Entity::HYDROData_Entity()
101 HYDROData_Entity::~HYDROData_Entity()
105 void HYDROData_Entity::CopyTo(Handle_HYDROData_Entity theDestination) const
107 TDF_CopyLabel aCopy(myLab, theDestination->Label());
111 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
113 Handle(HYDROData_Entity) aFather;
115 if ( !myLab.IsNull() )
117 TDF_Label aFatherLabel = myLab.Father();
119 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
121 aFather = HYDROData_Iterator::Object( aFatherLabel );
122 aFatherLabel = aFatherLabel.Father();
129 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
131 return HYDROData_SequenceOfObjects();
134 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
139 void HYDROData_Entity::SaveByteArray(const int theTag,
140 const char* theData, const int theLen)
142 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
143 // array is empty, remove the attribute
145 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
148 // store data of image in byte array
149 Handle(TDataStd_ByteArray) aData;
150 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
151 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
153 // copy bytes one by one
154 if (aData->Length() != theLen) {
155 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
156 for(int a = 0; a < theLen; a++)
157 aNewData->SetValue(a + 1, theData[a]);
158 aData->ChangeArray(aNewData);
160 for(int a = 0; a < theLen; a++)
161 aData->SetValue(a + 1, theData[a]);
165 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
167 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
168 Handle(TDataStd_ByteArray) aData;
169 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
170 return NULL; // return empty image if there is no array
171 theLen = aData->Length();
173 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
177 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
179 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
180 return aRefs.IsNull() ? 0 : aRefs->Extent();
183 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
184 const int theTag ) const
186 if ( theObj.IsNull() )
189 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
190 if ( aRefs.IsNull() || aRefs->IsEmpty() )
193 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
194 for ( ; aListIt.More(); aListIt.Next() )
196 const TDF_Label& aRefLabel = aListIt.Value();
197 if ( theObj->Label() == aRefLabel )
204 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
207 if ( theObj.IsNull() )
210 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
211 aRefs->Append( theObj->Label() );
214 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
218 if ( theObj.IsNull() )
220 RemoveReferenceObject( theTag, theIndex );
224 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
226 if ( theIndex >= aRefs->Extent() )
228 aRefs->Append( theObj->Label() );
230 else if ( theIndex < 0 )
232 aRefs->Prepend( theObj->Label() );
236 RemoveReferenceObject( theTag, theIndex );
238 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
240 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
241 if ( !aBeforeObj.IsNull() )
242 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
244 aRefs->Append( theObj->Label() );
248 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
250 const int theBeforeIndex )
252 if ( theObj.IsNull() )
255 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
257 if ( theBeforeIndex >= aRefs->Extent() )
259 aRefs->Append( theObj->Label() );
261 else if ( theBeforeIndex < 0 )
263 aRefs->Prepend( theObj->Label() );
267 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
268 if ( !aBeforeObj.IsNull() )
269 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
271 aRefs->Append( theObj->Label() );
275 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
278 ClearReferenceObjects( theTag );
279 if ( theObjects.IsEmpty() )
282 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
283 for ( ; anIter.More(); anIter.Next() )
284 AddReferenceObject( anIter.Value(), theTag );
287 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
288 const int theIndex ) const
290 Handle(HYDROData_Entity) aRes;
292 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
293 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
296 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
297 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
299 const TDF_Label& aRefLabel = anIter.Value();
300 aRes = HYDROData_Iterator::Object( aRefLabel );
305 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
307 HYDROData_SequenceOfObjects aRes;
309 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
310 if ( aRefs.IsNull() )
313 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
314 for ( ; anIter.More(); anIter.Next() )
316 const TDF_Label& aRefLabel = anIter.Value();
318 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
319 if ( aRefObject.IsNull() )
322 aRes.Append( aRefObject );
328 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
331 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
332 if ( aRefs.IsNull() )
335 if ( aRefs->Extent() == 1 )
337 // remove all if only one
338 ClearReferenceObjects( theTag );
342 aRefs->Remove( theRefLabel );
345 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
348 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
349 if ( aRefs.IsNull() )
352 if ( aRefs->Extent() == 1 && theIndex == 0 )
354 // remove all if only one
355 ClearReferenceObjects( theTag );
360 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
361 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
363 if ( anIndex != theIndex || !anIter.More() )
366 const TDF_Label& aRefLabel = anIter.Value();
367 aRefs->Remove( aRefLabel );
370 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
372 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
373 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
376 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
377 const bool theIsCreate ) const
379 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
381 Handle(TDataStd_ReferenceList) aRefs;
382 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
383 aRefs = TDataStd_ReferenceList::Set( aLabel );
388 void HYDROData_Entity::SetColor( const QColor& theColor,
391 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
393 Handle(TDataStd_IntegerArray) aColorArray;
394 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
395 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
397 aColorArray->SetValue( 1, theColor.red() );
398 aColorArray->SetValue( 2, theColor.green() );
399 aColorArray->SetValue( 3, theColor.blue() );
400 aColorArray->SetValue( 4, theColor.alpha() );
403 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
404 const int theTag ) const
406 QColor aResColor = theDefColor;
408 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
410 Handle(TDataStd_IntegerArray) aColorArray;
411 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
413 aResColor.setRed( aColorArray->Value( 1 ) );
414 aResColor.setGreen( aColorArray->Value( 2 ) );
415 aResColor.setBlue( aColorArray->Value( 3 ) );
416 aResColor.setAlpha( aColorArray->Value( 4 ) );
422 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
423 QStringList& theScript,
424 const Handle(HYDROData_Entity)& theRefObject,
425 const QString& theMethod ) const
427 if ( theRefObject.IsNull() )
430 QString aRefObjName = theRefObject->GetName();
431 if ( aRefObjName.isEmpty() )
434 bool anIsToSetObject = true;
436 // The definition of reference object must be dumped before this
437 if ( !theTreatedObjects.contains( aRefObjName ) )
439 // Write definition of reference polyline
440 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
441 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
443 QStringList aTmpList = theScript;
444 theScript = aRefObjDump;
446 theScript << QString( "" );
447 theScript << aTmpList;
449 theTreatedObjects.insert( aRefObjName, theRefObject );
453 if ( anIsToSetObject )
455 theScript << QString( "%1.%2( %3 );" )
456 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );