-
Notifications
You must be signed in to change notification settings - Fork 3
/
flatcam.py
executable file
·185 lines (150 loc) · 6.18 KB
/
flatcam.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
#!/usr/bin/python3
import sys
import os
import traceback
from datetime import datetime
from PyQt6 import QtWidgets, QtGui
from PyQt6.QtCore import QSettings, QTimer
from appMain import App
from appGUI import VisPyPatches
from appGUI.GUIElements import FCMessageBox
from multiprocessing import freeze_support
MIN_VERSION_MAJOR = 3
MIN_VERSION_MINOR = 6
def debug_trace():
"""
Set a tracepoint in the Python debugger that works with Qt
:return: None
"""
from PyQt6.QtCore import pyqtRemoveInputHook
# from pdb import set_trace
pyqtRemoveInputHook()
# set_trace()
if __name__ == '__main__':
# All X11 calling should be thread safe otherwise we have strange issues
# QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
# NOTE: Never talk to the GUI from threads! This is why I commented the above.
freeze_support()
portable = False
# Folder for user settings.
if sys.platform == 'win32':
# #######################################################################################################
# ####### CONFIG FILE WITH PARAMETERS REGARDING PORTABILITY #############################################
# #######################################################################################################
config_file = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '\\config\\configuration.txt'
try:
with open(config_file, 'r'):
pass
except FileNotFoundError:
config_file = os.path.dirname(os.path.realpath(__file__)) + '\\config\\configuration.txt'
with open(config_file, 'r') as f:
for line in f:
param = str(line).replace('\n', '').rpartition('=')
if param[0] == 'portable':
try:
portable = eval(param[2])
except NameError:
portable = False
if portable is False:
# data_path = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, None, 0) + '\\FlatCAM'
data_path = os.path.join(os.getenv('appdata'), 'FlatCAM')
else:
data_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '\\config'
else:
data_path = os.path.expanduser('~') + '/.FlatCAM'
if not os.path.exists(data_path):
os.makedirs(data_path)
log_file_path = os.path.join(data_path, "log.txt")
major_v = sys.version_info.major
minor_v = sys.version_info.minor
v_msg = "FlatCAM Evo uses PYTHON 3 or later. The version minimum is %s.%s\n"\
"Your Python version is: %s.%s" % (MIN_VERSION_MAJOR, MIN_VERSION_MINOR, str(major_v), str(minor_v))
# Supported Python version is >= 3.6
if major_v < MIN_VERSION_MAJOR or (major_v >= MIN_VERSION_MAJOR and minor_v < MIN_VERSION_MINOR):
print(v_msg)
msg = '%s\n' % str(datetime.today())
msg += v_msg
try:
with open(log_file_path) as f:
log_file = f.read()
log_file += '\n' + msg
with open(log_file_path, 'w') as f:
f.write(log_file)
except IOError:
with open(log_file_path, 'w') as f:
f.write(msg)
# if minor_v >= 8:
# os._exit(0)
# else:
# sys.exit(0)
sys.exit(0)
debug_trace()
VisPyPatches.apply_patches()
def excepthook(exc_type, exc_value, exc_tb):
msg = '%s\n' % str(datetime.today())
if exc_type != KeyboardInterrupt:
msg += "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
try:
with open(log_file_path) as f:
log_file = f.read()
log_file += '\n' + msg
with open(log_file_path, 'w') as f:
f.write(log_file)
except IOError:
with open(log_file_path, 'w') as f:
f.write(msg)
# show the message
try:
msgbox = FCMessageBox()
displayed_msg = "The application encountered a critical error and it will close.\n"\
"Please report this error to the developers."
title = "Critical Error"
msgbox.setWindowTitle(title) # taskbar still shows it
ic = QtGui.QIcon()
ic.addPixmap(QtGui.QPixmap("assets/resources/warning.png"), QtGui.QIcon.Mode.Normal)
msgbox.setWindowIcon(ic)
msgbox.setText('<b>%s</b>' % displayed_msg)
msgbox.setDetailedText(msg)
msgbox.setIcon(QtWidgets.QMessageBox.Icon.Critical)
bt_yes = msgbox.addButton("Quit", QtWidgets.QMessageBox.ButtonRole.YesRole)
bt_ret = msgbox.addButton("Return", QtWidgets.QMessageBox.ButtonRole.NoRole)
msgbox.setDefaultButton(bt_yes)
# msgbox.setTextFormat(Qt.TextFormat.RichText)
msgbox.exec()
response = msgbox.clickedButton()
if response == bt_ret:
pass
except Exception:
QtWidgets.QApplication.quit()
else:
QtWidgets.QApplication.quit()
# or QtWidgets.QApplication.exit(0)
sys.excepthook = excepthook
app = QtWidgets.QApplication(sys.argv)
# apply style
settings = QSettings("Open Source", "FlatCAM_EVO")
if settings.contains("style"):
style_index = settings.value('style', type=str)
try:
idx = int(style_index)
except Exception:
idx = 0
style = QtWidgets.QStyleFactory.keys()[idx]
app.setStyle(style)
else:
app.setStyle('windowsvista')
if settings.contains("font_size"):
font_size = int(settings.value("font_size", type=str)) # noqa
font = QtGui.QFont()
font.setPointSize(font_size)
app.setFont(font)
fc = App(qapp=app)
# interrupt the Qt loop such that Python events have a chance to be responsive
timer = QTimer()
timer.timeout.connect(lambda: None)
timer.start(100)
try:
sys.exit(app.exec())
except SystemError:
pass
# app.exec()