Salome HOME
minmax_plugin: Don't reset a current control at selection change
[modules/gui.git] / src / SalomeApp / pluginsdemo / minmax_plugin.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2016  CEA/DEN, EDF R&D, 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, studyId, salomeGui
26   study = context.study
27   studyId = context.studyId
28   sg = context.sg
29
30   from minmax_ui import Ui_Dialog
31
32   import salome
33   import SMESH
34   from salome.smesh import smeshBuilder
35   smesh = smeshBuilder.New(salome.myStudy)
36
37   controls_dict = {
38     "Aspect Ratio 3D" :     SMESH.FT_AspectRatio3D,
39     "Volume" :              SMESH.FT_Volume3D,
40     "Element Diameter 3D" : SMESH.FT_MaxElementLength3D,
41     "Length 2D" :           SMESH.FT_Length2D,
42     "MultiConnection 2D" :  SMESH.FT_MultiConnection2D,
43     "Area" :                SMESH.FT_Area,
44     "Taper" :               SMESH.FT_Taper,
45     "Aspect Ratio" :        SMESH.FT_AspectRatio,
46     "Minimum Angle" :       SMESH.FT_MinimumAngle,
47     "Warping" :             SMESH.FT_Warping,
48     "Skew" :                SMESH.FT_Skew,
49     "Element Diameter 2D" : SMESH.FT_MaxElementLength2D,
50     "Length" :              SMESH.FT_Length,
51     "MultiConnection" :     SMESH.FT_MultiConnection,
52     }
53
54   controls_3d = [
55     "Aspect Ratio 3D",
56     "Volume",
57     "Element Diameter 3D",
58     ]
59   controls_2d = [
60     "Length 2D",
61     "MultiConnection 2D",
62     "Area",
63     "Taper",
64     "Aspect Ratio",
65     "Minimum Angle",
66     "Warping",
67     "Skew",
68     "Element Diameter 2D"
69     ]
70   controls_1d = [
71     "Length",
72     "MultiConnection",
73     ]
74
75   class MinmaxDialog(QDialog):
76     def __init__(self):
77       QDialog.__init__(self, None, Qt.Tool)
78       # Set up the user interface from Designer.
79       self.ui = Ui_Dialog()
80       self.ui.setupUi(self)
81       self.show()
82
83       self.clearLineEdit()
84
85       # Connect up the selectionChanged() event of the object browser.
86       sg.getObjectBrowser().selectionChanged.connect(self.select)
87
88       self.mm = None
89       self.ui.control.setFocus()
90       self.select()
91
92       pass
93
94     def OnCancel(self):
95       sg.getObjectBrowser().selectionChanged.disconnect(self.select)
96       self.reject()
97       pass
98
99     def clearLineEdit(self):
100       self.ui.mesh.setText("Select a Mesh")
101       self.ui.mesh.setStyleSheet("QLineEdit { color: grey }")
102       self.ui.minvalue.setText("")
103       self.ui.maxvalue.setText("")
104
105     def select(self):
106       sg.getObjectBrowser().selectionChanged.disconnect(self.select)
107       self.ui.minvalue.setText("")
108       self.ui.maxvalue.setText("")
109       objId = salome.sg.getSelected(0)
110       if objId:
111         mm = study.FindObjectID(objId).GetObject()
112         mesh = None
113         try:
114           mm.Load()
115           mesh = mm
116         except:
117           self.clearLineEdit()
118           mesh = None
119           pass
120         if mesh:
121           name = smeshBuilder.GetName( mm )
122           self.ui.mesh.setStyleSheet("")
123           self.ui.mesh.setText( name )
124           self.mm = mm
125           e = self.mm.NbEdges()
126           f = self.mm.NbFaces()
127           v = self.mm.NbVolumes()
128           controls = []
129           if e:
130             controls += controls_1d
131             pass
132           if f:
133             controls += controls_2d
134             pass
135           if v:
136             controls += controls_3d
137             pass
138           if self.ui.control.count() != len( controls ):
139             self.ui.control.clear()
140             self.ui.control.addItems(controls)
141           self.compute_minmax()
142       sg.getObjectBrowser().selectionChanged.connect(self.select)
143       pass
144
145     def helpMessage(self):
146       QMessageBox.about(None, "About Min/Max value of control",
147       """
148       Displays the min/max value of a control
149       ---------------------------------
150
151 This plugin displays the min and max value of a control
152 on a mesh.
153 Inputs:
154   - The mesh to analyse
155   - The control to compute
156       """)
157       pass
158
159     def compute_minmax(self):
160       control = self.ui.control.currentText()
161       if self.mm and control:
162         fun = smesh.GetFunctor(controls_dict[str(control)])
163         fun.SetMesh(self.mm)
164         hist = fun.GetHistogram(1,False)
165         maxVal = hist[0].max
166         minVal = hist[0].min
167         self.ui.maxvalue.setText("%f"%(maxVal))
168         self.ui.minvalue.setText("%f"%(minVal))
169       else:
170         pass
171       pass
172     pass
173
174   window = MinmaxDialog()
175   window.exec_()
176   pass
177