-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileWriter.py
793 lines (628 loc) · 24.5 KB
/
FileWriter.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import Model # contains tower design components
import ProjectSettings # contains data in project settings
import DisplaySettings # contains data in display settings
from Definition import * # file extensions, EnumToString conversion
import pandas as pd # use data frame to write files
from Performance import * # tower performance data
from Message import * # pop up window for illegal entries
from csv import writer
import os # create new directory
class FileWriter:
def __init__(self, fileLoc, tower=None, psData=None):
self.mainFileLoc = fileLoc
self.folderLoc = fileLoc.replace('.ab', '')
self.tower = tower
self.psData = psData
# Create new directory for data
try:
os.mkdir(self.folderLoc)
except:
pass
def writeFiles(self):
''' Wrapper function to write all files '''
self.writeProjectSettings()
self.writeDisplaySettings()
self.writeFloorPlans()
self.writeFloors()
self.writePanels()
self.writeBracings()
self.writeBracingGroups()
self.writeSectionGroups()
self.writeAreaSectionGroups()
self.writeBracingAssignments()
self.writeSectionAssignments()
self.writeAreaSectionAssignments()
self.writeMainFile()
def writeMainFile(self):
psettings = ''
projectSettingsLoc = self.folderLoc + FileExtension.projectSettings
with open(projectSettingsLoc, 'r') as f:
psettings = f.read()
dsettings = ''
displaySettingsLoc = self.folderLoc + FileExtension.displaySettings
with open(displaySettingsLoc, 'r') as f:
dsettings = f.read()
floorPlan = ''
floorPlanLoc = self.folderLoc + FileExtension.floorPlan
with open(floorPlanLoc, 'r') as f:
floorPlan = f.read()
floor = ''
floorLoc = self.folderLoc + FileExtension.floor
with open(floorLoc, 'r') as f:
floor = f.read()
panel = ''
panelLoc = self.folderLoc + FileExtension.panel
with open(panelLoc, 'r') as f:
panel = f.read()
bracings = ''
bracingsLoc = self.folderLoc + FileExtension.bracings
with open(bracingsLoc, 'r') as f:
bracings = f.read()
bGroups = ''
bGroupsLoc = self.folderLoc + FileExtension.bracingGroups
with open(bGroupsLoc, 'r') as f:
bGroups = f.read()
sGroups = ''
sGroupsLoc = self.folderLoc + FileExtension.sectionGroups
with open(sGroupsLoc, 'r') as f:
sGroups = f.read()
asGroups = ''
asGroupsLoc = self.folderLoc + FileExtension.areaSectionGroups
with open(asGroupsLoc, 'r') as f:
asGroups = f.read()
bAssignments = ''
bAssignmentsLoc = self.folderLoc + FileExtension.bracingAssignments
with open(bAssignmentsLoc, 'r') as f:
bAssignments = f.read()
sAssignments = ''
sAssignmentsLoc = self.folderLoc + FileExtension.sectionAssignments
with open(sAssignmentsLoc, 'r') as f:
sAssignments = f.read()
asAssignments = ''
asAssignmentsLoc = self.folderLoc + FileExtension.areaSectionAssignments
with open(asAssignmentsLoc, 'r') as f:
asAssignments = f.read()
headers = [
MFHeader.projectSettings,
MFHeader.displaySettings,
MFHeader.floorPlans,
MFHeader.floors,
MFHeader.panels,
MFHeader.bracings,
MFHeader.bracingGroups,
MFHeader.sectionGroups,
MFHeader.areaSectionGroups,
MFHeader.bracingAssignments,
MFHeader.sectionAssignments,
MFHeader.areaSectionAssignments,
]
datasets = [
psettings,
dsettings,
floorPlan,
floor,
panel,
bracings,
bGroups,
sGroups,
asGroups,
bAssignments,
sAssignments,
asAssignments,
]
with open(self.mainFileLoc, 'w') as mainFile:
for i, header in enumerate(headers):
mainFile.write(header)
mainFile.write('\n')
mainFile.write(datasets[i])
if i == len(headers)-1: # avoid the unnecessary new line at the end
break
mainFile.write('\n')
# TODO: move all strings to Definition module
def writeProjectSettings(self):
''' Write project settings data to file '''
projectSettingsLoc = self.folderLoc + FileExtension.projectSettings
psData = self.psData
psData_dict = {
'setting':[],
'value':[],
}
# Convert to string
# elevations
elevs = ' '.join([str(elev) for elev in psData.floorElevs])
psData_dict['setting'].append('tower_elevs')
psData_dict['value'].append(elevs)
# sections
for key in psData.sections:
sect = psData.sections[key]
psData_dict['setting'].append('sect_props')
val = str(sect.name) + ' ' + str(sect.rank)
psData_dict['value'].append(val)
# area sections
for key in psData.areaSections:
sect = psData.areaSections[key]
psData_dict['setting'].append('area_sect_props')
val = str(sect.name) + ' ' + str(sect.rank)
psData_dict['value'].append(val)
# Other settings
psKeys = [
# Project Settings
'gm',
'analysis',
'modelLoc',
'modelName',
'renderX',
'renderY',
'renderZ',
'tensileStrength',
'compressiveStrength',
'shearStrength',
# Run Towers
'SAPPath',
'nodesList',
'footprint',
'totalHeight',
'totalMass',
'forceReductionFactor',
'gmIdentifier',
'memberUtilizationId',
'membersList',
'keepExistingMembers',
'divideAllMembersAtIntersections',
'toRun',
'centreOfRigidity',
]
values = [
psData.groundMotion,
EnumToString.ATYPE[psData.analysisType],
psData.SAPModelLoc,
psData.modelName,
psData.renderX,
psData.renderY,
psData.renderZ,
psData.tensileStrength,
psData.compressiveStrength,
psData.shearStrength,
psData.SAPPath,
' '.join(psData.nodesList),
psData.footprint,
psData.totalHeight,
psData.totalMass,
psData.forceReductionFactor,
psData.gmIdentifier,
psData.memberUtilizationId,
' '.join(psData.membersList),
psData.keepExistingMembers,
psData.divideAllMembersAtIntersections,
psData.toRun,
EnumToString.CRTYPE[psData.centreOfRigidity],
]
for i, psKey in enumerate(psKeys):
psData_dict['setting'].append(psKey)
psData_dict['value'].append(values[i])
df = pd.DataFrame(psData_dict)
df.to_csv(projectSettingsLoc, index=False)
try:
df.to_csv(projectSettingsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to create project settings')
def writeDisplaySettings(self):
''' Write display settings to file '''
displaySettingsLoc = self.folderLoc + FileExtension.displaySettings
dSettings = self.tower.displaySettings
dSettings_dict = {
'setting':[],
'value':[],
}
dsKeys = [
'2D_pName',
'2D_pLength',
]
values = [
dSettings.pName,
dSettings.pLength,
]
for i, dsKey in enumerate(dsKeys):
dSettings_dict['setting'].append(dsKey)
dSettings_dict['value'].append(values[i])
df = pd.DataFrame(dSettings_dict)
df.to_csv(displaySettingsLoc, index=False)
try:
df.to_csv(displaySettingsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to create display settings')
def writeFloorPlans(self):
''' Write floor plans to file '''
floorPlanLoc = self.folderLoc + FileExtension.floorPlan
tower = self.tower
floorPlans = tower.floorPlans
floorPlanData = {
'floorPlanName':[],
'x':[],
'y':[],
'bot':[],
'top':[],
'start':[],
'end':[],
}
for fpName in floorPlans:
floorPlan = floorPlans[fpName]
botConnections = [0 for i in range(len(floorPlan.nodes))]
for botLabel in floorPlan.bottomConnections:
for index in floorPlan.bottomConnections[botLabel]:
botConnections[index] = botLabel
topConnections = [0 for i in range(len(floorPlan.nodes))]
for topLabel in floorPlan.topConnections:
for index in floorPlan.topConnections[topLabel]:
topConnections[index] = topLabel
for i, node in enumerate(floorPlan.nodes):
floorPlanData['floorPlanName'].append(str(fpName))
floorPlanData['x'].append(str(node.x))
floorPlanData['y'].append(str(node.y))
floorPlanData['bot'].append(str(botConnections[i]))
floorPlanData['top'].append(str(topConnections[i]))
floorPlanData['start'].append(Constants.filler)
floorPlanData['end'].append(Constants.filler)
for start, end in floorPlan.nodePairs:
floorPlanData['floorPlanName'].append(str(fpName))
floorPlanData['x'].append(Constants.filler)
floorPlanData['y'].append(Constants.filler)
floorPlanData['bot'].append(Constants.filler)
floorPlanData['top'].append(Constants.filler)
floorPlanData['start'].append(str(start))
floorPlanData['end'].append(str(end))
df = pd.DataFrame(floorPlanData)
try:
df.to_csv(floorPlanLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to create floor plan')
def writeFloors(self):
''' Write floors to file '''
floorLoc = self.folderLoc + FileExtension.floor
floors = self.tower.floors
floorData = {
'elevation':[],
'floorPlans':[],
'comX':[],
'comY':[],
}
for elev in floors:
floor = floors[elev]
fpNames = ' '.join([str(fp.name) for fp in floor.floorPlans])
comX = floor.comX
comY = floor.comY
floorData['elevation'].append(str(elev))
floorData['floorPlans'].append(fpNames)
floorData['comX'].append(str(comX))
floorData['comY'].append(str(comY))
df = pd.DataFrame(floorData)
try:
df.to_csv(floorLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save floor')
def writePanels(self):
''' Write panels to file '''
panelLoc = self.folderLoc + FileExtension.panel
tower = self.tower
panels = tower.panels
panelData = {
'panelName':[],
'nodeLocation':[],
'x':[],
'y':[],
'z':[],
}
for pName in panels:
panel = panels[pName]
panelData['panelName'].append(str(pName))
panelData['nodeLocation'].append('lowerLeft')
panelData['x'].append(str(panel.lowerLeft.x))
panelData['y'].append(str(panel.lowerLeft.y))
panelData['z'].append(str(panel.lowerLeft.z))
panelData['panelName'].append(str(pName))
panelData['nodeLocation'].append('upperLeft')
panelData['x'].append(str(panel.upperLeft.x))
panelData['y'].append(str(panel.upperLeft.y))
panelData['z'].append(str(panel.upperLeft.z))
panelData['panelName'].append(str(pName))
panelData['nodeLocation'].append('upperRight')
panelData['x'].append(str(panel.upperRight.x))
panelData['y'].append(str(panel.upperRight.y))
panelData['z'].append(str(panel.upperRight.z))
panelData['panelName'].append(str(pName))
panelData['nodeLocation'].append('lowerRight')
panelData['x'].append(str(panel.lowerRight.x))
panelData['y'].append(str(panel.lowerRight.y))
panelData['z'].append(str(panel.lowerRight.z))
df = pd.DataFrame(panelData)
try:
df.to_csv(panelLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save panels')
def writeBracings(self):
''' Write Bracings to file '''
bracingsLoc = self.folderLoc + FileExtension.bracings
tower = self.tower
bracings = tower.bracings
bracingsData = {
'bracingName':[],
'x1':[],
'y1':[],
'x2':[],
'y2':[],
'material':[],
}
for bName in bracings:
bracing = bracings[bName]
for member in bracing.members:
x1 = member.start_node.x
y1 = member.start_node.y
x2 = member.end_node.x
y2 = member.end_node.y
mat = member.material
bracingsData['bracingName'].append(bName)
bracingsData['x1'].append(str(x1))
bracingsData['y1'].append(str(y1))
bracingsData['x2'].append(str(x2))
bracingsData['y2'].append(str(y2))
bracingsData['material'].append(mat)
df = pd.DataFrame(bracingsData)
try:
df.to_csv(bracingsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save bracings')
def writeBracingGroups(self):
''' Write Bracing Groups to file '''
bGroupsLoc = self.folderLoc + FileExtension.bracingGroups
tower = self.tower
bGroups = tower.bracingGroups
print('bgroups', bGroups)
bGroupsData = {
'bracingGroup':[],
'bracing':[],
}
for bGroupName in bGroups:
bGroup = bGroups[bGroupName]
for bName in bGroup.variables:
bGroupsData['bracingGroup'].append(bGroupName)
bGroupsData['bracing'].append(bName)
df = pd.DataFrame(bGroupsData)
try:
df.to_csv(bGroupsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save bracing groups')
def writeSectionGroups(self):
''' Write Section Groups to file '''
sGroupsLoc = self.folderLoc + FileExtension.sectionGroups
tower = self.tower
sGroups = tower.sectionGroups
sGroupsData = {
'sectionGroup':[],
'section':[],
}
for sGroupName in sGroups:
sGroup = sGroups[sGroupName]
for sName in sGroup.variables:
sGroupsData['sectionGroup'].append(sGroupName)
sGroupsData['section'].append(sName)
df = pd.DataFrame(sGroupsData)
try:
df.to_csv(sGroupsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save section groups')
def writeAreaSectionGroups(self):
''' Write Section Groups to file '''
asGroupsLoc = self.folderLoc + FileExtension.areaSectionGroups
tower = self.tower
asGroups = tower.areaSectionGroups
asGroupsData = {
'areaSectionGroup':[],
'areaSection':[],
}
for asGroupName in asGroups:
asGroup = asGroups[asGroupName]
for asName in asGroup.variables:
asGroupsData['areaSectionGroup'].append(asGroupName)
asGroupsData['areaSection'].append(asName)
df = pd.DataFrame(asGroupsData)
try:
df.to_csv(asGroupsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save area section groups')
def writeBracingAssignments(self):
''' Write Bracing Assignments to file '''
bAssignmentsLoc = self.folderLoc + FileExtension.bracingAssignments
tower = self.tower
panels = tower.panels
bAssignmentsData = {
'panelName':[],
'bracingGroup':[],
}
for pName in panels:
panel = panels[pName]
bGroup = panel.bracingGroup
if bGroup:
bAssignmentsData['panelName'].append(pName)
bAssignmentsData['bracingGroup'].append(bGroup)
df = pd.DataFrame(bAssignmentsData)
try:
df.to_csv(bAssignmentsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save bracing assignments')
def writeSectionAssignments(self):
''' Write Section Assignments to file '''
sAssignmentsLoc = self.folderLoc + FileExtension.sectionAssignments
tower = self.tower
member_ids = tower.member_ids
sAssignmentsData = {
'memberid':[],
'sectionGroup':[],
}
for member_id in member_ids:
sGroup = member_ids[member_id]
sAssignmentsData['memberid'].append(member_id)
sAssignmentsData['sectionGroup'].append(sGroup)
df = pd.DataFrame(sAssignmentsData)
try:
df.to_csv(sAssignmentsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save section assignments')
def writeAreaSectionAssignments(self):
''' Write Area Section Assignments to file '''
asAssignmentsLoc = self.folderLoc + FileExtension.areaSectionAssignments
tower = self.tower
panels = tower.panels
asAssignmentsData = {
'panelName':[],
'areaSectionGroup':[],
}
for pName in panels:
panel = panels[pName]
asGroup = panel.areaSectionGroup
if asGroup:
asAssignmentsData['panelName'].append(pName)
asAssignmentsData['areaSectionGroup'].append(asGroup)
df = pd.DataFrame(asAssignmentsData)
try:
df.to_csv(asAssignmentsLoc, index=False)
except:
warning = WarningMessage()
warning.popUpErrorBox('Unable to save area section assignments')
def writeInputTable(self, inputTable):
''' Write Input Table to file '''
inputTableLoc = self.folderLoc + FileExtension.inputTable
df = pd.DataFrame(inputTable)
df.to_csv(inputTableLoc, index=False)
def writeOutputTable(self, towerPerformances, logSignal=None):
''' Write tower performances to output file '''
outputTableLoc = self.folderLoc + FileExtension.outputTable
outputTable = {
'towerNum': [],
}
towerAttrConst = [
'totalWeight', 'period', 'maxEcc', 'avgEcc',
]
towerAttrVarCombo = [
'maxAcc','maxDisp','basesh', 'buildingCost', 'seismicCost',
]
towerAttrVarFloor = [
'CR',
]
towerDCRs = [
'tensionDCR', 'compDCR', 'shearDCR',
]
for towerNum in towerPerformances:
outputTable['towerNum'].append(towerNum)
towerPerformance = towerPerformances[towerNum]
for var in towerPerformance.variables:
value = towerPerformance.variables[var]
if var in outputTable:
outputTable[var].append(value)
else:
outputTable[var] = [value]
for attr in towerAttrConst:
result = getattr(towerPerformance, attr)
if attr in outputTable:
# column name for this result
outputTable[attr].append(result)
else:
outputTable[attr] = [result]
for attr in towerAttrVarCombo:
results = getattr(towerPerformance, attr)
for combo in results:
# column name for this result
resultName = combo + '-' + attr
result = results[combo]
if resultName in outputTable:
outputTable[resultName].append(result)
else:
outputTable[resultName] = [result]
for attr in towerAttrVarFloor:
results = getattr(towerPerformance, attr)
for floor in results:
crX = str(results[floor][0])
crY = str(results[floor][1])
# column name for this result
crXName = str(floor) + ' - CRx'
crYName = str(floor) + ' - CRy'
if crXName in outputTable:
outputTable[crXName].append(crX)
else:
outputTable[crXName] = [crX]
if crYName in outputTable:
outputTable[crYName].append(crY)
else:
outputTable[crYName] = [crY]
for attr in towerDCRs:
result = getattr(towerPerformance, attr)
if attr in outputTable:
# column name for this result
outputTable[attr].append(result)
else:
outputTable[attr] = [result]
df = pd.DataFrame(outputTable)
try:
df.to_csv(outputTableLoc, index=False)
except:
if logSignal:
logSignal.emit('Fail to generate output table')
else:
# NOTE: no warning message for SAPModelCreation due to thread safety issues
warning = WarningMessage()
warning.popUpErrorBox('Unable to save output table')
def writeStressOut(self, towerPerformances, logSignal = None):
'''Write member stresses to a file'''
stressTableLoc = self.folderLoc + FileExtension.stressTable
stressTable = {
"towerNum": [],
}
stressDf = [
'max_T', 'max_C', 'max_M', 'max_V'
]
maxStressList = [
'max_CombT', 'max_CombC'
]
nameComb = ["_Stress (MPa)", "_Stress_Type", "_Stress_LC", "_Stress_ID"]
for towerNum in towerPerformances:
stressTable['towerNum'].append(towerNum)
towerPerformance = towerPerformances[towerNum]
for attr in maxStressList:
result = getattr(towerPerformance, attr)
resultName = attr + nameComb[0]
if resultName in stressTable:
# column name for this result
stressTable[resultName].append(result)
else:
stressTable[resultName] = [result]
for attr in stressDf:
result = getattr(towerPerformance, attr)
id = result["Stress"].idxmax()
max_row = list(result.iloc[id])
for i in range(len(nameComb)):
resultName = attr + nameComb[i]
if resultName in stressTable:
# column name for this result
stressTable[resultName].append(max_row[i])
else:
stressTable[resultName] = [max_row[i]]
df = pd.DataFrame(stressTable)
try:
df.to_csv(stressTableLoc, index=False)
except:
if logSignal:
logSignal.emit('Fail to generate member stress result table')
else:
# NOTE: no warning message for SAPModelCreation due to thread safety issues
warning = WarningMessage()
warning.popUpErrorBox('Unable to save member stress result table')