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 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
89 Handle(HYDROData_Entity) aFather;
91 if ( !myLab.IsNull() )
93 TDF_Label aFatherLabel = myLab.Father();
95 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
97 aFather = HYDROData_Iterator::Object( aFatherLabel );
98 aFatherLabel = aFatherLabel.Father();
105 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
110 void HYDROData_Entity::SaveByteArray(const int theTag,
111 const char* theData, const int theLen)
113 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
114 // array is empty, remove the attribute
116 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
119 // store data of image in byte array
120 Handle(TDataStd_ByteArray) aData;
121 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
122 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
124 // copy bytes one by one
125 if (aData->Length() != theLen) {
126 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
127 for(int a = 0; a < theLen; a++)
128 aNewData->SetValue(a + 1, theData[a]);
129 aData->ChangeArray(aNewData);
131 for(int a = 0; a < theLen; a++)
132 aData->SetValue(a + 1, theData[a]);
136 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
138 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
139 Handle(TDataStd_ByteArray) aData;
140 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
141 return NULL; // return empty image if there is no array
142 theLen = aData->Length();
144 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
148 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
150 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
151 return aRefs.IsNull() ? 0 : aRefs->Extent();
154 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
155 const int theTag ) const
157 if ( theObj.IsNull() )
160 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
161 if ( aRefs.IsNull() || aRefs->IsEmpty() )
164 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
165 for ( ; aListIt.More(); aListIt.Next() )
167 const TDF_Label& aRefLabel = aListIt.Value();
168 if ( theObj->Label() == aRefLabel )
175 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
178 if ( theObj.IsNull() )
181 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
182 aRefs->Append( theObj->Label() );
185 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
189 if ( theObj.IsNull() )
191 RemoveReferenceObject( theTag, theIndex );
195 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
197 if ( theIndex >= aRefs->Extent() )
199 aRefs->Append( theObj->Label() );
201 else if ( theIndex < 0 )
203 aRefs->Prepend( theObj->Label() );
207 RemoveReferenceObject( theTag, theIndex );
209 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
211 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
212 if ( !aBeforeObj.IsNull() )
213 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
215 aRefs->Append( theObj->Label() );
219 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
221 const int theBeforeIndex )
223 if ( theObj.IsNull() )
226 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
228 if ( theBeforeIndex >= aRefs->Extent() )
230 aRefs->Append( theObj->Label() );
232 else if ( theBeforeIndex < 0 )
234 aRefs->Prepend( theObj->Label() );
238 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
239 if ( !aBeforeObj.IsNull() )
240 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
242 aRefs->Append( theObj->Label() );
246 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
249 ClearReferenceObjects( theTag );
250 if ( theObjects.IsEmpty() )
253 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
254 for ( ; anIter.More(); anIter.Next() )
255 AddReferenceObject( anIter.Value(), theTag );
258 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
259 const int theIndex ) const
261 Handle(HYDROData_Entity) aRes;
263 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
264 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
267 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
268 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
270 const TDF_Label& aRefLabel = anIter.Value();
271 aRes = HYDROData_Iterator::Object( aRefLabel );
276 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
278 HYDROData_SequenceOfObjects aRes;
280 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
281 if ( aRefs.IsNull() )
284 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
285 for ( ; anIter.More(); anIter.Next() )
287 const TDF_Label& aRefLabel = anIter.Value();
289 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
290 if ( aRefObject.IsNull() )
293 aRes.Append( aRefObject );
299 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
302 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
303 if ( aRefs.IsNull() )
306 if ( aRefs->Extent() == 1 )
308 // remove all if only one
309 ClearReferenceObjects( theTag );
313 aRefs->Remove( theRefLabel );
316 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
319 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
320 if ( aRefs.IsNull() )
323 if ( aRefs->Extent() == 1 && theIndex == 0 )
325 // remove all if only one
326 ClearReferenceObjects( theTag );
331 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
332 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
334 if ( anIndex != theIndex || !anIter.More() )
337 const TDF_Label& aRefLabel = anIter.Value();
338 aRefs->Remove( aRefLabel );
341 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
343 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
344 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
347 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
348 const bool theIsCreate ) const
350 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
352 Handle(TDataStd_ReferenceList) aRefs;
353 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
354 aRefs = TDataStd_ReferenceList::Set( aLabel );
359 void HYDROData_Entity::SetColor( const QColor& theColor,
362 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
364 Handle(TDataStd_IntegerArray) aColorArray;
365 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
366 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
368 aColorArray->SetValue( 1, theColor.red() );
369 aColorArray->SetValue( 2, theColor.green() );
370 aColorArray->SetValue( 3, theColor.blue() );
371 aColorArray->SetValue( 4, theColor.alpha() );
374 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
375 const int theTag ) const
377 QColor aResColor = theDefColor;
379 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
381 Handle(TDataStd_IntegerArray) aColorArray;
382 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
384 aResColor.setRed( aColorArray->Value( 1 ) );
385 aResColor.setGreen( aColorArray->Value( 2 ) );
386 aResColor.setBlue( aColorArray->Value( 3 ) );
387 aResColor.setAlpha( aColorArray->Value( 4 ) );
393 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
394 QStringList& theScript,
395 const Handle(HYDROData_Entity)& theRefObject,
396 const QString& theMethod ) const
398 if ( theRefObject.IsNull() )
401 QString aRefObjName = theRefObject->GetName();
402 if ( aRefObjName.isEmpty() )
405 bool anIsToSetObject = true;
407 // The definition of reference object must be dumped before this
408 if ( !theTreatedObjects.contains( aRefObjName ) )
410 // Write definition of reference polyline
411 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
412 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
414 QStringList aTmpList = theScript;
415 theScript = aRefObjDump;
417 theScript << QString( "" );
418 theScript << aTmpList;
420 theTreatedObjects.insert( aRefObjName, theRefObject );
424 if ( anIsToSetObject )
426 theScript << QString( "%1.%2( %3 );" )
427 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );