Salome HOME
getting access to Python plugin helpers in other modules
[modules/gui.git] / src / SalomeApp / pluginsdemo / minmax_plugin.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2015  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 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           mm.Load()
119           mesh = mm
120         except:
121           self.clearLineEdit()
122           mesh = None
123           pass
124         if mesh:
125           name = smeshBuilder.GetName( mm )
126           self.ui.mesh.setStyleSheet("")
127           self.ui.mesh.setText( name )
128           self.mm = mm
129           e = self.mm.NbEdges()
130           f = self.mm.NbFaces()
131           v = self.mm.NbVolumes()
132           controls = []
133           if e:
134             controls += controls_1d
135             pass
136           if f:
137             controls += controls_2d
138             pass
139           if v:
140             controls += controls_3d
141             pass
142           self.ui.control.addItems(controls)
143           self.compute_minmax()
144       self.connect(sg.getObjectBrowser(), SIGNAL("selectionChanged()"), self.select)
145       pass
146
147     def helpMessage(self):
148       QMessageBox.about(None, "About Min/Max value of control",
149       """
150       Displays the min/max value of a control
151       ---------------------------------
152
153 This plugin displays the min and max value of a control
154 on a mesh.
155 Inputs:
156   - The mesh to analyse
157   - The control to compute
158       """)
159       pass
160
161     def compute_minmax(self):
162       control = self.ui.control.currentText()
163       if self.mm and control:
164         fun = smesh.GetFunctor(controls_dict[str(control)])
165         fun.SetMesh(self.mm)
166         hist = fun.GetHistogram(1,False)
167         maxVal = hist[0].max
168         minVal = hist[0].min
169         self.ui.maxvalue.setText("%f"%(maxVal))
170         self.ui.minvalue.setText("%f"%(minVal))
171       else:
172         pass
173       pass
174     pass
175
176   window = MinmaxDialog()
177   window.exec_()
178   pass
179