]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROTools/plugins/cutMeshDialog.py
Salome HOME
Correction for hydro_test
[modules/hydro.git] / src / HYDROTools / plugins / cutMeshDialog.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 import salome
21
22 salome.salome_init()
23
24 import SalomePyQt
25 sgPyQt = SalomePyQt.SalomePyQt()
26 import libSALOME_Swig
27 salome_gui = libSALOME_Swig.SALOMEGUI_Swig()
28
29 from PyQt5.QtCore import *
30 from PyQt5.QtGui import *
31 from PyQt5.QtWidgets import *
32 from PyQt5 import uic
33 from HYDROPy import *
34 import json
35
36 import tempfile
37
38 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
39 from salome.hydrotools.changeCoords import changeCoords
40 from salome.hydrotools.cutMesh import cutMesh
41
42 import  SMESH, SALOMEDS
43 from salome.smesh import smeshBuilder
44
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')
47
48 class cutMeshDialog(QDialog):
49     """
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.
52     """
53
54     def __init__(self, parent = None, modal = 0):
55         QDialog.__init__(self, parent)
56         uic.loadUi(os.path.join(hydro_root, 'cutMesh.ui'), self )
57
58         # Connections
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)
69         self.medFile = None
70         self.outDir = None
71         self.tmpdir = tempfile.mkdtemp()
72         print("tmpdir=",self.tmpdir)
73         
74     def get_selected_mesh(self):
75         """
76         Select a mesh in the object browser and return associated filename
77         """
78         nbsel = salome.sg.SelectedCount()
79         if nbsel != 1:
80             return ""
81         sel=salome.sg.getSelected(0)
82         so=salome.myStudy.FindObjectID(sel)
83         if so is None:
84             return ""
85         obj=so.GetObject()
86         smesh = smeshBuilder.New()
87         smesh.SetEnablePublish( False )
88         mesh1 = smesh.Mesh()
89         smesh.SetEnablePublish( True )
90         if not isinstance(obj, type(mesh1.GetMesh())):
91             return ""
92         filename = obj.GetMesh().GetMEDFileInfo().fileName
93         return filename
94     
95     def on_select_med_file(self):
96         """
97         set selected mesh filename on dialog
98         """
99         filename = self.get_selected_mesh()
100         print("selected mesh: %s"%filename)
101         if filename != "":
102             self.medFile = filename
103             self.le_medFile.setText(self.medFile)
104         
105     def on_med_file_browse(self):
106         """
107         Select MED file in file system
108         """
109         print("on_med_file_browse")
110         self.medFile, filt = QFileDialog.getOpenFileName(self, self.tr("Input MED file"), "", self.tr("MED files (*.med)"))
111         print(self.medFile)
112         if not self.medFile:
113             return
114         self.le_medFile.setText(self.medFile)
115
116     def get_selected_polyline(self):
117         """
118         Select a polyline2D in the HYDRO object browser
119         """
120         ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
121         doc = HYDROData_Document.Document()
122         for i in ind:
123             if i.column()==0:
124                 name = str(i.data())
125                 case = doc.FindObjectByName( name )
126                 if isinstance(case, HYDROData_PolylineXY):
127                     print("selected %s"%name)
128                     return name
129         return None
130     
131     def on_select_shape_file(self):
132         """
133         Get selected Polyline in the HYDRO object browser
134         """
135         name = self.get_selected_polyline()
136         if name is None:
137             return 
138         doc = HYDROData_Document.Document()
139         polyXY = doc.FindObjectByName(name)
140         if polyXY is None:
141             return
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)
145
146     def on_shape_file_browse(self):
147         """
148         Select shapefile to cut mesh
149         """
150         print("on_shape_file_browse")
151         self.shapeCut, filt = QFileDialog.getOpenFileName(self, self.tr("shapefile of mesh border edges"), "", self.tr("shapefiles (*.shp)"))
152         print(self.shapeCut)
153         if not self.shapeCut:
154             return
155         self.le_shapeCut.setText(self.shapeCut)
156
157
158     def on_outputDir_browse(self):
159         """
160         Select OutputDirectory
161         """
162         print("on_ouptutDir_browse")
163         self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
164         print(self.outDir)
165         if not self.outDir:
166             return
167         self.le_outDir.setText(self.outDir)
168         
169     def on_help(self):
170         """
171         display a help message
172         """
173         msg = """
174         <h2>Cut a mesh by a polyline shape Dialog</h2>
175
176         This dialog is used to cut a mesh using a polygonal shape, removing all the nodes and elements inside the shape.
177         <br><br>
178         The shape should be in the same system of coordinates of the mesh, without origin offset.
179         <br>        
180         The mesh is saved in a new file.
181         <br>
182         If the mesh uses a local coordinate system with an origin offset, the coordinates of this origin should be set in the dialog.
183         <br><br>         
184         Below is the description of the dialog controls.
185
186         <h3>MED file</h3>
187         This field allows selection of a med file (via the standard file open dialog).
188         The filling of this field is mandatory.
189         
190         <h3>offsetX, offsetY</h3>
191         These fields are used to set the Origin of the local coordinates system of the mesh, if any. 
192
193         <h3>Cutting shapefile</h3>
194         Select the shape via the standard file open dialog or by selection in the object browser.
195         
196         <h3>Output directory</h3>
197         This field allows selection of a directory to store the shapes and the outputMesh.
198
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. 
201         """
202         QMessageBox.about(self, self.tr("About cut mesh by shape dialog"), msg);
203        
204    
205     def on_accept(self):
206         print("accept")
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()
214         self.accept()
215         print(medFile)
216         print(offsetX, offsetY)
217         print(shapeCut)
218         print(outDir)
219         print(outOffsetX, outOffsetY)
220         a = os.path.splitext(medFile)
221         
222         medFileTrans = os.path.join(self.tmpdir, os.path.basename(a[0])+'_trs.med')
223         changeCoords(medFile, medFileTrans, 2154, 2154, offsetX, offsetY, 0, 0)
224         
225         medFileOut = os.path.join(outDir, os.path.basename(a[0]) + '_cut' + a[1])
226         print(medFileOut)
227         meshFileOut = cutMesh(medFileTrans,
228                               shapeCut,
229                               medFileOut, outOffsetX, outOffsetY)
230         
231         if salome.sg.hasDesktop():
232             salome.sg.updateObjBrowser()
233             
234
235     def on_reject(self):
236         print("reject")
237         self.reject()
238         
239
240 def execDialog(context):
241     print("execDialog")
242     desktop = sgPyQt.getDesktop()
243     dlg = cutMeshDialog(desktop)
244     dlg.show()