Salome HOME
Merge Python 3 porting.
[modules/smesh.git] / src / Tools / MeshCut / meshcut_plugin.py
1 # Copyright (C) 2006-2016  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, or (at your option) any later version.
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 def MeshCut(context):
24   # get context study, salomeGui
25   study = context.study
26   sg = context.sg
27   
28   import os
29   import subprocess
30   import tempfile
31   from qtsalome import QFileDialog, QMessageBox, QDialog
32   from MeshCutDialog_ui import Ui_Dialog
33   
34   class CutDialog(QDialog):
35     
36     def __init__(self):
37       QDialog.__init__(self)
38       # Set up the user interface from Designer.
39       self.ui = Ui_Dialog()
40       self.ui.setupUi(self)
41       # Connect up the buttons.
42       self.ui.pb_origMeshFile.clicked.connect(self.setInputFile)
43       self.ui.pb_cutMeshFile.clicked.connect(self.setOutputFile)
44       self.ui.pb_help.clicked.connect(self.helpMessage)
45       pass
46     
47     def setInputFile(self):
48       fd = QFileDialog(self, "select an existing Med file", self.ui.le_origMeshFile.text(), "MED-Files (*.med);;All Files (*)")
49       if fd.exec_():
50         infile = fd.selectedFiles()[0]
51         self.ui.le_origMeshFile.setText(infile)
52         insplit = os.path.splitext(str(infile).encode())
53         outfile = insplit[0] + '_cut' + insplit[1]
54         self.ui.le_cutMeshFile.setText(outfile)
55       pass
56     
57     def setOutputFile(self):
58       fd = QFileDialog(self, "select an output Med file", self.ui.le_cutMeshFile.text(), "MED-Files (*.med);;All Files (*)")
59       if fd.exec_():
60         self.ui.le_cutMeshFile.setText(fd.selectedFiles()[0])
61       pass
62     
63     def helpMessage(self):
64       QMessageBox.about(None, "About MeshCut",
65       """
66       Cut a tetrahedron mesh by a plane
67       ---------------------------------
68                  
69 MeshCut allows to cut a mesh constituted of linear
70 tetrahedrons by a plane. The tetrahedrons intersected
71 by the plane are cut and replaced by elements of
72 various types (tetrahedron, pyramid, pentahedron).
73
74 MeshCut is a standalone program, reading and
75 producing med files. The cutting plane is defined
76 by a vector normal to the plane and a vertex
77 belonging to the plane.
78
79 Vertices of a tetrahedron are considered as belonging to
80 the cut plane if their distance to the plane is inferior
81 to L*T where L is the mean edge size of the tetrahedron
82 and T the tolerance.
83       """)
84       pass
85     pass
86   
87   
88                      
89   window = CutDialog()
90   window.ui.dsb_tolerance.setValue(0.01)
91   retry = True
92   while(retry):
93     retry = False
94     window.exec_()
95     result = window.result()
96     if result:
97       # dialog accepted
98       args = ['MeshCut']
99       args += [str(window.ui.le_origMeshFile.text()).encode()]
100       args += [str(window.ui.le_cutMeshFile.text()).encode()]
101       args += [str(window.ui.le_outMeshName.text()).encode()]
102       args += [str(window.ui.le_groupAbove.text()).encode()]
103       args += [str(window.ui.le_groupBelow.text()).encode()]
104       args += [str(window.ui.dsb_normX.value())]
105       args += [str(window.ui.dsb_normY.value())]
106       args += [str(window.ui.dsb_normZ.value())]
107       args += [str(window.ui.dsb_vertX.value())]
108       args += [str(window.ui.dsb_vertY.value())]
109       args += [str(window.ui.dsb_vertZ.value())]
110       args += [str(window.ui.dsb_tolerance.value())]
111       f= tempfile.NamedTemporaryFile(delete=False)
112       fname = f.name
113       p = subprocess.Popen(args, stdout=f, stderr=f)
114       err = p.wait()
115       f.close()
116       if err==0:
117         os.remove(fname)
118       else:
119         f = open(fname, 'r')
120         m = f.read()
121         msgBox = QMessageBox()
122         msgBox.setText("Parameters are not OK")
123         msgBox.setInformativeText("Do you want to retry ?")
124         msgBox.setDetailedText(m)
125         msgBox.setStandardButtons(QMessageBox.Retry | QMessageBox.Cancel)
126         msgBox.setDefaultButton(QMessageBox.Retry)
127         ret = msgBox.exec_()
128         if ret == QMessageBox.Retry:
129           retry = True
130         pass
131       pass
132     pass
133   pass