-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
259 lines (219 loc) · 10.1 KB
/
gui.py
File metadata and controls
259 lines (219 loc) · 10.1 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
259
from PySide6 import QtWidgets
# Important:
# You need to run the following command to generate the ui_form.py file
# pyside6-uic form.ui -o ui_form.py, or
# pyside2-uic form.ui -o ui_form.py
from ui_MainWindow import Ui_MainWindow
import os
import sys
import time
import usb.core
import subprocess
import configparser
from PySide6 import QtGui
from threading import Thread
from multiprocessing import Value
from StackShot3X_API_for_Python.commdefs import *
sys.path.append('./StackShot3X_API_for_Python')
from StackShot3X_API_for_Python.stackshot_controller import StackShotController
from action_parser import action_parser
from exec_actions import exec_actions
def isValidValue(s, min, max):
try:
int(s)
except ValueError:
return False
b = int(s)
if min <= b <= max:
return True
else:
return False
def moveAxis(controller, axis, dir, dist, speedPercent):
try:
controller.move_at_speed(axis, dir, dist, speedPercent / 100.0)
except:
# pass
print('move')
def stopAxis(controller, axis):
try:
controller.stop(axis)
except:
# pass
print('stop')
class GUI(QtWidgets.QMainWindow):
def __init__(self):
super(GUI, self).__init__()
self.gui = Ui_MainWindow()
self.gui.setupUi(self)
self.gui.startButton.clicked.connect(self.startAction)
self.gui.stopButton.clicked.connect(self.stopAction)
self.gui.saveActionButton.clicked.connect(self.saveAction)
self.gui.loadActionButton.clicked.connect(self.loadAction)
self.gui.imageSrcFolderReferenceButton.clicked.connect(self.updateImageSrcFolder)
self.gui.imageSaveFolderReferenceButton.clicked.connect(self.updateImageSaveFolder)
self.gui.metashapeProjectFolderPathReferenceButton.clicked.connect(self.updateMetashapeProjectFolder)
short_dist = 0.1
middle_dist = 1.0
long_dist = 5.0
# move forward
self.gui.fwdShortPushButton.pressed.connect(lambda: self.moveStackShot(RailDir.FWD, short_dist))
self.gui.fwdMiddlePushButton.pressed.connect(lambda: self.moveStackShot(RailDir.FWD, middle_dist))
self.gui.fwdLongPushButton.pressed.connect(lambda: self.moveStackShot(RailDir.FWD, long_dist))
# move back
self.gui.backShortPushButton.pressed.connect(lambda: self.moveStackShot(RailDir.BACK, short_dist))
self.gui.backMiddlePushButton.pressed.connect(lambda: self.moveStackShot(RailDir.BACK, middle_dist))
self.gui.backLongPushButton.pressed.connect(lambda: self.moveStackShot(RailDir.BACK, long_dist))
self.config = configparser.ConfigParser()
self.loadConfig()
self.working_thread = None
self.stop_flag = Value('i', 0) # 0: false, 1: true
# when start app, connect with StackShot3X
self.controller = StackShotController()
try:
print('connecting to Stackshot3X...')
self.controller.open()
self.working_thread = Thread(target=self.controller.get_status, args=(RailAxis.ANY,), daemon=True)
self.working_thread.start()
self.working_thread.join(timeout=3)
if self.working_thread.is_alive() == True:
raise RuntimeError("connection failed.")
except Exception as excpt:
print(excpt)
exit()
# load config from config.ini
def loadConfig(self):
self.config.read('config.ini')
if not 'general' in self.config:
self.config['general'] = {}
# general/image_src_folder
if 'image_src_folder' in self.config['general'] and 0 < len(self.config['general']['image_src_folder']):
self.gui.imageSrcFolderPath.setText(self.config['general']['image_src_folder'])
else:
self.gui.imageSrcFolderPath.setText('Not selected.')
# general/image_save_folder
if 'image_save_folder' in self.config['general'] and 0 < len(self.config['general']['image_save_folder']):
self.gui.imageSaveFolderPath.setText(self.config['general']['image_save_folder'])
else:
self.gui.imageSaveFolderPath.setText('Not selected.')
# general/metashape_project_folder
if 'metashape_project_folder' in self.config['general'] and 0 < len(self.config['general']['metashape_project_folder']):
self.gui.metashapeProjectFolderPath.setText(self.config['general']['metashape_project_folder'])
else:
self.gui.metashapeProjectFolderPath.setText('Not selected.')
# general/brackets
if 'brackets' in self.config['general'] and \
isValidValue(self.config['general']['brackets'], \
self.gui.brackets.minimum(), self.gui.brackets.maximum()) == True:
self.gui.brackets.setValue(int(self.config['general']['brackets']))
else:
self.gui.brackets.setValue(self.gui.brackets.minimum())
self.config['general']['brackets'] = str(self.gui.brackets.minimum())
f = open('config.ini', 'w')
self.config.write(f)
f.close()
# general/speed_percent
if 'speed_percent' in self.config['general'] and \
isValidValue(self.config['general']['speed_percent'], \
self.gui.speedPercent.minimum(), self.gui.speedPercent.maximum()) == True:
self.gui.speedPercent.setValue(int(self.config['general']['speed_percent']))
else:
self.gui.speedPercent.setValue(self.gui.brackets.minimum())
self.config['general']['speed_percent'] = str(self.gui.speedPercent.maximum())
f = open('config.ini', 'w')
self.config.write(f)
f.close()
# select "Image Src Folder" PATH from file dialog and save to config
def updateImageSrcFolder(self):
file = QtWidgets.QFileDialog.getExistingDirectory()
if len(file) != 0:
self.gui.imageSrcFolderPath.setText(file)
self.config['general']['image_src_folder'] = self.gui.imageSrcFolderPath.text()
f = open('config.ini', 'w')
self.config.write(f)
f.close()
# select "Image Save Folder" PATH from file dialog and save to config
def updateImageSaveFolder(self):
file = QtWidgets.QFileDialog.getExistingDirectory()
if len(file) != 0:
self.gui.imageSaveFolderPath.setText(file)
self.config['general']['image_save_folder'] = self.gui.imageSaveFolderPath.text()
f = open('config.ini', 'w')
self.config.write(f)
f.close()
# select "Metashpae Project Folder" PATH from file dialog and save to config
def updateMetashapeProjectFolder(self):
file = QtWidgets.QFileDialog.getExistingDirectory()
if len(file) != 0:
self.gui.metashapeProjectFolderPath.setText(file)
self.config['general']['metashape_project_folder'] = self.gui.metashapeProjectFolderPath.text()
f = open('config.ini', 'w')
self.config.write(f)
f.close()
def saveBracketsAndSpeed(self):
self.config['general']['brackets'] = str(self.gui.brackets.value())
self.config['general']['speed_percent'] = str(self.gui.speedPercent.value())
f = open('config.ini', 'w')
self.config.write(f)
f.close()
# load action and display on "Actions Panel"
def loadAction(self):
fname = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File', '.', "Text file (*.txt)")
if fname[0]:
f = open(fname[0], 'r')
actions = f.read()
f.close()
self.gui.actionsPanel.setPlainText(actions)
# save action written in "Actions Panel"
def saveAction(self):
fname = QtWidgets.QFileDialog.getSaveFileName(self, 'Save As', '.', "Text file (*.txt)")
if fname[0]:
f = open(fname[0], 'w')
f.write(self.gui.actionsPanel.toPlainText())
f.close()
def moveStackShot(self, dir: RailDir, dist: float):
axis = None
if self.gui.xRadioButton.isChecked() == True:
axis = RailAxis.X
elif self.gui.yRadioButton.isChecked() == True:
axis = RailAxis.Y
elif self.gui.zRadioButton.isChecked() == True:
axis = RailAxis.Z
moveAxis(self.controller, axis, dir, dist, self.gui.speedPercent.value())
# wait for rail stop
while self.controller.get_status(axis) != RailStatus.IDLE:
time.sleep(0.2)
# start action
def startAction(self):
print('start')
self.saveBracketsAndSpeed()
# set stop_flag to 0(false)
with self.stop_flag.get_lock():
self.stop_flag.value = 0 # false
# create and thread to execute actions
# exec_actions.py内のexec_action関数を別スレッドで起動
self.working_thread = Thread(target=exec_actions, args=(self.stop_flag, self.gui.actionsPanel.toPlainText(), self.controller, self.gui.brackets.value(), self.gui.doFocusStacking.isChecked(), self.gui.doMetashape.isChecked(), self.gui.speedPercent.value(), self.config), daemon=True)
self.working_thread.start()
# stop in progress action
def stopAction(self):
if self.working_thread != None and self.working_thread.is_alive():
print('stop')
# set stop_flag to 1(true)
with self.stop_flag.get_lock():
self.stop_flag.value = 1 # true
# when close window, this function is fired
def closeEvent(self, event: QtGui.QCloseEvent):
if self.working_thread != None and self.working_thread.is_alive():
print('stop')
with self.stop_flag.get_lock():
self.stop_flag.value = 1 # true
self.working_thread.join()
# after stop all axis, disconnect from StackShot3X
try:
print('discoonnecting from Stackshot3X...')
self.controller.stop(RailAxis.X)
self.controller.stop(RailAxis.Y)
self.controller.stop(RailAxis.Z)
self.controller.close()
except Exception as excpt:
print(excpt)
return