Back to index

salome-smesh  6.5.0
meshcut_plugin.py
Go to the documentation of this file.
00001 # Copyright (C) 2006-2012  EDF R&D
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 
00020 # if you already have plugins defined in a salome_plugins.py file, add this file at the end.
00021 # if not, copy this file as ${HOME}/Plugins/smesh_plugins.py or ${APPLI}/Plugins/smesh_plugins.py
00022 
00023 def MeshCut(context):
00024   # get context study, studyId, salomeGui
00025   study = context.study
00026   studyId = context.studyId
00027   sg = context.sg
00028   
00029   import os
00030   import subprocess
00031   import tempfile
00032   from PyQt4 import QtCore
00033   from PyQt4 import QtGui
00034   from PyQt4.QtGui import QFileDialog
00035   from PyQt4.QtGui import QMessageBox
00036   from MeshCutDialog import Ui_Dialog
00037   
00038   class CutDialog(QtGui.QDialog):
00039     
00040     def __init__(self):
00041       QtGui.QDialog.__init__(self)
00042       # Set up the user interface from Designer.
00043       self.ui = Ui_Dialog()
00044       self.ui.setupUi(self)
00045       # Connect up the buttons.
00046       self.connect(self.ui.pb_origMeshFile, QtCore.SIGNAL("clicked()"),
00047                    self.setInputFile)
00048       self.connect(self.ui.pb_cutMeshFile, QtCore.SIGNAL("clicked()"),
00049                    self.setOutputFile)
00050       self.connect(self.ui.pb_help, QtCore.SIGNAL("clicked()"),
00051                    self.helpMessage)
00052       pass
00053     
00054     def setInputFile(self):
00055       fd = QFileDialog(self, "select an existing Med file", self.ui.le_origMeshFile.text(), "MED-Files (*.med);;All Files (*)")
00056       if fd.exec_():
00057         infile = fd.selectedFiles()[0]
00058         self.ui.le_origMeshFile.setText(infile)
00059         insplit = os.path.splitext(infile.toLocal8Bit().data())
00060         outfile = insplit[0] + '_cut' + insplit[1]
00061         self.ui.le_cutMeshFile.setText(outfile)
00062       pass
00063     
00064     def setOutputFile(self):
00065       fd = QFileDialog(self, "select an output Med file", self.ui.le_cutMeshFile.text(), "MED-Files (*.med);;All Files (*)")
00066       if fd.exec_():
00067         self.ui.le_cutMeshFile.setText(fd.selectedFiles()[0])
00068       pass
00069     
00070     def helpMessage(self):
00071       QMessageBox.about(None, "About MeshCut",
00072       """
00073       Cut a tetrahedron mesh by a plane
00074       ---------------------------------
00075                  
00076 MeshCut allows to cut a mesh constituted of linear
00077 tetrahedrons by a plane. The tetrahedrons intersected
00078 by the plane are cut and replaced by elements of
00079 various types (tetrahedron, pyramid, pentahedron).
00080 
00081 MeshCut is a standalone program, reading and
00082 producing med files. The cutting plane is defined
00083 by a vector normal to the plane and a vertex
00084 belonging to the plane.
00085 
00086 Vertices of a tetrahedron are considered as belonging to
00087 the cut plane if their distance to the plane is inferior
00088 to L*T where L is the mean edge size of the tetrahedron
00089 and T the tolerance.
00090       """)
00091       pass
00092     pass
00093   
00094   
00095                      
00096   window = CutDialog()
00097   window.ui.dsb_tolerance.setValue(0.01)
00098   retry = True
00099   while(retry):
00100     retry = False
00101     window.exec_()
00102     result = window.result()
00103     if result:
00104       # dialog accepted
00105       args = ['MeshCut']
00106       args += [window.ui.le_origMeshFile.text().toLocal8Bit().data()]
00107       args += [window.ui.le_cutMeshFile.text().toLocal8Bit().data()]
00108       args += [window.ui.le_outMeshName.text().toLocal8Bit().data()]
00109       args += [window.ui.le_groupAbove.text().toLocal8Bit().data()]
00110       args += [window.ui.le_groupBelow.text().toLocal8Bit().data()]
00111       args += [str(window.ui.dsb_normX.value())]
00112       args += [str(window.ui.dsb_normY.value())]
00113       args += [str(window.ui.dsb_normZ.value())]
00114       args += [str(window.ui.dsb_vertX.value())]
00115       args += [str(window.ui.dsb_vertY.value())]
00116       args += [str(window.ui.dsb_vertZ.value())]
00117       args += [str(window.ui.dsb_tolerance.value())]
00118       f= tempfile.NamedTemporaryFile(delete=False)
00119       fname = f.name
00120       p = subprocess.Popen(args, stdout=f, stderr=f)
00121       err = p.wait()
00122       f.close()
00123       if err==0:
00124         os.remove(fname)
00125       else:
00126         f = open(fname, 'r')
00127         m = f.read()
00128         msgBox = QMessageBox()
00129         msgBox.setText("Parameters are not OK")
00130         msgBox.setInformativeText("Do you want to retry ?")
00131         msgBox.setDetailedText(m)
00132         msgBox.setStandardButtons(QMessageBox.Retry | QMessageBox.Cancel)
00133         msgBox.setDefaultButton(QMessageBox.Retry)
00134         ret = msgBox.exec_()
00135         if ret == QMessageBox.Retry:
00136           retry = True
00137         pass
00138       pass
00139     pass
00140   pass