Salome HOME
spns #19079: Top-ii-vol integration in SMESH
[modules/smesh.git] / src / Tools / TopIIVolMeshPlug / TopIIVolMeshPluginDialog.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2013-2020  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.qcbDistributed.stateChanged[int].connect(self.OnqcbDistributedClicked)
52     self.qlbXParts.setVisible(False)
53     self.qlbYParts.setVisible(False)
54     self.qlbZParts.setVisible(False)
55     self.qsbXParts.setVisible(False)
56     self.qsbYParts.setVisible(False)
57     self.qsbZParts.setVisible(False)
58     self.SALOME_TMP_DIR = None
59     try:
60       self.qleTmpDir.setText(os.path.join('/tmp',getpass.getuser(),'top-ii-vol'))
61     except:
62       self.qleTmpDir.setText('/tmp')
63     self.resize(800, 500)
64     self.outputMesh = ''
65
66   def OnQpbHelpClicked(self):
67     import SalomePyQt
68     sgPyQt = SalomePyQt.SalomePyQt()
69     try:
70       mydir=os.environ["SMESH_ROOT_DIR"]
71     except Exception:
72       QMessageBox.warning(self, "Help", "Help unavailable $SMESH_ROOT_DIR not found")
73       return
74
75     myDoc=mydir + "/share/doc/salome/gui/SMESH/TopIIVolMesh/index.html"
76     sgPyQt.helpContext(myDoc,"")
77
78   def OnQpbMeshFileClicked(self):
79     fd = QFileDialog(self, "select an existing Mesh file", self.qleMeshFile.text(), "Mesh-Files (*.xyz);;All Files (*)")
80     if fd.exec_():
81       infile = fd.selectedFiles()[0]
82       self.qleMeshFile.setText(infile)
83
84   def OnQpbComputeClicked(self):
85     if self.qleMeshFile.text() == '':
86       QMessageBox.critical(self, "Mesh", "select an input mesh")
87       return
88     inputMesh = self.qleMeshFile.text()
89     # retrieve x,y,z and depth parameters
90     xPoints = self.qsbXPoints.value()
91     yPoints = self.qsbYPoints.value()
92     zPoints = self.qsbZPoints.value()
93     depth   = self.qsbDepth.value()
94     nProcs  = self.qsbNBprocs.value()
95     if not self.qcbDistributed.isChecked():
96       if nProcs == 1:
97         shellCmd = "topIIvol_Mesher"
98       else:
99         shellCmd = "mpirun -np {} topIIvol_ParMesher".format(nProcs)
100       shellCmd+= " --xpoints " + str(xPoints)
101       shellCmd+= " --ypoints " + str(yPoints)
102       shellCmd+= " --zpoints " + str(zPoints)
103       shellCmd+= " --depth   " + str(depth)
104       shellCmd+= " --in " + inputMesh
105     else:
106       xParts = self.qsbXParts.value()
107       yParts = self.qsbYParts.value()
108       zParts = self.qsbZParts.value()
109       shellCmd = "mpirun -np {} topIIvol_DistMesher".format(nProcs)
110       shellCmd+= " --xpoints " + str(xPoints)
111       shellCmd+= " --ypoints " + str(yPoints)
112       shellCmd+= " --zpoints " + str(zPoints)
113       shellCmd+= " --depth   " + str(depth)
114       shellCmd+= " --partition_x " + str(xParts)
115       shellCmd+= " --partition_y " + str(yParts)
116       shellCmd+= " --partition_z " + str(zParts)
117       shellCmd+= " --in " + inputMesh
118     if platform.system()=="Windows" :
119       self.SALOME_TMP_DIR = os.getenv("SALOME_TMP_DIR")
120     else:
121       self.SALOME_TMP_DIR = os.path.join(self.qleTmpDir.text(), time.strftime("%Y-%m-%d-%H-%M-%S"))
122       pathlib.Path(self.SALOME_TMP_DIR).mkdir(parents=True, exist_ok=True)
123     self.outputMesh= os.path.join(self.SALOME_TMP_DIR, inputMesh.split('/').pop().replace('.xyz','.mesh'))
124     shellCmd+= " --out " + self.outputMesh
125     print("INFO: ", shellCmd)
126     myMonitorView=TopIIVolMeshMonitor(self, shellCmd)
127
128   def OnqcbDistributedClicked(self):
129     state = self.qcbDistributed.isChecked()
130     self.qlbXParts.setVisible(state)
131     self.qlbYParts.setVisible(state)
132     self.qlbZParts.setVisible(state)
133     self.qsbXParts.setVisible(state)
134     self.qsbYParts.setVisible(state)
135     self.qsbZParts.setVisible(state)
136
137   def OnQpbCloseClicked(self):
138     self.close()
139
140   def saveOutputMesh(self):
141     if not self.qcbDisplayMesh.isChecked():
142       return True
143     import salome
144     import  SMESH, SALOMEDS
145     from salome.smesh import smeshBuilder
146     smesh = smeshBuilder.New()
147     self.outputMesh.split('/')
148     for mesh in pathlib.Path(self.SALOME_TMP_DIR).glob('*.mesh'):
149       (outputMesh, status) = smesh.CreateMeshesFromGMF(os.path.join(self.SALOME_TMP_DIR, mesh))
150     if salome.sg.hasDesktop():
151         salome.sg.updateObjBrowser()
152     return True
153
154 __instance = None
155
156 def getInstance():
157   """
158   This function returns a singleton instance of the plugin dialog.
159   It is mandatory in order to call show without a parent ...
160   """
161   global __instance
162   if __instance is None:
163     __instance = TopIIVolMeshPluginDialog()
164   return __instance