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 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
61 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
62 if ( aDocument.IsNull() )
65 QString aDocName = aDocument->GetDocPyName();
66 QString aName = GetName();
68 aResList << QString( "%1 = %2.CreateObject( %3 );" )
69 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
70 aResList << QString( "%1.SetName( \"%2\" );" )
71 .arg( aName ).arg( aName );
72 aResList << QString( "" );
77 void HYDROData_Entity::Update()
82 QString HYDROData_Entity::getPyTypeID() const
86 case KIND_IMAGE: return "KIND_IMAGE";
87 case KIND_POLYLINE: return "KIND_POLYLINE";
88 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
89 case KIND_ALTITUDE: return "KIND_ALTITUDE";
90 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
91 case KIND_RIVER: return "KIND_RIVER";
92 case KIND_STREAM: return "KIND_STREAM";
93 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
94 case KIND_CHANNEL: return "KIND_CHANNEL";
95 case KIND_OBSTACLE: return "KIND_OBSTACLE";
96 case KIND_DIGUE: return "KIND_DIGUE";
97 case KIND_PROFILE: return "KIND_PROFILE";
98 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
99 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
100 case KIND_CALCULATION: return "KIND_CALCULATION";
101 case KIND_ZONE: return "KIND_ZONE";
102 case KIND_REGION: return "KIND_REGION";
103 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
104 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
105 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
106 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
107 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
108 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
109 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
110 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
111 default: return "KIND_UNKNOWN"; ///! Unrecognized object
115 QVariant HYDROData_Entity::GetDataVariant()
120 void HYDROData_Entity::SetToUpdate( bool theFlag )
122 if ( IsMustBeUpdated() == theFlag )
127 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
129 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
130 if ( !aDocument.IsNull() )
132 // Change the states of this and all depended objects
133 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
138 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
142 bool HYDROData_Entity::IsMustBeUpdated() const
144 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
147 bool HYDROData_Entity::CanBeUpdated() const
152 bool HYDROData_Entity::IsRemoved() const
154 return !myLab.HasAttribute();
157 void HYDROData_Entity::Remove()
159 return myLab.ForgetAllAttributes( true );
162 bool HYDROData_Entity::CanRemove()
167 HYDROData_Entity::HYDROData_Entity()
171 HYDROData_Entity::~HYDROData_Entity()
175 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
177 TDF_CopyLabel aCopy(myLab, theDestination->Label());
181 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
183 Handle(HYDROData_Entity) aFather;
185 if ( !myLab.IsNull() )
187 TDF_Label aFatherLabel = myLab.Father();
189 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
191 aFather = HYDROData_Iterator::Object( aFatherLabel );
192 aFatherLabel = aFatherLabel.Father();
199 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
201 return HYDROData_SequenceOfObjects();
204 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
209 void HYDROData_Entity::SaveByteArray(const int theTag,
210 const char* theData, const int theLen)
212 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
213 // array is empty, remove the attribute
215 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
218 // store data of image in byte array
219 Handle(TDataStd_ByteArray) aData;
220 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
221 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
223 // copy bytes one by one
224 if (aData->Length() != theLen) {
225 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
226 for(int a = 0; a < theLen; a++)
227 aNewData->SetValue(a + 1, theData[a]);
228 aData->ChangeArray(aNewData);
230 for(int a = 0; a < theLen; a++)
231 aData->SetValue(a + 1, theData[a]);
235 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
237 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
238 Handle(TDataStd_ByteArray) aData;
239 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
240 return NULL; // return empty image if there is no array
241 theLen = aData->Length();
243 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
247 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
249 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
250 return aRefs.IsNull() ? 0 : aRefs->Extent();
253 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
254 const int theTag ) const
256 if ( theObj.IsNull() )
259 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
260 if ( aRefs.IsNull() || aRefs->IsEmpty() )
263 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
264 for ( ; aListIt.More(); aListIt.Next() )
266 const TDF_Label& aRefLabel = aListIt.Value();
267 if ( theObj->Label() == aRefLabel )
274 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
277 if ( theObj.IsNull() )
280 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
281 aRefs->Append( theObj->Label() );
284 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
288 if ( theObj.IsNull() )
290 RemoveReferenceObject( theTag, theIndex );
294 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
296 if ( theIndex >= aRefs->Extent() )
298 aRefs->Append( theObj->Label() );
300 else if ( theIndex < 0 )
302 aRefs->Prepend( theObj->Label() );
306 RemoveReferenceObject( theTag, theIndex );
308 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
310 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
311 if ( !aBeforeObj.IsNull() )
312 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
314 aRefs->Append( theObj->Label() );
318 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
320 const int theBeforeIndex )
322 if ( theObj.IsNull() )
325 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
327 if ( theBeforeIndex >= aRefs->Extent() )
329 aRefs->Append( theObj->Label() );
331 else if ( theBeforeIndex < 0 )
333 aRefs->Prepend( theObj->Label() );
337 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
338 if ( !aBeforeObj.IsNull() )
339 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
341 aRefs->Append( theObj->Label() );
345 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
348 ClearReferenceObjects( theTag );
349 if ( theObjects.IsEmpty() )
352 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
353 for ( ; anIter.More(); anIter.Next() )
354 AddReferenceObject( anIter.Value(), theTag );
357 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
358 const int theIndex ) const
360 Handle(HYDROData_Entity) aRes;
362 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
363 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
366 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
367 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
369 const TDF_Label& aRefLabel = anIter.Value();
370 aRes = HYDROData_Iterator::Object( aRefLabel );
375 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
377 HYDROData_SequenceOfObjects aRes;
379 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
380 if ( aRefs.IsNull() )
383 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
384 for ( ; anIter.More(); anIter.Next() )
386 const TDF_Label& aRefLabel = anIter.Value();
388 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
389 if ( aRefObject.IsNull() )
392 aRes.Append( aRefObject );
398 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
401 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
402 if ( aRefs.IsNull() )
405 if ( aRefs->Extent() == 1 )
407 // remove all if only one
408 ClearReferenceObjects( theTag );
412 aRefs->Remove( theRefLabel );
415 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
418 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
419 if ( aRefs.IsNull() )
422 if ( aRefs->Extent() == 1 && theIndex == 0 )
424 // remove all if only one
425 ClearReferenceObjects( theTag );
430 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
431 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
433 if ( anIndex != theIndex || !anIter.More() )
436 const TDF_Label& aRefLabel = anIter.Value();
437 aRefs->Remove( aRefLabel );
440 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
442 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
443 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
446 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
447 const bool theIsCreate ) const
449 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
451 Handle(TDataStd_ReferenceList) aRefs;
452 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
453 aRefs = TDataStd_ReferenceList::Set( aLabel );
458 void HYDROData_Entity::SetColor( const QColor& theColor,
461 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
463 Handle(TDataStd_IntegerArray) aColorArray;
464 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
465 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
467 aColorArray->SetValue( 1, theColor.red() );
468 aColorArray->SetValue( 2, theColor.green() );
469 aColorArray->SetValue( 3, theColor.blue() );
470 aColorArray->SetValue( 4, theColor.alpha() );
473 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
474 const int theTag ) const
476 QColor aResColor = theDefColor;
478 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
480 Handle(TDataStd_IntegerArray) aColorArray;
481 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
483 aResColor.setRed( aColorArray->Value( 1 ) );
484 aResColor.setGreen( aColorArray->Value( 2 ) );
485 aResColor.setBlue( aColorArray->Value( 3 ) );
486 aResColor.setAlpha( aColorArray->Value( 4 ) );
492 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
493 QStringList& theScript,
494 const Handle(HYDROData_Entity)& theRefObject,
495 const QString& theMethod ) const
497 if ( theRefObject.IsNull() )
500 QString aRefObjName = theRefObject->GetName();
501 if ( aRefObjName.isEmpty() )
504 bool anIsToSetObject = true;
506 // The definition of reference object must be dumped before this
507 if ( !theTreatedObjects.contains( aRefObjName ) )
509 // Write definition of reference polyline
510 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
511 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
513 QStringList aTmpList = theScript;
514 theScript = aRefObjDump;
516 theScript << QString( "" );
517 theScript << aTmpList;
519 theTreatedObjects.insert( aRefObjName, theRefObject );
523 if ( anIsToSetObject )
525 theScript << QString( "%1.%2( %3 );" )
526 .arg( GetName() ).arg( theMethod ).arg( aRefObjName );