forked from evanchodora/stl-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.py
508 lines (451 loc) · 34 KB
/
GUI.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import pygame
import numpy as np
import os
import base64
import gtransform
from orient import orient
from drawlines import draw_lines
'''
STL Viewer Application
Application designed to open and display ASCII STL files for ME8720 at Clemson University
Features:
- Reads object face geometry and associated outward normal vectors from STL files
- Centers the object and rescales to fit appropriately within the viewing/clipping window
- User can select wireframe, no hidden lines, or shaded hidden line views when displaying the object on screen
- Allows for user selection of isometric, dimetric, and trimetric views with user-defined settings popup
- Menu for quick selection of the 6 standard orthographic views of the object
- Allows for zooming, rotation and panning of the object with onscreen controls and keyboard bindings
Evan Chodora, 2018
https://github.com/evanchodora/viewer
'''
# Class to draw an STL object from an ASCII STL file
class DrawObject:
pxarray = [] # Initialize the pixel array variable to empty for the class
def __init__(self):
# Initiate new Loader class and run load_stl with the selected file
self.model = Loader()
self.model.load_stl(window.filename)
# Function to plot the initial object after loading
def initial_plot(self, loc):
# Copy original geometry to new internal variable to be used for transformations (keep original
# geometry unchanged for use in displaying the orthographic views of the object)
self.model.coordinates, self.model.normals = self.model.geometry, self.model.normal
# Apply selected perspective with appropriate settings of fz, phi, and theta
plot_geometry, camera = gtransform.perspective(persp.get(), self.model.coordinates, fz.get(), phi.get(),
theta.get())
# Draw lines between points and clip to viewing window based on window height and width
plot_geometry = draw_lines(plot_geometry, self.model.normals, camera, view.get(), embed_w, embed_h)
# Clear pixel array to white and then change each pixel color based on the XY pixel map
self.pxarray = pygame.PixelArray(loc)
self.pxarray[:][:] = (255, 255, 255)
for point in range(0, plot_geometry.shape[0]):
x = int(embed_w/2 + plot_geometry[point, 0]) # X coordinate (0,0 of screen is top left)
y = int(embed_h/2 + plot_geometry[point, 1]) # Y coordinate (0,0 of screen is top left)
# Plot all front facing lines and back facing when wireplot is selected
if plot_geometry[point, 2] == 1 or view.get() == 'wire':
self.pxarray[x][y] = (0, 0, 0) # Color = black
# Plot grey lines only if grey lines are selected and the line is not already plotted black
elif plot_geometry[point, 2] == 0 and view.get() == 'grey' and self.pxarray[x][y] != 0:
self.pxarray[x][y] = (210, 210, 210) # Color = grey
# Plot pixel array to screen and refresh window/GUI
pygame.surfarray.blit_array(loc, self.pxarray)
pygame.display.flip()
window.update()
# Function to re-plot the object with a specified transformation/perspective
def plot_transform(self, loc, transtype, data):
if transtype == 'ortho':
# Transform the original geometry according to the selected orthographic view
new_geometry, new_normals = gtransform.transform(self.model.geometry,
self.model.normal, transtype,
data)
# Draw lines between points and clip to viewing window based on window height and width
new_geometry = draw_lines(new_geometry, new_normals, [0, 0, 1], view.get(), embed_w,
embed_h)
else:
# Transform geometry based on the selected transformation
self.model.coordinates, self.model.normals = gtransform.transform(self.model.coordinates,
self.model.normals, transtype,
data)
# Apply selected perspective with appropriate settings of fz, phi, and theta
new_geometry, camera = gtransform.perspective(persp.get(), self.model.coordinates,
fz.get(), phi.get(), theta.get())
# Draw lines between points and clip to viewing window
new_geometry = draw_lines(new_geometry, self.model.normals, camera,
view.get(), embed_w, embed_h)
# Clear pixel array to white and then change each pixel color based on the XY pixel map
self.pxarray[:][:] = (255, 255, 255)
for point in range(0, new_geometry.shape[0]):
x = int(embed_w/2 + new_geometry[point, 0]) # X coordinate (0,0 of screen is top left)
y = int(embed_h/2 + new_geometry[point, 1]) # Y coordinate (0,0 of screen is top left)
# Plot all front facing lines and back facing when wireplot is selected
if new_geometry[point, 2] == 1 or view.get() == 'wire':
self.pxarray[x][y] = (0, 0, 0) # Color = black
# Plot grey lines only if grey lines are selected and the line is not already plotted black
elif new_geometry[point, 2] == 0 and view.get() == 'grey' and self.pxarray[x][y] != 0:
self.pxarray[x][y] = (210, 210, 210) # Color = grey
# Plot pixel array to screen and refresh window/GUI
pygame.surfarray.blit_array(loc, self.pxarray)
pygame.display.flip()
window.update()
# STL file loader class
class Loader:
# Initialize class variables
geometry = []
normal = []
name = []
normal_face = []
# Load ASCII STL File (no Binary STLs - based on project requirements)
def load_stl(self, filename):
self.geometry = [] # Clear previous geometry data
self.name = [] # Clear previous STL model name
self.normal = [] # Clear previous STL normal data
fp = open(filename, 'r') # Open and read selected file into memory
# Loop over each line in the STL file
for line in fp.readlines():
parts = line.split() # Split line into parts by spaces
if len(parts) > 0:
# Start of filename, store embedded filename
if parts[0] == 'solid':
self.name = line[6:-1]
# Beginning of a new face - store normals and begin new triangle variable
if parts[0] == 'facet':
triangle = []
# Select face normal components
self.normal_face = (float(parts[2]), float(parts[3]), float(parts[4]), 1)
# Store all the vertex points in 'triangle'
if parts[0] == 'vertex':
triangle.append((float(parts[1]), float(parts[2]), float(parts[3]), 1))
# End of face - append new face to the model data
if parts[0] == 'endloop':
self.geometry.append([triangle[0], triangle[1], triangle[2]])
self.normal.append(self.normal_face)
fp.close()
# Convert lists to numpy arrays of the correct dimensions (Nx4 matrices)
self.normal = np.asarray(self.normal).reshape((-1, 4))
self.geometry = np.asarray(self.geometry).reshape((-1, 4))
self.geometry = orient(self.geometry, embed_w, embed_h) # Orient object geometry in screen space
window.title("STL Viewer Application - " + self.name) # Put filename in the GUI header
# Class to create a perspective settings popup dialog box for user input
class SettingsDialog:
def __init__(self, parent):
top = self.top = Toplevel(parent) # Use Tkinter top for a separate popup GUI
top.geometry("240x200") # Window dimensions
top.resizable(0, 0) # Un-resizable
top.title('Perspective') # Window title
# Code to assign an icon to the settings popup box using base64 stored image
icon = \
"AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAANcNAADXDQAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZwAA\
AOkAAAD5AAAA+QAAAOUAAABVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAABVAAAA4QAAAOoAAADoAAAA5AAAAGcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAA//gf///wD///8A8/+GAGH/AAAA/gAAAHwAAAB+AAAAfgAAAH8AAAD/AA\
AA/gAAAHgAfgAQAP8AAAD/AAAA/wAAAP8AAAD/AAAAfwAAAD4AHgAAAH8AAAD/AAAA/gAAAH4AAAB+AA\
AAPgAAAH8AAAD/hgBh/88A////AP///4H/8="
icodata = base64.b64decode(icon) # Decode base64 image
temfile = "icon.ico" # Create temporary file
icofile = open(temfile, "wb") # Open temporary file
icofile.write(icodata) # Write icon data
icofile.close()
top.wm_iconbitmap(temfile) # Set window icon to the icon image
os.remove(temfile)
self.DiLabel = Label(top, text='Dimetric Settings').place(x=50, rely=.05, anchor="c")
self.fzLabel = Label(top, text='Fz').place(x=45, rely=.2, anchor="c")
self.phiLabel = Label(top, text='Phi').place(x=45, rely=.5, anchor="c")
self.thetaLabel = Label(top, text='Theta').place(x=45, rely=.65, anchor="c")
self.TriLabel = Label(top, text='Trimetric Settings').place(x=50, rely=.35, anchor="c")
self.fzBox = Entry(top) # Fz entry box
self.fzBox.place(x=140, rely=.2, anchor="c")
self.fzBox.insert(0, fz.get()) # Prefill with Fz variable value
self.phiBox = Entry(top) # Phi entry box
self.phiBox.place(x=140, rely=.5, anchor="c")
self.phiBox.insert(0, phi.get()) # Prefill with Phi variable value
self.thetaBox = Entry(top) # Theta entry box
self.thetaBox.place(x=140, rely=.65, anchor="c")
self.thetaBox.insert(0, theta.get()) # Prefill with Theta variable value
# Save button, runs command to store/send variables back to the main window space
self.mySubmitButton = Button(top, text='Save', command=self.send).place(relx=.5, rely=.85, anchor="c")
def send(self):
# Update main window variables with those filled in the entry boxes
fz.set(self.fzBox.get())
phi.set(self.phiBox.get())
theta.set(self.thetaBox.get())
self.top.destroy() # Destroy popup window and return to main window loop
def save_click():
SettingsDialog(window) # Create a new instance of the popup window class
def file_select():
# Function to select an STL file and store the path as "filename"
window.filename = filedialog.askopenfilename(initialdir="C:\\", title="Select STL File",
filetypes=(("STL files", "*.STL"), ("All files", "*.*")))
status_text = "Opened: " + window.filename # Add file name/path to bottom status bar
status.configure(text=status_text)
file_select.stlobject = DrawObject() # Create new stlobject class for the selected file
DrawObject.initial_plot(file_select.stlobject, screen) # Run initial object plot function for the class
def about_popup():
# Info box about the software from the Help menu
messagebox.showinfo('About STL Viewer',
'Created by Evan Chodora, 2018\n\n Designed to open and view ASCII STL files')
# ****** Initialize Main Window ******
window = Tk()
window.title('STL Viewer Application') # Main window title
# Code to embed base64 version of the window icon into the title bar
cube = \
"AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAANcNAADXDQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwAAAFgAAADEAAAAxAAAAFgAAAAPAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAADQAAAE4AAACwAAAA8gAAAP0AAAD+AAAA8AAAAK8AAABOAAAADQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADQAAAE8AAACxAAAA9AAAAP0A\
AADSAAAAjAAAAOwAAAD/AAAA/wAAAPIAAACxAAAATwAAAA0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
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ile = "icon.ico" # Create temporary file
iconfile = open(tempfile, "wb") # Open temporary file
iconfile.write(icondata) # Write icon data
iconfile.close()
window.wm_iconbitmap(tempfile) # Set window icon to the icon image
os.remove(tempfile)
window.geometry("1200x800") # Main overall window size
window.resizable(0, 0) # Scaling disallowed in X and Y
# ****** Embed PyGame Window (Pixel Map Display) ******
embed_w = 900 # Width of object display screen
embed_h = 700 # Height of object display screen
embed = Frame(window, width=embed_w, height=embed_h) # Embed in the GUI window
embed.place(x=50, y=40) # Location of placement
# Set appropriate environment variables for embedding the PyGame pixel display window in the GUI
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((embed_w, embed_h)) # Create screen with specified width and height
# Set embed screen to white and refresh the screen object
screen.fill((255, 255, 255))
pygame.display.init()
pygame.display.flip()
# ****** Define Default Perspective Settings and View Type ******
persp = StringVar()
persp.set('iso')
view = StringVar()
view.set('hide')
phi = DoubleVar()
phi.set(45)
theta = DoubleVar()
theta.set(35)
fz = DoubleVar()
fz.set(0.375)
# ****** Toolbar ******
# Create main menu bar
menu = Menu(window, tearoff=False)
window.config(menu=menu)
# Create "File" submenu
subMenu = Menu(menu, tearoff=False)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="Open File", command=file_select)
subMenu.add_command(label="Exit", command=window.destroy)
# Create "Edit View" submenu
subMenu = Menu(menu, tearoff=False)
menu.add_cascade(label="Edit View", menu=subMenu)
perspMenu = Menu(subMenu, tearoff=False)
subMenu.add_cascade(label="Change Perspective", menu=perspMenu)
perspMenu.add_radiobutton(label='Isometric', variable=persp, value='iso') # Isometric projection
perspMenu.add_radiobutton(label='Dimetric', variable=persp, value='di') # Dimetric projection
perspMenu.add_radiobutton(label='Trimetric', variable=persp, value='tri') # Trimetric projection
viewMenu = Menu(subMenu, tearoff=False)
subMenu.add_cascade(label="View Type", menu=viewMenu)
viewMenu.add_radiobutton(label='Wireframe', variable=view, value='wire') # Full wireframe
viewMenu.add_radiobutton(label='Hide Faces', variable=view, value='hide') # Hide non-visible faces
viewMenu.add_radiobutton(label='Partial Hidden', variable=view, value='grey') # Grey hidden lines
subMenu.add_command(label="Recenter Object", command=lambda: DrawObject.initial_plot(file_select.stlobject, screen))
subMenu.add_command(label="Perspective Settings", command=save_click)
# Create "Orthographic" submenu
subMenu = Menu(menu, tearoff=False)
menu.add_cascade(label="Orthographic", menu=subMenu)
subMenu.add_command(label="Top", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'top'))
subMenu.add_command(label="Bottom", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'bottom'))
subMenu.add_command(label="Left", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'left'))
subMenu.add_command(label="Right", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'right'))
subMenu.add_command(label="Front", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'front'))
subMenu.add_command(label="Back", command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'ortho', 'back'))
# Create "Help" submenu
subMenu = Menu(menu, tearoff=False)
menu.add_cascade(label="Help", menu=subMenu)
subMenu.add_command(label="About", command=about_popup)
# ****** Control Panel ******
# Control text labels
rotate = Label(window, text="Rotate", font=("Helvetica", 16))
rotate.place(x=1075, rely=0.15, anchor="c")
zoom = Label(window, text="Zoom", font=("Helvetica", 16))
zoom.place(x=1075, rely=0.45, anchor="c")
pan = Label(window, text="Pan", font=("Helvetica", 16))
pan.place(x=1075, rely=0.65, anchor="c")
# Rotation buttons layout
rot_l = Button(window, text="<-", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'rotation', [2, -15]))
rot_l.place(x=1025, rely=.25, anchor="c")
rot_r = Button(window, text="->", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'rotation', [2, 15]))
rot_r.place(x=1125, rely=.25, anchor="c")
rot_u = Button(window, text="/\\", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'rotation', [1, -15]))
rot_u.place(x=1075, rely=.2, anchor="c")
rot_d = Button(window, text="\\/", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'rotation', [1, 15]))
rot_d.place(x=1075, rely=.3, anchor="c")
# Zoom buttons layout
zoom_in = Button(window, text="+", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'zoom', [0.8]))
zoom_in.place(x=1025, rely=.5, anchor="c")
zoom_out = Button(window, text="-", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'zoom', [1.25]))
zoom_out.place(x=1125, rely=.5, anchor="c")
# Panning buttons layout
pan_l = Button(window, text="<-", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'translate', [-20, 0, 0]))
pan_l.place(x=1025, rely=.75, anchor="c")
pan_r = Button(window, text="->", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'translate', [20, 0, 0]))
pan_r.place(x=1125, rely=.75, anchor="c")
pan_u = Button(window, text="/\\", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'translate', [0, -20, 0]))
pan_u.place(x=1075, rely=.7, anchor="c")
pan_d = Button(window, text="\\/", width=5, command=lambda: DrawObject.plot_transform(file_select.stlobject, screen,
'translate', [0, 20, 0]))
pan_d.place(x=1075, rely=.8, anchor="c")
# ****** Keyboard Control Bindings ******
window.bind("<Left>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'rotation', [2, -15]))
window.bind("<Right>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'rotation', [2, 15]))
window.bind("<Up>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'rotation', [1, -15]))
window.bind("<Down>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'rotation', [1, 15]))
window.bind("<a>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'translate', [-20, 0, 0]))
window.bind("<d>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'translate', [20, 0, 0]))
window.bind("<w>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'translate', [0, -20, 0]))
window.bind("<s>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'translate', [0, 20, 0]))
window.bind("<k>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'zoom', [0.8]))
window.bind("<l>", lambda event: DrawObject.plot_transform(file_select.stlobject, screen, 'zoom', [1.25]))
# ****** Status Bar ******
status = Label(window, text="Waiting...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)
# ****** Run Main GUI Loop ******
window.mainloop() # Main loop to run the GUI, waits for button input