1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_Entity.h"
25 #include "HYDROData_Iterator.h"
26 #include "HYDROData_Tool.h"
28 #include <TDataStd_Name.hxx>
29 #include <TDataStd_ByteArray.hxx>
30 #include <TDataStd_UAttribute.hxx>
31 #include <TDataStd_IntegerArray.hxx>
32 #include <TDataStd_BooleanArray.hxx>
33 #include <TDataStd_Integer.hxx>
34 #include <TDataStd_RealArray.hxx>
35 #include <TDataStd_ReferenceList.hxx>
37 #include <TDF_CopyLabel.hxx>
38 #include <TDF_ListIteratorOfLabelList.hxx>
42 #include <QStringList>
45 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
47 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
48 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
50 // is equal function for unique object mapping
51 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
53 if ( !theObj1.IsNull() && !theObj2.IsNull() )
54 return theObj1->Label() == theObj2->Label();
58 QString HYDROData_Entity::GetName() const
60 Handle(TDataStd_Name) aName;
61 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
62 TCollection_AsciiString aStr(aName->Get());
63 return QString(aStr.ToCString());
68 QString HYDROData_Entity::GetObjPyName() const
70 QString aName = GetName();
71 aName.replace(QRegExp("[\\W]"), "_");
76 void HYDROData_Entity::SetName(const QString& theName)
78 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
81 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
83 QStringList anEmptyList;
87 void HYDROData_Entity::Update()
92 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
94 //On the base level no actions are necessary
97 bool HYDROData_Entity::IsHas2dPrs() const
102 void HYDROData_Entity::Show()
107 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
108 if ( aDocument.IsNull() )
111 aDocument->Show( this );
114 QVariant HYDROData_Entity::GetDataVariant()
119 void HYDROData_Entity::SetToUpdate( bool theFlag )
121 if ( IsMustBeUpdated() == theFlag )
126 TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
128 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
129 if ( !aDocument.IsNull() )
131 // Change the states of this and all depended objects
132 HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
137 myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
141 bool HYDROData_Entity::IsMustBeUpdated() const
143 return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
146 bool HYDROData_Entity::CanBeUpdated() const
151 bool HYDROData_Entity::IsRemoved() const
153 return !myLab.HasAttribute();
156 void HYDROData_Entity::Remove()
158 return myLab.ForgetAllAttributes( true );
161 bool HYDROData_Entity::CanRemove()
166 HYDROData_Entity::HYDROData_Entity()
170 HYDROData_Entity::~HYDROData_Entity()
174 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
176 TDF_CopyLabel aCopy(myLab, theDestination->Label());
180 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
182 Handle(HYDROData_Entity) aFather;
184 if ( !myLab.IsNull() )
186 TDF_Label aFatherLabel = myLab.Father();
188 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
190 aFather = HYDROData_Iterator::Object( aFatherLabel );
191 aFatherLabel = aFatherLabel.Father();
198 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
200 return HYDROData_SequenceOfObjects();
203 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
207 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
208 if ( !aLabel.IsNull() )
210 Handle(TDataStd_Integer) anIntVal;
211 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
213 theLevel = anIntVal->Get();
214 return Standard_True;
218 return Standard_False;
221 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
223 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
226 void HYDROData_Entity::RemoveZLevel()
228 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
229 if ( !aLabel.IsNull() )
230 aLabel.ForgetAllAttributes();
233 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
238 void HYDROData_Entity::SaveByteArray( const int theTag,
242 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
243 // array is empty, remove the attribute
245 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
248 // store data of image in byte array
249 Handle(TDataStd_ByteArray) aData;
250 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
251 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
253 // copy bytes one by one
254 if (aData->Length() != theLen) {
255 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
256 for(int a = 0; a < theLen; a++)
257 aNewData->SetValue(a + 1, theData[a]);
258 aData->ChangeArray(aNewData);
260 for(int a = 0; a < theLen; a++)
261 aData->SetValue(a + 1, theData[a]);
265 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
267 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
268 Handle(TDataStd_ByteArray) aData;
269 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
270 return NULL; // return empty image if there is no array
271 theLen = aData->Length();
273 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
277 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
279 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
280 return aRefs.IsNull() ? 0 : aRefs->Extent();
283 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
284 const int theTag ) const
286 if ( theObj.IsNull() )
289 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
290 if ( aRefs.IsNull() || aRefs->IsEmpty() )
293 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
294 for ( ; aListIt.More(); aListIt.Next() )
296 const TDF_Label& aRefLabel = aListIt.Value();
297 if ( theObj->Label() == aRefLabel )
304 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
307 if ( theObj.IsNull() )
310 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
311 aRefs->Append( theObj->Label() );
314 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
318 if ( theObj.IsNull() )
320 RemoveReferenceObject( theTag, theIndex );
324 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
326 if ( theIndex >= aRefs->Extent() )
328 aRefs->Append( theObj->Label() );
330 else if ( theIndex < 0 )
332 aRefs->Prepend( theObj->Label() );
336 RemoveReferenceObject( theTag, theIndex );
338 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
340 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
341 if ( !aBeforeObj.IsNull() )
342 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
344 aRefs->Append( theObj->Label() );
348 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
350 const int theBeforeIndex )
352 if ( theObj.IsNull() )
355 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
357 if ( theBeforeIndex >= aRefs->Extent() )
359 aRefs->Append( theObj->Label() );
361 else if ( theBeforeIndex < 0 )
363 aRefs->Prepend( theObj->Label() );
367 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
368 if ( !aBeforeObj.IsNull() )
369 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
371 aRefs->Append( theObj->Label() );
375 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
378 ClearReferenceObjects( theTag );
379 if ( theObjects.IsEmpty() )
382 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
383 for ( ; anIter.More(); anIter.Next() )
384 AddReferenceObject( anIter.Value(), theTag );
387 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
388 const int theIndex ) const
390 Handle(HYDROData_Entity) aRes;
392 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
393 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
396 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
397 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
399 const TDF_Label& aRefLabel = anIter.Value();
400 aRes = HYDROData_Iterator::Object( aRefLabel );
405 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
407 HYDROData_SequenceOfObjects aRes;
409 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
410 if ( aRefs.IsNull() )
413 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
414 for ( ; anIter.More(); anIter.Next() )
416 const TDF_Label& aRefLabel = anIter.Value();
418 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
419 if ( aRefObject.IsNull() )
422 aRes.Append( aRefObject );
428 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
431 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
432 if ( aRefs.IsNull() )
435 if ( aRefs->Extent() == 1 )
437 // remove all if only one
438 ClearReferenceObjects( theTag );
442 aRefs->Remove( theRefLabel );
445 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
448 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
449 if ( aRefs.IsNull() )
452 if ( aRefs->Extent() == 1 && theIndex == 0 )
454 // remove all if only one
455 ClearReferenceObjects( theTag );
460 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
461 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
463 if ( anIndex != theIndex || !anIter.More() )
466 const TDF_Label& aRefLabel = anIter.Value();
467 aRefs->Remove( aRefLabel );
470 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
472 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
473 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
476 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
477 const bool theIsCreate ) const
479 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
481 Handle(TDataStd_ReferenceList) aRefs;
482 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
483 aRefs = TDataStd_ReferenceList::Set( aLabel );
488 void HYDROData_Entity::SetColor( const QColor& theColor,
491 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
493 Handle(TDataStd_IntegerArray) aColorArray;
494 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
495 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
497 aColorArray->SetValue( 1, theColor.red() );
498 aColorArray->SetValue( 2, theColor.green() );
499 aColorArray->SetValue( 3, theColor.blue() );
500 aColorArray->SetValue( 4, theColor.alpha() );
503 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
504 const int theTag ) const
506 QColor aResColor = theDefColor;
508 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
510 Handle(TDataStd_IntegerArray) aColorArray;
511 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
513 aResColor.setRed( aColorArray->Value( 1 ) );
514 aResColor.setGreen( aColorArray->Value( 2 ) );
515 aResColor.setBlue( aColorArray->Value( 3 ) );
516 aResColor.setAlpha( aColorArray->Value( 4 ) );
522 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
524 QStringList aResList;
526 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
527 if ( aDocument.IsNull() )
530 QString aDocName = aDocument->GetDocPyName();
531 QString aName = GetObjPyName();
533 aResList << QString( "%1 = %2.CreateObject( %3 );" )
534 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
535 aResList << QString( "%1.SetName( \"%2\" );" )
536 .arg( aName ).arg( GetName() );
537 aResList << QString( "" );
541 // Dump object z-level in viewer
542 Standard_Integer anObjZLevel = -1;
543 if ( GetZLevel( anObjZLevel ) )
545 aResList << QString( "%1.SetZLevel( %2 );" )
546 .arg( aName ).arg( anObjZLevel );
547 aResList << QString( "" );
554 QString HYDROData_Entity::getPyTypeID() const
558 case KIND_IMAGE: return "KIND_IMAGE";
559 case KIND_POLYLINE: return "KIND_POLYLINE";
560 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
561 case KIND_ALTITUDE: return "KIND_ALTITUDE";
562 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
563 case KIND_RIVER: return "KIND_RIVER";
564 case KIND_STREAM: return "KIND_STREAM";
565 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
566 case KIND_CHANNEL: return "KIND_CHANNEL";
567 case KIND_OBSTACLE: return "KIND_OBSTACLE";
568 case KIND_DIGUE: return "KIND_DIGUE";
569 case KIND_PROFILE: return "KIND_PROFILE";
570 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
571 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
572 case KIND_CALCULATION: return "KIND_CALCULATION";
573 case KIND_ZONE: return "KIND_ZONE";
574 case KIND_REGION: return "KIND_REGION";
575 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
576 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
577 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
578 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
579 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
580 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
581 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
582 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
583 default: return "KIND_UNKNOWN"; ///! Unrecognized object
587 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
588 QStringList& theScript,
589 const Handle(HYDROData_Entity)& theRefObject,
590 const QString& theMethod ) const
592 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
595 QString aRefObjName = theRefObject->GetObjPyName();
597 QString anObjName = GetObjPyName();
598 theScript << QString( "%1.%2( %3 );" )
599 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
602 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
603 QStringList& theScript,
604 const Handle(HYDROData_Entity)& theRefObject ) const
606 if ( theRefObject.IsNull() )
609 QString aRefObjName = theRefObject->GetName();
610 if ( aRefObjName.isEmpty() )
613 if ( theTreatedObjects.contains( aRefObjName ) )
616 // The definition of reference object must be dumped before this
617 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
618 if ( aRefObjDump.isEmpty() )
621 QStringList aTmpList = theScript;
622 theScript = aRefObjDump;
624 theScript << QString( "" );
625 theScript << aTmpList;
627 theTreatedObjects.insert( aRefObjName, theRefObject );
632 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
633 const QColor& theColor,
634 const QColor& theDefaultColor,
635 const QString& theMethod ) const
637 if ( theColor == theDefaultColor )
638 return; //Do not set the color for object if it like default
640 QString anObjName = GetObjPyName();
641 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
642 .arg( anObjName ).arg( theMethod )
643 .arg( theColor.red() ).arg( theColor.green() )
644 .arg( theColor.blue() ).arg( theColor.alpha() );
647 void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
648 QStringList& theScript ) const
650 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
651 if ( aDocument.IsNull() )
654 theScript << QString( "%1 = %2.FindObjectByName( \"%3\" );" ).arg( GetObjPyName() )
655 .arg( aDocument->GetDocPyName() )