-
Notifications
You must be signed in to change notification settings - Fork 2
/
BracingScheme.py
376 lines (291 loc) · 14.5 KB
/
BracingScheme.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
from PyQt5.QtCore import * # core Qt functionality
from PyQt5.QtWidgets import *
from PyQt5.QtGui import * # extends QtCore with GUI functionality
from PyQt5.QtOpenGL import * # provides QGLWidget, a special OpenGL QWidget
from PyQt5 import uic
import sys # We need sys so that we can pass argv to QApplication
import os
import copy
import resources # For icons and UIs
from Model import * # import Model to access tower objects
from View2DEngine import *
from Definition import * # import constants from Definition
from Message import *
class BracingScheme(QDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Load the UI Page
fileh = QFile(':/UI/autobuilder_bracingscheme_v1.ui', self)
fileh.open(QFile.ReadOnly)
uic.loadUi(fileh, self)
fileh.close()
# Set UI Elements
self.setIconsForButtons()
self.setOkandCancelButtons()
# Add Empty Row to List of Bracing Schemes
self.addBracingSchemeButton.clicked.connect(self.addBracingScheme)
# Delete Selected Row from List of Bracing Schemes
self.deleteBracingSchemeButton.clicked.connect(self.deleteBracingScheme)
# Update whole screen upon picking bracing scheme
self.bracingSchemeTable.itemClicked.connect(self.updateScreen)
# Add Empty Row to List of Bracing Coordinates
self.addCoordButton.clicked.connect(self.addBracingCoord)
# Delete Selected Row from List of Bracing Coordinates
self.deleteCoordButton.clicked.connect(self.deleteBracingCoord)
# Update XY Coord Table upon SELECTION change
# ie. change is only saved when user clicks on a different cell after editing
self.bracingCoordTable.itemSelectionChanged.connect(self.updateCoord)
# Passing in main.tower into bracing scheme
self.towerRef = args[0].tower
# Passing in main.projectSettingsData into bracing scheme
self.projectSettingsData = args[0].projectSettingsData
# Create copy of tower to reassign if user saves
self.tower = copy.deepcopy(args[0].tower)
# Defined sections
self.sections = list(self.projectSettingsData.sections.keys())
# Fill in bracing scheme table
self.Populate()
# Save the current bracing name if it exists
if self.bracingSchemeTable.item(0,0) is not None:
self.currentBracingName = self.bracingSchemeTable.item(0,0).text()
else:
self.currentBracingName = None
# change Name in main table after a name is assigned in coord table
self.bracingSchemeTable.cellChanged.connect(self.nameChange)
# Update 2D view constantly
timer = QTimer(self)
timer.timeout.connect(self.set2DViewDimension)
timer.timeout.connect(self.updateBracingView)
timer.timeout.connect(self.bracingSchemeViewer.update)
timer.start()
def setIconsForButtons(self):
self.addBracingSchemeButton.setIcon(QIcon(':/Icons/plus.png'))
self.deleteBracingSchemeButton.setIcon(QIcon(':/Icons/minus.png'))
self.addCoordButton.setIcon(QIcon(':/Icons/plus.png'))
self.deleteCoordButton.setIcon(QIcon(':/Icons/minus.png'))
def setOkandCancelButtons(self):
'''OK and Cancel button both exit dialog but have no save function!'''
self.OkButton = self.bracingSchemeButtonBox.button(QDialogButtonBox.Ok)
self.OkButton.clicked.connect(self.saveBracingSchemes)
self.OkButton.clicked.connect(lambda x: self.close())
self.CancelButton = self.bracingSchemeButtonBox.button(QDialogButtonBox.Cancel)
self.CancelButton.clicked.connect(lambda x: self.close())
def saveBracingSchemes(self):
'''Overwrite the tower linked to the main model'''
# ensure everything is updated before saving
self.updateCoord()
self.towerRef.bracings = self.tower.bracings
def addBracingScheme(self):
'''add bracing and clear coord properties '''
# Reset -----------------------------------
# empty coord. table
self.bracingCoordTable.setRowCount(0)
# clear bracing name
self.bracingNameEdit.clear()
# Initialize new bracing scheme with at least one node pairs --------------------
newBracing = Bracing()
newBracing.members, newBracing.nodePairs, newBracing.materials = [], [], []
newBracing.nodePairs.append([Node(0,0), Node(0,0)])
newBracing.materials.append(self.sections[0])
# Generate members
newBracing.generateMembersfromNodes()
id = 1
newBracing.name = "New Bracing " + str(id)
while newBracing.name in self.tower.bracings:
id += 1
newBracing.name = "New Bracing " + str(id)
self.tower.addBracing(newBracing)
# Refresh
self.Populate()
self.bracingSchemeTable.selectRow(self.bracingSchemeTable.rowCount()-1)
self.updateScreen()
def deleteBracingScheme(self):
'''delete bracing and associated coord properties'''
# remove selected rows from table
indices = self.bracingSchemeTable.selectionModel().selectedRows()
for i, index in enumerate(sorted(indices)):
# remove from dictionary
updatedRow = index.row()-i
bcName = self.bracingSchemeTable.item(updatedRow,index.column()).text()
self.tower.bracings.pop(bcName, None)
# remove row from table
self.bracingSchemeTable.removeRow(updatedRow)
# empty coord. table
self.bracingCoordTable.setRowCount(0)
# clear bracing name
self.bracingNameEdit.clear()
def updateScreen(self):
'''Update BracingCoordTable'''
# save when switching between bracing schemes
self.updateCoord()
# clear selections in coord table to avoid error
self.bracingCoordTable.clearSelection()
# if switching to an existing bracing
if self.currentBracingName is not None:
self.bracingCoordTable.setRowCount(0)
X1 = 0
Y1 = 1
X2 = 2
Y2 = 3
mat = 4
row = self.bracingSchemeTable.currentRow()
bcName = self.bracingSchemeTable.item(row,0)
bracing = self.tower.bracings[bcName.text()]
self.currentBracingName = bcName.text()
# Fill node coordinate table
for rows, member in enumerate(bracing.members):
sNode = member.start_node
eNode = member.end_node
self.bracingCoordTable.insertRow(self.bracingCoordTable.rowCount())
self.bracingCoordTable.setItem(rows, X1, QTableWidgetItem(str(sNode.x)))
self.bracingCoordTable.setItem(rows, Y1, QTableWidgetItem(str(sNode.y)))
self.bracingCoordTable.setItem(rows, X2, QTableWidgetItem(str(eNode.x)))
self.bracingCoordTable.setItem(rows, Y2, QTableWidgetItem(str(eNode.y)))
# make drop-down menu for brace material
materialCombo = QComboBox()
for m in self.sections:
materialCombo.addItem(m)
self.bracingCoordTable.setCellWidget(rows,mat,materialCombo)
# In case of corrupted file
if member.material in self.sections:
materialCombo.setCurrentText(member.material)
else:
materialCombo.setCurrentText(self.sections[0])
# match bracing name above coord. table to main table
self.bracingNameEdit.setText(self.bracingSchemeTable.item(row,0).text())
def addBracingCoord(self):
''' Add empty row to coordinate table'''
row = self.bracingCoordTable.rowCount()
self.bracingCoordTable.insertRow(row)
self.bracingCoordTable.setItem(row, 0, QTableWidgetItem(str(0)))
self.bracingCoordTable.setItem(row, 1, QTableWidgetItem(str(0)))
self.bracingCoordTable.setItem(row, 2, QTableWidgetItem(str(0)))
self.bracingCoordTable.setItem(row, 3, QTableWidgetItem(str(0)))
# make drop-down menu for brace material
materialCombo = QComboBox()
for m in self.sections:
materialCombo.addItem(m)
self.bracingCoordTable.setCellWidget(row,4,materialCombo)
materialCombo.setCurrentText(self.sections[0])
self.updateCoord()
def deleteBracingCoord(self):
''' Delete selected rows from coordinate table'''
indices = self.bracingCoordTable.selectionModel().selectedRows()
# clear selections in coord table to avoid error
self.bracingCoordTable.clearSelection()
for i, index in enumerate(sorted(indices)):
updatedRow = index.row()-i
self.bracingCoordTable.removeRow(updatedRow)
self.updateCoord()
def updateCoord(self):
'''Update coordinates and material associated with current bracing'''
# problematic when currname isn't defined?
currName = self.bracingNameEdit.toPlainText()
if currName != "":
newBracing = self.tower.bracings[currName]
# delete all existing definitions
newBracing.members = []
newBracing.nodePairs = []
newBracing.materials = []
for row in range(self.bracingCoordTable.rowCount()):
# changed from 1,2,3,4
x1 = self.bracingCoordTable.item(row, 0).text()
y1 = self.bracingCoordTable.item(row, 1).text()
x2 = self.bracingCoordTable.item(row, 2).text()
y2 = self.bracingCoordTable.item(row, 3).text()
coords = Algebra.strToFloat(x1, y1, x2, y2)
if not coords:
warning = WarningMessage()
warning.popUpErrorBox('Coordinates must only contain numbers or following operators: "+", "-", "*", "/"')
self.bracingCoordTable.setItem(row, 0, QTableWidgetItem('0'))
self.bracingCoordTable.setItem(row, 1, QTableWidgetItem('0'))
self.bracingCoordTable.setItem(row, 2, QTableWidgetItem('0'))
self.bracingCoordTable.setItem(row, 3, QTableWidgetItem('0'))
return
try:
material = str(self.bracingCoordTable.cellWidget(row,4).currentText())
except:
material = self.sections[0]
self.bracingCoordTable.setItem(row, 0, QTableWidgetItem(str(coords[0])))
self.bracingCoordTable.setItem(row, 1, QTableWidgetItem(str(coords[1])))
self.bracingCoordTable.setItem(row, 2, QTableWidgetItem(str(coords[2])))
self.bracingCoordTable.setItem(row, 3, QTableWidgetItem(str(coords[3])))
node1 = Node(coords[0], coords[1])
node2 = Node(coords[2], coords[3])
newBracing.nodePairs.append([node1, node2])
newBracing.materials.append(material)
# generate members
newBracing.generateMembersfromNodes()
def Populate(self):
'''Add existing bracing schemes to bracingSchemeTable'''
column = 0
self.bracingSchemeTable.setRowCount(0)
for row, bracingScheme in enumerate(self.tower.bracings):
self.bracingSchemeTable.insertRow(self.bracingSchemeTable.rowCount())
bcItem = QTableWidgetItem(bracingScheme)
self.bracingSchemeTable.setItem(row, column, bcItem)
def nameChange(self):
''' change bracing name after it is defined in main table '''
row = self.bracingSchemeTable.currentRow() #returns -1 when repopulating empty table
if row != -1 and self.currentBracingName != None:
bcName = self.bracingSchemeTable.item(row,0).text()
warning = WarningMessage()
if bcName == '':
warning.popUpErrorBox('Bracing name is missing.')
self.bracingSchemeTable.item(row,0).setText(self.currentBracingName)
return
if bcName in self.tower.bracings:
warning.popUpErrorBox('Bracing name already exists.')
self.bracingSchemeTable.item(row,0).setText(self.currentBracingName)
return
bracing = self.tower.bracings[self.currentBracingName]
# update bracing name to match changed cell
bracing.name = bcName
# assign original coordinates to new bracing name
self.tower.bracings[bcName] = self.tower.bracings.pop(self.currentBracingName)
# match bracing name above coord. table to main table
self.bracingNameEdit.setText(bcName)
self.updateScreen()
def updateBracingView(self):
# update only when at least 1 bracing has been defined
if not self.tower.bracings:
return
vMembers, vNodes = self.View2DBracings()
self.bracingSchemeViewer.reset()
for vMember in vMembers:
self.bracingSchemeViewer.addMember(vMember)
for vNode in vNodes:
self.bracingSchemeViewer.addNode(vNode)
def View2DBracings(self):
renderX = 1.0 # scaled to 1.0x regardless of project setting
renderY = 1.0
vMembers = []
vNodes = []
color_bracing = Color.Member['Bracing']
color_node = Color.Node['Bracing']
vMember = ViewMember()
vNode = ViewNode()
# Set View Objects attributes
vMember.setColor(color_bracing)
vMember.setSize(View2DConstants.MEMBER_SIZE)
vMember.setDimX(renderX)
vMember.setDimY(renderY)
vNode.setColor(color_node)
vNode.setSize(View2DConstants.NODE_SIZE)
vNode.setDimX(renderX)
vNode.setDimY(renderY)
currName = self.bracingNameEdit.toPlainText()
if currName in self.tower.bracings.keys():
bracing = self.tower.bracings[currName]
for member in bracing.members:
vMember.addMember(member)
vNode.addNode(member.start_node)
vNode.addNode(member.end_node)
vMembers.append(vMember)
vNodes.append(vNode)
return vMembers, vNodes
def set2DViewDimension(self):
'''scale bracing based on project settings'''
size = self.bracingSchemeViewer.size()
self.bracingSchemeViewer.dimension_x = size.width()
self.bracingSchemeViewer.dimension_y = size.height()