Salome HOME
PR: plugin for MeshCut
[modules/smesh.git] / src / Tools / MeshCut / meshcut_plugin.py
1 # if you already have plugins defined in a salome_plugins.py file, add this file at the end.
2 # if not, copy this file as ${HOME}/Plugins/salome_plugins.py or ${APPLI}/Plugins/salome_plugins.py
3
4 import salome_pluginsmanager
5
6 def MeshCut(context):
7   # get context study, studyId, salomeGui
8   study = context.study
9   studyId = context.studyId
10   sg = context.sg
11   
12   import os
13   import subprocess
14   import tempfile
15   from PyQt4 import QtGui
16   from PyQt4.QtGui import QFileDialog
17   from PyQt4.QtGui import QMessageBox
18   from MeshCutDialog import Ui_Dialog
19   
20   class CutDialog(QtGui.QDialog):
21     
22     def __init__(self):
23       QtGui.QDialog.__init__(self)
24       # Set up the user interface from Designer.
25       self.ui = Ui_Dialog()
26       self.ui.setupUi(self)
27       # Connect up the buttons.
28       self.connect(self.ui.pb_origMeshFile, QtCore.SIGNAL("clicked()"),
29                    self.setInputFile)
30       self.connect(self.ui.pb_cutMeshFile, QtCore.SIGNAL("clicked()"),
31                    self.setOutputFile)
32       self.connect(self.ui.pb_help, QtCore.SIGNAL("clicked()"),
33                    self.helpMessage)
34       pass
35     
36     def setInputFile(self):
37       fd = QFileDialog(self, "select an existing Med file", self.ui.le_origMeshFile.text(), "MED-Files (*.med);;All Files (*)")
38       if fd.exec_():
39         infile = fd.selectedFiles()[0]
40         self.ui.le_origMeshFile.setText(infile)
41         insplit = os.path.splitext(infile.toLocal8Bit().data())
42         outfile = insplit[0] + '_cut' + insplit[1]
43         self.ui.le_cutMeshFile.setText(outfile)
44       pass
45     
46     def setOutputFile(self):
47       fd = QFileDialog(self, "select an output Med file", self.ui.le_cutMeshFile.text(), "MED-Files (*.med);;All Files (*)")
48       if fd.exec_():
49         self.ui.le_cutMeshFile.setText(fd.selectedFiles()[0])
50       pass
51     
52     def helpMessage(self):
53       QMessageBox.about(None, "About MeshCut",
54       """
55       Cut a tetrahedron mesh by a plane
56       ---------------------------------
57                  
58 MeshCut allows to cut a mesh constituted of linear
59 tetrahedrons by a plane. The tetrahedrons intersected
60 by the plane are cut and replaced by elements of
61 various types (tetrahedron, pyramid, pentahedron).
62
63 MeshCut is a standalone program, reading and
64 producing med files. The cutting plane is defined
65 by a vector normal to the plane and a vertex
66 belonging to the plane.
67
68 Vertices of a tetrahedron are considered as belonging to
69 the cut plane if their distance to the plane is inferior
70 to L*T where L is the mean edge size of the tetrahedron
71 and T the tolerance.
72       """)
73       pass
74     pass
75   
76   
77                      
78   window = CutDialog()
79   window.ui.dsb_tolerance.setValue(0.01)
80   retry = True
81   while(retry):
82     retry = False
83     window.exec_()
84     result = window.result()
85     if result:
86       # dialog accepted
87       args = ['MeshCut']
88       args += [window.ui.le_origMeshFile.text().toLocal8Bit().data()]
89       args += [window.ui.le_cutMeshFile.text().toLocal8Bit().data()]
90       args += [window.ui.le_outMeshName.text().toLocal8Bit().data()]
91       args += [window.ui.le_groupAbove.text().toLocal8Bit().data()]
92       args += [window.ui.le_groupBelow.text().toLocal8Bit().data()]
93       args += [str(window.ui.dsb_normX.value())]
94       args += [str(window.ui.dsb_normY.value())]
95       args += [str(window.ui.dsb_normZ.value())]
96       args += [str(window.ui.dsb_vertX.value())]
97       args += [str(window.ui.dsb_vertY.value())]
98       args += [str(window.ui.dsb_vertZ.value())]
99       args += [str(window.ui.dsb_tolerance.value())]
100       f= tempfile.NamedTemporaryFile(delete=False)
101       fname = f.name
102       p = subprocess.Popen(args, stdout=f, stderr=f)
103       err = p.wait()
104       f.close()
105       if err==0:
106         os.remove(fname)
107       else:
108         f = open(fname, 'r')
109         m = f.read()
110         msgBox = QMessageBox()
111         msgBox.setText("Parameters are not OK")
112         msgBox.setInformativeText("Do you want to retry ?")
113         msgBox.setDetailedText(m)
114         msgBox.setStandardButtons(QMessageBox.Retry | QMessageBox.Cancel)
115         msgBox.setDefaultButton(QMessageBox.Retry)
116         ret = msgBox.exec_()
117         if ret == QMessageBox.Retry:
118           retry = True
119         pass
120       pass
121     pass
122   pass
123
124 # register the function in the plugin manager
125 salome_pluginsmanager.AddFunction('MeshCut', 'Cut a tetrahedron mesh by a plane', MeshCut)
126