File size: 5,145 Bytes
bcad657
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)