Salome HOME
Update copyright
[modules/smesh.git] / src / Tools / MeshCut / meshcut_plugin.py
1 # Copyright (C) 2006-2011  EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # if you already have plugins defined in a salome_plugins.py file, add this file at the end.
21 # if not, copy this file as ${HOME}/Plugins/smesh_plugins.py or ${APPLI}/Plugins/smesh_plugins.py
22
23 import salome_pluginsmanager
24
25 def MeshCut(context):
26   # get context study, studyId, salomeGui
27   study = context.study
28   studyId = context.studyId
29   sg = context.sg
30   
31   import os
32   import subprocess
33   import tempfile
34   from PyQt4 import QtGui
35   from PyQt4.QtGui import QFileDialog
36   from PyQt4.QtGui import QMessageBox
37   from MeshCutDialog import Ui_Dialog
38   
39   class CutDialog(QtGui.QDialog):
40     
41     def __init__(self):
42       QtGui.QDialog.__init__(self)
43       # Set up the user interface from Designer.
44       self.ui = Ui_Dialog()
45       self.ui.setupUi(self)
46       # Connect up the buttons.
47       self.connect(self.ui.pb_origMeshFile, QtCore.SIGNAL("clicked()"),
48                    self.setInputFile)
49       self.connect(self.ui.pb_cutMeshFile, QtCore.SIGNAL("clicked()"),
50                    self.setOutputFile)
51       self.connect(self.ui.pb_help, QtCore.SIGNAL("clicked()"),
52                    self.helpMessage)
53       pass
54     
55     def setInputFile(self):
56       fd = QFileDialog(self, "select an existing Med file", self.ui.le_origMeshFile.text(), "MED-Files (*.med);;All Files (*)")
57       if fd.exec_():
58         infile = fd.selectedFiles()[0]
59         self.ui.le_origMeshFile.setText(infile)
60         insplit = os.path.splitext(infile.toLocal8Bit().data())
61         outfile = insplit[0] + '_cut' + insplit[1]
62         self.ui.le_cutMeshFile.setText(outfile)
63       pass
64     
65     def setOutputFile(self):
66       fd = QFileDialog(self, "select an output Med file", self.ui.le_cutMeshFile.text(), "MED-Files (*.med);;All Files (*)")
67       if fd.exec_():
68         self.ui.le_cutMeshFile.setText(fd.selectedFiles()[0])
69       pass
70     
71     def helpMessage(self):
72       QMessageBox.about(None, "About MeshCut",
73       """
74       Cut a tetrahedron mesh by a plane
75       ---------------------------------
76                  
77 MeshCut allows to cut a mesh constituted of linear
78 tetrahedrons by a plane. The tetrahedrons intersected
79 by the plane are cut and replaced by elements of
80 various types (tetrahedron, pyramid, pentahedron).
81
82 MeshCut is a standalone program, reading and
83 producing med files. The cutting plane is defined
84 by a vector normal to the plane and a vertex
85 belonging to the plane.
86
87 Vertices of a tetrahedron are considered as belonging to
88 the cut plane if their distance to the plane is inferior
89 to L*T where L is the mean edge size of the tetrahedron
90 and T the tolerance.
91       """)
92       pass
93     pass
94   
95   
96                      
97   window = CutDialog()
98   window.ui.dsb_tolerance.setValue(0.01)
99   retry = True
100   while(retry):
101     retry = False
102     window.exec_()
103     result = window.result()
104     if result:
105       # dialog accepted
106       args = ['MeshCut']
107       args += [window.ui.le_origMeshFile.text().toLocal8Bit().data()]
108       args += [window.ui.le_cutMeshFile.text().toLocal8Bit().data()]
109       args += [window.ui.le_outMeshName.text().toLocal8Bit().data()]
110       args += [window.ui.le_groupAbove.text().toLocal8Bit().data()]
111       args += [window.ui.le_groupBelow.text().toLocal8Bit().data()]
112       args += [str(window.ui.dsb_normX.value())]
113       args += [str(window.ui.dsb_normY.value())]
114       args += [str(window.ui.dsb_normZ.value())]
115       args += [str(window.ui.dsb_vertX.value())]
116       args += [str(window.ui.dsb_vertY.value())]
117       args += [str(window.ui.dsb_vertZ.value())]
118       args += [str(window.ui.dsb_tolerance.value())]
119       f= tempfile.NamedTemporaryFile(delete=False)
120       fname = f.name
121       p = subprocess.Popen(args, stdout=f, stderr=f)
122       err = p.wait()
123       f.close()
124       if err==0:
125         os.remove(fname)
126       else:
127         f = open(fname, 'r')
128         m = f.read()
129         msgBox = QMessageBox()
130         msgBox.setText("Parameters are not OK")
131         msgBox.setInformativeText("Do you want to retry ?")
132         msgBox.setDetailedText(m)
133         msgBox.setStandardButtons(QMessageBox.Retry | QMessageBox.Cancel)
134         msgBox.setDefaultButton(QMessageBox.Retry)
135         ret = msgBox.exec_()
136         if ret == QMessageBox.Retry:
137           retry = True
138         pass
139       pass
140     pass
141   pass
142
143 # register the function in the plugin manager
144 salome_pluginsmanager.AddFunction('MeshCut', 'Cut a tetrahedron mesh by a plane', MeshCut)
145