1 # Copyright (C) 2012-2013 EDF
3 # This file is part of SALOME HYDRO module.
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.
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.
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/>.
25 sgPyQt = SalomePyQt.SalomePyQt()
27 salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
29 from PyQt5.QtCore import *
30 from PyQt5.QtGui import *
31 from PyQt5.QtWidgets import *
36 from salome.hydrotools.shapesGroups import freeBordersGroup, exploreEdgeGroups
37 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
39 import SMESH, SALOMEDS
40 from salome.smesh import smeshBuilder
42 hydro_root = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'plugins', 'hydro', 'plugins')
43 hydro_resources = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'resources', 'hydro')
45 class meshEdgesToShapesDialog(QDialog):
47 This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
48 and write the groups as shapefiles.
51 def __init__(self, parent = None, modal = 0):
52 QDialog.__init__(self, parent)
53 uic.loadUi(os.path.join(hydro_root, 'meshEdgesToShapes.ui'), self )
56 self.pb_medFile.clicked.connect(self.on_med_file_browse)
57 self.tb_medFile.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
58 self.tb_medFile.clicked.connect(self.on_select_med_file)
59 self.pb_outDir.clicked.connect(self.on_outputDir_browse)
60 self.pb_help.clicked.connect(self.on_help)
61 self.pb_ok.accepted.connect(self.on_accept)
62 self.pb_ok.rejected.connect(self.on_reject)
63 self.cb_keepOutMed.setChecked(True)
67 def get_selected_mesh(self):
69 Select a mesh in the object browser and return associated filename
71 nbsel = salome.sg.SelectedCount()
74 sel=salome.sg.getSelected(0)
75 so=salome.myStudy.FindObjectID(sel)
79 smesh = smeshBuilder.New()
80 smesh.SetEnablePublish( False )
82 smesh.SetEnablePublish( True )
83 if not isinstance(obj, type(mesh1.GetMesh())):
85 filename = obj.GetMesh().GetMEDFileInfo().fileName
88 def on_select_med_file(self):
90 set selected mesh filename on dialog
92 filename = self.get_selected_mesh()
93 print("selected mesh: %s"%filename)
95 self.medFile = filename
96 self.le_medFile.setText(self.medFile)
98 def on_med_file_browse(self):
100 Select MED file in file system
102 print("on_med_file_browse")
103 self.medFile, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
107 self.le_medFile.setText(self.medFile)
109 def on_outputDir_browse(self):
111 Select OutputDirectory
113 print("on_ouptutDir_browse")
114 self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
118 self.le_outDir.setText(self.outDir)
122 display a help message
125 <h2>Mesh Edges to Shapes Dialog</h2>
127 This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
128 and write the groups as shapefiles.
130 The free edges regroup the external border of the mesh, and all the internal borders (isles).
131 A group containing the free edges is added to the mesh.
133 The mesh is saved in a new file, in an ouput directory used also to store the shapefiles.
135 A shapefile of edges and a shapefile of points are generated for each group of edges.
136 The shapefiles are intended to be loaded in a SIG tool (Qgis) and should preferaby be set in a correct coordinates system.
138 If the mesh uses a local coordinate system with an origin offset, the coordinates of this origin should be set in the dialog.
140 Below is the description of the dialog controls.
143 This field allows selection of a med file (via the standard file open dialog).
144 The filling of this field is mandatory.
146 <h3>offsetX, offsetY</h3>
147 These fields are used to set the Origin of the local coordinates system of the mesh, if any.
149 <h3>Output directory</h3>
150 This field allows selection of a directory to store the shapes and the outputMesh.
152 <h3>Keep output MED file</h3>
153 If this checkbox is unchecked, the output mesh will be removed after calculation of the shapes.
155 QMessageBox.about(self, self.tr("About mesh edges to shapes dialog"), msg);
160 medFile = self.le_medFile.text()
161 outDir = self.le_outDir.text()
162 offsetX = self.dsb_offsetX.value()
163 offsetY = self.dsb_offsetY.value()
164 isOutMedKept = self.cb_keepOutMed.isChecked()
165 isLoadFreeBorders = self.cb_loadFreeBorders.isChecked()
166 isLoadOthers = self.cb_loadOthers.isChecked()
171 print(offsetX, offsetY)
172 a = os.path.splitext(medFile)
173 medFileOut = os.path.join(outDir, os.path.basename(a[0]) + '_brd' + a[1])
175 smesh = smeshBuilder.New()
176 smesh.SetEnablePublish( False )
177 medFileOut = freeBordersGroup(medFile, medFileOut)
178 exploreEdgeGroups(medFileOut, outDir, offsetX, offsetY)
179 smesh.SetEnablePublish( True )
181 print("remove", medFileOut)
182 os.remove(medFileOut)
183 shapesListFile = os.path.join(outDir, "shapesList.json")
185 with open(shapesListFile, 'r') as f:
186 fileShapes = json.load(f)
188 hydro_doc = HYDROData_Document.Document()
190 if isLoadFreeBorders:
191 l = l + [ a for a in fileShapes if "FreeBorders.shp" in a]
193 l = l + [ a for a in fileShapes if "FreeBorders.shp" not in a]
195 a = os.path.splitext(os.path.basename(fileShape))
197 shapes = importPolylines(hydro_doc, fileShape, 0, 0)
198 if salome.sg.hasDesktop():
199 salome.sg.updateObjBrowser()
207 def execDialog(context):
209 desktop = sgPyQt.getDesktop()
210 dlg = meshEdgesToShapesDialog(desktop)