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_Integer.hxx>
13 #include <TDataStd_RealArray.hxx>
14 #include <TDataStd_ReferenceList.hxx>
16 #include <TDF_CopyLabel.hxx>
17 #include <TDF_ListIteratorOfLabelList.hxx>
21 #include <QStringList>
24 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
26 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
27 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
29 // is equal function for unique object mapping
30 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
32 if ( !theObj1.IsNull() && !theObj2.IsNull() )
33 return theObj1->Label() == theObj2->Label();
37 QString HYDROData_Entity::GetName() const
39 Handle(TDataStd_Name) aName;
40 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
41 TCollection_AsciiString aStr(aName->Get());
42 return QString(aStr.ToCString());
47 QString HYDROData_Entity::GetObjPyName() const
49 return GetName().replace(" ", "_");
52 void HYDROData_Entity::SetName(const QString& theName)
54 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
57 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
59 QStringList anEmptyList;
63 void HYDROData_Entity::Update()
68 bool HYDROData_Entity::IsHas2dPrs() const
73 void HYDROData_Entity::Show()
78 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
79 if ( aDocument.IsNull() )
82 aDocument->Show( this );
85 QVariant HYDROData_Entity::GetDataVariant()
90 void HYDROData_Entity::SetToUpdate( bool theFlag )
92 if ( IsMustBeUpdated() == theFlag )
97 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
99 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
100 if ( !aDocument.IsNull() )
102 // Change the states of this and all depended objects
103 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
108 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
112 bool HYDROData_Entity::IsMustBeUpdated() const
114 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
117 bool HYDROData_Entity::CanBeUpdated() const
122 bool HYDROData_Entity::IsRemoved() const
124 return !myLab.HasAttribute();
127 void HYDROData_Entity::Remove()
129 return myLab.ForgetAllAttributes( true );
132 bool HYDROData_Entity::CanRemove()
137 HYDROData_Entity::HYDROData_Entity()
141 HYDROData_Entity::~HYDROData_Entity()
145 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
147 TDF_CopyLabel aCopy(myLab, theDestination->Label());
151 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
153 Handle(HYDROData_Entity) aFather;
155 if ( !myLab.IsNull() )
157 TDF_Label aFatherLabel = myLab.Father();
159 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
161 aFather = HYDROData_Iterator::Object( aFatherLabel );
162 aFatherLabel = aFatherLabel.Father();
169 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
171 return HYDROData_SequenceOfObjects();
174 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
178 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
179 if ( !aLabel.IsNull() )
181 Handle(TDataStd_Integer) anIntVal;
182 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
184 theLevel = anIntVal->Get();
185 return Standard_True;
189 return Standard_False;
192 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
194 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
197 void HYDROData_Entity::RemoveZLevel()
199 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
200 if ( !aLabel.IsNull() )
201 aLabel.ForgetAllAttributes();
204 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
209 void HYDROData_Entity::SaveByteArray( const int theTag,
213 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
214 // array is empty, remove the attribute
216 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
219 // store data of image in byte array
220 Handle(TDataStd_ByteArray) aData;
221 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
222 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
224 // copy bytes one by one
225 if (aData->Length() != theLen) {
226 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
227 for(int a = 0; a < theLen; a++)
228 aNewData->SetValue(a + 1, theData[a]);
229 aData->ChangeArray(aNewData);
231 for(int a = 0; a < theLen; a++)
232 aData->SetValue(a + 1, theData[a]);
236 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
238 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
239 Handle(TDataStd_ByteArray) aData;
240 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
241 return NULL; // return empty image if there is no array
242 theLen = aData->Length();
244 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
248 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
250 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
251 return aRefs.IsNull() ? 0 : aRefs->Extent();
254 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
255 const int theTag ) const
257 if ( theObj.IsNull() )
260 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
261 if ( aRefs.IsNull() || aRefs->IsEmpty() )
264 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
265 for ( ; aListIt.More(); aListIt.Next() )
267 const TDF_Label& aRefLabel = aListIt.Value();
268 if ( theObj->Label() == aRefLabel )
275 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
278 if ( theObj.IsNull() )
281 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
282 aRefs->Append( theObj->Label() );
285 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
289 if ( theObj.IsNull() )
291 RemoveReferenceObject( theTag, theIndex );
295 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
297 if ( theIndex >= aRefs->Extent() )
299 aRefs->Append( theObj->Label() );
301 else if ( theIndex < 0 )
303 aRefs->Prepend( theObj->Label() );
307 RemoveReferenceObject( theTag, theIndex );
309 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
311 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
312 if ( !aBeforeObj.IsNull() )
313 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
315 aRefs->Append( theObj->Label() );
319 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
321 const int theBeforeIndex )
323 if ( theObj.IsNull() )
326 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
328 if ( theBeforeIndex >= aRefs->Extent() )
330 aRefs->Append( theObj->Label() );
332 else if ( theBeforeIndex < 0 )
334 aRefs->Prepend( theObj->Label() );
338 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
339 if ( !aBeforeObj.IsNull() )
340 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
342 aRefs->Append( theObj->Label() );
346 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
349 ClearReferenceObjects( theTag );
350 if ( theObjects.IsEmpty() )
353 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
354 for ( ; anIter.More(); anIter.Next() )
355 AddReferenceObject( anIter.Value(), theTag );
358 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
359 const int theIndex ) const
361 Handle(HYDROData_Entity) aRes;
363 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
364 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
367 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
368 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
370 const TDF_Label& aRefLabel = anIter.Value();
371 aRes = HYDROData_Iterator::Object( aRefLabel );
376 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
378 HYDROData_SequenceOfObjects aRes;
380 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
381 if ( aRefs.IsNull() )
384 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
385 for ( ; anIter.More(); anIter.Next() )
387 const TDF_Label& aRefLabel = anIter.Value();
389 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
390 if ( aRefObject.IsNull() )
393 aRes.Append( aRefObject );
399 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
402 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
403 if ( aRefs.IsNull() )
406 if ( aRefs->Extent() == 1 )
408 // remove all if only one
409 ClearReferenceObjects( theTag );
413 aRefs->Remove( theRefLabel );
416 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
419 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
420 if ( aRefs.IsNull() )
423 if ( aRefs->Extent() == 1 && theIndex == 0 )
425 // remove all if only one
426 ClearReferenceObjects( theTag );
431 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
432 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
434 if ( anIndex != theIndex || !anIter.More() )
437 const TDF_Label& aRefLabel = anIter.Value();
438 aRefs->Remove( aRefLabel );
441 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
443 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
444 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
447 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
448 const bool theIsCreate ) const
450 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
452 Handle(TDataStd_ReferenceList) aRefs;
453 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
454 aRefs = TDataStd_ReferenceList::Set( aLabel );
459 void HYDROData_Entity::SetColor( const QColor& theColor,
462 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
464 Handle(TDataStd_IntegerArray) aColorArray;
465 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
466 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
468 aColorArray->SetValue( 1, theColor.red() );
469 aColorArray->SetValue( 2, theColor.green() );
470 aColorArray->SetValue( 3, theColor.blue() );
471 aColorArray->SetValue( 4, theColor.alpha() );
474 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
475 const int theTag ) const
477 QColor aResColor = theDefColor;
479 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
481 Handle(TDataStd_IntegerArray) aColorArray;
482 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
484 aResColor.setRed( aColorArray->Value( 1 ) );
485 aResColor.setGreen( aColorArray->Value( 2 ) );
486 aResColor.setBlue( aColorArray->Value( 3 ) );
487 aResColor.setAlpha( aColorArray->Value( 4 ) );
493 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
495 QStringList aResList;
497 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
498 if ( aDocument.IsNull() )
501 QString aDocName = aDocument->GetDocPyName();
502 QString aName = GetObjPyName();
504 aResList << QString( "%1 = %2.CreateObject( %3 );" )
505 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
506 aResList << QString( "%1.SetName( \"%2\" );" )
507 .arg( aName ).arg( GetName() );
508 aResList << QString( "" );
512 // Dump object z-level in viewer
513 Standard_Integer anObjZLevel = -1;
514 if ( GetZLevel( anObjZLevel ) )
516 aResList << QString( "%1.SetZLevel( %2 );" )
517 .arg( aName ).arg( anObjZLevel );
518 aResList << QString( "" );
525 QString HYDROData_Entity::getPyTypeID() const
529 case KIND_IMAGE: return "KIND_IMAGE";
530 case KIND_POLYLINE: return "KIND_POLYLINE";
531 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
532 case KIND_ALTITUDE: return "KIND_ALTITUDE";
533 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
534 case KIND_RIVER: return "KIND_RIVER";
535 case KIND_STREAM: return "KIND_STREAM";
536 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
537 case KIND_CHANNEL: return "KIND_CHANNEL";
538 case KIND_OBSTACLE: return "KIND_OBSTACLE";
539 case KIND_DIGUE: return "KIND_DIGUE";
540 case KIND_PROFILE: return "KIND_PROFILE";
541 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
542 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
543 case KIND_CALCULATION: return "KIND_CALCULATION";
544 case KIND_ZONE: return "KIND_ZONE";
545 case KIND_REGION: return "KIND_REGION";
546 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
547 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
548 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
549 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
550 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
551 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
552 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
553 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
554 default: return "KIND_UNKNOWN"; ///! Unrecognized object
558 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
559 QStringList& theScript,
560 const Handle(HYDROData_Entity)& theRefObject,
561 const QString& theMethod ) const
563 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
566 QString aRefObjName = theRefObject->GetObjPyName();
568 QString anObjName = GetObjPyName();
569 theScript << QString( "%1.%2( %3 );" )
570 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
573 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
574 QStringList& theScript,
575 const Handle(HYDROData_Entity)& theRefObject ) const
577 if ( theRefObject.IsNull() )
580 QString aRefObjName = theRefObject->GetName();
581 if ( aRefObjName.isEmpty() )
584 if ( theTreatedObjects.contains( aRefObjName ) )
587 // The definition of reference object must be dumped before this
588 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
589 if ( aRefObjDump.isEmpty() )
592 QStringList aTmpList = theScript;
593 theScript = aRefObjDump;
595 theScript << QString( "" );
596 theScript << aTmpList;
598 theTreatedObjects.insert( aRefObjName, theRefObject );
603 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
604 const QColor& theColor,
605 const QColor& theDefaultColor,
606 const QString& theMethod ) const
608 if ( theColor == theDefaultColor )
609 return; //Do not set the color for object if it like default
611 QString anObjName = GetObjPyName();
612 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
613 .arg( anObjName ).arg( theMethod )
614 .arg( theColor.red() ).arg( theColor.green() )
615 .arg( theColor.blue() ).arg( theColor.alpha() );