mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-21 21:58:01 -06:00
Catch EnvironmentError when writing to UFP files
This should result in a user-visible error message rather than a crashing application. Fixes Sentry issue CURA-28E.
This commit is contained in:
parent
9a65008952
commit
515c7459d3
1 changed files with 44 additions and 20 deletions
|
@ -48,35 +48,53 @@ class UFPWriter(MeshWriter):
|
||||||
archive = VirtualFile()
|
archive = VirtualFile()
|
||||||
archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)
|
archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)
|
||||||
|
|
||||||
self._writeObjectList(archive)
|
try:
|
||||||
|
self._writeObjectList(archive)
|
||||||
|
|
||||||
# Store the g-code from the scene.
|
# Store the g-code from the scene.
|
||||||
archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
|
archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
|
||||||
|
except EnvironmentError as e:
|
||||||
|
error_msg = catalog.i18nc("@info:error", "Can't write to UFP file:") + " " + str(e)
|
||||||
|
self.setInformation(error_msg)
|
||||||
|
Logger.error(error_msg)
|
||||||
|
return False
|
||||||
gcode_textio = StringIO() # We have to convert the g-code into bytes.
|
gcode_textio = StringIO() # We have to convert the g-code into bytes.
|
||||||
gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
|
gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
|
||||||
success = gcode_writer.write(gcode_textio, None)
|
success = gcode_writer.write(gcode_textio, None)
|
||||||
if not success: # Writing the g-code failed. Then I can also not write the gzipped g-code.
|
if not success: # Writing the g-code failed. Then I can also not write the gzipped g-code.
|
||||||
self.setInformation(gcode_writer.getInformation())
|
self.setInformation(gcode_writer.getInformation())
|
||||||
return False
|
return False
|
||||||
gcode = archive.getStream("/3D/model.gcode")
|
try:
|
||||||
gcode.write(gcode_textio.getvalue().encode("UTF-8"))
|
gcode = archive.getStream("/3D/model.gcode")
|
||||||
archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode")
|
gcode.write(gcode_textio.getvalue().encode("UTF-8"))
|
||||||
|
archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode")
|
||||||
|
except EnvironmentError as e:
|
||||||
|
error_msg = catalog.i18nc("@info:error", "Can't write to UFP file:") + " " + str(e)
|
||||||
|
self.setInformation(error_msg)
|
||||||
|
Logger.error(error_msg)
|
||||||
|
return False
|
||||||
|
|
||||||
# Attempt to store the thumbnail, if any:
|
# Attempt to store the thumbnail, if any:
|
||||||
backend = CuraApplication.getInstance().getBackend()
|
backend = CuraApplication.getInstance().getBackend()
|
||||||
snapshot = None if getattr(backend, "getLatestSnapshot", None) is None else backend.getLatestSnapshot()
|
snapshot = None if getattr(backend, "getLatestSnapshot", None) is None else backend.getLatestSnapshot()
|
||||||
if snapshot:
|
if snapshot:
|
||||||
archive.addContentType(extension = "png", mime_type = "image/png")
|
try:
|
||||||
thumbnail = archive.getStream("/Metadata/thumbnail.png")
|
archive.addContentType(extension = "png", mime_type = "image/png")
|
||||||
|
thumbnail = archive.getStream("/Metadata/thumbnail.png")
|
||||||
|
|
||||||
thumbnail_buffer = QBuffer()
|
thumbnail_buffer = QBuffer()
|
||||||
thumbnail_buffer.open(QBuffer.ReadWrite)
|
thumbnail_buffer.open(QBuffer.ReadWrite)
|
||||||
snapshot.save(thumbnail_buffer, "PNG")
|
snapshot.save(thumbnail_buffer, "PNG")
|
||||||
|
|
||||||
thumbnail.write(thumbnail_buffer.data())
|
thumbnail.write(thumbnail_buffer.data())
|
||||||
archive.addRelation(virtual_path = "/Metadata/thumbnail.png",
|
archive.addRelation(virtual_path = "/Metadata/thumbnail.png",
|
||||||
relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail",
|
relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail",
|
||||||
origin = "/3D/model.gcode")
|
origin = "/3D/model.gcode")
|
||||||
|
except EnvironmentError as e:
|
||||||
|
error_msg = catalog.i18nc("@info:error", "Can't write to UFP file:") + " " + str(e)
|
||||||
|
self.setInformation(error_msg)
|
||||||
|
Logger.error(error_msg)
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
Logger.log("w", "Thumbnail not created, cannot save it")
|
Logger.log("w", "Thumbnail not created, cannot save it")
|
||||||
|
|
||||||
|
@ -121,11 +139,17 @@ class UFPWriter(MeshWriter):
|
||||||
Logger.log("e", "Unable serialize material container with root id: %s", material_root_id)
|
Logger.log("e", "Unable serialize material container with root id: %s", material_root_id)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
material_file = archive.getStream(material_file_name)
|
try:
|
||||||
material_file.write(serialized_material.encode("UTF-8"))
|
material_file = archive.getStream(material_file_name)
|
||||||
archive.addRelation(virtual_path = material_file_name,
|
material_file.write(serialized_material.encode("UTF-8"))
|
||||||
relation_type = "http://schemas.ultimaker.org/package/2018/relationships/material",
|
archive.addRelation(virtual_path = material_file_name,
|
||||||
origin = "/3D/model.gcode")
|
relation_type = "http://schemas.ultimaker.org/package/2018/relationships/material",
|
||||||
|
origin = "/3D/model.gcode")
|
||||||
|
except EnvironmentError as e:
|
||||||
|
error_msg = catalog.i18nc("@info:error", "Can't write to UFP file:") + " " + str(e)
|
||||||
|
self.setInformation(error_msg)
|
||||||
|
Logger.error(error_msg)
|
||||||
|
return False
|
||||||
|
|
||||||
added_materials.append(material_file_name)
|
added_materials.append(material_file_name)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue