Salome HOME
Separate action "Find bottom" for context menu.
[modules/hydro.git] / src / HYDROData / HYDROData_PriorityQueue.cxx
index fd72c219ea6de35d279920a5a8685f387f77e63e..59ffc56d24ecb319dd2a4180ffa603694fd96899 100644 (file)
@@ -1,3 +1,24 @@
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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_PriorityQueue.h>
 #include <HYDROData_CalculationCase.h>
@@ -29,19 +50,38 @@ HYDROData_PriorityQueue::~HYDROData_PriorityQueue()
 Handle(HYDROData_Object) HYDROData_PriorityQueue::GetMostPriorityObject( const QStringList& theZoneObjects,
                                                                          HYDROData_Zone::MergeAltitudesType& theMergeType ) const
 {
+  QStringList aSortedZoneObjects;
+  for( int i=myGeomObjects.Lower(), n=myGeomObjects.Upper(); i<=n; i++ )
+  {
+    QString aName = myGeomObjects.Value( i )->GetName();
+    if( theZoneObjects.contains( aName ) )
+      aSortedZoneObjects.append( aName );
+  }
+
   Handle(HYDROData_Object) aMostPriorityObj;
-  theMergeType = HYDROData_Zone::Merge_Object;
-  QStringList::const_iterator anIt = theZoneObjects.begin(), aLast = theZoneObjects.end();
+  theMergeType = HYDROData_Zone::Merge_UNKNOWN;
+  QStringList::const_iterator anIt = aSortedZoneObjects.begin(), aLast = aSortedZoneObjects.end();
   for( ; anIt!=aLast; anIt++ )
   {
     HYDROData_Zone::MergeAltitudesType aLocalMerge = HYDROData_Zone::Merge_UNKNOWN;
     Handle(HYDROData_Object) anObj = myNames[*anIt];
     if( !anObj.IsNull() )
-      if( aMostPriorityObj.IsNull() || IsMorePriority( anObj, aMostPriorityObj, aLocalMerge ) )
+    {
+      if( aMostPriorityObj.IsNull() )
       {
         aMostPriorityObj = anObj;
-        theMergeType = aLocalMerge;
+        continue;
       }
+
+      bool isMorePriority = IsMorePriority( anObj, aMostPriorityObj, aLocalMerge );
+
+      if( isMorePriority )
+        aMostPriorityObj = anObj;
+
+      if( aLocalMerge != HYDROData_Zone::Merge_UNKNOWN && 
+          ( theMergeType==HYDROData_Zone::Merge_UNKNOWN || isMorePriority ) )
+        theMergeType = aLocalMerge;
+    }
   }
   return aMostPriorityObj;
 }
@@ -102,7 +142,14 @@ void HYDROData_PriorityQueue::AddRule( TDF_Label&                         theRul
                                        const Handle(HYDROData_Object)&    theObject2,
                                        HYDROData_Zone::MergeAltitudesType theMergeType )
 {
-  TDF_Label aNewRuleLab = theRulesLabel.NewChild();
+  // Get the last rule index
+  Standard_Integer aRuleIndex = 0;
+  Handle(TDataStd_Integer) anIntVal;
+  if ( theRulesLabel.FindAttribute( TDataStd_Integer::GetID(), anIntVal ) ) {
+    aRuleIndex = anIntVal->Get();
+  }
+
+  TDF_Label aNewRuleLab = theRulesLabel.FindChild( aRuleIndex );
 
   TDF_Label anObj1Lab = aNewRuleLab.FindChild( Object1_Tag );
   Handle(TDataStd_ReferenceList) aRefs = TDataStd_ReferenceList::Set( anObj1Lab );
@@ -117,6 +164,9 @@ void HYDROData_PriorityQueue::AddRule( TDF_Label&                         theRul
 
   TDF_Label aMergeLab = aNewRuleLab.FindChild( Merge_Tag );
   TDataStd_Integer::Set( aMergeLab, theMergeType );
+
+  // Increment the last rule index
+  TDataStd_Integer::Set( theRulesLabel, aRuleIndex + 1 );
 }
 
 HYDROData_ListOfRules HYDROData_PriorityQueue::GetRules( const TDF_Label& theRulesLabel )
@@ -180,3 +230,67 @@ QString HYDROData_PriorityQueue::DumpRules( const TDF_Label& theRulesLab )
   }
   return aDump;
 }
+
+void HYDROData_PriorityQueue::DumpRulesToPython( const TDF_Label& theRulesLab,
+                                                 const QString& theCalcCaseName,
+                                                 QStringList& theScript )
+{
+  HYDROData_ListOfRules aRules = GetRules( theRulesLab );
+  HYDROData_ListOfRules::const_iterator anIt = aRules.begin(), aLast = aRules.end();
+  for( ; anIt!=aLast; anIt++ )
+  {
+    QString anObj1 = anIt->Object1->GetObjPyName();
+    QString anObj2 = anIt->Object2->GetObjPyName();
+    QString aPriority = anIt->Priority == LESS ? "LESS" : "GREATER";
+    QString aMergeType;
+
+    switch( anIt->MergeType )
+    {
+    case HYDROData_Zone::Merge_UNKNOWN:
+      aMergeType = "HYDROData_Zone.Merge_UNKNOWN";
+      break;
+    case HYDROData_Zone::Merge_ZMIN:
+      aMergeType = "HYDROData_Zone.Merge_ZMIN";
+      break;
+    case HYDROData_Zone::Merge_ZMAX:
+      aMergeType = "HYDROData_Zone.Merge_ZMAX";
+      break;
+    case HYDROData_Zone::Merge_Object:
+      aMergeType = "HYDROData_Zone.Merge_Object";
+      break;
+    }
+
+    QString aRule = QString( "%0.AddRule( %1, %2, %3, %4 )" ).
+      arg( theCalcCaseName ).arg( anObj1 ).arg( aPriority ).arg( anObj2 ).arg( aMergeType );
+
+    theScript << aRule;
+  }
+}
+
+bool HYDROData_PriorityQueue::GetRule( const TDF_Label& theRulesLab,
+                                       int theIndex, 
+                                       Handle(HYDROData_Object)&           theObject1,
+                                       HYDROData_PriorityType&             thePriority,
+                                       Handle(HYDROData_Object)&           theObject2,
+                                       HYDROData_Zone::MergeAltitudesType& theMergeType )
+{
+  TDF_Label aRuleLabel = theRulesLab.FindChild( theIndex );
+
+  Handle(TDataStd_ReferenceList) aRefs1, aRefs2;
+  Handle(TDataStd_Integer) aPriorityAttr, aMergeAttr;
+
+  bool isObj1OK = aRuleLabel.FindChild    ( Object1_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs1 );
+  bool isPriorityOK = aRuleLabel.FindChild( Priority_Tag ).FindAttribute( TDataStd_Integer::GetID(),       aPriorityAttr );
+  bool isObj2OK = aRuleLabel.FindChild    ( Object2_Tag ). FindAttribute( TDataStd_ReferenceList::GetID(), aRefs2 );
+  bool isMergeOK = aRuleLabel.FindChild   ( Merge_Tag ).   FindAttribute( TDataStd_Integer::GetID(),       aMergeAttr );
+
+  bool isOK = isObj1OK && isPriorityOK && isObj2OK && isMergeOK;
+  if( isOK )
+  {
+    theObject1   = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs1->First() ) );
+    thePriority  = ( HYDROData_PriorityType ) aPriorityAttr->Get();
+    theObject2   = Handle_HYDROData_Object::DownCast( HYDROData_Iterator::Object( aRefs2->First() ) );
+    theMergeType = ( HYDROData_Zone::MergeAltitudesType ) aMergeAttr->Get();
+  }
+  return isOK;
+}