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