Salome HOME
updated copyright message
[modules/gui.git] / src / SalomeApp / pluginsdemo / minmax_plugin.py
index c743e5279f1e4c992ef00433b10138a7e63f550c..fbb6eae68a114237b9ad4dc59705864ea48b3275 100644 (file)
@@ -1,10 +1,10 @@
 # -*- coding: utf-8 -*-
-# Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2010-2023  CEA, EDF, OPEN CASCADE
 #
 # 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.
+# 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
 #
 # Author : Guillaume Boulant (EDF)
 
+from qtsalome import *
+
 def minmax(context):
-  # get context study, studyId, salomeGui
+  # get context study, salomeGui
   study = context.study
-  studyId = context.studyId
   sg = context.sg
 
-  from PyQt4.QtGui import QDialog
-  from PyQt4.QtGui import QMessageBox
-  from PyQt4.QtCore import Qt
-  from PyQt4.QtCore import SIGNAL
-
-  from minmax_dialog import Ui_Dialog
+  from minmax_ui import Ui_Dialog
 
   import salome
-  import smesh
+  import SMESH
+  from salome.smesh import smeshBuilder
+  smesh = smeshBuilder.New()
 
   controls_dict = {
-    "Aspect Ratio 3D" :     smesh.FT_AspectRatio3D,
-    "Volume" :              smesh.FT_Volume3D,
-    "Element Diameter 3D" : smesh.FT_MaxElementLength3D,
-    "Length 2D" :           smesh.FT_Length2D,
-    "MultiConnection 2D" :  smesh.FT_MultiConnection2D,
-    "Area" :                smesh.FT_Area,
-    "Taper" :               smesh.FT_Taper,
-    "Aspect Ratio" :        smesh.FT_AspectRatio,
-    "Minimum Angle" :       smesh.FT_MinimumAngle,
-    "Warping" :             smesh.FT_Warping,
-    "Skew" :                smesh.FT_Skew,
-    "Element Diameter 2D" : smesh.FT_MaxElementLength2D,
-    "Length" :              smesh.FT_Length,
-    "MultiConnection" :     smesh.FT_MultiConnection,
+    "Aspect Ratio 3D" :     SMESH.FT_AspectRatio3D,
+    "Volume" :              SMESH.FT_Volume3D,
+    "Element Diameter 3D" : SMESH.FT_MaxElementLength3D,
+    "Length 2D" :           SMESH.FT_Length2D,
+    "MultiConnection 2D" :  SMESH.FT_MultiConnection2D,
+    "Area" :                SMESH.FT_Area,
+    "Taper" :               SMESH.FT_Taper,
+    "Aspect Ratio" :        SMESH.FT_AspectRatio,
+    "Minimum Angle" :       SMESH.FT_MinimumAngle,
+    "Warping" :             SMESH.FT_Warping,
+    "Skew" :                SMESH.FT_Skew,
+    "Element Diameter 2D" : SMESH.FT_MaxElementLength2D,
+    "Length" :              SMESH.FT_Length,
+    "MultiConnection" :     SMESH.FT_MultiConnection,
     }
 
   controls_3d = [
@@ -84,7 +82,7 @@ def minmax(context):
       self.clearLineEdit()
 
       # Connect up the selectionChanged() event of the object browser.
-      self.connect(sg.getObjectBrowser(), SIGNAL("selectionChanged()"), self.select)
+      sg.getObjectBrowser().selectionChanged.connect(self.select)
 
       self.mm = None
       self.ui.control.setFocus()
@@ -93,7 +91,7 @@ def minmax(context):
       pass
 
     def OnCancel(self):
-      self.disconnect(sg.getObjectBrowser(), SIGNAL("selectionChanged()"), self.select)
+      sg.getObjectBrowser().selectionChanged.disconnect(self.select)
       self.reject()
       pass
 
@@ -104,8 +102,7 @@ def minmax(context):
       self.ui.maxvalue.setText("")
 
     def select(self):
-      self.disconnect(sg.getObjectBrowser(), SIGNAL("selectionChanged()"), self.select)
-      self.ui.control.clear()
+      sg.getObjectBrowser().selectionChanged.disconnect(self.select)
       self.ui.minvalue.setText("")
       self.ui.maxvalue.setText("")
       objId = salome.sg.getSelected(0)
@@ -113,23 +110,20 @@ def minmax(context):
         mm = study.FindObjectID(objId).GetObject()
         mesh = None
         try:
-          mesh = mm.GetMEDMesh()
+          mm.Load()
+          mesh = mm
         except:
-          #print "No mesh selected"
           self.clearLineEdit()
           mesh = None
           pass
         if mesh:
+          name = smeshBuilder.GetName( mm )
           self.ui.mesh.setStyleSheet("")
-          self.ui.mesh.setText(mesh.getName())
-          #print "Mesh selected: ", mesh.getName()
+          self.ui.mesh.setText( name )
           self.mm = mm
           e = self.mm.NbEdges()
           f = self.mm.NbFaces()
           v = self.mm.NbVolumes()
-          #print "NbEdges: ",e
-          #print "NbFaces: ",f
-          #print "NbVolumes: ",v
           controls = []
           if e:
             controls += controls_1d
@@ -140,18 +134,20 @@ def minmax(context):
           if v:
             controls += controls_3d
             pass
-          self.ui.control.addItems(controls)
+          if self.ui.control.count() != len( controls ):
+            self.ui.control.clear()
+            self.ui.control.addItems(controls)
           self.compute_minmax()
-      self.connect(sg.getObjectBrowser(), SIGNAL("selectionChanged()"), self.select)
+      sg.getObjectBrowser().selectionChanged.connect(self.select)
       pass
 
     def helpMessage(self):
-      QMessageBox.about(None, "About Min/Max value of control",
+      QMessageBox.about(None, "About Min/Max and Average value of control",
       """
-      Displays the min/max value of a control
-      ---------------------------------
+      Displays the min/max and average value of a control
+      ---------------------------------------------------
 
-This plugin displays the min and max value of a control
+      This plugin displays the min, max and average value of a control
 on a mesh.
 Inputs:
   - The mesh to analyse
@@ -160,20 +156,25 @@ Inputs:
       pass
 
     def compute_minmax(self):
-      if self.mm:
-        control = self.ui.control.currentText()
-        #print "Compute control: ",control
+      control = self.ui.control.currentText()
+      if self.mm and control:
         fun = smesh.GetFunctor(controls_dict[str(control)])
-        fun.SetMesh(self.mm.GetMesh())
-        hist = fun.GetHistogram(1)
-        maxVal = hist[0].max
+        fun.SetMesh(self.mm)
+        nbRectangles = int(max(100, self.mm.NbElements() / 100 ))
+        hist = fun.GetHistogram(nbRectangles,False)
+        maxVal = hist[-1].max
         minVal = hist[0].min
-        #print "Max value for %s: %f"%(control, maxVal)
-        #print "Min value for %s: %f"%(control, minVal)
+        avgVal = 0
+        nbElems = 0
+        for rect in hist:
+          avgVal += 0.5 * ( hist[0].max + hist[0].min ) * rect.nbEvents
+          nbElems += rect.nbEvents
+        if nbElems > 0:
+          avgVal /= nbElems
         self.ui.maxvalue.setText("%f"%(maxVal))
         self.ui.minvalue.setText("%f"%(minVal))
+        self.ui.avgvalue.setText("%f"%(avgVal))
       else:
-        print "Pas de maillage"
         pass
       pass
     pass