forked from geosim/QAD
/
qad_break_cmd.py
295 lines (245 loc) · 13.4 KB
/
qad_break_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
/***************************************************************************
QAD Quantum Aided Design plugin
comando SPEZZA per tagliare un oggetto
-------------------
begin : 2014-01-09
copyright : iiiii
email : hhhhh
developers : bbbbb aaaaa ggggg
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qad_generic_cmd import QadCommandClass
from qad_snapper import *
from qad_getpoint import *
from qad_textwindow import *
from qad_entsel_cmd import QadEntSelClass
from qad_msg import QadMsg
import qad_layer
# Classe che gestisce il comando BREAK
class QadBREAKCommandClass(QadCommandClass):
def instantiateNewCmd(self):
""" istanzia un nuovo comando dello stesso tipo """
return QadBREAKCommandClass(self.plugIn)
def getName(self):
return QadMsg.translate("Command_list", "SPEZZA")
def getEnglishName(self):
return "BREAK"
def connectQAction(self, action):
QObject.connect(action, SIGNAL("triggered()"), self.plugIn.runBREAKCommand)
def getIcon(self):
return QIcon(":/plugins/qad/icons/break.png")
def getNote(self):
# impostare le note esplicative del comando
return QadMsg.translate("Command_BREAK", "Divide un oggetto grafico.")
def __init__(self, plugIn):
QadCommandClass.__init__(self, plugIn)
self.EntSelClass = None
self.firstPt = None
self.secondPt = None
def __del__(self):
QadCommandClass.__del__(self)
if self.EntSelClass is not None:
self.EntSelClass.entity.deselectOnLayer()
del self.EntSelClass
def getPointMapTool(self, drawMode = QadGetPointDrawModeEnum.NONE):
if self.step == 1: # quando si é in fase di selezione entità
return self.EntSelClass.getPointMapTool(drawMode)
else:
return QadCommandClass.getPointMapTool(self, drawMode)
def waitForEntsel(self, msgMapTool, msg):
if self.EntSelClass is not None:
del self.EntSelClass
self.step = 1
self.EntSelClass = QadEntSelClass(self.plugIn)
self.EntSelClass.msg = QadMsg.translate("Command_BREAK", "Selezionare l'oggetto da spezzare: ")
# scarto la selezione di punti e poligoni
self.EntSelClass.checkPointLayer = False
self.EntSelClass.checkLineLayer = True
self.EntSelClass.checkPolygonLayer = False
self.EntSelClass.checkDimLayers = False
self.EntSelClass.onlyEditableLayers = True
self.EntSelClass.run(msgMapTool, msg)
#============================================================================
# breakFeatures
#============================================================================
def breakFeatures(self):
layer = self.EntSelClass.entity.layer
LineTempLayer = None
self.plugIn.beginEditCommand("Feature broken", layer)
tolerance2ApproxCurve = qad_utils.distMapToLayerCoordinates(QadVariables.get(QadMsg.translate("Environment variables", "TOLERANCE2APPROXCURVE")), \
self.plugIn.canvas,\
layer)
f = self.EntSelClass.entity.getFeature()
g = f.geometry()
if self.plugIn.canvas.mapRenderer().destinationCrs() != layer.crs():
# Trasformo i punti di break nel sistema di coordinate del layer
self.firstPt = self.mapToLayerCoordinates(layer, self.firstPt)
self.secondPt = self.mapToLayerCoordinates(layer, self.secondPt)
result = qad_utils.breakQgsGeometry(layer, f.geometry(), self.firstPt, self.secondPt, \
tolerance2ApproxCurve)
if result is not None:
line1 = result[0]
line2 = result[1]
atSubGeom = result[2]
if layer.geometryType() == QGis.Line:
updGeom = qad_utils.setSubGeom(f.geometry(), line1, atSubGeom)
if updGeom is None:
self.plugIn.destroyEditCommand()
return
brokenFeature1 = QgsFeature(f)
brokenFeature1.setGeometry(updGeom)
# plugIn, layer, feature, refresh, check_validity
if qad_layer.updateFeatureToLayer(self.plugIn, layer, brokenFeature1, False, False) == False:
self.plugIn.destroyEditCommand()
return
if line2 is not None:
brokenFeature2 = QgsFeature(f)
brokenFeature2.setGeometry(line2)
# plugIn, layer, feature, coordTransform, refresh, check_validity
if qad_layer.addFeatureToLayer(self.plugIn, layer, brokenFeature2, None, False, False) == False:
self.plugIn.destroyEditCommand()
return
else:
# aggiungo le linee nei layer temporanei di QAD
if LineTempLayer is None:
LineTempLayer = qad_layer.createQADTempLayer(self.plugIn, QGis.Line)
self.plugIn.addLayerToLastEditCommand("Feature broken", LineTempLayer)
lineGeoms = [line1]
if line2 is not None:
lineGeoms.append(line2)
# trasformo la geometria in quella dei layer temporanei
# plugIn, pointGeoms, lineGeoms, polygonGeoms, coord, refresh
if qad_layer.addGeometriesToQADTempLayers(self.plugIn, None, lineGeoms, None, layer.crs(), False) == False:
self.plugIn.destroyEditCommand()
return
updGeom = qad_utils.delSubGeom(f.geometry(), atSubGeom)
if updGeom is None or updGeom.isGeosEmpty(): # da cancellare
# plugIn, layer, feature id, refresh
if qad_layer.deleteFeatureToLayer(self.plugIn, layer, f.id(), False) == False:
self.plugIn.destroyEditCommand()
return
else:
brokenFeature1 = QgsFeature(f)
brokenFeature1.setGeometry(updGeom)
# plugIn, layer, feature, refresh, check_validity
if qad_layer.updateFeatureToLayer(self.plugIn, layer, brokenFeature1, False, False) == False:
self.plugIn.destroyEditCommand()
return
self.plugIn.endEditCommand()
def run(self, msgMapTool = False, msg = None):
if self.plugIn.canvas.mapRenderer().destinationCrs().geographicFlag():
self.showMsg(QadMsg.translate("QAD", "\nIl sistema di riferimento del progetto deve essere un sistema di coordinate proiettate.\n"))
return True # fine comando
if self.step == 0:
self.waitForEntsel(msgMapTool, msg)
return False # continua
#=========================================================================
# RISPOSTA ALLA SELEZIONE DI UN'ENTITA' (da step = 0)
elif self.step == 1:
if self.EntSelClass.run(msgMapTool, msg) == True:
if self.EntSelClass.entity.isInitialized():
layer = self.EntSelClass.entity.layer
self.firstPt = self.EntSelClass.point
self.plugIn.setLastPoint(self.firstPt)
keyWords = QadMsg.translate("Command_BREAK", "Primo punto")
prompt = QadMsg.translate("Command_BREAK", "Specificare secondo punto di interruzione o [{0}]: ").format(keyWords)
self.step = 2
self.getPointMapTool().refreshSnapType() # aggiorno lo snapType che può essere variato dal maptool di selezione entità
englishKeyWords = "First point"
keyWords += "_" + englishKeyWords
# si appresta ad attendere un punto o enter o una parola chiave
# msg, inputType, default, keyWords, nessun controllo
self.waitFor(prompt, \
QadInputTypeEnum.POINT2D | QadInputTypeEnum.KEYWORDS, \
None, \
keyWords, QadInputModeEnum.NONE)
return False
else:
self.showMsg(QadMsg.translate("Command_BREAK", "Non ci sono geometrie in questa posizione."))
self.waitForEntsel(msgMapTool, msg)
return False # continua
#=========================================================================
# RISPOSTA ALLA RICHIESTA DEL SECONDO PUNTO DI INTERRUZIONE (da step = 1)
elif self.step == 2: # dopo aver atteso un punto o una parola chiave si riavvia il comando
if msgMapTool == True: # il punto arriva da una selezione grafica
# la condizione seguente si verifica se durante la selezione di un punto
# é stato attivato un altro plugin che ha disattivato Qad
# quindi stato riattivato il comando che torna qui senza che il maptool
# abbia selezionato un punto
if self.getPointMapTool().point is None: # il maptool é stato attivato senza un punto
if self.getPointMapTool().rightButton == True: # se usato il tasto destro del mouse
pass # opzione di default "secondo punto"
else:
self.setMapTool(self.getPointMapTool()) # riattivo il maptool
return False
value = self.getPointMapTool().point
else: # il punto arriva come parametro della funzione
value = msg
if value is None or type(value) == unicode:
# si appresta ad attendere un punto
self.waitForPoint(QadMsg.translate("Command_BREAK", "Specificare primo punto di interruzione: "))
self.step = 3
elif type(value) == QgsPoint: # se é stato inserito il secondo punto
self.secondPt = value
self.plugIn.setLastPoint(self.secondPt)
self.breakFeatures()
return True
return False
#=========================================================================
# RISPOSTA ALLA RICHIESTA DEL PRIMO PUNTO DI INTERRUZIONE (da step = 2)
elif self.step == 3: # dopo aver atteso un punto si riavvia il comando
if msgMapTool == True: # il punto arriva da una selezione grafica
# la condizione seguente si verifica se durante la selezione di un punto
# é stato attivato un altro plugin che ha disattivato Qad
# quindi stato riattivato il comando che torna qui senza che il maptool
# abbia selezionato un punto
if self.getPointMapTool().point is None: # il maptool é stato attivato senza un punto
if self.getPointMapTool().rightButton == True: # se usato il tasto destro del mouse
return True # fine comando
else:
self.setMapTool(self.getPointMapTool()) # riattivo il maptool
return False
value = self.getPointMapTool().point
else: # il punto arriva come parametro della funzione
value = msg
self.firstPt = value
self.plugIn.setLastPoint(self.firstPt)
# si appresta ad attendere un punto
self.waitForPoint(QadMsg.translate("Command_BREAK", "Specificare secondo punto di interruzione: "))
self.step = 4
return False
#=========================================================================
# RISPOSTA ALLA RICHIESTA DEL SECONDO PUNTO DI INTERRUZIONE (da step = 3)
elif self.step == 4: # dopo aver atteso un punto si riavvia il comando
if msgMapTool == True: # il punto arriva da una selezione grafica
# la condizione seguente si verifica se durante la selezione di un punto
# é stato attivato un altro plugin che ha disattivato Qad
# quindi stato riattivato il comando che torna qui senza che il maptool
# abbia selezionato un punto
if self.getPointMapTool().point is None: # il maptool é stato attivato senza un punto
if self.getPointMapTool().rightButton == True: # se usato il tasto destro del mouse
return True # fine comando
else:
self.setMapTool(self.getPointMapTool()) # riattivo il maptool
return False
value = self.getPointMapTool().point
else: # il punto arriva come parametro della funzione
value = msg
self.secondPt = value
self.plugIn.setLastPoint(self.secondPt)
self.breakFeatures()
return True