Salome HOME
Correction for hydro_test
[modules/hydro.git] / src / HYDROTools / plugins / fitShapePointsToMeshEdgesDialog.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
35 from salome.hydrotools.shapesGroups import fitShapePointsToMesh
36 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
37
38 import tempfile
39
40 hydro_root = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'plugins', 'hydro', 'plugins')
41 hydro_resources = os.path.join(os.environ['HYDRO_ROOT_DIR'], 'share', 'salome', 'resources', 'hydro')
42
43 class fitShapePointsToMeshEdgesDialog(QDialog):
44     """
45     This dialog is used to adjust a shapefile crossing another shapefile corresponding to edges of a mesh, for instance the free borders.
46     the shapeFile to adjust must be a closed line or polygon crossing the free border shapefile in 2 points.
47     """
48
49     def __init__(self, parent = None, modal = 0):
50         QDialog.__init__(self, parent)
51         uic.loadUi(os.path.join(hydro_root, 'fitShapePointsToMeshEdges.ui'), self )
52
53         # Connections
54         self.tb_meshEdges.clicked.connect(self.on_select_meshEdges)
55         self.tb_shapeToAdjust.clicked.connect(self.on_select_shapeToAdjust)
56         self.tb_meshEdges.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
57         self.tb_shapeToAdjust.setIcon(QIcon(QPixmap(os.path.join(hydro_resources, "icon_select.png"))))
58         self.pb_meshEdges.clicked.connect(self.on_meshEdges_browse)
59         self.pb_shapeToAdjust.clicked.connect(self.on_shapeToAdjust_browse)
60         self.pb_outDir.clicked.connect(self.on_outputDir_browse)
61         self.pb_help.clicked.connect(self.on_help)
62         self.pb_ok.accepted.connect(self.on_accept)
63         self.pb_ok.rejected.connect(self.on_reject)
64         self.cb_splineMeshEdges.setEnabled(False)
65         self.cb_splineShapeToAdjust.setEnabled(False)
66         self.meshEdges = None
67         self.shapeToAdjust = None
68         self.tmpdir = tempfile.mkdtemp()
69         print("tmpdir=",self.tmpdir)
70
71         
72     def get_selected_polyline(self):
73         """
74         Select a polyline2D in the HYDRO object browser
75         """
76         ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
77         doc = HYDROData_Document.Document()
78         for i in ind:
79             if i.column()==0:
80                 name = str(i.data())
81                 case = doc.FindObjectByName( name )
82                 if isinstance(case, HYDROData_PolylineXY):
83                     print("selected %s"%name)
84                     return name
85         return None
86     
87     def on_select_meshEdges(self):
88         """
89         Get selected Polyline in the HYDRO object browser
90         """
91         name = self.get_selected_polyline()
92         if name is None:
93             return 
94         doc = HYDROData_Document.Document()
95         polyXY = doc.FindObjectByName(name)
96         if polyXY is None:
97             return
98         self.meshEdges = os.path.join(self.tmpdir, name + ".shp")
99         res = HYDROData_PolylineXY.ExportShapeXY(doc, self.meshEdges, [polyXY])
100         self.le_meshEdges.setText(self.meshEdges)
101         
102     def on_select_shapeToAdjust(self):
103         """
104         Get selected Polyline in the HYDRO object browser
105         """
106         name = self.get_selected_polyline()
107         if name is None:
108             return 
109         doc = HYDROData_Document.Document()
110         polyXY = doc.FindObjectByName(name)
111         if polyXY is None:
112             return
113         self.shapeToAdjust = os.path.join(self.tmpdir, name + ".shp")
114         res = HYDROData_PolylineXY.ExportShapeXY(doc, self.shapeToAdjust, [polyXY])
115         self.le_shapeToAdjust.setText(self.shapeToAdjust)
116
117     def on_meshEdges_browse(self):
118         """
119         Select Shapefile of mesh edges
120         """
121         print("on_meshEdges_browse")
122         self.meshEdges, filt = QFileDialog.getOpenFileName(self, self.tr("shapefile of mesh border edges"), "", self.tr("shapefiles (*.shp)"))
123         print(self.meshEdges)
124         if not self.meshEdges:
125             return
126         self.le_meshEdges.setText(self.meshEdges)
127         
128     def on_shapeToAdjust_browse(self):
129         """
130         Select shapefile to adjust
131         """
132         print("on_shapeToAdjust_browse")
133         self.shapeToAdjust, filt = QFileDialog.getOpenFileName(self, self.tr("shapefile to adjust"), "", self.tr("shapefiles (*.shp)"))
134         print(self.shapeToAdjust)
135         if not self.shapeToAdjust:
136             return
137         self.le_shapeToAdjust.setText(self.shapeToAdjust)
138         self.le_outDir.setText(os.path.dirname(self.shapeToAdjust))
139         
140     def on_outputDir_browse(self):
141         """
142         Select OutputDirectory
143         """
144         print("on_ouptutDir_browse")
145         self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
146         print(self.outDir)
147         if not self.outDir:
148             return
149         self.le_outDir.setText(self.outDir)
150         
151     def on_help(self):
152         """
153         display a help message
154         """
155         msg = """
156         <h2>Fit shape points to mesh edges dialog</h2>
157
158         This dialog is used to adjust a shapefile crossing another shapefile corresponding to edges of a mesh, for instance the free borders.
159         the shapeFile to adjust must be a closed line or polygon crossing the free border shapefile in 2 points.
160         <br><br>
161         The algorithm find in the shapefile to adjust and in the free border shapefile the two closest pairs of corresponding points 
162         and move the points in the shapefile to adjust to correspond to the points found in the free border shapefile.
163         <br>
164         If requested, it splits the free border shapefile and/or the shapefile adjusted in two parts, at their intersection.
165         <br>
166         The shapefile adusted is written by default in the directory of the shapefile to adjust, with a suffix '_adj'.
167         <br>
168         The split free border shapefile is written in the directory of the free border shapefile, with a suffix '_split'.
169         <br><br>         
170         Below is the description of the dialog controls.
171
172         <h3>Mesh edges shapefile</h3>
173         This field allows selection of a shapefile (via the standard file open dialog or by selection in the Object Browser).
174         <h3>Shapefile to adjust</h3>
175         This field allows selection of a shapefile (via the standard file open dialog or by selection in the Object Browser).
176         <h3>Output dir</h3>
177         This field allows selection of the directory used to store the results.
178         <h3>Split mesh edges shapefile</h3>
179         If this checkbox is checked, the mesh edges shapefile is split at the intersection with the other shapefile.
180         <h3>Split shapefile to adjust</h3>
181         If this checkbox is checked, the shapefile to adjust is split at the intersection with the other shapefile.
182         <h3>Load result</h3>
183         If this checkbox is checked, the corresponding shapefile is loaded.
184         <h3>Load as spline</h3>
185         If this checkbox is checked, the corresponding shapefile is loaded as a spline.
186         """
187         QMessageBox.about(self, self.tr("About fit shape points to mesh edges dialog"), msg);
188        
189    
190     def on_accept(self):
191         print("accept")
192         meshEdges = self.le_meshEdges.text()
193         shapeToAdjust = self.le_shapeToAdjust.text()
194         isMeshEdgesSplit = self.cb_splitMeshEdges.isChecked()
195         isShapeToAdjustSplit = self.cb_splitShapeToAdjust.isChecked()
196         isLoadMeshEdges = self.cb_loadMeshEdges.isChecked()
197         isLoadShapeToAdjust = self.cb_loadShapeToAdjust.isChecked()
198         isSplineMeshEdges = self.cb_splineMeshEdges.isChecked()
199         isSplineShapeToAdjust = self.cb_splineShapeToAdjust.isChecked()
200         outdir = self.le_outDir.text()
201         if not os.path.isdir(outdir):
202             msgBox = QMessageBox()
203             msgBox.setText( "Output directory is not set, please select" )
204             msgBox.exec_()
205             return
206         self.accept()
207         print(meshEdges)
208         print(shapeToAdjust)
209         print(isMeshEdgesSplit)
210         print(isShapeToAdjustSplit)
211         fitShapePointsToMesh(meshEdges, shapeToAdjust, outdir, isMeshEdgesSplit, isShapeToAdjustSplit)
212         hydro_doc = HYDROData_Document.Document()
213         if isLoadMeshEdges:
214             a = os.path.splitext(os.path.basename(meshEdges))
215             ext = ""
216             if isMeshEdgesSplit:
217                 ext = "_split"
218             meshEdgesShapefile = os.path.join(outdir, a[0] + ext + a[1]) 
219             shapeName = a[0] #+ ext + "_PolyXY"
220             print("isSplineMeshEdges %s"%isSplineMeshEdges)
221             meshEdgesShapes = importPolylines(hydro_doc, meshEdgesShapefile, isSplineMeshEdges, 0)
222         if isLoadShapeToAdjust:            
223             a = os.path.splitext(os.path.basename(shapeToAdjust))            
224             ext = "_adj"
225             adjustedShapefile = os.path.join(outdir, a[0] + ext + a[1]) 
226             shapeName = a[0] #+ ext + "_PolyXY"
227             print("isSplineShapeToAdjust %s"%isSplineShapeToAdjust)
228             adjustedShapes = importPolylines(hydro_doc, adjustedShapefile, isSplineShapeToAdjust, 0)
229         if salome.sg.hasDesktop():
230             salome.sg.updateObjBrowser()
231
232
233     def on_reject(self):
234         print("reject")
235         self.reject()
236         
237
238 def execDialog(context):
239     print("execDialog")
240     desktop = sgPyQt.getDesktop()
241     dlg = fitShapePointsToMeshEdgesDialog(desktop)
242     dlg.show()