/
TaskPanel.py
291 lines (264 loc) · 10.8 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
#***************************************************************************
#* *
#* 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 FreeCAD as App
import FreeCADGui as Gui
import Units
from PySide import QtGui, QtCore
import WeightInstance as Instance
from shipUtils import Paths
import shipUtils.Units as USys
import shipUtils.Locale as Locale
class TaskPanel:
def __init__(self):
"""Constructor"""
self.ui = Paths.modulePath() + "/shipCreateWeight/TaskPanel.ui"
def accept(self):
"""Create the ship instance"""
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.ship = self.widget(QtGui.QComboBox, "Ship")
form.weight = self.widget(QtGui.QLineEdit, "Weight")
# Create the object
ship = self.ships[form.ship.currentIndex()]
obj = App.ActiveDocument.addObject("Part::FeaturePython", "Weight")
weight = Instance.Weight(obj, self.shapes, ship)
Instance.ViewProviderWeight(obj.ViewObject)
# Set the mass/density
m_unit = USys.getMassUnits()
l_unit = USys.getLengthUnits()
qty = Units.parseQuantity(Locale.fromString(form.weight.text()))
if self.elem_type == 1:
w_unit = m_unit
obj.Mass = qty.getValueAs(w_unit).Value
elif self.elem_type == 2:
w_unit = m_unit + '/' + l_unit
obj.LineDens = qty.getValueAs(w_unit).Value
elif self.elem_type == 3:
w_unit = m_unit + '/' + l_unit + '^2'
obj.AreaDens = qty.getValueAs(w_unit).Value
elif self.elem_type == 4:
w_unit = m_unit + '/' + l_unit + '^3'
obj.Dens = qty.getValueAs(w_unit).Value
# Set it as a child of the ship
weights = ship.Weights[:]
weights.append(obj.Name)
ship.Weights = weights
ship.Proxy.cleanWeights(ship)
ship.Proxy.cleanTanks(ship)
ship.Proxy.cleanLoadConditions(ship)
App.ActiveDocument.recompute()
return True
def reject(self):
"""Cancel the job"""
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):
"""Create and configurate the user interface"""
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.ship = self.widget(QtGui.QComboBox, "Ship")
form.weight = self.widget(QtGui.QLineEdit, "Weight")
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 Exception("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):
"""Setup the initial values"""
# Ensure that there are at least one valid object to generate the
# weight
selObjs = Gui.Selection.getSelection()
self.shapes = []
if not selObjs:
msg = QtGui.QApplication.translate(
"ship_weight",
"Weight objects can only be created on top of its geometry"
" (no objects selected)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
for obj in selObjs:
try:
self.shapes.append(obj.Shape)
except:
continue
if not len(self.shapes):
msg = QtGui.QApplication.translate(
"ship_weight",
"No geometrical shapes found in the selected objects",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
# Get the element type
# 0 = unknow, 1 = vertex, 2 = line, 3 = face, 4 = solids
self.elem_type = 0
for shape in self.shapes:
# Doing it in this way we are protected under strange entities,
# and we are prepared to add higher level type of entities in the
# future, just in case...
try:
if len(shape.Solids):
self.elem_type = max(4, self.elem_type)
except:
pass
try:
if len(shape.Faces):
self.elem_type = max(3, self.elem_type)
except:
pass
try:
if len(shape.Edges):
self.elem_type = max(2, self.elem_type)
except:
pass
try:
if len(shape.Vertexes):
self.elem_type = max(1, self.elem_type)
except:
pass
# Could it happens???
if self.elem_type == 0:
msg = QtGui.QApplication.translate(
"ship_weight",
"Unknow object shapes selected",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
# Ensure as well that exist at least one valid ship to create the
# entity inside it
self.ships = []
for obj in App.ActiveDocument.Objects:
try:
if obj.IsShip:
self.ships.append(obj)
except:
continue
if not len(self.ships):
msg = QtGui.QApplication.translate(
"ship_weight",
"There are not ship objects to create weights into them",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return True
# Fill the ships combo box
mw = self.getMainWindow()
form = mw.findChild(QtGui.QWidget, "TaskPanel")
form.ship = self.widget(QtGui.QComboBox, "Ship")
form.weight = self.widget(QtGui.QLineEdit, "Weight")
icon = QtGui.QIcon(QtGui.QPixmap(":/icons/Ship_Instance.svg"))
form.ship.clear()
for ship in self.ships:
form.ship.addItem(icon, ship.Label)
form.ship.setCurrentIndex(0)
# Initialize the 0 mass/density string field
m_unit = USys.getMassUnits()
l_unit = USys.getLengthUnits()
if self.elem_type == 1:
w_unit = m_unit
elif self.elem_type == 2:
w_unit = m_unit + '/' + l_unit
elif self.elem_type == 3:
w_unit = m_unit + '/' + l_unit + '^2'
elif self.elem_type == 4:
w_unit = m_unit + '/' + l_unit + '^3'
form.weight.setText('0 ' + w_unit)
return False
def retranslateUi(self):
"""Set the user interface locale strings."""
self.form.setWindowTitle(QtGui.QApplication.translate(
"ship_weight",
"Create a new weight",
None,
QtGui.QApplication.UnicodeUTF8))
self.widget(QtGui.QLabel, "ShipLabel").setText(
QtGui.QApplication.translate(
"ship_weight",
"Ship",
None,
QtGui.QApplication.UnicodeUTF8))
if self.elem_type == 1:
self.widget(QtGui.QLabel, "WeightLabel").setText(
QtGui.QApplication.translate(
"ship_weight",
"Mass",
None,
QtGui.QApplication.UnicodeUTF8))
elif self.elem_type == 2:
self.widget(QtGui.QLabel, "WeightLabel").setText(
QtGui.QApplication.translate(
"ship_weight",
"Linear density",
None,
QtGui.QApplication.UnicodeUTF8))
elif self.elem_type == 3:
self.widget(QtGui.QLabel, "WeightLabel").setText(
QtGui.QApplication.translate(
"ship_weight",
"Area density",
None,
QtGui.QApplication.UnicodeUTF8))
elif self.elem_type == 4:
self.widget(QtGui.QLabel, "WeightLabel").setText(
QtGui.QApplication.translate(
"ship_weight",
"Density",
None,
QtGui.QApplication.UnicodeUTF8))
def createTask():
panel = TaskPanel()
Gui.Control.showDialog(panel)
if panel.setupUi():
Gui.Control.closeDialog(panel)
return None
return panel