UFPWriter.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #Copyright (c) 2018 Ultimaker B.V.
  2. #Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import cast
  4. from Charon.VirtualFile import VirtualFile #To open UFP files.
  5. from Charon.OpenMode import OpenMode #To indicate that we want to write to UFP files.
  6. from io import StringIO #For converting g-code to bytes.
  7. from UM.Application import Application
  8. from UM.Logger import Logger
  9. from UM.Mesh.MeshWriter import MeshWriter #The writer we need to implement.
  10. from UM.PluginRegistry import PluginRegistry #To get the g-code writer.
  11. from PyQt5.QtCore import QBuffer
  12. from cura.Snapshot import Snapshot
  13. from UM.i18n import i18nCatalog
  14. catalog = i18nCatalog("cura")
  15. class UFPWriter(MeshWriter):
  16. def __init__(self):
  17. super().__init__(add_to_recent_files = False)
  18. self._snapshot = None
  19. Application.getInstance().getOutputDeviceManager().writeStarted.connect(self._createSnapshot)
  20. def _createSnapshot(self, *args):
  21. # must be called from the main thread because of OpenGL
  22. Logger.log("d", "Creating thumbnail image...")
  23. self._snapshot = Snapshot.snapshot(width = 300, height = 300)
  24. def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
  25. archive = VirtualFile()
  26. archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)
  27. #Store the g-code from the scene.
  28. archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
  29. gcode_textio = StringIO() #We have to convert the g-code into bytes.
  30. gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
  31. success = gcode_writer.write(gcode_textio, None)
  32. if not success: #Writing the g-code failed. Then I can also not write the gzipped g-code.
  33. self.setInformation(gcode_writer.getInformation())
  34. return False
  35. gcode = archive.getStream("/3D/model.gcode")
  36. gcode.write(gcode_textio.getvalue().encode("UTF-8"))
  37. archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode")
  38. #Store the thumbnail.
  39. if self._snapshot:
  40. archive.addContentType(extension = "png", mime_type = "image/png")
  41. thumbnail = archive.getStream("/Metadata/thumbnail.png")
  42. thumbnail_buffer = QBuffer()
  43. thumbnail_buffer.open(QBuffer.ReadWrite)
  44. thumbnail_image = self._snapshot
  45. thumbnail_image.save(thumbnail_buffer, "PNG")
  46. thumbnail.write(thumbnail_buffer.data())
  47. archive.addRelation(virtual_path = "/Metadata/thumbnail.png", relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail", origin = "/3D/model.gcode")
  48. else:
  49. Logger.log("d", "Thumbnail not created, cannot save it")
  50. archive.close()
  51. return True