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