UFPWriter.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import cast, List, Dict
  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.Logger import Logger
  8. from UM.Mesh.MeshWriter import MeshWriter # The writer we need to implement.
  9. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
  10. from UM.PluginRegistry import PluginRegistry # To get the g-code writer.
  11. from PyQt5.QtCore import QBuffer
  12. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  13. from UM.Scene.SceneNode import SceneNode
  14. from cura.CuraApplication import CuraApplication
  15. from cura.Snapshot import Snapshot
  16. from cura.Utils.Threading import call_on_qt_thread
  17. from UM.i18n import i18nCatalog
  18. METADATA_OBJECTS_PATH = "metadata/objects"
  19. catalog = i18nCatalog("cura")
  20. class UFPWriter(MeshWriter):
  21. def __init__(self):
  22. super().__init__(add_to_recent_files = False)
  23. MimeTypeDatabase.addMimeType(
  24. MimeType(
  25. name = "application/x-ufp",
  26. comment = "Ultimaker Format Package",
  27. suffixes = ["ufp"]
  28. )
  29. )
  30. self._snapshot = None
  31. def _createSnapshot(self, *args):
  32. # must be called from the main thread because of OpenGL
  33. Logger.log("d", "Creating thumbnail image...")
  34. try:
  35. self._snapshot = Snapshot.snapshot(width = 300, height = 300)
  36. except Exception:
  37. Logger.logException("w", "Failed to create snapshot image")
  38. self._snapshot = None # Failing to create thumbnail should not fail creation of UFP
  39. # This needs to be called on the main thread (Qt thread) because the serialization of material containers can
  40. # trigger loading other containers. Because those loaded containers are QtObjects, they must be created on the
  41. # Qt thread. The File read/write operations right now are executed on separated threads because they are scheduled
  42. # by the Job class.
  43. @call_on_qt_thread
  44. def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
  45. archive = VirtualFile()
  46. archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)
  47. self._writeObjectList(archive)
  48. # Store the g-code from the scene.
  49. archive.addContentType(extension = "gcode", mime_type = "text/x-gcode")
  50. gcode_textio = StringIO() # We have to convert the g-code into bytes.
  51. gcode_writer = cast(MeshWriter, PluginRegistry.getInstance().getPluginObject("GCodeWriter"))
  52. success = gcode_writer.write(gcode_textio, None)
  53. if not success: # Writing the g-code failed. Then I can also not write the gzipped g-code.
  54. self.setInformation(gcode_writer.getInformation())
  55. return False
  56. gcode = archive.getStream("/3D/model.gcode")
  57. gcode.write(gcode_textio.getvalue().encode("UTF-8"))
  58. archive.addRelation(virtual_path = "/3D/model.gcode", relation_type = "http://schemas.ultimaker.org/package/2018/relationships/gcode")
  59. self._createSnapshot()
  60. # Store the thumbnail.
  61. if self._snapshot:
  62. archive.addContentType(extension = "png", mime_type = "image/png")
  63. thumbnail = archive.getStream("/Metadata/thumbnail.png")
  64. thumbnail_buffer = QBuffer()
  65. thumbnail_buffer.open(QBuffer.ReadWrite)
  66. thumbnail_image = self._snapshot
  67. thumbnail_image.save(thumbnail_buffer, "PNG")
  68. thumbnail.write(thumbnail_buffer.data())
  69. archive.addRelation(virtual_path = "/Metadata/thumbnail.png",
  70. relation_type = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail",
  71. origin = "/3D/model.gcode")
  72. else:
  73. Logger.log("d", "Thumbnail not created, cannot save it")
  74. # Store the material.
  75. application = CuraApplication.getInstance()
  76. machine_manager = application.getMachineManager()
  77. container_registry = application.getContainerRegistry()
  78. global_stack = machine_manager.activeMachine
  79. material_extension = "xml.fdm_material"
  80. material_mime_type = "application/x-ultimaker-material-profile"
  81. try:
  82. archive.addContentType(extension = material_extension, mime_type = material_mime_type)
  83. except:
  84. Logger.log("w", "The material extension: %s was already added", material_extension)
  85. added_materials = []
  86. for extruder_stack in global_stack.extruderList:
  87. material = extruder_stack.material
  88. try:
  89. material_file_name = material.getMetaData()["base_file"] + ".xml.fdm_material"
  90. except KeyError:
  91. Logger.log("w", "Unable to get base_file for the material %s", material.getId())
  92. continue
  93. material_file_name = "/Materials/" + material_file_name
  94. # The same material should not be added again.
  95. if material_file_name in added_materials:
  96. continue
  97. material_root_id = material.getMetaDataEntry("base_file")
  98. material_root_query = container_registry.findContainers(id = material_root_id)
  99. if not material_root_query:
  100. Logger.log("e", "Cannot find material container with root id {root_id}".format(root_id = material_root_id))
  101. return False
  102. material_container = material_root_query[0]
  103. try:
  104. serialized_material = material_container.serialize()
  105. except NotImplementedError:
  106. Logger.log("e", "Unable serialize material container with root id: %s", material_root_id)
  107. return False
  108. material_file = archive.getStream(material_file_name)
  109. material_file.write(serialized_material.encode("UTF-8"))
  110. archive.addRelation(virtual_path = material_file_name,
  111. relation_type = "http://schemas.ultimaker.org/package/2018/relationships/material",
  112. origin = "/3D/model.gcode")
  113. added_materials.append(material_file_name)
  114. try:
  115. archive.close()
  116. except OSError as e:
  117. error_msg = catalog.i18nc("@info:error", "Can't write to UFP file:") + " " + str(e)
  118. self.setInformation(error_msg)
  119. Logger.error(error_msg)
  120. return False
  121. return True
  122. @staticmethod
  123. def _writeObjectList(archive):
  124. """Write a json list of object names to the METADATA_OBJECTS_PATH metadata field
  125. To retrieve, use: `archive.getMetadata(METADATA_OBJECTS_PATH)`
  126. """
  127. objects_model = CuraApplication.getInstance().getObjectsModel()
  128. object_metas = []
  129. for item in objects_model.items:
  130. object_metas.extend(UFPWriter._getObjectMetadata(item["node"]))
  131. data = {METADATA_OBJECTS_PATH: object_metas}
  132. archive.setMetadata(data)
  133. @staticmethod
  134. def _getObjectMetadata(node: SceneNode) -> List[Dict[str, str]]:
  135. """Get object metadata to write for a Node.
  136. :return: List of object metadata dictionaries.
  137. Might contain > 1 element in case of a group node.
  138. Might be empty in case of nonPrintingMesh
  139. """
  140. return [{"name": item.getName()}
  141. for item in DepthFirstIterator(node)
  142. if item.getMeshData() is not None and not item.callDecoration("isNonPrintingMesh")]