-
Notifications
You must be signed in to change notification settings - Fork 0
/
XFLR5XMLGenerator.py
126 lines (90 loc) · 4.56 KB
/
XFLR5XMLGenerator.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
import xml.etree.ElementTree as ET
import numpy as np
import os, sys
import argparse
import shutil
def main():
parser = argparse.ArgumentParser(description='Generate XML files for XFLR5.')
parser.add_argument('-i', type=str, dest='input', required = True, help = 'File with the input data.')
parser.add_argument('-o', type=str, dest='output', required = True, help = 'Folder where output data will be stored.')
parser.add_argument('-p', type=str, dest='plane', required = True, help = 'File corresponding to the model XML for the plane.')
parser.add_argument('-a', type=str, dest='analysis', required = True, help = 'File corresponding to the model XML for the analysis.')
parser.add_argument('-d', type=bool, dest='delOutput', default = False, help = 'Whether or not to delete output folder before outputting new data')
args = parser.parse_args()
inputData = np.loadtxt(args.input, dtype=str, ndmin=2).tolist()
effectiveData = [inputData[0]]
for row in inputData[1:]:
toAppend = [row.copy()]
parallelGroups = {}
parallelArgsCol = {}
for i, col in enumerate(row):
if col[-1] == ')' and '(' in col:
splitted = col.split('(', 1)
if splitted[0] not in parallelGroups:
parallelGroups[splitted[0]] = []
parallelArgsCol[splitted[0]] = []
parallelGroups[splitted[0]].append(splitted[1].strip(')').split(','))
parallelArgsCol[splitted[0]].append(i)
for key in parallelGroups.keys():
for i in range(1, len(parallelGroups[key])):
if len(parallelGroups[key][i-1]) != len(parallelGroups[key][i]):
print('Error in input file: Group ' + key + ': Argument lists in "(" ")" within the same row must have the same amount of elements')
sys.exit(2)
newToAppend = []
for toAppendRow in toAppend:
for i in range( len(parallelGroups[key][0])-1, -1, -1):
copyToAppendRow = toAppendRow.copy()
for j, argList in enumerate(parallelGroups[key]):
copyToAppendRow[parallelArgsCol[key][j]] = argList[i]
newToAppend.append(copyToAppendRow)
toAppend = newToAppend
toAppend.reverse()
for toAppendRow in toAppend:
effectiveData.append(toAppendRow)
if args.delOutput and os.path.exists(args.output):
shutil.rmtree(args.output)
outputPlanes = args.output + '/' + 'planes'
outputAnaly = args.output + '/' + 'analysis'
os.makedirs(outputPlanes, exist_ok = True)
os.makedirs(outputAnaly, exist_ok = True)
modelPlane = ET.parse(args.plane)
modelAnaly = ET.parse(args.analysis)
names = [None] * (len(effectiveData)-1)
elemArray = []
for columnIndex, columnName in enumerate(effectiveData[0]):
if columnName.lower() == 'name':
names = [row[columnIndex] for row in effectiveData[1:]]
elemArray.append([])
continue
split = columnName.split(':')
if split[0] == 'p':
modelToChange = modelPlane.getroot()
elif split[0] == 'a':
modelToChange = modelAnaly.getroot()
else:
print('Error in input file: ' + split[0] + ' does not represent a valid model file')
sys.exit(2)
elemArray.append(modelToChange.findall('' if split[1][0] == '.' else '.' + split[1]))
if len(elemArray[-1]) == 0:
print('Error in input file: ' + columnName + ' does not represent a valid element(s)')
sys.exit(2)
usedNameIndex = {}
for i, name in zip(effectiveData[1:], names):
if name is None or name.lower() == 'none':
name = '-'.join([value.replace('\\','') for value in i if value.lower() != 'none'])
if name in usedNameIndex:
usedIndex = usedNameIndex[name] + 1
usedNameIndex[name] = usedIndex
name = name + ' (' + str(usedIndex) + ')'
else:
usedNameIndex[name] = 0
modelAnaly.getroot().find('Polar').find('Plane_Name').text = name
modelAnaly.getroot().find('Polar').find('Polar_Name').text = name
modelPlane.getroot().find('Plane').find('Name').text = name
for j, k in zip(elemArray, i):
for elem in j:
elem.text = k.replace('\\_', ' ')
modelPlane.write(outputPlanes + '/' + name + '.xml')
modelAnaly.write(outputAnaly + '/' + name + '.xml')
if __name__ == "__main__":
main()