/
__init__.py
192 lines (175 loc) · 8.28 KB
/
__init__.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
#***************************************************************************
#* *
#* 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 Spreadsheet
READ_ONLY_FOREGROUND = (0.5, 0.5, 0.5)
READ_ONLY_BACKGROUND = (0.9, 0.9, 0.9)
def load():
"""Directly create the load condition"""
# Check that a ship has been selected
ship = None
selObjs = Gui.Selection.getSelection()
if not selObjs:
msg = QtGui.QApplication.translate(
"ship_console",
"A ship instance must be selected before using this tool (no"
" objects selected)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return
for i in range(len(selObjs)):
obj = selObjs[i]
props = obj.PropertiesList
try:
props.index("IsShip")
except ValueError:
continue
if obj.IsShip:
if ship:
msg = QtGui.QApplication.translate(
"ship_console",
"More than one ship have been selected (the extra"
" ships will be ignored)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintWarning(msg + '\n')
break
ship = obj
if not ship:
msg = QtGui.QApplication.translate(
"ship_console",
"A ship instance must be selected before using this tool (no"
" valid ship found at the selected objects)",
None,
QtGui.QApplication.UnicodeUTF8)
App.Console.PrintError(msg + '\n')
return
# Create the spreadsheet
s = App.activeDocument().addObject('Spreadsheet::Sheet',
'LoadCondition')
# Add a description
s.setForeground('A1:B2', READ_ONLY_FOREGROUND)
s.setBackground('A1:B2', READ_ONLY_BACKGROUND)
s.setAlignment('B1:B2', 'center', 'keep')
s.setStyle('B1:B2', 'italic', 'add')
s.set("A1", "Ship:")
s.set("A2", "Load condition:")
s.set("B1", "=" + ship.Name + ".Label")
s.set("B2", "=Label")
# Add the weights data
s.setAlignment('A4:A5', 'center', 'keep')
s.setStyle('A4:A5', 'bold', 'add')
s.setStyle('A4:A5', 'underline', 'add')
s.set("A4", "WEIGHTS DATA")
s.set("A5", "name")
for i in range(len(ship.Weights)):
weight = App.activeDocument().getObject(ship.Weights[i])
s.set("A{}".format(i + 6), "=" + weight.Name + ".Label")
s.setForeground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_FOREGROUND)
s.setBackground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_BACKGROUND)
# Add the tanks data
s.mergeCells('C4:E4')
s.setForeground('C4:E5', READ_ONLY_FOREGROUND)
s.setBackground('C4:E5', READ_ONLY_BACKGROUND)
s.setAlignment('C4:E5', 'center', 'keep')
s.setStyle('C4:E5', 'bold', 'add')
s.setStyle('C4:E5', 'underline', 'add')
s.set("C4", "TANKS DATA")
s.set("C5", "name")
s.set("D5", "Fluid density [kg/m^3]")
s.set("E5", "Filling ratio (interval [0, 1])")
if len(ship.Tanks):
for i in range(len(ship.Tanks)):
tank = App.activeDocument().getObject(ship.Tanks[i])
s.set("C{}".format(i + 6), "=" + tank.Name + ".Label")
s.set("D{}".format(i + 6), "998.0")
s.set("E{}".format(i + 6), "0.0")
s.setForeground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_FOREGROUND)
s.setBackground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_BACKGROUND)
s.setColumnWidth('A', 128)
s.setColumnWidth('B', 128)
s.setColumnWidth('C', 128)
s.setColumnWidth('D', 150)
s.setColumnWidth('E', 200)
"""
# Add a reference to the owner ship
s.mergeCells('A1:D1')
s.setAlignment('A1:B2', 'center', 'keep')
s.setStyle('A1:B2', 'bold', 'add')
s.setStyle('A1:B2', 'underline', 'add')
s.set("A1", "SHIP DATA")
s.set("A2", "ship")
s.set("A3", ship.Label)
s.set("B2", "internal ref")
s.set("B3", ship.Name)
s.setForeground('A1:B3', (0.5,0.5,0.5))
# Clean the Ship instance before generating the load condition
ship.Proxy.cleanWeights(ship)
ship.Proxy.cleanTanks(ship)
# Add the weights data
s.mergeCells('A4:D4')
s.setAlignment('A4:B5', 'center', 'keep')
s.setStyle('A4:B5', 'bold', 'add')
s.setStyle('A4:B5', 'underline', 'add')
s.set("A4", "WEIGHTS DATA")
s.set("A5", "weight")
s.set("B5", "internal ref")
for i in range(len(ship.Weights)):
weight = App.activeDocument().getObject(ship.Weights[i])
s.set("A{}".format(i + 6), weight.Label)
s.set("B{}".format(i + 6), weight.Name)
s.setForeground('A4:B{}'.format(5 + len(ship.Weights)), (0.5,0.5,0.5))
# Add the tanks data
s.mergeCells('A{0}:D{0}'.format(6 + len(ship.Weights)))
s.setAlignment('A{0}:A{0}'.format(6 + len(ship.Weights)), 'center', 'keep')
s.setAlignment('A{0}:D{0}'.format(7 + len(ship.Weights)), 'center', 'keep')
s.setStyle('A{0}:A{0}'.format(6 + len(ship.Weights)), 'bold', 'add')
s.setStyle('A{0}:D{0}'.format(7 + len(ship.Weights)), 'bold', 'add')
s.setStyle('A{0}:A{0}'.format(6 + len(ship.Weights)), 'underline', 'add')
s.setStyle('A{0}:D{0}'.format(7 + len(ship.Weights)), 'underline', 'add')
s.set("A{}".format(6 + len(ship.Weights)), "TANKS DATA")
s.set("A{}".format(7 + len(ship.Weights)), "tank")
s.set("B{}".format(7 + len(ship.Weights)), "internal ref")
s.set("C{}".format(7 + len(ship.Weights)), "Fluid density [kg/m^3]")
s.set("D{}".format(7 + len(ship.Weights)), "Filling ratio (interval [0.0,1.0])")
for i in range(len(ship.Tanks)):
tank = App.activeDocument().getObject(ship.Tanks[i])
s.set("A{}".format(i + 8 + len(ship.Weights)), tank.Label)
s.set("B{}".format(i + 8 + len(ship.Weights)), tank.Name)
s.set("C{}".format(i + 8 + len(ship.Weights)), "998.0")
s.set("D{}".format(i + 8 + len(ship.Weights)), "0.0")
s.setForeground('A{0}:A{0}'.format(6 + len(ship.Weights)), (0.5,0.5,0.5))
s.setForeground('A{0}:D{0}'.format(7 + len(ship.Weights)), (0.5,0.5,0.5))
s.setForeground('A{}:B{}'.format(8 + len(ship.Weights),
8 + len(ship.Weights) + len(ship.Tanks)),
(0.5,0.5,0.5))
"""
# Add the spreadsheet to the list of loading conditions of the ship
lcs = ship.LoadConditions[:]
lcs.append(s.Name)
ship.LoadConditions = lcs
ship.Proxy.cleanLoadConditions(ship)
# Recompute to take the changes
App.activeDocument().recompute()