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 QString aName = GetName();
50 aName.replace(QRegExp("[\\W]"), "_");
55 void HYDROData_Entity::SetName(const QString& theName)
57 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
60 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
62 QStringList anEmptyList;
66 void HYDROData_Entity::Update()
71 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
73 //On the base level no actions are necessary
76 bool HYDROData_Entity::IsHas2dPrs() const
81 void HYDROData_Entity::Show()
86 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
87 if ( aDocument.IsNull() )
90 aDocument->Show( this );
93 QVariant HYDROData_Entity::GetDataVariant()
98 void HYDROData_Entity::SetToUpdate( bool theFlag )
100 if ( IsMustBeUpdated() == theFlag )
105 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
107 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
108 if ( !aDocument.IsNull() )
110 // Change the states of this and all depended objects
111 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
116 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
120 bool HYDROData_Entity::IsMustBeUpdated() const
122 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
125 bool HYDROData_Entity::CanBeUpdated() const
130 bool HYDROData_Entity::IsRemoved() const
132 return !myLab.HasAttribute();
135 void HYDROData_Entity::Remove()
137 return myLab.ForgetAllAttributes( true );
140 bool HYDROData_Entity::CanRemove()
145 HYDROData_Entity::HYDROData_Entity()
149 HYDROData_Entity::~HYDROData_Entity()
153 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
155 TDF_CopyLabel aCopy(myLab, theDestination->Label());
159 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
161 Handle(HYDROData_Entity) aFather;
163 if ( !myLab.IsNull() )
165 TDF_Label aFatherLabel = myLab.Father();
167 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
169 aFather = HYDROData_Iterator::Object( aFatherLabel );
170 aFatherLabel = aFatherLabel.Father();
177 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
179 return HYDROData_SequenceOfObjects();
182 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
186 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
187 if ( !aLabel.IsNull() )
189 Handle(TDataStd_Integer) anIntVal;
190 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
192 theLevel = anIntVal->Get();
193 return Standard_True;
197 return Standard_False;
200 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
202 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
205 void HYDROData_Entity::RemoveZLevel()
207 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
208 if ( !aLabel.IsNull() )
209 aLabel.ForgetAllAttributes();
212 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
217 void HYDROData_Entity::SaveByteArray( const int theTag,
221 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
222 // array is empty, remove the attribute
224 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
227 // store data of image in byte array
228 Handle(TDataStd_ByteArray) aData;
229 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
230 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
232 // copy bytes one by one
233 if (aData->Length() != theLen) {
234 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
235 for(int a = 0; a < theLen; a++)
236 aNewData->SetValue(a + 1, theData[a]);
237 aData->ChangeArray(aNewData);
239 for(int a = 0; a < theLen; a++)
240 aData->SetValue(a + 1, theData[a]);
244 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
246 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
247 Handle(TDataStd_ByteArray) aData;
248 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
249 return NULL; // return empty image if there is no array
250 theLen = aData->Length();
252 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
256 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
258 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
259 return aRefs.IsNull() ? 0 : aRefs->Extent();
262 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
263 const int theTag ) const
265 if ( theObj.IsNull() )
268 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
269 if ( aRefs.IsNull() || aRefs->IsEmpty() )
272 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
273 for ( ; aListIt.More(); aListIt.Next() )
275 const TDF_Label& aRefLabel = aListIt.Value();
276 if ( theObj->Label() == aRefLabel )
283 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
286 if ( theObj.IsNull() )
289 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
290 aRefs->Append( theObj->Label() );
293 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
297 if ( theObj.IsNull() )
299 RemoveReferenceObject( theTag, theIndex );
303 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
305 if ( theIndex >= aRefs->Extent() )
307 aRefs->Append( theObj->Label() );
309 else if ( theIndex < 0 )
311 aRefs->Prepend( theObj->Label() );
315 RemoveReferenceObject( theTag, theIndex );
317 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
319 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
320 if ( !aBeforeObj.IsNull() )
321 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
323 aRefs->Append( theObj->Label() );
327 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
329 const int theBeforeIndex )
331 if ( theObj.IsNull() )
334 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
336 if ( theBeforeIndex >= aRefs->Extent() )
338 aRefs->Append( theObj->Label() );
340 else if ( theBeforeIndex < 0 )
342 aRefs->Prepend( theObj->Label() );
346 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
347 if ( !aBeforeObj.IsNull() )
348 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
350 aRefs->Append( theObj->Label() );
354 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
357 ClearReferenceObjects( theTag );
358 if ( theObjects.IsEmpty() )
361 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
362 for ( ; anIter.More(); anIter.Next() )
363 AddReferenceObject( anIter.Value(), theTag );
366 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
367 const int theIndex ) const
369 Handle(HYDROData_Entity) aRes;
371 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
372 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
375 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
376 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
378 const TDF_Label& aRefLabel = anIter.Value();
379 aRes = HYDROData_Iterator::Object( aRefLabel );
384 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
386 HYDROData_SequenceOfObjects aRes;
388 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
389 if ( aRefs.IsNull() )
392 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
393 for ( ; anIter.More(); anIter.Next() )
395 const TDF_Label& aRefLabel = anIter.Value();
397 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
398 if ( aRefObject.IsNull() )
401 aRes.Append( aRefObject );
407 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
410 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
411 if ( aRefs.IsNull() )
414 if ( aRefs->Extent() == 1 )
416 // remove all if only one
417 ClearReferenceObjects( theTag );
421 aRefs->Remove( theRefLabel );
424 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
427 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
428 if ( aRefs.IsNull() )
431 if ( aRefs->Extent() == 1 && theIndex == 0 )
433 // remove all if only one
434 ClearReferenceObjects( theTag );
439 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
440 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
442 if ( anIndex != theIndex || !anIter.More() )
445 const TDF_Label& aRefLabel = anIter.Value();
446 aRefs->Remove( aRefLabel );
449 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
451 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
452 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
455 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
456 const bool theIsCreate ) const
458 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
460 Handle(TDataStd_ReferenceList) aRefs;
461 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
462 aRefs = TDataStd_ReferenceList::Set( aLabel );
467 void HYDROData_Entity::SetColor( const QColor& theColor,
470 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
472 Handle(TDataStd_IntegerArray) aColorArray;
473 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
474 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
476 aColorArray->SetValue( 1, theColor.red() );
477 aColorArray->SetValue( 2, theColor.green() );
478 aColorArray->SetValue( 3, theColor.blue() );
479 aColorArray->SetValue( 4, theColor.alpha() );
482 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
483 const int theTag ) const
485 QColor aResColor = theDefColor;
487 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
489 Handle(TDataStd_IntegerArray) aColorArray;
490 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
492 aResColor.setRed( aColorArray->Value( 1 ) );
493 aResColor.setGreen( aColorArray->Value( 2 ) );
494 aResColor.setBlue( aColorArray->Value( 3 ) );
495 aResColor.setAlpha( aColorArray->Value( 4 ) );
501 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
503 QStringList aResList;
505 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
506 if ( aDocument.IsNull() )
509 QString aDocName = aDocument->GetDocPyName();
510 QString aName = GetObjPyName();
512 aResList << QString( "%1 = %2.CreateObject( %3 );" )
513 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
514 aResList << QString( "%1.SetName( \"%2\" );" )
515 .arg( aName ).arg( GetName() );
516 aResList << QString( "" );
520 // Dump object z-level in viewer
521 Standard_Integer anObjZLevel = -1;
522 if ( GetZLevel( anObjZLevel ) )
524 aResList << QString( "%1.SetZLevel( %2 );" )
525 .arg( aName ).arg( anObjZLevel );
526 aResList << QString( "" );
533 QString HYDROData_Entity::getPyTypeID() const
537 case KIND_IMAGE: return "KIND_IMAGE";
538 case KIND_POLYLINE: return "KIND_POLYLINE";
539 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
540 case KIND_ALTITUDE: return "KIND_ALTITUDE";
541 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
542 case KIND_RIVER: return "KIND_RIVER";
543 case KIND_STREAM: return "KIND_STREAM";
544 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
545 case KIND_CHANNEL: return "KIND_CHANNEL";
546 case KIND_OBSTACLE: return "KIND_OBSTACLE";
547 case KIND_DIGUE: return "KIND_DIGUE";
548 case KIND_PROFILE: return "KIND_PROFILE";
549 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
550 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
551 case KIND_CALCULATION: return "KIND_CALCULATION";
552 case KIND_ZONE: return "KIND_ZONE";
553 case KIND_REGION: return "KIND_REGION";
554 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
555 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
556 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
557 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
558 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
559 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
560 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
561 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
562 default: return "KIND_UNKNOWN"; ///! Unrecognized object
566 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
567 QStringList& theScript,
568 const Handle(HYDROData_Entity)& theRefObject,
569 const QString& theMethod ) const
571 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
574 QString aRefObjName = theRefObject->GetObjPyName();
576 QString anObjName = GetObjPyName();
577 theScript << QString( "%1.%2( %3 );" )
578 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
581 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
582 QStringList& theScript,
583 const Handle(HYDROData_Entity)& theRefObject ) const
585 if ( theRefObject.IsNull() )
588 QString aRefObjName = theRefObject->GetName();
589 if ( aRefObjName.isEmpty() )
592 if ( theTreatedObjects.contains( aRefObjName ) )
595 // The definition of reference object must be dumped before this
596 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
597 if ( aRefObjDump.isEmpty() )
600 QStringList aTmpList = theScript;
601 theScript = aRefObjDump;
603 theScript << QString( "" );
604 theScript << aTmpList;
606 theTreatedObjects.insert( aRefObjName, theRefObject );
611 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
612 const QColor& theColor,
613 const QColor& theDefaultColor,
614 const QString& theMethod ) const
616 if ( theColor == theDefaultColor )
617 return; //Do not set the color for object if it like default
619 QString anObjName = GetObjPyName();
620 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
621 .arg( anObjName ).arg( theMethod )
622 .arg( theColor.red() ).arg( theColor.green() )
623 .arg( theColor.blue() ).arg( theColor.alpha() );
626 void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
627 QStringList& theScript ) const
629 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
630 if ( aDocument.IsNull() )
633 theScript << QString( "%1 = %2.FindObjectByName( \"%3\" );" ).arg( GetObjPyName() )
634 .arg( aDocument->GetDocPyName() )