Spaces:
Sleeping
Sleeping
import numpy as np | |
import csv | |
import plotly.graph_objects as go | |
from scipy.interpolate import griddata | |
import os | |
# Creates a 3D surface plot based on the selected CSV file | |
encoding = "utf-16" | |
sprint_kugel = 3 | |
stegdicke = 6.5 | |
tolerance = 12 #in µm | |
breite = 13.7 | |
laenge = 39 | |
x_values = [2.4, 3, 3.6, 4.2, 4.8, 5.4, 6, 6.6, 7.2, 7.8, 8.4, 9, 9.6, 10.2, 10.8, 11.4, 12, 12.6] | |
# Die Milltap hatte einen globalen Fehler von 20 µm. Dieser muss von den Messwerten abgezogen werden. | |
offset = 0 | |
def surface_plot(csv_messung): | |
x = [] | |
y = [] | |
z = [] | |
# Read CSV | |
with open(csv_messung, 'r', encoding=encoding) as file: | |
csv_reader = csv.reader(file) | |
for row in csv_reader: | |
if len(row) >= 2: | |
x.append(float(row[0])) | |
y.append(float(row[1])) | |
z.append(float(row[2])) | |
x = np.array(x) | |
y = np.array(y) | |
z = np.array(z) | |
# delete all data in x, y and z where y is not between -31 and -2: | |
x = x[(y > -31) & (y < -2)] | |
z = z[(y > -31) & (y < -2)] | |
y = y[(y > -31) & (y < -2)] | |
x = x +7.5 | |
z = (np.array(z)-(sprint_kugel/2)) | |
z = (z - (stegdicke/2) - offset)*1000 | |
corner_limits = { | |
'plane_horizontal': {'x': [0, breite], 'y': [-laenge, 0]}, | |
'plane_vertical': {'y': [-laenge, 0], 'z': [-10, 10], } | |
} | |
xi = np.linspace(x.min(), x.max(), 100) | |
yi = np.linspace(y.min(), y.max(), 100) | |
Xi, Yi = np.meshgrid(xi, yi) | |
Zi = griddata((x, y), z, (Xi, Yi), method='linear') | |
mask = np.logical_or(Zi > tolerance, Zi < -tolerance) | |
colorscale = [[0, 'rgba(0, 80, 155, 0.85)'], [1, 'rgba(0, 80, 155, 0.85)']] | |
# Messung | |
trace_surface = go.Surface( | |
x=Xi, | |
y=Yi, | |
z=Zi, | |
colorscale=colorscale, | |
surfacecolor=np.where(mask, 1, 0), | |
showscale=False, | |
) | |
# Ebene z = 0 | |
trace_plane_0 = go.Surface( | |
x=corner_limits['plane_horizontal']['x'], | |
y=corner_limits['plane_horizontal']['y'], | |
z=np.zeros((2, 2)), | |
showscale=False, | |
opacity=0.5, | |
colorscale=[[0, 'rgba(0, 160, 0, 0.5)'], [1, 'rgba(0, 160, 0, 0.5)']], | |
) | |
# Plane at z = tolerance | |
trace_plane_tol = go.Surface( | |
x=corner_limits['plane_horizontal']['x'], | |
y=corner_limits['plane_horizontal']['y'], | |
z=np.full((2, 2), tolerance), | |
showscale=False, | |
opacity=0.3, | |
colorscale=[[0, 'rgba(255, 0, 0, 0.5)'], [1, 'rgba(255, 0, 0, 0.5)']], | |
) | |
# Plane at z = -tolerance | |
trace_plane_minus_tol = go.Surface( | |
x=corner_limits['plane_horizontal']['x'], | |
y=corner_limits['plane_horizontal']['y'], | |
z=np.full((2, 2), -tolerance), | |
showscale=False, | |
opacity=0.3, | |
colorscale=[[0, 'rgba(255, 0, 0, 0.5)'], [1, 'rgba(255, 0, 0, 0.5)']], | |
) | |
# Vertical plane trace | |
trace_plane_x = go.Surface( | |
x=np.full((2, 2), 4), | |
y=corner_limits['plane_vertical']['y'], | |
z=corner_limits['plane_vertical']['z'], | |
showscale=False, | |
opacity=0.3, | |
colorscale=[[0, 'rgba(255, 0, 0, 0.5)'], [1, 'rgba(255, 0, 0, 0.5)']], | |
) | |
titlefont=dict(family="Arial", size=20, color='black') | |
tickfont=dict(family="Arial", size=14, color='black') | |
layout = go.Layout( | |
scene=dict( | |
xaxis=dict(title='X [mm]', titlefont=titlefont, tickfont=tickfont), | |
yaxis=dict(title='Y [mm]', titlefont=titlefont, tickfont=tickfont), | |
zaxis=dict(title='Z [µm]', titlefont=titlefont, tickfont=tickfont), | |
aspectratio=dict(x=0.8, y=2.5, z=1), | |
camera=dict( | |
eye=dict(x=-1.5, y=2, z=0.4), # Set camera position | |
center=dict(x=0, y=0, z=-0.65))), | |
updatemenus=[ | |
dict( | |
type='buttons', | |
buttons=[ | |
dict( | |
label='Standard', | |
method='update', | |
args=[{'visible': [True, False, False, False, True]}] | |
), | |
dict( | |
label='Toleranzen einblenden', | |
method='update', | |
args=[{'visible': [True, True, True, True, True]}] | |
) | |
], | |
direction='down', | |
showactive=True, | |
x=0.1, | |
y=1.2 | |
)] | |
) | |
# Set the 'visible' attribute for each trace | |
trace_surface.update(visible=True) | |
trace_plane_0.update(visible=False) | |
trace_plane_tol.update(visible=False) | |
trace_plane_minus_tol.update(visible=False) | |
trace_plane_x.update(visible=True) | |
fig = go.Figure(data=[trace_surface, trace_plane_0, trace_plane_tol, trace_plane_minus_tol, trace_plane_x], layout=layout) | |
#fig.update_layout(scene=dict(xaxis=dict(range=[0, 10]), yaxis=dict(range=[-40, 10]), zaxis=dict(range=[-40, 40]))) | |
return(fig) | |