Salome HOME
Updated copyright comment
[modules/gui.git] / src / SalomeApp / pluginsdemo / minmax_plugin.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2024  CEA, EDF, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF)
21
22 from qtsalome import *
23
24 def minmax(context):
25   # get context study, salomeGui
26   study = context.study
27   sg = context.sg
28
29   from minmax_ui import Ui_Dialog
30
31   import salome
32   import SMESH
33   from salome.smesh import smeshBuilder
34   smesh = smeshBuilder.New()
35
36   controls_dict = {
37     "Aspect Ratio 3D" :     SMESH.FT_AspectRatio3D,
38     "Volume" :              SMESH.FT_Volume3D,
39     "Element Diameter 3D" : SMESH.FT_MaxElementLength3D,
40     "Length 2D" :           SMESH.FT_Length2D,
41     "MultiConnection 2D" :  SMESH.FT_MultiConnection2D,
42     "Area" :                SMESH.FT_Area,
43     "Taper" :               SMESH.FT_Taper,
44     "Aspect Ratio" :        SMESH.FT_AspectRatio,
45     "Minimum Angle" :       SMESH.FT_MinimumAngle,
46     "Warping" :             SMESH.FT_Warping,
47     "Skew" :                SMESH.FT_Skew,
48     "Element Diameter 2D" : SMESH.FT_MaxElementLength2D,
49     "Length" :              SMESH.FT_Length,
50     "MultiConnection" :     SMESH.FT_MultiConnection,
51     }
52
53   controls_3d = [
54     "Aspect Ratio 3D",
55     "Volume",
56     "Element Diameter 3D",
57     ]
58   controls_2d = [
59     "Length 2D",
60     "MultiConnection 2D",
61     "Area",
62     "Taper",
63     "Aspect Ratio",
64     "Minimum Angle",
65     "Warping",
66     "Skew",
67     "Element Diameter 2D"
68     ]
69   controls_1d = [
70     "Length",
71     "MultiConnection",
72     ]
73
74   class MinmaxDialog(QDialog):
75     def __init__(self):
76       QDialog.__init__(self, None, Qt.Tool)
77       # Set up the user interface from Designer.
78       self.ui = Ui_Dialog()
79       self.ui.setupUi(self)
80       self.show()
81
82       self.clearLineEdit()
83
84       # Connect up the selectionChanged() event of the object browser.
85       sg.getObjectBrowser().selectionChanged.connect(self.select)
86
87       self.mm = None
88       self.ui.control.setFocus()
89       self.select()
90
91       pass
92
93     def OnCancel(self):
94       sg.getObjectBrowser().selectionChanged.disconnect(self.select)
95       self.reject()
96       pass
97
98     def clearLineEdit(self):
99       self.ui.mesh.setText("Select a Mesh")
100       self.ui.mesh.setStyleSheet("QLineEdit { color: grey }")
101       self.ui.minvalue.setText("")
102       self.ui.maxvalue.setText("")
103
104     def select(self):
105       sg.getObjectBrowser().selectionChanged.disconnect(self.select)
106       self.ui.minvalue.setText("")
107       self.ui.maxvalue.setText("")
108       objId = salome.sg.getSelected(0)
109       if objId:
110         mm = study.FindObjectID(objId).GetObject()
111         mesh = None
112         try:
113           mm.Load()
114           mesh = mm
115         except:
116           self.clearLineEdit()
117           mesh = None
118           pass
119         if mesh:
120           name = smeshBuilder.GetName( mm )
121           self.ui.mesh.setStyleSheet("")
122           self.ui.mesh.setText( name )
123           self.mm = mm
124           e = self.mm.NbEdges()
125           f = self.mm.NbFaces()
126           v = self.mm.NbVolumes()
127           controls = []
128           if e:
129             controls += controls_1d
130             pass
131           if f:
132             controls += controls_2d
133             pass
134           if v:
135             controls += controls_3d
136             pass
137           if self.ui.control.count() != len( controls ):
138             self.ui.control.clear()
139             self.ui.control.addItems(controls)
140           self.compute_minmax()
141       sg.getObjectBrowser().selectionChanged.connect(self.select)
142       pass
143
144     def helpMessage(self):
145       QMessageBox.about(None, "About Min/Max and Average value of control",
146       """
147       Displays the min/max and average value of a control
148       ---------------------------------------------------
149
150       This plugin displays the min, max and average value of a control
151 on a mesh.
152 Inputs:
153   - The mesh to analyse
154   - The control to compute
155       """)
156       pass
157
158     def compute_minmax(self):
159       control = self.ui.control.currentText()
160       if self.mm and control:
161         fun = smesh.GetFunctor(controls_dict[str(control)])
162         fun.SetMesh(self.mm)
163         nbRectangles = int(max(100, self.mm.NbElements() / 100 ))
164         hist = fun.GetHistogram(nbRectangles,False)
165         maxVal = hist[-1].max
166         minVal = hist[0].min
167         avgVal = 0
168         nbElems = 0
169         for rect in hist:
170           avgVal += 0.5 * ( hist[0].max + hist[0].min ) * rect.nbEvents
171           nbElems += rect.nbEvents
172         if nbElems > 0:
173           avgVal /= nbElems
174         self.ui.maxvalue.setText("%f"%(maxVal))
175         self.ui.minvalue.setText("%f"%(minVal))
176         self.ui.avgvalue.setText("%f"%(avgVal))
177       else:
178         pass
179       pass
180     pass
181
182   window = MinmaxDialog()
183   window.exec_()
184   pass
185