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