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