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>
42 #include "HYDRO_trace.hxx"
44 //static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
46 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
47 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
49 // is equal function for unique object mapping
50 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
52 if ( !theObj1.IsNull() && !theObj2.IsNull() )
53 return theObj1->Label() == theObj2->Label();
57 QString HYDROData_Entity::GetName() const
59 Handle(TDataStd_Name) aName;
60 if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
61 TCollection_AsciiString aStr(aName->Get());
62 return QString(aStr.ToCString());
67 QString HYDROData_Entity::GetObjPyName() const
69 QString aName = GetName();
70 aName.replace(QRegExp("[\\W]"), "_");
75 void HYDROData_Entity::SetName(const QString& theName)
77 TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
80 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
82 QStringList anEmptyList;
86 void HYDROData_Entity::Update()
91 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
93 //On the base level no actions are necessary
96 bool HYDROData_Entity::IsHas2dPrs() const
101 void HYDROData_Entity::Show()
106 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
107 if ( aDocument.IsNull() )
110 aDocument->Show( this );
113 QVariant HYDROData_Entity::GetDataVariant()
118 void HYDROData_Entity::ClearChanged()
120 TDataStd_Integer::Set( myLab.FindChild( DataTag_GeomChange ), 0 );
123 int HYDROData_Entity::GetGeomChangeFlag() const
125 int aGeomChangeFlag = 0;
126 Handle(TDataStd_Integer) aGeomChangeAttr;
127 TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
128 aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
129 if ( !aGeomChangeAttr.IsNull() )
130 aGeomChangeFlag = aGeomChangeAttr->Get();
131 return aGeomChangeFlag;
134 void HYDROData_Entity::Changed( Geometry theChangedGeometry )
136 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
137 if( aDocument.IsNull() )
140 int aGeomChangeFlag = 0;
141 Handle(TDataStd_Integer) aGeomChangeAttr;
142 TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
143 aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
144 if ( !aGeomChangeAttr.IsNull() )
145 aGeomChangeFlag = aGeomChangeAttr->Get();
147 int aBitsToChange = ( myGeom & theChangedGeometry );
148 if( aBitsToChange == 0 )
151 aGeomChangeFlag = ( aGeomChangeFlag | aBitsToChange );
152 TDataStd_Integer::Set( aGeomChangeLab, aGeomChangeFlag );
154 HYDROData_Iterator anIter( aDocument );
155 for ( ; anIter.More(); anIter.Next() )
157 Handle(HYDROData_Entity) anObject = anIter.Current();
158 HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
159 for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
161 Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
162 if( aRefObject->Label()==myLab )
163 anObject->Changed( theChangedGeometry );
168 bool HYDROData_Entity::IsMustBeUpdated( Geometry theGeom ) const
170 return ( ( GetGeomChangeFlag() & theGeom ) != 0 );
173 bool HYDROData_Entity::CanBeUpdated() const
178 bool HYDROData_Entity::IsRemoved() const
180 return !myLab.HasAttribute();
183 void HYDROData_Entity::Remove()
185 return myLab.ForgetAllAttributes( true );
188 bool HYDROData_Entity::CanRemove()
193 HYDROData_Entity::HYDROData_Entity( Geometry theGeom )
198 HYDROData_Entity::~HYDROData_Entity()
202 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination,
203 bool isGenerateNewName ) const
205 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
206 if ( aDocument.IsNull() ) {
210 TDF_CopyLabel aCopy(myLab, theDestination->Label());
213 if( isGenerateNewName )
215 // generate a new unique name for the clone object:
216 // case 1: Image_1 -> Image_2
217 // case 2: ImageObj -> ImageObj_1
218 QString aName = theDestination->GetName();
219 QString aPrefix = aName;
220 if( aName.contains( '_' ) ) { // case 1
221 QString aSuffix = aName.section( '_', -1 );
222 bool anIsInteger = false;
223 aSuffix.toInt( &anIsInteger );
225 aPrefix = aName.section( '_', 0, -2 );
230 aName = HYDROData_Tool::GenerateObjectName( aDocument, aPrefix );
231 theDestination->SetName( aName );
235 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
237 Handle(HYDROData_Entity) aFather;
239 if ( !myLab.IsNull() )
241 TDF_Label aFatherLabel = myLab.Father();
243 while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
245 aFather = HYDROData_Iterator::Object( aFatherLabel );
246 aFatherLabel = aFatherLabel.Father();
253 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
255 return HYDROData_SequenceOfObjects();
258 Standard_Boolean HYDROData_Entity::GetZLevel( Standard_Integer& theLevel ) const
262 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
263 if ( !aLabel.IsNull() )
265 Handle(TDataStd_Integer) anIntVal;
266 if ( aLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) )
268 theLevel = anIntVal->Get();
269 return Standard_True;
273 return Standard_False;
276 void HYDROData_Entity::SetZLevel( const Standard_Integer& theLevel )
278 TDataStd_Integer::Set( myLab.FindChild( DataTag_ZLevel ), theLevel );
281 void HYDROData_Entity::RemoveZLevel()
283 TDF_Label aLabel = myLab.FindChild( DataTag_ZLevel, false );
284 if ( !aLabel.IsNull() )
285 aLabel.ForgetAllAttributes();
288 void HYDROData_Entity::SetLabel( const TDF_Label& theLabel )
293 void HYDROData_Entity::SaveByteArray( const int theTag,
297 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
298 // array is empty, remove the attribute
300 aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
303 // store data of image in byte array
304 Handle(TDataStd_ByteArray) aData;
305 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
306 aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
308 // copy bytes one by one
309 if (aData->Length() != theLen) {
310 Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
311 for(int a = 0; a < theLen; a++)
312 aNewData->SetValue(a + 1, theData[a]);
313 aData->ChangeArray(aNewData);
315 for(int a = 0; a < theLen; a++)
316 aData->SetValue(a + 1, theData[a]);
320 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
322 TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
323 Handle(TDataStd_ByteArray) aData;
324 if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
325 return NULL; // return empty image if there is no array
326 theLen = aData->Length();
328 return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
332 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
334 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
335 return aRefs.IsNull() ? 0 : aRefs->Extent();
338 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
339 const int theTag ) const
341 if ( theObj.IsNull() )
344 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
345 if ( aRefs.IsNull() || aRefs->IsEmpty() )
348 TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
349 for ( ; aListIt.More(); aListIt.Next() )
351 const TDF_Label& aRefLabel = aListIt.Value();
352 if ( theObj->Label() == aRefLabel )
359 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
362 if ( theObj.IsNull() )
365 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
366 aRefs->Append( theObj->Label() );
369 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
373 if ( theObj.IsNull() )
375 RemoveReferenceObject( theTag, theIndex );
379 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
381 if ( theIndex >= aRefs->Extent() )
383 aRefs->Append( theObj->Label() );
385 else if ( theIndex < 0 )
387 aRefs->Prepend( theObj->Label() );
391 RemoveReferenceObject( theTag, theIndex );
393 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
395 aRefs = getReferenceList( theTag, true ); // because reference list can be removed
396 if ( !aBeforeObj.IsNull() )
397 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
399 aRefs->Append( theObj->Label() );
403 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
405 const int theBeforeIndex )
407 if ( theObj.IsNull() )
410 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
412 if ( theBeforeIndex >= aRefs->Extent() )
414 aRefs->Append( theObj->Label() );
416 else if ( theBeforeIndex < 0 )
418 aRefs->Prepend( theObj->Label() );
422 Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
423 if ( !aBeforeObj.IsNull() )
424 aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
426 aRefs->Append( theObj->Label() );
430 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
433 ClearReferenceObjects( theTag );
434 if ( theObjects.IsEmpty() )
437 HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
438 for ( ; anIter.More(); anIter.Next() )
439 AddReferenceObject( anIter.Value(), theTag );
442 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
443 const int theIndex ) const
445 Handle(HYDROData_Entity) aRes;
447 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
448 if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
451 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
452 for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
454 const TDF_Label& aRefLabel = anIter.Value();
455 aRes = HYDROData_Iterator::Object( aRefLabel );
460 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
462 HYDROData_SequenceOfObjects aRes;
464 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
465 if ( aRefs.IsNull() )
468 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
469 for ( ; anIter.More(); anIter.Next() )
471 const TDF_Label& aRefLabel = anIter.Value();
473 Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
474 if ( aRefObject.IsNull() )
477 aRes.Append( aRefObject );
483 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
486 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
487 if ( aRefs.IsNull() )
490 if ( aRefs->Extent() == 1 )
492 // remove all if only one
493 ClearReferenceObjects( theTag );
497 aRefs->Remove( theRefLabel );
500 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
503 Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
504 if ( aRefs.IsNull() )
507 if ( aRefs->Extent() == 1 && theIndex == 0 )
509 // remove all if only one
510 ClearReferenceObjects( theTag );
515 TDF_ListIteratorOfLabelList anIter( aRefs->List() );
516 for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
518 if ( anIndex != theIndex || !anIter.More() )
521 const TDF_Label& aRefLabel = anIter.Value();
522 aRefs->Remove( aRefLabel );
525 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
527 TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
528 aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
531 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
532 const bool theIsCreate ) const
534 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
536 Handle(TDataStd_ReferenceList) aRefs;
537 if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
538 aRefs = TDataStd_ReferenceList::Set( aLabel );
543 void HYDROData_Entity::SetColor( const QColor& theColor,
546 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
548 Handle(TDataStd_IntegerArray) aColorArray;
549 if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
550 aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
552 aColorArray->SetValue( 1, theColor.red() );
553 aColorArray->SetValue( 2, theColor.green() );
554 aColorArray->SetValue( 3, theColor.blue() );
555 aColorArray->SetValue( 4, theColor.alpha() );
558 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
559 const int theTag ) const
561 QColor aResColor = theDefColor;
563 TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
565 Handle(TDataStd_IntegerArray) aColorArray;
566 if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
568 aResColor.setRed( aColorArray->Value( 1 ) );
569 aResColor.setGreen( aColorArray->Value( 2 ) );
570 aResColor.setBlue( aColorArray->Value( 3 ) );
571 aResColor.setAlpha( aColorArray->Value( 4 ) );
577 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
579 QStringList aResList;
581 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
582 if ( aDocument.IsNull() )
585 QString aDocName = aDocument->GetDocPyName();
586 QString aName = GetObjPyName();
588 aResList << QString( "%1 = %2.CreateObject( %3 );" )
589 .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
590 aResList << QString( "%1.SetName( \"%2\" );" )
591 .arg( aName ).arg( GetName() );
592 aResList << QString( "" );
596 // Dump object z-level in viewer
597 Standard_Integer anObjZLevel = -1;
598 if ( GetZLevel( anObjZLevel ) )
600 aResList << QString( "%1.SetZLevel( %2 );" )
601 .arg( aName ).arg( anObjZLevel );
602 aResList << QString( "" );
609 QString HYDROData_Entity::getPyTypeID() const
611 DEBTRACE("HYDROData_Entity::getPyTypeID " << GetKind());
614 case KIND_IMAGE: return "KIND_IMAGE";
615 case KIND_POLYLINE: return "KIND_POLYLINE";
616 case KIND_BATHYMETRY: return "KIND_BATHYMETRY";
617 case KIND_ALTITUDE: return "KIND_ALTITUDE";
618 case KIND_IMMERSIBLE_ZONE: return "KIND_IMMERSIBLE_ZONE";
619 case KIND_RIVER: return "KIND_RIVER";
620 case KIND_STREAM: return "KIND_STREAM";
621 case KIND_CONFLUENCE: return "KIND_CONFLUENCE";
622 case KIND_CHANNEL: return "KIND_CHANNEL";
623 case KIND_OBSTACLE: return "KIND_OBSTACLE";
624 case KIND_DIGUE: return "KIND_DIGUE";
625 case KIND_PROFILE: return "KIND_PROFILE";
626 case KIND_PROFILEUZ: return "KIND_PROFILEUZ";
627 case KIND_POLYLINEXY: return "KIND_POLYLINEXY";
628 case KIND_CALCULATION: return "KIND_CALCULATION";
629 case KIND_ZONE: return "KIND_ZONE";
630 case KIND_REGION: return "KIND_REGION";
631 case KIND_VISUAL_STATE: return "KIND_VISUAL_STATE";
632 case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
633 case KIND_NATURAL_OBJECT: return "KIND_NATURAL_OBJECT";
634 case KIND_DUMMY_3D: return "KIND_DUMMY_3D";
635 case KIND_SHAPES_GROUP: return "KIND_SHAPES_GROUP";
636 case KIND_SPLITTED_GROUP: return "KIND_SPLITTED_GROUP";
637 case KIND_STREAM_ALTITUDE: return "KIND_STREAM_ALTITUDE";
638 case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
639 case KIND_STRICKLER_TABLE: return "KIND_STRICKLER_TABLE";
640 case KIND_LAND_COVER: return "KIND_LAND_COVER";
641 case KIND_CHANNEL_ALTITUDE: return "KIND_CHANNEL_ALTITUDE";
642 default: return "KIND_UNKNOWN"; ///! Unrecognized object
646 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
647 QStringList& theScript,
648 const Handle(HYDROData_Entity)& theRefObject,
649 const QString& theMethod ) const
651 if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
654 QString aRefObjName = theRefObject->GetObjPyName();
656 QString anObjName = GetObjPyName();
657 theScript << QString( "%1.%2( %3 );" )
658 .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
661 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects& theTreatedObjects,
662 QStringList& theScript,
663 const Handle(HYDROData_Entity)& theRefObject ) const
665 if ( theRefObject.IsNull() )
668 QString aRefObjName = theRefObject->GetName();
669 if ( aRefObjName.isEmpty() )
672 if ( theTreatedObjects.contains( aRefObjName ) )
675 // The definition of reference object must be dumped before this
676 QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
677 if ( aRefObjDump.isEmpty() )
680 QStringList aTmpList = theScript;
681 theScript = aRefObjDump;
683 theScript << QString( "" );
684 theScript << aTmpList;
686 theTreatedObjects.insert( aRefObjName, theRefObject );
691 void HYDROData_Entity::setPythonObjectColor( QStringList& theScript,
692 const QColor& theColor,
693 const QColor& theDefaultColor,
694 const QString& theMethod ) const
696 if ( theColor == theDefaultColor )
697 return; //Do not set the color for object if it like default
699 QString anObjName = GetObjPyName();
700 theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
701 .arg( anObjName ).arg( theMethod )
702 .arg( theColor.red() ).arg( theColor.green() )
703 .arg( theColor.blue() ).arg( theColor.alpha() );
706 void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreatedObjects,
707 QStringList& theScript ) const
709 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
710 if ( aDocument.IsNull() )
713 theScript << QString( "%1 = %2.FindObjectByName( \"%3\" );" ).arg( GetObjPyName() )
714 .arg( aDocument->GetDocPyName() )