/
TaskPanel.py
436 lines (405 loc) · 17.6 KB
/
TaskPanel.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#***************************************************************************
#* *
#* Copyright (c) 2011, 2016 *
#* Jose Luis Cercos Pita <jlcercos@gmail.com> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import math
import FreeCAD as App
import FreeCADGui as Gui
import Units
from PySide import QtGui, QtCore
import PlotAux
import Tools
from shipUtils import Paths
import shipUtils.Units as USys
import shipUtils.Locale as Locale
class TaskPanel:
def __init__(self):
self.ui = Paths.modulePath() + "/shipGZ/TaskPanel.ui"
def accept(self):
if self.lc is None:
return False
self.save()
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.angle = self.widget(QtGui.QLineEdit, "Angle")
form.n_points = self.widget(QtGui.QSpinBox, "NumPoints")
form.var_trim = self.widget(QtGui.QCheckBox, "VariableTrim")
rolls = []
roll = Units.Quantity(Locale.fromString(
form.angle.text())).getValueAs('deg').Value
n_points = form.n_points.value()
for i in range(n_points):
rolls.append(roll * i / float(n_points - 1))
gzs, drafts, trims = Tools.solve(self.ship,
self.weights,
self.tanks,
rolls,
form.var_trim.isChecked())
PlotAux.Plot(rolls, gzs, drafts, trims)
return True
def reject(self):
return True
def clicked(self, index):
pass
def open(self):
pass
def needsFullSpace(self):
return True
def isAllowedAlterSelection(self):
return False
def isAllowedAlterView(self):
return True
def isAllowedAlterDocument(self):
return False
def helpRequested(self):
pass
def setupUi(self):
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.angle = self.widget(QtGui.QLineEdit, "Angle")
form.n_points = self.widget(QtGui.QSpinBox, "NumPoints")
form.var_trim = self.widget(QtGui.QCheckBox, "VariableTrim")
self.form = form
if self.initValues():
return True
self.retranslateUi()
def getMainWindow(self):
toplevel = QtGui.qApp.topLevelWidgets()
for i in toplevel:
if i.metaObject().className() == "Gui::MainWindow":
return i
raise RuntimeError("No main window found")
def widget(self, class_id, name):
"""Return the selected widget.
Keyword arguments:
class_id -- Class identifier
name -- Name of the widget
"""
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
return form.findChild(class_id, name)
def initValues(self):
""" Set initial values for fields
"""
# Look for selected loading conditions (Spreadsheets)
self.lc = None
selObjs = Gui.Selection.getSelection()
if not selObjs:
msg = QtGui.QApplication.translate(
"ship_console",
"A loading condition instance must be selected before using"
" this tool (no objects selected)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
for i in range(len(selObjs)):
obj = selObjs[i]
try:
if obj.TypeId != 'Spreadsheet::Sheet':
continue
except ValueError:
continue
# Check if it is a Loading condition:
# B1 cell must be a ship
# B2 cell must be the loading condition itself
doc = App.ActiveDocument
try:
if obj not in doc.getObjectsByLabel(obj.get('B2')):
continue
ships = doc.getObjectsByLabel(obj.get('B1'))
if len(ships) != 1:
if len(ships) == 0:
msg = QtGui.QApplication.translate(
"ship_console",
"Wrong Ship label! (no instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
obj.get('B1')))
else:
msg = QtGui.QApplication.translate(
"ship_console",
"Ambiguous Ship label! ({} instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
len(ships),
obj.get('B1')))
continue
ship = ships[0]
if ship is None or not ship.PropertiesList.index("IsShip"):
continue
except ValueError:
continue
# Extract the weights and the tanks
weights = []
index = 6
while True:
try:
ws = doc.getObjectsByLabel(obj.get('A{}'.format(index)))
except ValueError:
break
index += 1
if len(ws) != 1:
if len(ws) == 0:
msg = QtGui.QApplication.translate(
"ship_console",
"Wrong Weight label! (no instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
obj.get('A{}'.format(index - 1))))
else:
msg = QtGui.QApplication.translate(
"ship_console",
"Ambiguous Weight label! ({} instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
len(ws),
obj.get('A{}'.format(index - 1))))
continue
w = ws[0]
try:
if w is None or not w.PropertiesList.index("IsWeight"):
msg = QtGui.QApplication.translate(
"ship_console",
"Invalid Weight! (the object labeled as"
"'{}' is not a weight)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
len(ws),
obj.get('A{}'.format(index - 1))))
continue
except ValueError:
continue
weights.append(w)
tanks = []
index = 6
while True:
try:
ts = doc.getObjectsByLabel(obj.get('C{}'.format(index)))
dens = float(obj.get('D{}'.format(index)))
level = float(obj.get('E{}'.format(index)))
except ValueError:
break
index += 1
if len(ts) != 1:
if len(ts) == 0:
msg = QtGui.QApplication.translate(
"ship_console",
"Wrong Tank label! (no instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
obj.get('C{}'.format(index - 1))))
else:
msg = QtGui.QApplication.translate(
"ship_console",
"Ambiguous Tank label! ({} instances labeled as"
"'{}' found)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
len(ts),
obj.get('C{}'.format(index - 1))))
continue
t = ts[0]
try:
if t is None or not t.PropertiesList.index("IsTank"):
msg = QtGui.QApplication.translate(
"ship_console",
"Invalid Tank! (the object labeled as"
"'{}' is not a tank)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n'.format(
len(ws),
obj.get('C{}'.format(index - 1))))
continue
except ValueError:
continue
tanks.append((t, dens, level))
# Let's see if several loading conditions have been selected (and
# prompt a warning)
if self.lc:
msg = QtGui.QApplication.translate(
"ship_console",
"More than one loading condition have been selected (the"
" extra loading conditions will be ignored)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintWarning(msg + '\n')
break
self.lc = obj
self.ship = ship
self.weights = weights
self.tanks = tanks
if not self.lc:
msg = QtGui.QApplication.translate(
"ship_console",
"A loading condition instance must be selected before using"
" this tool (no valid loading condition found at the selected"
" objects)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
# We have a valid loading condition, let's set the initial field values
angle_format = USys.getAngleFormat()
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.angle = self.widget(QtGui.QLineEdit, "Angle")
form.n_points = self.widget(QtGui.QSpinBox, "NumPoints")
form.var_trim = self.widget(QtGui.QCheckBox, "VariableTrim")
form.angle.setText(Locale.toString(angle_format.format(90.0)))
# Try to use saved values
props = self.ship.PropertiesList
try:
props.index("GZAngle")
form.angle.setText(Locale.toString(angle_format.format(
self.ship.GZAngle.getValueAs(
USys.getAngleUnits()).Value)))
except:
pass
try:
props.index("GZNumPoints")
form.n_points.setValue(self.ship.GZNumPoints)
except ValueError:
pass
try:
props.index("GZVariableTrim")
if self.ship.GZVariableTrim:
form.var_trim.setCheckState(QtCore.Qt.Checked)
else:
form.var_trim.setCheckState(QtCore.Qt.Unchecked)
except ValueError:
pass
return False
def retranslateUi(self):
""" Set user interface locale strings. """
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.setWindowTitle(QtGui.QApplication.translate(
"ship_gz",
"Plot the GZ curve",
None,
QtGui.QApplication.UnicodeUTF8))
self.widget(QtGui.QLabel, "AngleLabel").setText(
QtGui.QApplication.translate(
"ship_gz",
"Maximum angle",
None,
QtGui.QApplication.UnicodeUTF8))
self.widget(QtGui.QLabel, "NumPointsLabel").setText(
QtGui.QApplication.translate(
"ship_gz",
"Number of points",
None,
QtGui.QApplication.UnicodeUTF8))
self.widget(QtGui.QCheckBox, "VariableTrim").setText(
QtGui.QApplication.translate(
"ship_gz",
"Variable trim",
None,
QtGui.QApplication.UnicodeUTF8))
self.widget(QtGui.QCheckBox, "VariableTrim").setToolTip(
QtGui.QApplication.translate(
"ship_gz",
"The ship will be rotated to the equilibrium trim angle for" + \
" each roll angle. It will significantly increase the" + \
" required computing time",
None,
QtGui.QApplication.UnicodeUTF8))
def save(self):
""" Saves the data into ship instance. """
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.angle = self.widget(QtGui.QLineEdit, "Angle")
form.n_points = self.widget(QtGui.QSpinBox, "NumPoints")
form.var_trim = self.widget(QtGui.QCheckBox, "VariableTrim")
angle = Units.Quantity(Locale.fromString(
form.angle.text())).getValueAs('deg').Value
n_points = form.n_points.value()
var_trim = form.var_trim.isChecked()
props = self.ship.PropertiesList
try:
props.index("GZAngle")
except ValueError:
try:
tooltip = str(QtGui.QApplication.translate(
"ship_gz",
"GZ curve tool angle selected [deg]",
None,
QtGui.QApplication.UnicodeUTF8))
except:
tooltip = "GZ curve tool angle selected [deg]"
self.ship.addProperty("App::PropertyAngle",
"GZAngle",
"Ship",
tooltip)
self.ship.GZAngle = '{} deg'.format(angle)
try:
props.index("GZNumPoints")
except ValueError:
try:
tooltip = str(QtGui.QApplication.translate(
"ship_gz",
"GZ curve tool number of points selected",
None,
QtGui.QApplication.UnicodeUTF8))
except:
tooltip = "GZ curve tool number of points selected"
self.ship.addProperty("App::PropertyInteger",
"GZNumPoints",
"Ship",
tooltip)
self.ship.GZNumPoints = n_points
try:
props.index("GZVariableTrim")
except ValueError:
try:
tooltip = str(QtGui.QApplication.translate(
"ship_gz",
"GZ curve tool variable trim angle selection",
None,
QtGui.QApplication.UnicodeUTF8))
except:
tooltip = "GZ curve tool variable trim angle selection"
self.ship.addProperty("App::PropertyBool",
"GZVariableTrim",
"Ship",
tooltip)
self.ship.GZVariableTrim = var_trim
def createTask():
panel = TaskPanel()
Gui.Control.showDialog(panel)
if panel.setupUi():
Gui.Control.closeDialog(panel)
return None
return panel