forked from rgomezjnr/GcodeFilenameFormatPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDosNameOutputDevice.py
243 lines (185 loc) · 9.87 KB
/
DosNameOutputDevice.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
# LGPLv3
import re
import os.path
import sys
from typing import cast
from PyQt6.QtCore import QDateTime
from PyQt6.QtCore import QObject
from UM.i18n import i18nCatalog
from UM.Extension import Extension
from UM.Application import Application
from UM.Qt.Duration import DurationFormat
from UM.PluginRegistry import PluginRegistry
from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
from UM.Scene.SceneNode import SceneNode
from UM.Version import Version
from cura.CuraApplication import CuraApplication
from cura.Settings.ExtruderManager import ExtruderManager
from cura.UI.ObjectsModel import ObjectsModel
from GcodeFilenameFormatPlus.ParseFilenameFormat import parseFilenameFormat
from GcodeFilenameFormatPlus.PrintSettingConverter import getPrintSettings
from PyQt6.QtCore import QUrl
from PyQt6.QtGui import QDesktopServices
from PyQt6.QtWidgets import QFileDialog, QMessageBox
from UM.Message import Message
from UM.Logger import Logger
from UM.Mesh.MeshWriter import MeshWriter
from UM.FileHandler.WriteFileJob import WriteFileJob
from UM.OutputDevice import OutputDeviceError
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from GcodeFilenameFormatPlus.PrintSettingConverter import getPrintSettings
catalog = i18nCatalog("uranium")
class DosNameOutputDevicePlugin(OutputDevicePlugin):
def start(self):
self.getOutputDeviceManager().addOutputDevice(DosNameOutputDevice())
def stop(self):
self.getOutputDeviceManager().removeOutputDevice("dos_name_output_device")
class DosNameOutputDevice(OutputDevice):
def __init__(self):
# Give an ID which is used to refer to the output device.
super().__init__("dos_name_output_device")
Application.getInstance().getPreferences().addPreference(
"dos_name_output_device/last_used_type", "")
Application.getInstance().getPreferences().addPreference(
"dos_name_output_device/dialog_save_path", "")
# Optionally set some metadata.
# Human-readable name (you may want to internationalise this). Gets put in messages and such.
self.setName("DOS File Name Output Device")
# This is put on the save button.
self.setShortDescription("Save DOS Name")
self.setDescription("Save DOS Format File Name")
self.setIconName("save")
self._writing = False
def requestWrite(self, nodes, file_name=None, limit_mimetypes=None, file_handler=None, **kwargs):
fnext = ".g"
application = cast(CuraApplication, Application.getInstance())
machine_manager = application.getMachineManager()
#global_stack = machine_manager.activeMachine
print_information = application.getPrintInformation()
job_name = print_information.jobName
filename_format = application.getPreferences().getValue(
"gcode_filename_format_plus/filename_format")
# QMessageBox.information(None,'filename_format',filename_format)
print_settings = getPrintSettings(filename_format)
if print_settings:
file_name = parseFilenameFormat(print_settings, filename_format)
# QMessageBox.information(None,'file_name',file_name)
if file_name is None:
file_name = job_name
file_name = file_name[0:8] + fnext
if self._writing:
raise OutputDeviceError.DeviceBusyError()
if not file_handler:
file_handler = Application.getInstance().getMeshFileHandler()
file_types = file_handler.getSupportedFileTypesWrite()
selected_type = None
for item in file_types:
# QMessageBox.information(None,'id',item["id"])
# QMessageBox.information(None,'mime_type',item["mime_type"])
if item["id"] == 'GCodeWriter':
selected_type = item
break
if selected_type is None:
raise Exception('GCodeWrite Not Found')
dialog = QFileDialog()
dialog.setWindowTitle(catalog.i18nc("@title:window", "Save to File"))
dialog.setFileMode(QFileDialog.FileMode.AnyFile)
dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
dialog.setOption(QFileDialog.Option.DontConfirmOverwrite)
if sys.platform == "linux" and "KDE_FULL_SESSION" in os.environ:
dialog.setOption(QFileDialog.Option.DontUseNativeDialog)
filters = ["GCode File (*"+fnext+")"]
selected_filter = None
stored_directory = Application.getInstance().getPreferences().getValue(
"dos_name_output_device/dialog_save_path")
dialog.setDirectory(stored_directory)
if file_name is not None:
dialog.selectFile(file_name)
dialog.setNameFilters(filters)
if selected_filter is not None:
dialog.selectNameFilter(selected_filter)
if not dialog.exec():
raise OutputDeviceError.UserCanceledError()
save_path = dialog.directory().absolutePath()
Application.getInstance().getPreferences().setValue(
"dos_name_output_device/dialog_save_path", save_path)
file_name = dialog.selectedFiles()[0]
fn = os.path.splitext(os.path.basename(file_name))
fn = (fn[0][0:8] + fnext).replace(" ", "")
file_name = os.path.join(os.path.dirname(file_name), fn)
#QMessageBox.information(None, 'file_name', file_name)
if os.path.exists(file_name):
result = QMessageBox.question(None, catalog.i18nc("@title:window", "File Already Exists"), catalog.i18nc(
"@label Don't translate the XML tag <filename>!", "The file <filename>{0}</filename> already exists. Are you sure you want to overwrite it?").format(file_name))
if result == QMessageBox.ButtonRole.NoRole:
raise OutputDeviceError.UserCanceledError()
self.writeStarted.emit(self)
if file_handler:
file_writer = file_handler.getWriter(selected_type["id"])
else:
file_writer = Application.getInstance(
).getMeshFileHandler().getWriter(selected_type["id"])
try:
mode = selected_type["mode"]
if mode == MeshWriter.OutputMode.TextMode:
Logger.log(
"d", "Writing to Local File %s in text mode", file_name)
stream = open(file_name, "wt", encoding="utf-8")
elif mode == MeshWriter.OutputMode.BinaryMode:
Logger.log(
"d", "Writing to Local File %s in binary mode", file_name)
stream = open(file_name, "wb")
else:
Logger.log("e", "Unrecognised OutputMode.")
return None
job = WriteFileJob(file_writer, stream, nodes, mode)
job.setFileName(file_name)
job.setAddToRecentFiles(True)
job.progress.connect(self._onJobProgress)
job.finished.connect(self._onWriteJobFinished)
message = Message(catalog.i18nc("@info:progress Don't translate the XML tags <filename>!", "Saving to <filename>{0}</filename>").format(file_name),
0, False, -1, catalog.i18nc("@info:title", "Saving"))
message.show()
job.setMessage(message)
self._writing = True
job.start()
except PermissionError as e:
Logger.log(
"e", "Permission denied when trying to write to %s: %s", file_name, str(e))
raise OutputDeviceError.PermissionDeniedError(catalog.i18nc(
"@info:status Don't translate the XML tags <filename>!", "Permission denied when trying to save <filename>{0}</filename>").format(file_name)) from e
except OSError as e:
Logger.log(
"e", "Operating system would not let us write to %s: %s", file_name, str(e))
raise OutputDeviceError.WriteRequestFailedError(catalog.i18nc(
"@info:status Don't translate the XML tags <filename> or <message>!", "Could not save to <filename>{0}</filename>: <message>{1}</message>").format()) from e
def _onJobProgress(self, job, progress):
self.writeProgress.emit(self, progress)
def _onWriteJobFinished(self, job):
self._writing = False
self.writeFinished.emit(self)
if job.getResult():
self.writeSuccess.emit(self)
message = Message(catalog.i18nc("@info:status Don't translate the XML tags <filename>!",
"Saved to <filename>{0}</filename>").format(job.getFileName()), title=catalog.i18nc("@info:title", "File Saved"))
message.addAction("open_folder", catalog.i18nc("@action:button", "Open Folder"),
"open-folder", catalog.i18nc("@info:tooltip", "Open the folder containing the file"))
message._folder = os.path.dirname(job.getFileName())
message.actionTriggered.connect(self._onMessageActionTriggered)
message.show()
else:
message = Message(catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!", "Could not save to <filename>{0}</filename>: <message>{1}</message>").format(
job.getFileName(), str(job.getError())), lifetime=0, title=catalog.i18nc("@info:title", "Warning"))
message.show()
self.writeError.emit(self)
try:
job.getStream().close()
except (OSError, PermissionError):
message = Message(catalog.i18nc("@info:status", "Something went wrong saving to <filename>{0}</filename>: <message>{1}</message>").format(
job.getFileName(), str(job.getError())), title=catalog.i18nc("@info:title", "Error"))
message.show()
self.writeError.emit(self)
def _onMessageActionTriggered(self, message, action):
if action == "open_folder" and hasattr(message, "_folder"):
QDesktopServices.openUrl(QUrl.fromLocalFile(message._folder))