UFPWriter.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. self._default_data_path ="/Cura/"
  21. def _createSnapshot(self, *args):
  22. # must be called from the main thread because of OpenGL
  23. Logger.log("d", "Creating thumbnail image...")
  24. self._snapshot = Snapshot.snapshot(width = 300, height = 300)
  25. def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
  26. archive = VirtualFile()
  27. archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)
  28. #Store the g-code from the scene.
  29. archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
  30. gcode_textio = StringIO() #We have to convert the g-code into bytes.
  31. gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
  32. success = gcode_writer.write(gcode_textio, None)
  33. if not success: #Writing the g-code failed. Then I can also not write the gzipped g-code.
  34. self.setInformation(gcode_writer.getInformation())
  35. return False
  36. gcode = archive.getStream("/3D/model.gcode")
  37. gcode.write(gcode_textio.getvalue().encode("UTF-8"))
  38. archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode")
  39. #Store the thumbnail.
  40. if self._snapshot:
  41. archive.addContentType(extension = "png", mime_type = "image/png")
  42. thumbnail = archive.getStream("/Metadata/thumbnail.png")
  43. thumbnail_buffer = QBuffer()
  44. thumbnail_buffer.open(QBuffer.ReadWrite)
  45. thumbnail_image = self._snapshot
  46. thumbnail_image.save(thumbnail_buffer, "PNG")
  47. thumbnail.write(thumbnail_buffer.data())
  48. archive.addRelation(virtual_path = "/Metadata/thumbnail.png", relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail", origin = "/3D/model.gcode")
  49. else:
  50. Logger.log("d", "Thumbnail not created, cannot save it")
  51. #Store the material.
  52. application = Application.getInstance()
  53. machine_manager = application.getMachineManager()
  54. global_stack = machine_manager.activeMachine
  55. container_registry = application.getContainerRegistry()
  56. material_extension = "xml.fdm_material"
  57. material_mime_type = "application/x-ultimaker-material-profile"
  58. try:
  59. archive.addContentType(extension=material_extension, mime_type=material_mime_type)
  60. except:
  61. Logger.log("w", "The material extension: %s was already added", material_extension)
  62. added_materials = []
  63. for extruder_stack in global_stack.extruders.values():
  64. material = extruder_stack.material
  65. material_file_name = material.getMetaData()["base_file"] + ".xml.fdm_material"
  66. material_file_name = self._default_data_path + material_file_name
  67. #Same material cannot be added
  68. if material_file_name in added_materials:
  69. continue
  70. material_containers = container_registry.findContainers(id=material.getMetaDataEntry("base_file"))
  71. if not material_containers:
  72. Logger.log("e", "Cannot find material container with id: %s", material.id)
  73. return False
  74. material_container = material_containers[0]
  75. try:
  76. serialized_material = material_container.serialize()
  77. except NotImplementedError:
  78. Logger.log("e", "Unable serialize material container with id: %s", material.id)
  79. return False
  80. material_file = archive.getStream(material_file_name)
  81. material_file.write(serialized_material.encode("UTF-8"))
  82. archive.addRelation(virtual_path=material_file_name,relation_type="http://schemas.ultimaker.org/package/2018/relationships/xml.fdm_material")
  83. added_materials.append(material_file_name)
  84. archive.close()
  85. return True