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::CanBeUpdated() const
99 bool HYDROData_Entity::IsRemoved() const
101 return !myLab.HasAttribute();
104 void HYDROData_Entity::Remove()
106 return myLab.ForgetAllAttributes( true );
109 bool HYDROData_Entity::CanRemove()
114 HYDROData_Entity::HYDROData_Entity()
118 HYDROData_Entity::~HYDROData_Entity()
122 void HYDROData_Entity::CopyTo(Handle_HYDROData_Entity theDestination) const
124 TDF_CopyLabel aCopy(myLab, theDestination->Label());
128 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
130 Handle(HYDROData_Entity) aFather;
132 if ( !myLab.IsNull() )
134 TDF_Label aFatherLabel = myLab.Father();
136 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
138 aFather = HYDROData_Iterator::Object( aFatherLabel );
139 aFatherLabel = aFatherLabel.Father();
146 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
148 return HYDROData_SequenceOfObjects();
151 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
156 void HYDROData_Entity::SaveByteArray(const int theTag,
157 const char* theData, const int theLen)
159 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
160 // array is empty, remove the attribute
162 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
165 // store data of image in byte array
166 Handle(TDataStd_ByteArray) aData;
167 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
168 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
170 // copy bytes one by one
171 if (aData->Length() != theLen) {
172 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
173 for(int a = 0; a < theLen; a++)
174 aNewData->SetValue(a + 1, theData[a]);
175 aData->ChangeArray(aNewData);
177 for(int a = 0; a < theLen; a++)
178 aData->SetValue(a + 1, theData[a]);
182 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
184 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
185 Handle(TDataStd_ByteArray) aData;
186 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
187 return NULL; // return empty image if there is no array
188 theLen = aData->Length();
190 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
194 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
196 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
197 return aRefs.IsNull() ? 0 : aRefs->Extent();
200 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
201 const int theTag ) const
203 if ( theObj.IsNull() )
206 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
207 if ( aRefs.IsNull() || aRefs->IsEmpty() )
210 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
211 for ( ; aListIt.More(); aListIt.Next() )
213 const TDF_Label& aRefLabel = aListIt.Value();
214 if ( theObj->Label() == aRefLabel )
221 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
224 if ( theObj.IsNull() )
227 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
228 aRefs->Append( theObj->Label() );
231 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
235 if ( theObj.IsNull() )
237 RemoveReferenceObject( theTag, theIndex );
241 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
243 if ( theIndex >= aRefs->Extent() )
245 aRefs->Append( theObj->Label() );
247 else if ( theIndex < 0 )
249 aRefs->Prepend( theObj->Label() );
253 RemoveReferenceObject( theTag, theIndex );
255 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
257 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
258 if ( !aBeforeObj.IsNull() )
259 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
261 aRefs->Append( theObj->Label() );
265 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
267 const int theBeforeIndex )
269 if ( theObj.IsNull() )
272 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
274 if ( theBeforeIndex >= aRefs->Extent() )
276 aRefs->Append( theObj->Label() );
278 else if ( theBeforeIndex < 0 )
280 aRefs->Prepend( theObj->Label() );
284 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
285 if ( !aBeforeObj.IsNull() )
286 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
288 aRefs->Append( theObj->Label() );
292 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
295 ClearReferenceObjects( theTag );
296 if ( theObjects.IsEmpty() )
299 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
300 for ( ; anIter.More(); anIter.Next() )
301 AddReferenceObject( anIter.Value(), theTag );
304 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
305 const int theIndex ) const
307 Handle(HYDROData_Entity) aRes;
309 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
310 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
313 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
314 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
316 const TDF_Label& aRefLabel = anIter.Value();
317 aRes = HYDROData_Iterator::Object( aRefLabel );
322 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
324 HYDROData_SequenceOfObjects aRes;
326 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
327 if ( aRefs.IsNull() )
330 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
331 for ( ; anIter.More(); anIter.Next() )
333 const TDF_Label& aRefLabel = anIter.Value();
335 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
336 if ( aRefObject.IsNull() )
339 aRes.Append( aRefObject );
345 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
348 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
349 if ( aRefs.IsNull() )
352 if ( aRefs->Extent() == 1 )
354 // remove all if only one
355 ClearReferenceObjects( theTag );
359 aRefs->Remove( theRefLabel );
362 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
365 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
366 if ( aRefs.IsNull() )
369 if ( aRefs->Extent() == 1 && theIndex == 0 )
371 // remove all if only one
372 ClearReferenceObjects( theTag );
377 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
378 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
380 if ( anIndex != theIndex || !anIter.More() )
383 const TDF_Label& aRefLabel = anIter.Value();
384 aRefs->Remove( aRefLabel );
387 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
389 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
390 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
393 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
394 const bool theIsCreate ) const
396 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
398 Handle(TDataStd_ReferenceList) aRefs;
399 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
400 aRefs = TDataStd_ReferenceList::Set( aLabel );
405 void HYDROData_Entity::SetColor( const QColor& theColor,
408 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
410 Handle(TDataStd_IntegerArray) aColorArray;
411 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
412 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
414 aColorArray->SetValue( 1, theColor.red() );
415 aColorArray->SetValue( 2, theColor.green() );
416 aColorArray->SetValue( 3, theColor.blue() );
417 aColorArray->SetValue( 4, theColor.alpha() );
420 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
421 const int theTag ) const
423 QColor aResColor = theDefColor;
425 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
427 Handle(TDataStd_IntegerArray) aColorArray;
428 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
430 aResColor.setRed( aColorArray->Value( 1 ) );
431 aResColor.setGreen( aColorArray->Value( 2 ) );
432 aResColor.setBlue( aColorArray->Value( 3 ) );
433 aResColor.setAlpha( aColorArray->Value( 4 ) );
439 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
440 QStringList& theScript,
441 const Handle(HYDROData_Entity)& theRefObject,
442 const QString& theMethod ) const
444 if ( theRefObject.IsNull() )
447 QString aRefObjName = theRefObject->GetName();
448 if ( aRefObjName.isEmpty() )
451 bool anIsToSetObject = true;
453 // The definition of reference object must be dumped before this
454 if ( !theTreatedObjects.contains( aRefObjName ) )
456 // Write definition of reference polyline
457 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
458 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
460 QStringList aTmpList = theScript;
461 theScript = aRefObjDump;
463 theScript << QString( "" );
464 theScript << aTmpList;
466 theTreatedObjects.insert( aRefObjName, theRefObject );
470 if ( anIsToSetObject )
472 theScript << QString( "%1.%2( %3 );" )
473 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );