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