Salome HOME
trying to fix selection mecanism in OCC Viewer: Selection is OK, but broken after...
[modules/hydro.git] / src / HYDROTools / plugins / meshEdgesToShapesDialog.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import sys
20
21 from PyQt5.QtCore import *
22 from PyQt5.QtGui import *
23 from PyQt5.QtWidgets import *
24 from PyQt5 import uic
25
26 from salome.hydrotools.shapesGroups import freeBordersGroup, exploreEdgeGroups
27
28 #import sysconfig
29 #pythonVersion = 'python' + sysconfig.get_python_version()
30 hydro_root = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'plugins', 'hydro', 'plugins')
31
32 class meshEdgesToShapesDialog(QDialog):
33     """
34     """
35
36     def __init__(self, parent = None, modal = 0):
37         QDialog.__init__(self, parent)
38         uic.loadUi(os.path.join(hydro_root, 'meshEdgesToShapes.ui'), self )
39
40         # Connections
41         self.pb_medFile.clicked.connect(self.on_med_file_browse)
42         self.pb_outDir.clicked.connect(self.on_outputDir_browse)
43         self.pb_help.clicked.connect(self.on_help)
44         self.pb_ok.accepted.connect(self.on_accept)
45         self.pb_ok.rejected.connect(self.on_reject)
46         self.cb_keepOutMed.setChecked(True)
47         self.medFile = None
48         self.outDir = None
49         
50     def on_med_file_browse(self):
51         """
52         Select MED file
53         """
54         print("on_med_file_browse")
55         self.medFile, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
56         print(self.medFile)
57         if not self.medFile:
58             return
59         self.le_medFile.setText(self.medFile)
60         
61     def on_outputDir_browse(self):
62         """
63         Select OutputDirectory
64         """
65         print("on_ouptutDir_browse")
66         self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
67         print(self.outDir)
68         if not self.outDir:
69             return
70         self.le_outDir.setText(self.outDir)
71         
72     def on_help(self):
73         """
74         display a help message
75         """
76         msg = """
77         <h2>Mesh Edges to Shapes Dialog</h2>
78
79         This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
80         and write the groups as shapefiles.
81         <br><br>
82         The free edges regroup the external border of the mesh, and all the internal borders (isles).
83         A group containing the free edges is added to the mesh.
84         <br>        
85         The mesh is saved in a new file, in an ouput directory used also to store the shapefiles.
86         <br>        
87         A shapefile of edges and a shapefile of points are generated for each group of edges.
88         The shapefiles are intended to be loaded in a SIG tool (Qgis) and should preferaby be set in a correct coordinates system.
89         <br>
90         If the mesh uses a local coordinate system with an origin offset, the coordinates of this origin should be set in the dialog.
91         <br><br>         
92         Below is the description of the dialog controls.
93
94         <h3>MED file</h3>
95         This field allows selection of a med file (via the standard file open dialog).
96         The filling of this field is mandatory.
97         
98         <h3>offsetX, offsetY</h3>
99         These fields are used to set the Origin of the local coordinates system of the mesh, if any. 
100
101         <h3>Output directory</h3>
102         This field allows selection of a directory to store the shapes and the outputMesh.
103
104         <h3>Keep output MED file</h3>
105         If this checkbox is unchecked, the output mesh will be removed after calculation of the shapes.
106         """
107         QMessageBox.about(self, self.tr("About mesh edges to shapes dialog"), msg);
108        
109    
110     def on_accept(self):
111         print("accept")
112         medFile = self.le_medFile.text()
113         outDir = self.le_outDir.text()
114         offsetX = self.dsb_offsetX.value()
115         offsetY = self.dsb_offsetY.value()
116         isOutMedKept = self.cb_keepOutMed.isChecked()
117         self.accept()
118         print(medFile)
119         print(outDir)
120         print(isOutMedKept)
121         print(offsetX, offsetY)
122         a = os.path.splitext(medFile)
123         medFileOut = os.path.join(outDir, os.path.basename(a[0]) + '_brd' + a[1])
124         print(medFileOut)
125         medFileOut = freeBordersGroup(medFile, medFileOut)
126         exploreEdgeGroups(medFileOut, outDir, offsetX, offsetY)
127         if not isOutMedKept:
128             print("remove", medFileOut)
129             os.remove(medFileOut)
130
131     def on_reject(self):
132         print("reject")
133         self.reject()
134         
135
136 def execDialog(context):
137     print("execDialog")
138   # get context study, salomeGui
139     study = context.study
140     sg = context.sg
141     dlg = meshEdgesToShapesDialog()
142     dlg.exec_()