Salome HOME
Better layout with documentation update
[modules/smesh.git] / src / Tools / TopIIVolMeshPlug / TopIIVolMeshPluginDialog.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2022 CEA/DES, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21
22 import os, subprocess
23 import random
24 import getpass
25 import time
26 import pathlib
27
28 # set seed
29 from datetime import datetime
30 random.seed(datetime.now())
31
32 import platform
33 import tempfile
34 from TopIIVolMeshPluginDialog_ui import Ui_TopIIVolMeshMainFrame
35 from TopIIVolMeshMonitor import TopIIVolMeshMonitor
36 from qtsalome import *
37
38 verbose = True
39
40 class TopIIVolMeshPluginDialog(Ui_TopIIVolMeshMainFrame,QWidget):
41   """
42   """
43   def __init__(self):
44     QWidget.__init__(self)
45     self.setupUi(self)
46     self.qpbHelp.clicked.connect(self.OnQpbHelpClicked)
47     self.qpbCompute.clicked.connect(self.OnQpbComputeClicked)
48     self.qpbMeshFile.clicked.connect(self.OnQpbMeshFileClicked)
49     self.qpbMeshFile.setToolTip("Select input DEM file")
50     self.qpbClose.clicked.connect(self.OnQpbCloseClicked)
51     self.qrbDist.clicked.connect(self.OnqrbDistClicked)
52     self.qrbPar.clicked.connect(self.OnqrbParClicked)
53     self.qrbSeq.clicked.connect(self.OnqrbSeqClicked)
54     self.SALOME_TMP_DIR = None
55     try:
56       self.qleTmpDir.setText(os.path.join('/tmp',getpass.getuser(),'top-ii-vol'))
57     except:
58       self.qleTmpDir.setText('/tmp')
59     self.outputMesh = ''
60
61   def OnQpbHelpClicked(self):
62     import SalomePyQt
63     sgPyQt = SalomePyQt.SalomePyQt()
64     try:
65       mydir=os.environ["SMESH_ROOT_DIR"]
66     except Exception:
67       QMessageBox.warning(self, "Help", "Help unavailable $SMESH_ROOT_DIR not found")
68       return
69
70     myDoc=mydir + "/share/doc/salome/gui/SMESH/TopIIVolMesh/index.html"
71     sgPyQt.helpContext(myDoc,"")
72
73   def OnQpbMeshFileClicked(self):
74     fd = QFileDialog(self, "select an existing Mesh file", self.qleMeshFile.text(), "Mesh-Files (*.xyz);;All Files (*)")
75     if fd.exec_():
76       infile = fd.selectedFiles()[0]
77       self.qleMeshFile.setText(infile)
78
79   def OnQpbComputeClicked(self):
80     if self.qleMeshFile.text() == '':
81       QMessageBox.critical(self, "Mesh", "select an input mesh")
82       return
83     inputMesh = self.qleMeshFile.text()
84     # retrieve x,y,z and depth parameters
85     xPoints = self.qsbXPoints.value()
86     yPoints = self.qsbYPoints.value()
87     zPoints = self.qsbZPoints.value()
88     depth   = self.qsbDepth.value()
89     nProcs  = self.qsbNBprocs.value()
90     if not self.qrbDist.isChecked():
91       if nProcs == 1:
92         shellCmd = "topIIvol_Mesher"
93       else:
94         shellCmd = "mpirun -np {} topIIvol_ParMesher".format(nProcs)
95       shellCmd+= " --xpoints " + str(xPoints)
96       shellCmd+= " --ypoints " + str(yPoints)
97       shellCmd+= " --zpoints " + str(zPoints)
98       shellCmd+= " --depth   " + str(depth)
99       shellCmd+= " --in " + inputMesh
100     else:
101       xParts = self.qsbXParts.value()
102       yParts = self.qsbYParts.value()
103       zParts = self.qsbZParts.value()
104       shellCmd = "mpirun -np {} topIIvol_DistMesher".format(nProcs)
105       shellCmd+= " --xpoints " + str(xPoints)
106       shellCmd+= " --ypoints " + str(yPoints)
107       shellCmd+= " --zpoints " + str(zPoints)
108       shellCmd+= " --depth   " + str(depth)
109       shellCmd+= " --partition_x " + str(xParts)
110       shellCmd+= " --partition_y " + str(yParts)
111       shellCmd+= " --partition_z " + str(zParts)
112       shellCmd+= " --in " + inputMesh
113     if platform.system()=="Windows" :
114       self.SALOME_TMP_DIR = os.getenv("SALOME_TMP_DIR")
115     else:
116       self.SALOME_TMP_DIR = os.path.join(self.qleTmpDir.text(), time.strftime("%Y-%m-%d-%H-%M-%S"))
117       pathlib.Path(self.SALOME_TMP_DIR).mkdir(parents=True, exist_ok=True)
118     self.outputMesh= os.path.join(self.SALOME_TMP_DIR, inputMesh.split('/').pop().replace('.xyz','.mesh'))
119     shellCmd+= " --out " + self.outputMesh
120     os.chdir(self.SALOME_TMP_DIR)
121     print("INFO: ", shellCmd)
122     myMonitorView=TopIIVolMeshMonitor(self, shellCmd)
123
124   def OnqrbDistClicked(self):
125     state = self.qrbDist.isChecked()
126     self.qgbDist.setEnabled(state)
127     self.qsbNBprocs.setEnabled(state)
128     self.qlbNBprocs.setEnabled(state)
129
130   def OnqrbParClicked(self):
131     state = self.qrbPar.isChecked()
132     self.qgbDist.setEnabled(not state)
133     self.qsbNBprocs.setEnabled(state)
134     self.qlbNBprocs.setEnabled(state)
135
136   def OnqrbSeqClicked(self):
137     state = self.qrbSeq.isChecked()
138     if state:
139       self.qsbNBprocs.setValue(1)
140     self.qgbDist.setEnabled(not state)
141     self.qsbNBprocs.setEnabled(not state)
142     self.qlbNBprocs.setEnabled(not state)
143     
144   def OnQpbCloseClicked(self):
145     self.close()
146
147   def saveOutputMesh(self):
148     if not self.qcbDisplayMesh.isChecked():
149       return True
150     import salome
151     import  SMESH, SALOMEDS
152     from salome.smesh import smeshBuilder
153     smesh = smeshBuilder.New()
154     self.outputMesh.split('/')
155     for mesh in pathlib.Path(self.SALOME_TMP_DIR).glob('*.mesh'):
156       (outputMesh, status) = smesh.CreateMeshesFromGMF(os.path.join(self.SALOME_TMP_DIR, mesh))
157     if salome.sg.hasDesktop():
158         salome.sg.updateObjBrowser()
159     return True
160
161 __instance = None
162
163 def getInstance():
164   """
165   This function returns a singleton instance of the plugin dialog.
166   It is mandatory in order to call show without a parent ...
167   """
168   global __instance
169   if __instance is None:
170     __instance = TopIIVolMeshPluginDialog()
171   return __instance