Salome HOME
Merge branch 'V7_dev'
[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.control.clear()
108       self.ui.minvalue.setText("")
109       self.ui.maxvalue.setText("")
110       objId = salome.sg.getSelected(0)
111       if objId:
112         mm = study.FindObjectID(objId).GetObject()
113         mesh = None
114         try:
115           mm.Load()
116           mesh = mm
117         except:
118           self.clearLineEdit()
119           mesh = None
120           pass
121         if mesh:
122           name = smeshBuilder.GetName( mm )
123           self.ui.mesh.setStyleSheet("")
124           self.ui.mesh.setText( name )
125           self.mm = mm
126           e = self.mm.NbEdges()
127           f = self.mm.NbFaces()
128           v = self.mm.NbVolumes()
129           controls = []
130           if e:
131             controls += controls_1d
132             pass
133           if f:
134             controls += controls_2d
135             pass
136           if v:
137             controls += controls_3d
138             pass
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 value of control",
146       """
147       Displays the min/max value of a control
148       ---------------------------------
149
150 This plugin displays the min and max 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         hist = fun.GetHistogram(1,False)
164         maxVal = hist[0].max
165         minVal = hist[0].min
166         self.ui.maxvalue.setText("%f"%(maxVal))
167         self.ui.minvalue.setText("%f"%(minVal))
168       else:
169         pass
170       pass
171     pass
172
173   window = MinmaxDialog()
174   window.exec_()
175   pass
176