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