forked from RomanIlchenko1308/Python-Robot-App-Dash-VTK
/
app.py
243 lines (210 loc) · 8.77 KB
/
app.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
import dash
from dash import dcc, html, Input, Output, ctx
import dash_vtk
from dash_vtk.utils import to_mesh_state
import pyvista as pv
import pandas as pd
# ---------------------------------------------------------
def place_robot(input_x=0, input_y=0, input_f=0):
# ---
# Import 3D model
filename = pv.read("RobotTruck.stl")
filename.scale([0.1, 0.1, 0.1], inplace=True)
# ---
# XY rotation on 90 deg around z axis
filename.rotate_vector((0, 0, 1), input_f, inplace=True)
# ---
# filename.origin(5, 5, 0)
filename.translate([input_x, input_y, 0.5], inplace=True)
# ---
mesh = pv.Plane(
center=(2, 2, 0),
direction=(0, 0, 1),
i_size=5,
j_size=5,
i_resolution=5,
j_resolution=5
)
# ---
mesh = mesh.merge(filename)
mesh_state = to_mesh_state(mesh)
return mesh_state
# ---------------------------------------------------------
def txt_output_file(txt_output: str):
list_txt_output = txt_output.split("\n")
df = pd.DataFrame(list_txt_output, columns=['Commands'])
df[['Commands', 'Coord: X', 'Coord: Y', "Vector: F"]] = df['Commands'].str.split(',', expand=True)
return str(df)
# ---------------------------------------------------------
app = dash.Dash(__name__)
app.scripts.config.serve_locally = True
app.layout = html.Div([
html.Div([
html.Img(src="/assets/img/robot_icon.png", className="robotLogo"),
html.H2("Robot App"),
html.A(children=[
html.Img(src="/assets/img/Github_icon.svg", className="githubLogo")
],
href="https://github.com/RomanIlchenko1308/PythonRobotApp",
target="_blank",
className="githubLogo")
], className="projectHeader"),
html.Div([
html.Div([
html.Button(children=[
html.Div("Home", className="btnText"),
html.Div(children=[
html.Div(
children=[
html.Img(src="/assets/img/robot_home_origin_icon.svg", className="robNavLogo")],
className="btnText2")
], className="btnTwo"),
], className="button", id="home-btn", n_clicks=0),
html.Div(children=[
html.Div(children=[
dcc.Input(id='input-x', className="inputBox inputX", value=1, min=1, max=5, type='number'),
dcc.Input(id='input-y', className="inputBox inputY", value=1, min=1, max=5, type='number'),
dcc.Input(id='input-f', className="inputBox inputF", value=0, min=-360, max=360, step=90,
type='number'),
], className="inputBoxesBlock"),
html.Button(children=[
html.Div("Place", className="btnText"),
html.Div(children=[
html.Div(
children=[
html.Img(src="/assets/img/robot_place_icon.svg", className="robNavLogo")],
className="btnText2")
], className="btnTwo"),
], className="button", id="place-btn", n_clicks=0),
], className="enterPlaceBox"),
html.Button(children=[
html.Div("Left", className="btnText"),
html.Div(children=[
html.Div(
children=[
html.Img(src="/assets/img/robot_turn_left_icon.svg", className="robNavLogo")],
className="btnText2")
], className="btnTwo"),
], className="button", id="left-btn", n_clicks=0),
html.Button(children=[
html.Div("Right", className="btnText"),
html.Div(children=[
html.Div(
children=[
html.Img(src="/assets/img/robot_turn_right_icon.svg", className="robNavLogo")],
className="btnText2")
], className="btnTwo"),
], className="button", id="right-btn", n_clicks=0),
], className="menuButtons menuButtonsBackground"),
html.Div([
html.H4("Contact with me:"),
html.A(children=[
html.Img(src="/assets/img/LinkedIn_icon.svg", className="githubLogo")
],
href="https://www.linkedin.com/in/romanilichenko/",
target="_blank",
className="linkedInLogo")
], className="contactWithMe menuButtonsBackground")
], className="menuControlFlow"),
html.Div(
style={"width": "100%", "height": "100%"},
children=[
dash_vtk.View([
dash_vtk.GeometryRepresentation([
dash_vtk.Mesh(id="my-output")
], property={
"edgeVisibility": True,
"edgeColor": (0.5, 0, 0.5),
"color": (171 / 100, 235 / 100, 52 / 100)
}),
]),
], className="vtkFlow",
),
html.Div([
html.Div(children=[
dcc.Textarea(
id="text-area",
className='textArea',
value='HOME,X,Y,F',
style={"width": "100%", "height": "300px"},
),
html.Button(children=[
html.Div("Report", className="btnText"),
html.Div(children=[
html.Div(
children=[
html.Img(src="/assets/img/robot_report_icon.svg", className="robNavLogo")],
className="btnText2")
], className="btnTwo"),
], className="button", id="btn-download-txt", n_clicks=0),
dcc.Download(id="download-text"),
], className="manualMenu manualTextBtnBackground"),
], className="manualCommandsFlow")
], className="bodyDiv")
@app.callback(
Output(component_id='my-output', component_property='state'),
Output(component_id='input-x', component_property='value'),
Output(component_id='input-y', component_property='value'),
Output(component_id='input-f', component_property='value'),
Output(component_id='home-btn', component_property='n_clicks'),
Output(component_id='place-btn', component_property='n_clicks'),
Output(component_id='left-btn', component_property='n_clicks'),
Output(component_id='right-btn', component_property='n_clicks'),
Output(component_id='text-area', component_property='value'),
Input(component_id='input-x', component_property='value'),
Input(component_id='input-y', component_property='value'),
Input(component_id='input-f', component_property='value'),
Input(component_id='home-btn', component_property='n_clicks'),
Input(component_id='place-btn', component_property='n_clicks'),
Input(component_id='left-btn', component_property='n_clicks'),
Input(component_id='right-btn', component_property='n_clicks'),
Input(component_id='text-area', component_property='value'),
)
def update_place_btn(input_x, input_y, input_f,
home_clicks, place_clicks,
left_click, right_click,
text_area):
# text_area
# check which button was clicked:
button_clicked = ctx.triggered_id
# print(button_clicked)
# Home Button
if button_clicked == "home-btn":
home_clicks, place_clicks = 0, 0
input_x, input_y, input_f = 1, 1, 0
text_area += f"\nHOME,{input_x},{input_y},{input_f}"
# Place Button
if button_clicked == "place-btn":
text_area += f"\nPLACE,{input_x},{input_y},{input_f}"
# Turn Left Button
if button_clicked == "left-btn":
input_f = input_f + 90
# print(f"--> Left, angel is: {input_f}")
if input_f == 360:
input_f = 0
text_area += f"\nLEFT,{input_x},{input_y},{input_f}"
# Turn Right Button
if button_clicked == "right-btn":
input_f = input_f - 90
if input_f == -360:
input_f = 0
# print(f"--> Right, angel is: {input_f}")
text_area += f"\nRIGHT,{input_x},{input_y},{input_f}"
# text_area = "0"
return place_robot(input_x - 1, input_y - 1, input_f), \
input_x, input_y, input_f, \
home_clicks, place_clicks, \
left_click, right_click, text_area
@app.callback(
Output("download-text", "data"),
Input(component_id='text-area', component_property='value'),
Input("btn-download-txt", "n_clicks"),
prevent_initial_call=True,
)
def func(text_output_str, n_clicks):
button_clicked = ctx.triggered_id
if button_clicked == "btn-download-txt":
text = txt_output_file(text_output_str)
return dict(content=text, filename="hello.txt")
if __name__ == '__main__':
app.run_server(debug=True)