Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROData / HYDROData_Entity.cxx
index 30c2aa948705b81736dcf7e8183868795fbe6e36..f0b9e5fb034a06e9b26123375f8f69eab2fe0fc2 100644 (file)
@@ -1,27 +1,57 @@
-
-#include "HYDROData_Entity.h"
-
-#include "HYDROData_Iterator.h"
-#include "HYDROData_Tool.h"
-
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#include <HYDROData_Entity.h>
+#include <HYDROData_Document.h>
+#include <HYDROData_Iterator.h>
+#include <HYDROData_Tool.h>
+#include <Standard_GUID.hxx>
 #include <TDataStd_Name.hxx>
 #include <TDataStd_ByteArray.hxx>
 #include <TDataStd_UAttribute.hxx>
-#include <TDataStd_IntegerArray.hxx>
-#include <TDataStd_BooleanArray.hxx>
 #include <TDataStd_Integer.hxx>
-#include <TDataStd_RealArray.hxx>
+#include <TDataStd_IntegerArray.hxx>
 #include <TDataStd_ReferenceList.hxx>
-
 #include <TDF_CopyLabel.hxx>
 #include <TDF_ListIteratorOfLabelList.hxx>
-
+#include <TNaming_Builder.hxx>
+#include <TNaming_NamedShape.hxx>
+#include <TopoDS_Shape.hxx>
 #include <QColor>
-#include <QString>
+#include <QRegExp>
 #include <QStringList>
 #include <QVariant>
 
-static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
+HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects()
+  : NCollection_Sequence<Handle_HYDROData_Entity>()
+{
+}
+
+HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const HYDROData_SequenceOfObjects& theSequence )
+  : NCollection_Sequence<Handle_HYDROData_Entity>( theSequence )
+{
+}
+
+HYDROData_SequenceOfObjects::HYDROData_SequenceOfObjects( const NCollection_Sequence<Handle_HYDROData_Entity>& theSequence )
+  : NCollection_Sequence<Handle_HYDROData_Entity>( theSequence )
+{
+}
+
 
 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
@@ -46,7 +76,10 @@ QString HYDROData_Entity::GetName() const
 
 QString HYDROData_Entity::GetObjPyName() const
 {
-  return GetName().replace(" ", "_");
+  QString aName = GetName();
+  aName.replace(QRegExp("[\\W]"), "_");
+
+  return aName;
 }
 
 void HYDROData_Entity::SetName(const QString& theName)
@@ -62,7 +95,7 @@ QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjec
 
 void HYDROData_Entity::Update()
 {
-  SetToUpdate( false );
+  ClearChanged();
 }
 
 void HYDROData_Entity::UpdateLocalCS( double theDx, double theDy )
@@ -92,31 +125,61 @@ QVariant HYDROData_Entity::GetDataVariant()
   return QVariant();
 }
 
-void HYDROData_Entity::SetToUpdate( bool theFlag )
+void HYDROData_Entity::ClearChanged()
+{
+  TDataStd_Integer::Set( myLab.FindChild( DataTag_GeomChange ), 0 );
+}
+
+int HYDROData_Entity::GetGeomChangeFlag() const
+{
+  int aGeomChangeFlag = 0;
+  Handle(TDataStd_Integer) aGeomChangeAttr;
+  TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
+  aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
+  if ( !aGeomChangeAttr.IsNull() )
+    aGeomChangeFlag = aGeomChangeAttr->Get();
+  return aGeomChangeFlag;
+}
+
+void HYDROData_Entity::Changed( Geometry theChangedGeometry )
 {
-  if ( IsMustBeUpdated() == theFlag )
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+  if( aDocument.IsNull() )
     return;
 
-  if ( theFlag )
-  {
-    TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
+  int aGeomChangeFlag = 0;
+  Handle(TDataStd_Integer) aGeomChangeAttr;
+  TDF_Label aGeomChangeLab = myLab.FindChild( DataTag_GeomChange );
+  aGeomChangeLab.FindAttribute( TDataStd_Integer::GetID(), aGeomChangeAttr );
+  if ( !aGeomChangeAttr.IsNull() )
+    aGeomChangeFlag = aGeomChangeAttr->Get();
 
-    Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-    if ( !aDocument.IsNull() )
+  int aBitsToChange = ( myGeom & theChangedGeometry );
+  if( aBitsToChange == 0 )
+    return;
+
+  aGeomChangeFlag = ( aGeomChangeFlag | aBitsToChange );
+  TDataStd_Integer::Set( aGeomChangeLab, aGeomChangeFlag );
+
+  HYDROData_Iterator anIter( aDocument );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    Handle(HYDROData_Entity) anObject = anIter.Current();
+    if( anObject.IsNull() )
+      continue;
+    HYDROData_SequenceOfObjects aRefSeq = anObject->GetAllReferenceObjects();
+    for ( int i = 1, n = aRefSeq.Length(); i <= n; ++i )
     {
-      // Change the states of this and all depended objects
-      HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
+      Handle(HYDROData_Entity) aRefObject = aRefSeq.Value( i );
+      if( aRefObject->Label()==myLab )
+        anObject->Changed( theChangedGeometry );
     }
   }
-  else
-  {
-    myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
-  }
 }
 
-bool HYDROData_Entity::IsMustBeUpdated() const
+bool HYDROData_Entity::IsMustBeUpdated( Geometry theGeom ) const
 {
-  return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
+  return ( ( GetGeomChangeFlag() & theGeom ) != 0 );
 }
 
 bool HYDROData_Entity::CanBeUpdated() const
@@ -139,7 +202,8 @@ bool HYDROData_Entity::CanRemove()
   return true;
 }
 
-HYDROData_Entity::HYDROData_Entity()
+HYDROData_Entity::HYDROData_Entity( Geometry theGeom )
+  : myGeom( theGeom )
 {
 }
 
@@ -147,10 +211,37 @@ HYDROData_Entity::~HYDROData_Entity()
 {
 }
 
-void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
+void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination,
+                               bool isGenerateNewName ) const
 {
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+  if ( aDocument.IsNull() ) {
+    return;
+  }
+
   TDF_CopyLabel aCopy(myLab, theDestination->Label());
   aCopy.Perform();
+         
+  if( isGenerateNewName )
+  {
+    // generate a new unique name for the clone object:
+    // case 1: Image_1 -> Image_2
+    // case 2: ImageObj -> ImageObj_1
+    QString aName = theDestination->GetName();
+    QString aPrefix = aName;
+    if( aName.contains( '_' ) ) { // case 1
+      QString aSuffix = aName.section( '_', -1 );
+      bool anIsInteger = false;
+      aSuffix.toInt( &anIsInteger );
+      if( anIsInteger )
+        aPrefix = aName.section( '_', 0, -2 );
+    } else { // case 2
+      aPrefix = aName;
+    }
+
+    aName = HYDROData_Tool::GenerateObjectName( aDocument, aPrefix );
+    theDestination->SetName( aName );
+  }
 }
 
 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
@@ -556,6 +647,9 @@ QString HYDROData_Entity::getPyTypeID() const
     case KIND_SPLITTED_GROUP:    return "KIND_SPLITTED_GROUP";
     case KIND_STREAM_ALTITUDE:   return "KIND_STREAM_ALTITUDE";
     case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
+    case KIND_STRICKLER_TABLE:   return "KIND_STRICKLER_TABLE";
+    case KIND_LAND_COVER_OBSOLETE: return "";
+    case KIND_LAND_COVER_MAP:    return "KIND_LAND_COVER_MAP";
     default:                     return "KIND_UNKNOWN"; ///! Unrecognized object
   }
 }
@@ -631,3 +725,21 @@ void HYDROData_Entity::findPythonReferenceObject( MapOfTreatedObjects& theTreate
                                                                .arg( aDocument->GetDocPyName() )
                                                                .arg( GetName() );
 }
+
+void HYDROData_Entity::SetShape( int theTag, const TopoDS_Shape& theShape )
+{
+  TNaming_Builder aBuilder( myLab.FindChild( theTag ) );
+  aBuilder.Generated( theShape );
+}
+
+TopoDS_Shape HYDROData_Entity::GetShape( int theTag ) const
+{
+  TDF_Label aShapeLabel = myLab.FindChild( theTag, false );
+  if ( !aShapeLabel.IsNull() )
+  {
+    Handle(TNaming_NamedShape) aNamedShape;
+    if ( aShapeLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
+      return aNamedShape->Get();
+  }
+  return TopoDS_Shape();
+}