Version: 6.3.1

src/Tools/MeshCut/meshcut_plugin.py

Go to the documentation of this file.
00001 # Copyright (C) 2006-2011  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 import salome_pluginsmanager
00024 
00025 def MeshCut(context):
00026   # get context study, studyId, salomeGui
00027   study = context.study
00028   studyId = context.studyId
00029   sg = context.sg
00030   
00031   import os
00032   import subprocess
00033   import tempfile
00034   from PyQt4 import QtGui
00035   from PyQt4.QtGui import QFileDialog
00036   from PyQt4.QtGui import QMessageBox
00037   from MeshCutDialog import Ui_Dialog
00038   
00039   class CutDialog(QtGui.QDialog):
00040     
00041     def __init__(self):
00042       QtGui.QDialog.__init__(self)
00043       # Set up the user interface from Designer.
00044       self.ui = Ui_Dialog()
00045       self.ui.setupUi(self)
00046       # Connect up the buttons.
00047       self.connect(self.ui.pb_origMeshFile, QtCore.SIGNAL("clicked()"),
00048                    self.setInputFile)
00049       self.connect(self.ui.pb_cutMeshFile, QtCore.SIGNAL("clicked()"),
00050                    self.setOutputFile)
00051       self.connect(self.ui.pb_help, QtCore.SIGNAL("clicked()"),
00052                    self.helpMessage)
00053       pass
00054     
00055     def setInputFile(self):
00056       fd = QFileDialog(self, "select an existing Med file", self.ui.le_origMeshFile.text(), "MED-Files (*.med);;All Files (*)")
00057       if fd.exec_():
00058         infile = fd.selectedFiles()[0]
00059         self.ui.le_origMeshFile.setText(infile)
00060         insplit = os.path.splitext(infile.toLocal8Bit().data())
00061         outfile = insplit[0] + '_cut' + insplit[1]
00062         self.ui.le_cutMeshFile.setText(outfile)
00063       pass
00064     
00065     def setOutputFile(self):
00066       fd = QFileDialog(self, "select an output Med file", self.ui.le_cutMeshFile.text(), "MED-Files (*.med);;All Files (*)")
00067       if fd.exec_():
00068         self.ui.le_cutMeshFile.setText(fd.selectedFiles()[0])
00069       pass
00070     
00071     def helpMessage(self):
00072       QMessageBox.about(None, "About MeshCut",
00073       """
00074       Cut a tetrahedron mesh by a plane
00075       ---------------------------------
00076                  
00077 MeshCut allows to cut a mesh constituted of linear
00078 tetrahedrons by a plane. The tetrahedrons intersected
00079 by the plane are cut and replaced by elements of
00080 various types (tetrahedron, pyramid, pentahedron).
00081 
00082 MeshCut is a standalone program, reading and
00083 producing med files. The cutting plane is defined
00084 by a vector normal to the plane and a vertex
00085 belonging to the plane.
00086 
00087 Vertices of a tetrahedron are considered as belonging to
00088 the cut plane if their distance to the plane is inferior
00089 to L*T where L is the mean edge size of the tetrahedron
00090 and T the tolerance.
00091       """)
00092       pass
00093     pass
00094   
00095   
00096                      
00097   window = CutDialog()
00098   window.ui.dsb_tolerance.setValue(0.01)
00099   retry = True
00100   while(retry):
00101     retry = False
00102     window.exec_()
00103     result = window.result()
00104     if result:
00105       # dialog accepted
00106       args = ['MeshCut']
00107       args += [window.ui.le_origMeshFile.text().toLocal8Bit().data()]
00108       args += [window.ui.le_cutMeshFile.text().toLocal8Bit().data()]
00109       args += [window.ui.le_outMeshName.text().toLocal8Bit().data()]
00110       args += [window.ui.le_groupAbove.text().toLocal8Bit().data()]
00111       args += [window.ui.le_groupBelow.text().toLocal8Bit().data()]
00112       args += [str(window.ui.dsb_normX.value())]
00113       args += [str(window.ui.dsb_normY.value())]
00114       args += [str(window.ui.dsb_normZ.value())]
00115       args += [str(window.ui.dsb_vertX.value())]
00116       args += [str(window.ui.dsb_vertY.value())]
00117       args += [str(window.ui.dsb_vertZ.value())]
00118       args += [str(window.ui.dsb_tolerance.value())]
00119       f= tempfile.NamedTemporaryFile(delete=False)
00120       fname = f.name
00121       p = subprocess.Popen(args, stdout=f, stderr=f)
00122       err = p.wait()
00123       f.close()
00124       if err==0:
00125         os.remove(fname)
00126       else:
00127         f = open(fname, 'r')
00128         m = f.read()
00129         msgBox = QMessageBox()
00130         msgBox.setText("Parameters are not OK")
00131         msgBox.setInformativeText("Do you want to retry ?")
00132         msgBox.setDetailedText(m)
00133         msgBox.setStandardButtons(QMessageBox.Retry | QMessageBox.Cancel)
00134         msgBox.setDefaultButton(QMessageBox.Retry)
00135         ret = msgBox.exec_()
00136         if ret == QMessageBox.Retry:
00137           retry = True
00138         pass
00139       pass
00140     pass
00141   pass
00142 
00143 # register the function in the plugin manager
00144 salome_pluginsmanager.AddFunction('MeshCut', 'Cut a tetrahedron mesh by a plane', MeshCut)
00145 
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS