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 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
70 //On the base level no actions are necessary
73 bool HYDROData_Entity::IsHas2dPrs() const
78 void HYDROData_Entity::Show()
83 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
84 if ( aDocument.IsNull() )
87 aDocument->Show( this );
90 QVariant HYDROData_Entity::GetDataVariant()
95 void HYDROData_Entity::SetToUpdate( bool theFlag )
97 if ( IsMustBeUpdated() == theFlag )
102 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
104 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
105 if ( !aDocument.IsNull() )
107 // Change the states of this and all depended objects
108 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
113 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
117 bool HYDROData_Entity::IsMustBeUpdated() const
119 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
122 bool HYDROData_Entity::CanBeUpdated() const
127 bool HYDROData_Entity::IsRemoved() const
129 return !myLab.HasAttribute();
132 void HYDROData_Entity::Remove()
134 return myLab.ForgetAllAttributes( true );
137 bool HYDROData_Entity::CanRemove()
142 HYDROData_Entity::HYDROData_Entity()
146 HYDROData_Entity::~HYDROData_Entity()
150 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
152 TDF_CopyLabel aCopy(myLab, theDestination->Label());
156 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
158 Handle(HYDROData_Entity) aFather;
160 if ( !myLab.IsNull() )
162 TDF_Label aFatherLabel = myLab.Father();
164 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
166 aFather = HYDROData_Iterator::Object( aFatherLabel );
167 aFatherLabel = aFatherLabel.Father();
174 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
176 return HYDROData_SequenceOfObjects();
179 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
183 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
184 if ( !aLabel.IsNull() )
186 Handle(TDataStd_Integer) anIntVal;
187 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
189 theLevel = anIntVal->Get();
190 return Standard_True;
194 return Standard_False;
197 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
199 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
202 void HYDROData_Entity::RemoveZLevel()
204 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
205 if ( !aLabel.IsNull() )
206 aLabel.ForgetAllAttributes();
209 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
214 void HYDROData_Entity::SaveByteArray( const int theTag,
218 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
219 // array is empty, remove the attribute
221 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
224 // store data of image in byte array
225 Handle(TDataStd_ByteArray) aData;
226 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
227 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
229 // copy bytes one by one
230 if (aData->Length() != theLen) {
231 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
232 for(int a = 0; a < theLen; a++)
233 aNewData->SetValue(a + 1, theData[a]);
234 aData->ChangeArray(aNewData);
236 for(int a = 0; a < theLen; a++)
237 aData->SetValue(a + 1, theData[a]);
241 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
243 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
244 Handle(TDataStd_ByteArray) aData;
245 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
246 return NULL; // return empty image if there is no array
247 theLen = aData->Length();
249 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
253 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
255 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
256 return aRefs.IsNull() ? 0 : aRefs->Extent();
259 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
260 const int theTag ) const
262 if ( theObj.IsNull() )
265 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
266 if ( aRefs.IsNull() || aRefs->IsEmpty() )
269 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
270 for ( ; aListIt.More(); aListIt.Next() )
272 const TDF_Label& aRefLabel = aListIt.Value();
273 if ( theObj->Label() == aRefLabel )
280 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
283 if ( theObj.IsNull() )
286 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
287 aRefs->Append( theObj->Label() );
290 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
294 if ( theObj.IsNull() )
296 RemoveReferenceObject( theTag, theIndex );
300 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
302 if ( theIndex >= aRefs->Extent() )
304 aRefs->Append( theObj->Label() );
306 else if ( theIndex < 0 )
308 aRefs->Prepend( theObj->Label() );
312 RemoveReferenceObject( theTag, theIndex );
314 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
316 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
317 if ( !aBeforeObj.IsNull() )
318 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
320 aRefs->Append( theObj->Label() );
324 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
326 const int theBeforeIndex )
328 if ( theObj.IsNull() )
331 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
333 if ( theBeforeIndex >= aRefs->Extent() )
335 aRefs->Append( theObj->Label() );
337 else if ( theBeforeIndex < 0 )
339 aRefs->Prepend( theObj->Label() );
343 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
344 if ( !aBeforeObj.IsNull() )
345 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
347 aRefs->Append( theObj->Label() );
351 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
354 ClearReferenceObjects( theTag );
355 if ( theObjects.IsEmpty() )
358 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
359 for ( ; anIter.More(); anIter.Next() )
360 AddReferenceObject( anIter.Value(), theTag );
363 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
364 const int theIndex ) const
366 Handle(HYDROData_Entity) aRes;
368 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
369 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
372 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
373 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
375 const TDF_Label& aRefLabel = anIter.Value();
376 aRes = HYDROData_Iterator::Object( aRefLabel );
381 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
383 HYDROData_SequenceOfObjects aRes;
385 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
386 if ( aRefs.IsNull() )
389 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
390 for ( ; anIter.More(); anIter.Next() )
392 const TDF_Label& aRefLabel = anIter.Value();
394 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
395 if ( aRefObject.IsNull() )
398 aRes.Append( aRefObject );
404 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
407 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
408 if ( aRefs.IsNull() )
411 if ( aRefs->Extent() == 1 )
413 // remove all if only one
414 ClearReferenceObjects( theTag );
418 aRefs->Remove( theRefLabel );
421 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
424 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
425 if ( aRefs.IsNull() )
428 if ( aRefs->Extent() == 1 && theIndex == 0 )
430 // remove all if only one
431 ClearReferenceObjects( theTag );
436 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
437 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
439 if ( anIndex != theIndex || !anIter.More() )
442 const TDF_Label& aRefLabel = anIter.Value();
443 aRefs->Remove( aRefLabel );
446 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
448 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
449 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
452 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
453 const bool theIsCreate ) const
455 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
457 Handle(TDataStd_ReferenceList) aRefs;
458 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
459 aRefs = TDataStd_ReferenceList::Set( aLabel );
464 void HYDROData_Entity::SetColor( const QColor& theColor,
467 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
469 Handle(TDataStd_IntegerArray) aColorArray;
470 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
471 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
473 aColorArray->SetValue( 1, theColor.red() );
474 aColorArray->SetValue( 2, theColor.green() );
475 aColorArray->SetValue( 3, theColor.blue() );
476 aColorArray->SetValue( 4, theColor.alpha() );
479 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
480 const int theTag ) const
482 QColor aResColor = theDefColor;
484 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
486 Handle(TDataStd_IntegerArray) aColorArray;
487 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
489 aResColor.setRed( aColorArray->Value( 1 ) );
490 aResColor.setGreen( aColorArray->Value( 2 ) );
491 aResColor.setBlue( aColorArray->Value( 3 ) );
492 aResColor.setAlpha( aColorArray->Value( 4 ) );
498 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
500 QStringList aResList;
502 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
503 if ( aDocument.IsNull() )
506 QString aDocName = aDocument->GetDocPyName();
507 QString aName = GetObjPyName();
509 aResList << QString( "%1 = %2.CreateObject( %3 );" )
510 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
511 aResList << QString( "%1.SetName( \"%2\" );" )
512 .arg( aName ).arg( GetName() );
513 aResList << QString( "" );
517 // Dump object z-level in viewer
518 Standard_Integer anObjZLevel = -1;
519 if ( GetZLevel( anObjZLevel ) )
521 aResList << QString( "%1.SetZLevel( %2 );" )
522 .arg( aName ).arg( anObjZLevel );
523 aResList << QString( "" );
530 QString HYDROData_Entity::getPyTypeID() const
534 case KIND_IMAGE: return "KIND_IMAGE";
535 case KIND_POLYLINE: return "KIND_POLYLINE";
536 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
537 case KIND_ALTITUDE: return "KIND_ALTITUDE";
538 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
539 case KIND_RIVER: return "KIND_RIVER";
540 case KIND_STREAM: return "KIND_STREAM";
541 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
542 case KIND_CHANNEL: return "KIND_CHANNEL";
543 case KIND_OBSTACLE: return "KIND_OBSTACLE";
544 case KIND_DIGUE: return "KIND_DIGUE";
545 case KIND_PROFILE: return "KIND_PROFILE";
546 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
547 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
548 case KIND_CALCULATION: return "KIND_CALCULATION";
549 case KIND_ZONE: return "KIND_ZONE";
550 case KIND_REGION: return "KIND_REGION";
551 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
552 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
553 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
554 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
555 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
556 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
557 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
558 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
559 default: return "KIND_UNKNOWN"; ///! Unrecognized object
563 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
564 QStringList& theScript,
565 const Handle(HYDROData_Entity)& theRefObject,
566 const QString& theMethod ) const
568 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
571 QString aRefObjName = theRefObject->GetObjPyName();
573 QString anObjName = GetObjPyName();
574 theScript << QString( "%1.%2( %3 );" )
575 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
578 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
579 QStringList& theScript,
580 const Handle(HYDROData_Entity)& theRefObject ) const
582 if ( theRefObject.IsNull() )
585 QString aRefObjName = theRefObject->GetName();
586 if ( aRefObjName.isEmpty() )
589 if ( theTreatedObjects.contains( aRefObjName ) )
592 // The definition of reference object must be dumped before this
593 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
594 if ( aRefObjDump.isEmpty() )
597 QStringList aTmpList = theScript;
598 theScript = aRefObjDump;
600 theScript << QString( "" );
601 theScript << aTmpList;
603 theTreatedObjects.insert( aRefObjName, theRefObject );
608 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
609 const QColor& theColor,
610 const QColor& theDefaultColor,
611 const QString& theMethod ) const
613 if ( theColor == theDefaultColor )
614 return; //Do not set the color for object if it like default
616 QString anObjName = GetObjPyName();
617 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
618 .arg( anObjName ).arg( theMethod )
619 .arg( theColor.red() ).arg( theColor.green() )
620 .arg( theColor.blue() ).arg( theColor.alpha() );