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 *
38 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
39 from salome.hydrotools.changeCoords import changeCoords
40 from salome.hydrotools.cutMesh import cutMesh
42 import SMESH, SALOMEDS
43 from salome.smesh import smeshBuilder
45 hydro_root = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'plugins', 'hydro', 'plugins')
46 hydro_resources = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'resources', 'hydro')
48 class cutMeshDialog(QDialog):
50 This dialog is used to extract all groups of edges from a mesh, plus all the border (free) edges,
51 and write the groups as shapefiles.
54 def __init__(self, parent = None, modal = 0):
55 QDialog.__init__(self, parent)
56 uic.loadUi(os.path.join(hydro_root, 'cutMesh.ui'), self )
59 self.pb_medFile.clicked.connect(self.on_med_file_browse)
60 self.tb_medFile.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
61 self.tb_medFile.clicked.connect(self.on_select_med_file)
62 self.pb_shapeCut.clicked.connect(self.on_shape_file_browse)
63 self.tb_shapeCut.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
64 self.tb_shapeCut.clicked.connect(self.on_select_shape_file)
65 self.pb_outDir.clicked.connect(self.on_outputDir_browse)
66 self.pb_help.clicked.connect(self.on_help)
67 self.pb_ok.accepted.connect(self.on_accept)
68 self.pb_ok.rejected.connect(self.on_reject)
71 self.tmpdir = tempfile.mkdtemp()
72 print("tmpdir=",self.tmpdir)
74 def get_selected_mesh(self):
76 Select a mesh in the object browser and return associated filename
78 nbsel = salome.sg.SelectedCount()
81 sel=salome.sg.getSelected(0)
82 so=salome.myStudy.FindObjectID(sel)
86 smesh = smeshBuilder.New()
87 smesh.SetEnablePublish( False )
89 smesh.SetEnablePublish( True )
90 if not isinstance(obj, type(mesh1.GetMesh())):
92 filename = obj.GetMesh().GetMEDFileInfo().fileName
95 def on_select_med_file(self):
97 set selected mesh filename on dialog
99 filename = self.get_selected_mesh()
100 print("selected mesh: %s"%filename)
102 self.medFile = filename
103 self.le_medFile.setText(self.medFile)
105 def on_med_file_browse(self):
107 Select MED file in file system
109 print("on_med_file_browse")
110 self.medFile, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
114 self.le_medFile.setText(self.medFile)
116 def get_selected_polyline(self):
118 Select a polyline2D in the HYDRO object browser
120 ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
121 doc = HYDROData_Document.Document()
125 case = doc.FindObjectByName( name )
126 if isinstance(case, HYDROData_PolylineXY):
127 print("selected %s"%name)
131 def on_select_shape_file(self):
133 Get selected Polyline in the HYDRO object browser
135 name = self.get_selected_polyline()
138 doc = HYDROData_Document.Document()
139 polyXY = doc.FindObjectByName(name)
142 self.shapeCut = os.path.join(self.tmpdir, name + ".shp")
143 res = HYDROData_PolylineXY.ExportShapeXY(doc, self.shapeCut, [polyXY])
144 self.le_shapeCut.setText(self.shapeCut)
146 def on_shape_file_browse(self):
148 Select shapefile to cut mesh
150 print("on_shape_file_browse")
151 self.shapeCut, filt = QFileDialog.getOpenFileName(self, self.tr("shapefile of mesh border edges"), "", self.tr("shapefiles (*.shp)"))
153 if not self.shapeCut:
155 self.le_shapeCut.setText(self.shapeCut)
158 def on_outputDir_browse(self):
160 Select OutputDirectory
162 print("on_ouptutDir_browse")
163 self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
167 self.le_outDir.setText(self.outDir)
171 display a help message
174 <h2>Cut a mesh by a polyline shape Dialog</h2>
176 This dialog is used to cut a mesh using a polygonal shape, removing all the nodes and elements inside the shape.
178 The shape should be in the same system of coordinates of the mesh, without origin offset.
180 The mesh is saved in a new file.
182 If the mesh uses a local coordinate system with an origin offset, the coordinates of this origin should be set in the dialog.
184 Below is the description of the dialog controls.
187 This field allows selection of a med file (via the standard file open dialog).
188 The filling of this field is mandatory.
190 <h3>offsetX, offsetY</h3>
191 These fields are used to set the Origin of the local coordinates system of the mesh, if any.
193 <h3>Cutting shapefile</h3>
194 Select the shape via the standard file open dialog or by selection in the object browser.
196 <h3>Output directory</h3>
197 This field allows selection of a directory to store the shapes and the outputMesh.
199 <h3>offsetX, offsetY</h3>
200 These fields are used to set the Origin of the local coordinates system of the output mesh, if any.
202 QMessageBox.about(self, self.tr("About cut mesh by shape dialog"), msg);
207 medFile = self.le_medFile.text()
208 offsetX = self.dsb_offsetX.value()
209 offsetY = self.dsb_offsetY.value()
210 shapeCut = self.le_shapeCut.text()
211 outDir = self.le_outDir.text()
212 outOffsetX = self.dsb_outOffsetX.value()
213 outOffsetY = self.dsb_outOffsetY.value()
216 print(offsetX, offsetY)
219 print(outOffsetX, outOffsetY)
220 a = os.path.splitext(medFile)
222 medFileTrans = os.path.join(self.tmpdir, os.path.basename(a[0])+'_trs.med')
223 changeCoords(medFile, medFileTrans, 2154, 2154, offsetX, offsetY, 0, 0)
225 medFileOut = os.path.join(outDir, os.path.basename(a[0]) + '_cut' + a[1])
227 meshFileOut = cutMesh(medFileTrans,
229 medFileOut, outOffsetX, outOffsetY)
231 if salome.sg.hasDesktop():
232 salome.sg.updateObjBrowser()
240 def execDialog(context):
242 desktop = sgPyQt.getDesktop()
243 dlg = cutMeshDialog(desktop)