-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_local.py
More file actions
258 lines (210 loc) · 7.36 KB
/
Copy pathapp_local.py
File metadata and controls
258 lines (210 loc) · 7.36 KB
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
#from __future__ import print_function
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import rhino3dm as rhino
import sectionproperties.pre.sections as sections
from sectionproperties.analysis.cross_section import CrossSection
import sectionproperties.post.post as post
import uuid
import io
import base64
import sys
import json
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def parse_polyline(polyline, start_idx):
points = []
for i in range(polyline.PointCount - 1):
points.append([polyline.Point(i).X, polyline.Point(i).Y])
facets = []
for i in range(polyline.PointCount - 2):
facets.append([i + start_idx, i + start_idx + 1])
facets.append([polyline.PointCount + start_idx - 2, start_idx])
return points, facets
def rhino_mesh_from_meshpy(mesh):
rmesh = rhino.Mesh()
for p in mesh.points:
rmesh.Vertices.Add(p[0], p[1], 0.0)
for e in mesh.elements:
rmesh.Faces.AddFace(e[0], e[1], e[2])
return rmesh
def parse_input(data):
eprint("start parsing input data")
perimeter_polyline = rhino.CommonObject.Decode(data['perimeter'])
points = []
facets = []
# perimeter
pts, fs = parse_polyline(perimeter_polyline, 0)
points.extend(pts)
facets.extend(fs)
perimeter = [i for i in range(len(fs))]
# holes
holes = []
if 'holes' in data and 'hole_points' in data:
hole_polylines = [rhino.CommonObject.Decode(d) for d in data['holes']]
for h in hole_polylines:
pts, fs = parse_polyline(h, len(points))
points.extend(pts)
facets.extend(fs)
for p in data['hole_points']:
holes.append([p['X'], p['Y']])
# control_points
control_points = []
for p in data['control_points']:
control_points.append([p['X'], p['Y']])
# loads to check
loadcases = []
if 'loadcases' in data:
for load in data['loadcases']:
loadcase = []
loadcase.append(load['LC'])
loadcase.append(load['N'])
loadcase.append(load['Vx'])
loadcase.append(load['Vy'])
loadcase.append(load['Mxx'])
loadcase.append(load['Myy'])
loadcase.append(load['Mzz'])
loadcases.append(loadcase)
# eprint(loadcases)
# imagesToSend - list of images we want back again
# resultsToSend - list of result sets we want back again
# custom material - also check what is the default for clarity!
# note the following is in cm
# meshsize
mesh_size = 2.0
if 'mesh_size' in data:
mesh_size = data['mesh_size']
return points, facets, holes, control_points, perimeter, mesh_size, loadcases
def process_geometry(geometry, mesh_sizes, loadcases):
# update this to receive the geometry, mesh info, material and loads
# generate a finite element mesh
mesh = geometry.create_mesh(mesh_sizes=mesh_sizes)
# generate material - can be overwritten if needed --all in N and cm
# create a CrossSection object for analysis
section = CrossSection(geometry, mesh)
# calculate various cross-section properties
section.calculate_geometric_properties()
section.calculate_warping_properties()
section.calculate_plastic_properties()
# Area
area = section.get_area()
sheararea = section.get_As()
asx = sheararea[0]
asy = sheararea[1]
# Second Moment of Area about centroid
(ixx,iyy,ixy) = section.get_ic()
# Centroid
(xg,yg) = section.get_c()
# Radii of Gyration
(rxx,ryy) = section.get_rc()
# Principal bending axis angle
phi = section.get_phi()
# St. Venant torsion constant
ipp = section.get_j()
# Warping Constant
cw = section.get_gamma()
# Elastic Section Moduli
(welx_top,welx_bottom,wely_top,wely_bottom) = section.get_z()
# Plastic Section Moduli
(wplx,wply) = section.get_s()
# plot centroid to image
section.plot_centroids(pause=False)
buf = io.BytesIO()
plt.savefig(buf, format='png',bbox_inches='tight')
buf.seek(0)
plot_centroid = base64.b64encode(buf.getvalue()).decode()
plt.close()
# calculate torsion resistance from stress and torque
#from the below can also return torsional stress if wanted
stress_post = section.calculate_stress(Mzz=10)
unit_mzz_zxy = []
maxstress = []
for group in stress_post.material_groups:
maxstress.append(max(group.stress_result.sig_zxy_mzz))
unit_mzz_zxy.append(group.stress_result.sig_zxy_mzz.tolist())
#there should be only one maxstress value therefore:
wt = 10/maxstress[0]
#plot this image
stress_post.plot_stress_mzz_zxy(pause=False)
buf = io.BytesIO()
plt.savefig(buf, format='png',bbox_inches='tight')
buf.seek(0)
plot_unittorsionstress = base64.b64encode(buf.getvalue()).decode()
plt.close()
#foreach load case submitted calculate vm stress state and create image
vmStressImages = {}
vmStressStates = {}
for loadcase in loadcases:
lc_name = loadcase[0]
s_n = loadcase[1]
s_vx = loadcase[2]
s_vy = loadcase[3]
s_mxx = loadcase[4]
s_myy = loadcase[5]
s_mzz = loadcase[6]
stress_post = section.calculate_stress(N=s_n,Vx=s_vx,Vy=s_vy,Mxx=s_mxx,Myy=s_myy,Mzz=s_mzz)
stress_state = []
for group in stress_post.material_groups:
stress_state.append(group.stress_result.sig_vm.tolist())
vmStressStates['lc_'+str(lc_name)+'_vm_stress'] = stress_state
#plot this image
stress_post.plot_stress_vm(pause=False)
buf = io.BytesIO()
plt.savefig(buf, format='png',bbox_inches='tight')
buf.seek(0)
vmStressImages['lc_'+str(lc_name)+'_vm_stress'] = base64.b64encode(buf.getvalue()).decode()
plt.close()
# create rhino mesh
rmesh = rhino_mesh_from_meshpy(mesh)
# return send_file(path, as_attachment=True)
# get some of the calculated section properties
return_data = {}
return_data['properties'] = {
'area': area,
'Avx': asx,
'Avy':asy,
'xg': xg,
'yg': yg,
'rxx': rxx,
'ryy': ryy,
'phi': phi,
'ixx': ixx,
'iyy': iyy,
'ipp':ipp,
'cw':cw,
'welx+': welx_top,
'welx-': welx_bottom,
'wely+': wely_top,
'wely-': wely_bottom,
'wplx': wplx,
'wply': wply,
'wt':wt,
}
return_data['geometry'] = {
'mesh': rhino.CommonObject.Encode(rmesh),
}
return_data['images'] = {
'centroids': plot_centroid,
'unittorsion_vxy_stress': plot_unittorsionstress,
}
return_data['images'].update(vmStressImages)
return_data['stress_results'] = {
'unittorsion_vxy_stress': unit_mzz_zxy,
}
return_data['stress_results'].update(vmStressStates)
return return_data
#main part of the file starts here:
filename = sys.argv[1]
eprint("start working on the file:"+ filename)
with open(filename) as f:
lines = f.readlines()
data = eval(lines[0].strip())
points, facets, holes, control_points, perimeter, mesh_size, loadcases = parse_input(data)
mesh_sizes = [mesh_size]
geometry = sections.CustomSection(points, facets, holes, control_points)
geometry.clean_geometry()
data = process_geometry(geometry,mesh_sizes,loadcases)
json_result = json.dumps(data)
print(json_result)
eprint('done')