2 #include "HYDROData_Entity.h"
4 #include "HYDROData_Iterator.h"
5 #include "HYDROData_Tool.h"
7 #include <TDataStd_Name.hxx>
8 #include <TDataStd_ByteArray.hxx>
9 #include <TDataStd_UAttribute.hxx>
10 #include <TDataStd_IntegerArray.hxx>
11 #include <TDataStd_BooleanArray.hxx>
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_ReferenceList.hxx>
15 #include <TDF_CopyLabel.hxx>
16 #include <TDF_ListIteratorOfLabelList.hxx>
20 #include <QStringList>
23 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
28 // is equal function for unique object mapping
29 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
31 if ( !theObj1.IsNull() && !theObj2.IsNull() )
32 return theObj1->Label() == theObj2->Label();
36 QString HYDROData_Entity::GetName() const
38 Handle(TDataStd_Name) aName;
39 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
40 TCollection_AsciiString aStr(aName->Get());
41 return QString(aStr.ToCString());
46 void HYDROData_Entity::SetName(const QString& theName)
48 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
51 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
53 QStringList anEmptyList;
57 void HYDROData_Entity::Update()
62 QVariant HYDROData_Entity::GetDataVariant()
67 void HYDROData_Entity::SetToUpdate( bool theFlag )
69 if ( IsMustBeUpdated() == theFlag )
74 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
76 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
77 if ( !aDocument.IsNull() )
79 // Change the states of this and all depended objects
80 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
85 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
89 bool HYDROData_Entity::IsMustBeUpdated() const
91 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
94 bool HYDROData_Entity::IsRemoved() const
96 return !myLab.HasAttribute();
99 void HYDROData_Entity::Remove()
101 return myLab.ForgetAllAttributes( true );
104 bool HYDROData_Entity::CanRemove()
109 HYDROData_Entity::HYDROData_Entity()
113 HYDROData_Entity::~HYDROData_Entity()
117 void HYDROData_Entity::CopyTo(Handle_HYDROData_Entity theDestination) const
119 TDF_CopyLabel aCopy(myLab, theDestination->Label());
123 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
125 Handle(HYDROData_Entity) aFather;
127 if ( !myLab.IsNull() )
129 TDF_Label aFatherLabel = myLab.Father();
131 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
133 aFather = HYDROData_Iterator::Object( aFatherLabel );
134 aFatherLabel = aFatherLabel.Father();
141 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
143 return HYDROData_SequenceOfObjects();
146 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
151 void HYDROData_Entity::SaveByteArray(const int theTag,
152 const char* theData, const int theLen)
154 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
155 // array is empty, remove the attribute
157 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
160 // store data of image in byte array
161 Handle(TDataStd_ByteArray) aData;
162 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
163 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
165 // copy bytes one by one
166 if (aData->Length() != theLen) {
167 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
168 for(int a = 0; a < theLen; a++)
169 aNewData->SetValue(a + 1, theData[a]);
170 aData->ChangeArray(aNewData);
172 for(int a = 0; a < theLen; a++)
173 aData->SetValue(a + 1, theData[a]);
177 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
179 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
180 Handle(TDataStd_ByteArray) aData;
181 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
182 return NULL; // return empty image if there is no array
183 theLen = aData->Length();
185 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
189 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
191 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
192 return aRefs.IsNull() ? 0 : aRefs->Extent();
195 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
196 const int theTag ) const
198 if ( theObj.IsNull() )
201 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
202 if ( aRefs.IsNull() || aRefs->IsEmpty() )
205 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
206 for ( ; aListIt.More(); aListIt.Next() )
208 const TDF_Label& aRefLabel = aListIt.Value();
209 if ( theObj->Label() == aRefLabel )
216 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
219 if ( theObj.IsNull() )
222 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
223 aRefs->Append( theObj->Label() );
226 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
230 if ( theObj.IsNull() )
232 RemoveReferenceObject( theTag, theIndex );
236 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
238 if ( theIndex >= aRefs->Extent() )
240 aRefs->Append( theObj->Label() );
242 else if ( theIndex < 0 )
244 aRefs->Prepend( theObj->Label() );
248 RemoveReferenceObject( theTag, theIndex );
250 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
252 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
253 if ( !aBeforeObj.IsNull() )
254 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
256 aRefs->Append( theObj->Label() );
260 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
262 const int theBeforeIndex )
264 if ( theObj.IsNull() )
267 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
269 if ( theBeforeIndex >= aRefs->Extent() )
271 aRefs->Append( theObj->Label() );
273 else if ( theBeforeIndex < 0 )
275 aRefs->Prepend( theObj->Label() );
279 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
280 if ( !aBeforeObj.IsNull() )
281 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
283 aRefs->Append( theObj->Label() );
287 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
290 ClearReferenceObjects( theTag );
291 if ( theObjects.IsEmpty() )
294 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
295 for ( ; anIter.More(); anIter.Next() )
296 AddReferenceObject( anIter.Value(), theTag );
299 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
300 const int theIndex ) const
302 Handle(HYDROData_Entity) aRes;
304 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
305 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
308 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
309 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
311 const TDF_Label& aRefLabel = anIter.Value();
312 aRes = HYDROData_Iterator::Object( aRefLabel );
317 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
319 HYDROData_SequenceOfObjects aRes;
321 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
322 if ( aRefs.IsNull() )
325 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
326 for ( ; anIter.More(); anIter.Next() )
328 const TDF_Label& aRefLabel = anIter.Value();
330 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
331 if ( aRefObject.IsNull() )
334 aRes.Append( aRefObject );
340 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
343 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
344 if ( aRefs.IsNull() )
347 if ( aRefs->Extent() == 1 )
349 // remove all if only one
350 ClearReferenceObjects( theTag );
354 aRefs->Remove( theRefLabel );
357 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
360 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
361 if ( aRefs.IsNull() )
364 if ( aRefs->Extent() == 1 && theIndex == 0 )
366 // remove all if only one
367 ClearReferenceObjects( theTag );
372 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
373 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
375 if ( anIndex != theIndex || !anIter.More() )
378 const TDF_Label& aRefLabel = anIter.Value();
379 aRefs->Remove( aRefLabel );
382 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
384 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
385 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
388 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
389 const bool theIsCreate ) const
391 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
393 Handle(TDataStd_ReferenceList) aRefs;
394 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
395 aRefs = TDataStd_ReferenceList::Set( aLabel );
400 void HYDROData_Entity::SetColor( const QColor& theColor,
403 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
405 Handle(TDataStd_IntegerArray) aColorArray;
406 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
407 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
409 aColorArray->SetValue( 1, theColor.red() );
410 aColorArray->SetValue( 2, theColor.green() );
411 aColorArray->SetValue( 3, theColor.blue() );
412 aColorArray->SetValue( 4, theColor.alpha() );
415 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
416 const int theTag ) const
418 QColor aResColor = theDefColor;
420 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
422 Handle(TDataStd_IntegerArray) aColorArray;
423 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
425 aResColor.setRed( aColorArray->Value( 1 ) );
426 aResColor.setGreen( aColorArray->Value( 2 ) );
427 aResColor.setBlue( aColorArray->Value( 3 ) );
428 aResColor.setAlpha( aColorArray->Value( 4 ) );
434 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
435 QStringList& theScript,
436 const Handle(HYDROData_Entity)& theRefObject,
437 const QString& theMethod ) const
439 if ( theRefObject.IsNull() )
442 QString aRefObjName = theRefObject->GetName();
443 if ( aRefObjName.isEmpty() )
446 bool anIsToSetObject = true;
448 // The definition of reference object must be dumped before this
449 if ( !theTreatedObjects.contains( aRefObjName ) )
451 // Write definition of reference polyline
452 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
453 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
455 QStringList aTmpList = theScript;
456 theScript = aRefObjDump;
458 theScript << QString( "" );
459 theScript << aTmpList;
461 theTreatedObjects.insert( aRefObjName, theRefObject );
465 if ( anIsToSetObject )
467 theScript << QString( "%1.%2( %3 );" )
468 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );