1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Entity.h"
21 #include "HYDROData_Iterator.h"
22 #include "HYDROData_Tool.h"
24 #include <TDataStd_Name.hxx>
25 #include <TDataStd_ByteArray.hxx>
26 #include <TDataStd_UAttribute.hxx>
27 #include <TDataStd_IntegerArray.hxx>
28 #include <TDataStd_BooleanArray.hxx>
29 #include <TDataStd_Integer.hxx>
30 #include <TDataStd_RealArray.hxx>
31 #include <TDataStd_ReferenceList.hxx>
33 #include <TDF_CopyLabel.hxx>
34 #include <TDF_ListIteratorOfLabelList.hxx>
38 #include <QStringList>
41 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
42 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
44 // is equal function for unique object mapping
45 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
47 if ( !theObj1.IsNull() && !theObj2.IsNull() )
48 return theObj1->Label() == theObj2->Label();
52 QString HYDROData_Entity::GetName() const
54 Handle(TDataStd_Name) aName;
55 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
56 TCollection_AsciiString aStr(aName->Get());
57 return QString(aStr.ToCString());
62 QString HYDROData_Entity::GetObjPyName() const
64 QString aName = GetName();
65 aName.replace(QRegExp("[\\W]"), "_");
70 void HYDROData_Entity::SetName(const QString& theName)
72 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
75 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
77 QStringList anEmptyList;
81 void HYDROData_Entity::Update()
86 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
88 //On the base level no actions are necessary
91 bool HYDROData_Entity::IsHas2dPrs() const
96 void HYDROData_Entity::Show()
101 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
102 if ( aDocument.IsNull() )
105 aDocument->Show( this );
108 QVariant HYDROData_Entity::GetDataVariant()
113 void HYDROData_Entity::ClearChanged()
115 TDataStd_Integer::Set( myLab.FindChild( DataTag_GeomChange ), 0 );
118 int HYDROData_Entity::GetGeomChangeFlag() const
120 int aGeomChangeFlag = 0;
121 Handle(TDataStd_Integer) aGeomChangeAttr;
122 TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
123 aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
124 if ( !aGeomChangeAttr.IsNull() )
125 aGeomChangeFlag = aGeomChangeAttr->Get();
126 return aGeomChangeFlag;
129 void HYDROData_Entity::Changed( Geometry theChangedGeometry )
131 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
132 if( aDocument.IsNull() )
135 int aGeomChangeFlag = 0;
136 Handle(TDataStd_Integer) aGeomChangeAttr;
137 TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
138 aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
139 if ( !aGeomChangeAttr.IsNull() )
140 aGeomChangeFlag = aGeomChangeAttr->Get();
142 if( ( myGeom & theChangedGeometry ) == 0 )
145 aGeomChangeFlag = ( aGeomChangeFlag | theChangedGeometry );
146 TDataStd_Integer::Set( aGeomChangeLab, aGeomChangeFlag );
148 HYDROData_Iterator anIter( aDocument );
149 for ( ; anIter.More(); anIter.Next() )
151 Handle(HYDROData_Entity) anObject = anIter.Current();
152 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
153 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
155 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
156 if( aRefObject->Label()==myLab )
157 Changed( theChangedGeometry );
162 bool HYDROData_Entity::IsMustBeUpdated( Geometry theGeom ) const
164 return ( ( GetGeomChangeFlag() & theGeom ) != 0 );
167 bool HYDROData_Entity::CanBeUpdated() const
172 bool HYDROData_Entity::IsRemoved() const
174 return !myLab.HasAttribute();
177 void HYDROData_Entity::Remove()
179 return myLab.ForgetAllAttributes( true );
182 bool HYDROData_Entity::CanRemove()
187 HYDROData_Entity::HYDROData_Entity( Geometry theGeom )
192 HYDROData_Entity::~HYDROData_Entity()
196 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination,
197 bool isGenerateNewName ) const
199 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
200 if ( aDocument.IsNull() ) {
204 TDF_CopyLabel aCopy(myLab, theDestination->Label());
207 if( isGenerateNewName )
209 // generate a new unique name for the clone object:
210 // case 1: Image_1 -> Image_2
211 // case 2: ImageObj -> ImageObj_1
212 QString aName = theDestination->GetName();
213 QString aPrefix = aName;
214 if( aName.contains( '_' ) ) { // case 1
215 QString aSuffix = aName.section( '_', -1 );
216 bool anIsInteger = false;
217 aSuffix.toInt( &anIsInteger );
219 aPrefix = aName.section( '_', 0, -2 );
224 aName = HYDROData_Tool::GenerateObjectName( aDocument, aPrefix );
225 theDestination->SetName( aName );
229 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
231 Handle(HYDROData_Entity) aFather;
233 if ( !myLab.IsNull() )
235 TDF_Label aFatherLabel = myLab.Father();
237 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
239 aFather = HYDROData_Iterator::Object( aFatherLabel );
240 aFatherLabel = aFatherLabel.Father();
247 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
249 return HYDROData_SequenceOfObjects();
252 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
256 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
257 if ( !aLabel.IsNull() )
259 Handle(TDataStd_Integer) anIntVal;
260 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
262 theLevel = anIntVal->Get();
263 return Standard_True;
267 return Standard_False;
270 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
272 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
275 void HYDROData_Entity::RemoveZLevel()
277 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
278 if ( !aLabel.IsNull() )
279 aLabel.ForgetAllAttributes();
282 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
287 void HYDROData_Entity::SaveByteArray( const int theTag,
291 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
292 // array is empty, remove the attribute
294 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
297 // store data of image in byte array
298 Handle(TDataStd_ByteArray) aData;
299 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
300 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
302 // copy bytes one by one
303 if (aData->Length() != theLen) {
304 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
305 for(int a = 0; a < theLen; a++)
306 aNewData->SetValue(a + 1, theData[a]);
307 aData->ChangeArray(aNewData);
309 for(int a = 0; a < theLen; a++)
310 aData->SetValue(a + 1, theData[a]);
314 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
316 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
317 Handle(TDataStd_ByteArray) aData;
318 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
319 return NULL; // return empty image if there is no array
320 theLen = aData->Length();
322 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
326 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
328 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
329 return aRefs.IsNull() ? 0 : aRefs->Extent();
332 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
333 const int theTag ) const
335 if ( theObj.IsNull() )
338 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
339 if ( aRefs.IsNull() || aRefs->IsEmpty() )
342 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
343 for ( ; aListIt.More(); aListIt.Next() )
345 const TDF_Label& aRefLabel = aListIt.Value();
346 if ( theObj->Label() == aRefLabel )
353 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
356 if ( theObj.IsNull() )
359 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
360 aRefs->Append( theObj->Label() );
363 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
367 if ( theObj.IsNull() )
369 RemoveReferenceObject( theTag, theIndex );
373 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
375 if ( theIndex >= aRefs->Extent() )
377 aRefs->Append( theObj->Label() );
379 else if ( theIndex < 0 )
381 aRefs->Prepend( theObj->Label() );
385 RemoveReferenceObject( theTag, theIndex );
387 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
389 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
390 if ( !aBeforeObj.IsNull() )
391 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
393 aRefs->Append( theObj->Label() );
397 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
399 const int theBeforeIndex )
401 if ( theObj.IsNull() )
404 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
406 if ( theBeforeIndex >= aRefs->Extent() )
408 aRefs->Append( theObj->Label() );
410 else if ( theBeforeIndex < 0 )
412 aRefs->Prepend( theObj->Label() );
416 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
417 if ( !aBeforeObj.IsNull() )
418 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
420 aRefs->Append( theObj->Label() );
424 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
427 ClearReferenceObjects( theTag );
428 if ( theObjects.IsEmpty() )
431 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
432 for ( ; anIter.More(); anIter.Next() )
433 AddReferenceObject( anIter.Value(), theTag );
436 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
437 const int theIndex ) const
439 Handle(HYDROData_Entity) aRes;
441 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
442 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
445 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
446 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
448 const TDF_Label& aRefLabel = anIter.Value();
449 aRes = HYDROData_Iterator::Object( aRefLabel );
454 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
456 HYDROData_SequenceOfObjects aRes;
458 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
459 if ( aRefs.IsNull() )
462 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
463 for ( ; anIter.More(); anIter.Next() )
465 const TDF_Label& aRefLabel = anIter.Value();
467 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
468 if ( aRefObject.IsNull() )
471 aRes.Append( aRefObject );
477 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
480 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
481 if ( aRefs.IsNull() )
484 if ( aRefs->Extent() == 1 )
486 // remove all if only one
487 ClearReferenceObjects( theTag );
491 aRefs->Remove( theRefLabel );
494 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
497 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
498 if ( aRefs.IsNull() )
501 if ( aRefs->Extent() == 1 && theIndex == 0 )
503 // remove all if only one
504 ClearReferenceObjects( theTag );
509 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
510 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
512 if ( anIndex != theIndex || !anIter.More() )
515 const TDF_Label& aRefLabel = anIter.Value();
516 aRefs->Remove( aRefLabel );
519 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
521 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
522 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
525 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
526 const bool theIsCreate ) const
528 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
530 Handle(TDataStd_ReferenceList) aRefs;
531 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
532 aRefs = TDataStd_ReferenceList::Set( aLabel );
537 void HYDROData_Entity::SetColor( const QColor& theColor,
540 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
542 Handle(TDataStd_IntegerArray) aColorArray;
543 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
544 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
546 aColorArray->SetValue( 1, theColor.red() );
547 aColorArray->SetValue( 2, theColor.green() );
548 aColorArray->SetValue( 3, theColor.blue() );
549 aColorArray->SetValue( 4, theColor.alpha() );
552 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
553 const int theTag ) const
555 QColor aResColor = theDefColor;
557 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
559 Handle(TDataStd_IntegerArray) aColorArray;
560 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
562 aResColor.setRed( aColorArray->Value( 1 ) );
563 aResColor.setGreen( aColorArray->Value( 2 ) );
564 aResColor.setBlue( aColorArray->Value( 3 ) );
565 aResColor.setAlpha( aColorArray->Value( 4 ) );
571 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
573 QStringList aResList;
575 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
576 if ( aDocument.IsNull() )
579 QString aDocName = aDocument->GetDocPyName();
580 QString aName = GetObjPyName();
582 aResList << QString( "%1 = %2.CreateObject( %3 );" )
583 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
584 aResList << QString( "%1.SetName( \"%2\" );" )
585 .arg( aName ).arg( GetName() );
586 aResList << QString( "" );
590 // Dump object z-level in viewer
591 Standard_Integer anObjZLevel = -1;
592 if ( GetZLevel( anObjZLevel ) )
594 aResList << QString( "%1.SetZLevel( %2 );" )
595 .arg( aName ).arg( anObjZLevel );
596 aResList << QString( "" );
603 QString HYDROData_Entity::getPyTypeID() const
607 case KIND_IMAGE: return "KIND_IMAGE";
608 case KIND_POLYLINE: return "KIND_POLYLINE";
609 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
610 case KIND_ALTITUDE: return "KIND_ALTITUDE";
611 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
612 case KIND_RIVER: return "KIND_RIVER";
613 case KIND_STREAM: return "KIND_STREAM";
614 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
615 case KIND_CHANNEL: return "KIND_CHANNEL";
616 case KIND_OBSTACLE: return "KIND_OBSTACLE";
617 case KIND_DIGUE: return "KIND_DIGUE";
618 case KIND_PROFILE: return "KIND_PROFILE";
619 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
620 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
621 case KIND_CALCULATION: return "KIND_CALCULATION";
622 case KIND_ZONE: return "KIND_ZONE";
623 case KIND_REGION: return "KIND_REGION";
624 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
625 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
626 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
627 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
628 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
629 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
630 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
631 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
632 case KIND_STRICKLER_TABLE: return "KIND_STRICKLER_TABLE";
633 case KIND_LAND_COVER: return "KIND_LAND_COVER";
634 default: return "KIND_UNKNOWN"; ///! Unrecognized object
638 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
639 QStringList& theScript,
640 const Handle(HYDROData_Entity)& theRefObject,
641 const QString& theMethod ) const
643 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
646 QString aRefObjName = theRefObject->GetObjPyName();
648 QString anObjName = GetObjPyName();
649 theScript << QString( "%1.%2( %3 );" )
650 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
653 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
654 QStringList& theScript,
655 const Handle(HYDROData_Entity)& theRefObject ) const
657 if ( theRefObject.IsNull() )
660 QString aRefObjName = theRefObject->GetName();
661 if ( aRefObjName.isEmpty() )
664 if ( theTreatedObjects.contains( aRefObjName ) )
667 // The definition of reference object must be dumped before this
668 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
669 if ( aRefObjDump.isEmpty() )
672 QStringList aTmpList = theScript;
673 theScript = aRefObjDump;
675 theScript << QString( "" );
676 theScript << aTmpList;
678 theTreatedObjects.insert( aRefObjName, theRefObject );
683 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
684 const QColor& theColor,
685 const QColor& theDefaultColor,
686 const QString& theMethod ) const
688 if ( theColor == theDefaultColor )
689 return; //Do not set the color for object if it like default
691 QString anObjName = GetObjPyName();
692 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
693 .arg( anObjName ).arg( theMethod )
694 .arg( theColor.red() ).arg( theColor.green() )
695 .arg( theColor.blue() ).arg( theColor.alpha() );
698 void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
699 QStringList& theScript ) const
701 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
702 if ( aDocument.IsNull() )
705 theScript << QString( "%1 = %2.FindObjectByName( \"%3\" );" ).arg( GetObjPyName() )
706 .arg( aDocument->GetDocPyName() )