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 *
35 from salome.hydrotools.shapesGroups import fitShapePointsToMesh
36 from salome.hydrotools.hydroGeoMeshUtils import importPolylines
41 #pythonVersion = 'python' + sysconfig.get_python_version()
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 fitShapePointsToMeshEdgesDialog(QDialog):
49 def __init__(self, parent = None, modal = 0):
50 QDialog.__init__(self, parent)
51 uic.loadUi(os.path.join(hydro_root, 'fitShapePointsToMeshEdges.ui'), self )
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)
67 self.shapeToAdjust = None
68 self.tmpdir = tempfile.mkdtemp()
69 print("tmpdir=",self.tmpdir)
72 def get_selected_polyline(self):
74 Select a polyline2D in the HYDRO object browser
76 ind = SalomePyQt.SalomePyQt.getObjectBrowser().selectionModel().selectedIndexes()
77 doc = HYDROData_Document.Document()
81 case = doc.FindObjectByName( name )
82 if isinstance(case, HYDROData_PolylineXY):
83 print("selected %s"%name)
87 def on_select_meshEdges(self):
89 Get selected Polyline in the HYDRO object browser
91 name = self.get_selected_polyline()
94 doc = HYDROData_Document.Document()
95 polyXY = doc.FindObjectByName(name)
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)
102 def on_select_shapeToAdjust(self):
104 Get selected Polyline in the HYDRO object browser
106 name = self.get_selected_polyline()
109 doc = HYDROData_Document.Document()
110 polyXY = doc.FindObjectByName(name)
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)
117 def on_meshEdges_browse(self):
119 Select Shapefile of mesh edges
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:
126 self.le_meshEdges.setText(self.meshEdges)
128 def on_shapeToAdjust_browse(self):
130 Select shapefile to adjust
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:
137 self.le_shapeToAdjust.setText(self.shapeToAdjust)
138 self.le_outDir.setText(os.path.dirname(self.shapeToAdjust))
140 def on_outputDir_browse(self):
142 Select OutputDirectory
144 print("on_ouptutDir_browse")
145 self.outDir = QFileDialog.getExistingDirectory(self, self.tr("Output Directory"), "")
149 self.le_outDir.setText(self.outDir)
153 display a help message
156 <h2>Fit shape points to mesh edges dialog</h2>
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.
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.
164 If requested, it splits the free border shapefile and/or the shapefile adjusted in two parts, at their intersection.
166 The shapefile adusted is written by default in the directory of the shapefile to adjust, with a suffix '_adj'.
168 The split free border shapefile is written in the directory of the free border shapefile, with a suffix '_split'.
170 Below is the description of the dialog controls.
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).
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.
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.
187 QMessageBox.about(self, self.tr("About fit shape points to mesh edges dialog"), msg);
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" )
209 print(isMeshEdgesSplit)
210 print(isShapeToAdjustSplit)
211 fitShapePointsToMesh(meshEdges, shapeToAdjust, outdir, isMeshEdgesSplit, isShapeToAdjustSplit)
212 hydro_doc = HYDROData_Document.Document()
214 a = os.path.splitext(os.path.basename(meshEdges))
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, shapeName, isSplineMeshEdges, 0)
222 if isLoadShapeToAdjust:
223 a = os.path.splitext(os.path.basename(shapeToAdjust))
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, shapeName, isSplineShapeToAdjust, 0)
229 if salome.sg.hasDesktop():
230 salome.sg.updateObjBrowser()
238 def execDialog(context):
240 desktop = sgPyQt.getDesktop()
241 dlg = fitShapePointsToMeshEdgesDialog(desktop)