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 QString HYDROData_Entity::GetObjPyName() const
48 return GetName().replace(" ", "_");
51 void HYDROData_Entity::SetName(const QString& theName)
53 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
56 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
58 QStringList anEmptyList;
62 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
66 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
67 if ( aDocument.IsNull() )
70 QString aDocName = aDocument->GetDocPyName();
71 QString aName = GetObjPyName();
73 aResList << QString( "%1 = %2.CreateObject( %3 );" )
74 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
75 aResList << QString( "%1.SetName( \"%2\" );" )
76 .arg( aName ).arg( GetName() );
77 aResList << QString( "" );
82 void HYDROData_Entity::Update()
87 QString HYDROData_Entity::getPyTypeID() const
91 case KIND_IMAGE: return "KIND_IMAGE";
92 case KIND_POLYLINE: return "KIND_POLYLINE";
93 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
94 case KIND_ALTITUDE: return "KIND_ALTITUDE";
95 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
96 case KIND_RIVER: return "KIND_RIVER";
97 case KIND_STREAM: return "KIND_STREAM";
98 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
99 case KIND_CHANNEL: return "KIND_CHANNEL";
100 case KIND_OBSTACLE: return "KIND_OBSTACLE";
101 case KIND_DIGUE: return "KIND_DIGUE";
102 case KIND_PROFILE: return "KIND_PROFILE";
103 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
104 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
105 case KIND_CALCULATION: return "KIND_CALCULATION";
106 case KIND_ZONE: return "KIND_ZONE";
107 case KIND_REGION: return "KIND_REGION";
108 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
109 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
110 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
111 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
112 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
113 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
114 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
115 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
116 default: return "KIND_UNKNOWN"; ///! Unrecognized object
120 QVariant HYDROData_Entity::GetDataVariant()
125 void HYDROData_Entity::SetToUpdate( bool theFlag )
127 if ( IsMustBeUpdated() == theFlag )
132 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
134 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
135 if ( !aDocument.IsNull() )
137 // Change the states of this and all depended objects
138 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
143 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
147 bool HYDROData_Entity::IsMustBeUpdated() const
149 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
152 bool HYDROData_Entity::CanBeUpdated() const
157 bool HYDROData_Entity::IsRemoved() const
159 return !myLab.HasAttribute();
162 void HYDROData_Entity::Remove()
164 return myLab.ForgetAllAttributes( true );
167 bool HYDROData_Entity::CanRemove()
172 HYDROData_Entity::HYDROData_Entity()
176 HYDROData_Entity::~HYDROData_Entity()
180 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
182 TDF_CopyLabel aCopy(myLab, theDestination->Label());
186 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
188 Handle(HYDROData_Entity) aFather;
190 if ( !myLab.IsNull() )
192 TDF_Label aFatherLabel = myLab.Father();
194 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
196 aFather = HYDROData_Iterator::Object( aFatherLabel );
197 aFatherLabel = aFatherLabel.Father();
204 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
206 return HYDROData_SequenceOfObjects();
209 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
214 void HYDROData_Entity::SaveByteArray(const int theTag,
215 const char* theData, const int theLen)
217 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
218 // array is empty, remove the attribute
220 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
223 // store data of image in byte array
224 Handle(TDataStd_ByteArray) aData;
225 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
226 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
228 // copy bytes one by one
229 if (aData->Length() != theLen) {
230 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
231 for(int a = 0; a < theLen; a++)
232 aNewData->SetValue(a + 1, theData[a]);
233 aData->ChangeArray(aNewData);
235 for(int a = 0; a < theLen; a++)
236 aData->SetValue(a + 1, theData[a]);
240 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
242 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
243 Handle(TDataStd_ByteArray) aData;
244 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
245 return NULL; // return empty image if there is no array
246 theLen = aData->Length();
248 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
252 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
254 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
255 return aRefs.IsNull() ? 0 : aRefs->Extent();
258 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
259 const int theTag ) const
261 if ( theObj.IsNull() )
264 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
265 if ( aRefs.IsNull() || aRefs->IsEmpty() )
268 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
269 for ( ; aListIt.More(); aListIt.Next() )
271 const TDF_Label& aRefLabel = aListIt.Value();
272 if ( theObj->Label() == aRefLabel )
279 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
282 if ( theObj.IsNull() )
285 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
286 aRefs->Append( theObj->Label() );
289 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
293 if ( theObj.IsNull() )
295 RemoveReferenceObject( theTag, theIndex );
299 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
301 if ( theIndex >= aRefs->Extent() )
303 aRefs->Append( theObj->Label() );
305 else if ( theIndex < 0 )
307 aRefs->Prepend( theObj->Label() );
311 RemoveReferenceObject( theTag, theIndex );
313 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
315 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
316 if ( !aBeforeObj.IsNull() )
317 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
319 aRefs->Append( theObj->Label() );
323 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
325 const int theBeforeIndex )
327 if ( theObj.IsNull() )
330 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
332 if ( theBeforeIndex >= aRefs->Extent() )
334 aRefs->Append( theObj->Label() );
336 else if ( theBeforeIndex < 0 )
338 aRefs->Prepend( theObj->Label() );
342 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
343 if ( !aBeforeObj.IsNull() )
344 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
346 aRefs->Append( theObj->Label() );
350 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
353 ClearReferenceObjects( theTag );
354 if ( theObjects.IsEmpty() )
357 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
358 for ( ; anIter.More(); anIter.Next() )
359 AddReferenceObject( anIter.Value(), theTag );
362 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
363 const int theIndex ) const
365 Handle(HYDROData_Entity) aRes;
367 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
368 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
371 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
372 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
374 const TDF_Label& aRefLabel = anIter.Value();
375 aRes = HYDROData_Iterator::Object( aRefLabel );
380 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
382 HYDROData_SequenceOfObjects aRes;
384 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
385 if ( aRefs.IsNull() )
388 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
389 for ( ; anIter.More(); anIter.Next() )
391 const TDF_Label& aRefLabel = anIter.Value();
393 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
394 if ( aRefObject.IsNull() )
397 aRes.Append( aRefObject );
403 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
406 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
407 if ( aRefs.IsNull() )
410 if ( aRefs->Extent() == 1 )
412 // remove all if only one
413 ClearReferenceObjects( theTag );
417 aRefs->Remove( theRefLabel );
420 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
423 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
424 if ( aRefs.IsNull() )
427 if ( aRefs->Extent() == 1 && theIndex == 0 )
429 // remove all if only one
430 ClearReferenceObjects( theTag );
435 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
436 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
438 if ( anIndex != theIndex || !anIter.More() )
441 const TDF_Label& aRefLabel = anIter.Value();
442 aRefs->Remove( aRefLabel );
445 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
447 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
448 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
451 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
452 const bool theIsCreate ) const
454 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
456 Handle(TDataStd_ReferenceList) aRefs;
457 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
458 aRefs = TDataStd_ReferenceList::Set( aLabel );
463 void HYDROData_Entity::SetColor( const QColor& theColor,
466 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
468 Handle(TDataStd_IntegerArray) aColorArray;
469 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
470 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
472 aColorArray->SetValue( 1, theColor.red() );
473 aColorArray->SetValue( 2, theColor.green() );
474 aColorArray->SetValue( 3, theColor.blue() );
475 aColorArray->SetValue( 4, theColor.alpha() );
478 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
479 const int theTag ) const
481 QColor aResColor = theDefColor;
483 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
485 Handle(TDataStd_IntegerArray) aColorArray;
486 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
488 aResColor.setRed( aColorArray->Value( 1 ) );
489 aResColor.setGreen( aColorArray->Value( 2 ) );
490 aResColor.setBlue( aColorArray->Value( 3 ) );
491 aResColor.setAlpha( aColorArray->Value( 4 ) );
497 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
498 QStringList& theScript,
499 const Handle(HYDROData_Entity)& theRefObject,
500 const QString& theMethod ) const
502 if ( theRefObject.IsNull() )
505 QString aRefObjName = theRefObject->GetObjPyName();
506 if ( aRefObjName.isEmpty() )
509 bool anIsToSetObject = true;
511 // The definition of reference object must be dumped before this
512 if ( !theTreatedObjects.contains( aRefObjName ) )
514 // Write definition of reference polyline
515 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
516 if ( ( anIsToSetObject = !aRefObjDump.isEmpty() ) )
518 QStringList aTmpList = theScript;
519 theScript = aRefObjDump;
521 theScript << QString( "" );
522 theScript << aTmpList;
524 theTreatedObjects.insert( aRefObjName, theRefObject );
528 if ( anIsToSetObject )
530 QString anObjName = GetObjPyName();
531 theScript << QString( "%1.%2( %3 );" )
532 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
536 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
537 const QColor& theColor,
538 const QColor& theDefaultColor,
539 const QString& theMethod ) const
541 if ( theColor == theDefaultColor )
542 return; //Do not set the color for object if it like default
544 QString anObjName = GetObjPyName();
545 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
546 .arg( anObjName ).arg( theMethod )
547 .arg( theColor.red() ).arg( theColor.green() )
548 .arg( theColor.blue() ).arg( theColor.alpha() );