ThreeMFWorkspaceWriter.py 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from UM.Workspace.WorkspaceWriter import WorkspaceWriter
  2. from UM.Application import Application
  3. from UM.Preferences import Preferences
  4. from UM.Settings.ContainerRegistry import ContainerRegistry
  5. from UM.Settings.ContainerStack import ContainerStack
  6. from cura.Settings.ExtruderManager import ExtruderManager
  7. import zipfile
  8. from io import StringIO
  9. import copy
  10. class ThreeMFWorkspaceWriter(WorkspaceWriter):
  11. def __init__(self):
  12. super().__init__()
  13. def write(self, stream, nodes, mode=WorkspaceWriter.OutputMode.BinaryMode):
  14. mesh_writer = Application.getInstance().getMeshFileHandler().getWriter("3MFWriter")
  15. if not mesh_writer: # We need to have the 3mf mesh writer, otherwise we can't save the entire workspace
  16. return False
  17. # Indicate that the 3mf mesh writer should not close the archive just yet (we still need to add stuff to it).
  18. mesh_writer.setStoreArchive(True)
  19. mesh_writer.write(stream, nodes, mode)
  20. archive = mesh_writer.getArchive()
  21. if archive is None: # This happens if there was no mesh data to write.
  22. archive = zipfile.ZipFile(stream, "w", compression = zipfile.ZIP_DEFLATED)
  23. global_container_stack = Application.getInstance().getGlobalContainerStack()
  24. # Add global container stack data to the archive.
  25. self._writeContainerToArchive(global_container_stack, archive)
  26. # Also write all containers in the stack to the file
  27. for container in global_container_stack.getContainers():
  28. self._writeContainerToArchive(container, archive)
  29. # Check if the machine has extruders and save all that data as well.
  30. for extruder_stack in ExtruderManager.getInstance().getMachineExtruders(global_container_stack.getId()):
  31. self._writeContainerToArchive(extruder_stack, archive)
  32. for container in extruder_stack.getContainers():
  33. self._writeContainerToArchive(container, archive)
  34. # Write preferences to archive
  35. preferences_file = zipfile.ZipInfo("Cura/preferences.cfg")
  36. preferences_string = StringIO()
  37. Preferences.getInstance().writeToFile(preferences_string)
  38. archive.writestr(preferences_file, preferences_string.getvalue())
  39. # Close the archive & reset states.
  40. archive.close()
  41. mesh_writer.setStoreArchive(False)
  42. return True
  43. ## Helper function that writes ContainerStacks, InstanceContainers and DefinitionContainers to the archive.
  44. # \param container That follows the \type{ContainerInterface} to archive.
  45. # \param archive The archive to write to.
  46. @staticmethod
  47. def _writeContainerToArchive(container, archive):
  48. if type(container) == type(ContainerRegistry.getInstance().getEmptyInstanceContainer()):
  49. return # Empty file, do nothing.
  50. file_suffix = ContainerRegistry.getMimeTypeForContainer(type(container)).preferredSuffix
  51. # Some containers have a base file, which should then be the file to use.
  52. if "base_file" in container.getMetaData():
  53. base_file = container.getMetaDataEntry("base_file")
  54. container = ContainerRegistry.getInstance().findContainers(id = base_file)[0]
  55. file_name = "Cura/%s.%s" % (container.getId(), file_suffix)
  56. if file_name in archive.namelist():
  57. return # File was already saved, no need to do it again. Uranium guarantees unique ID's, so this should hold.
  58. file_in_archive = zipfile.ZipInfo(file_name)
  59. # For some reason we have to set the compress type of each file as well (it doesn't keep the type of the entire archive)
  60. file_in_archive.compress_type = zipfile.ZIP_DEFLATED
  61. if type(container) == ContainerStack and (container.getMetaDataEntry("network_authentication_id") or container.getMetaDataEntry("network_authentication_key")):
  62. # TODO: Hack
  63. # Create a shallow copy of the container, so we can filter out the network auth (if any)
  64. container_copy = copy.deepcopy(container)
  65. container_copy.removeMetaDataEntry("network_authentication_id")
  66. container_copy.removeMetaDataEntry("network_authentication_key")
  67. serialized_data = container_copy.serialize()
  68. else:
  69. serialized_data = container.serialize()
  70. archive.writestr(file_in_archive, serialized_data)