Salome HOME
updated copyright message
[modules/gui.git] / src / SalomeApp / pluginsdemo / minmax_plugin.py
index adc7594261714fe04b48b66808d42c4a122a0236..fbb6eae68a114237b9ad4dc59705864ea48b3275 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Copyright (C) 2010-2015  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
 #
 # 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
   from salome.smesh import smeshBuilder
-  smesh = smeshBuilder.New(salome.myStudy)
+  smesh = smeshBuilder.New()
 
   controls_dict = {
     "Aspect Ratio 3D" :     SMESH.FT_AspectRatio3D,
@@ -86,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()
@@ -95,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
 
@@ -106,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)
@@ -139,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
@@ -163,11 +160,20 @@ Inputs:
       if self.mm and control:
         fun = smesh.GetFunctor(controls_dict[str(control)])
         fun.SetMesh(self.mm)
-        hist = fun.GetHistogram(1,False)
-        maxVal = hist[0].max
+        nbRectangles = int(max(100, self.mm.NbElements() / 100 ))
+        hist = fun.GetHistogram(nbRectangles,False)
+        maxVal = hist[-1].max
         minVal = hist[0].min
+        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:
         pass
       pass