ThreeMFWorkspaceWriter.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Workspace.WorkspaceWriter import WorkspaceWriter
  4. from UM.Application import Application
  5. from UM.Preferences import Preferences
  6. from UM.Settings.ContainerRegistry import ContainerRegistry
  7. from cura.Settings.ExtruderManager import ExtruderManager
  8. import zipfile
  9. from io import StringIO
  10. import configparser
  11. class ThreeMFWorkspaceWriter(WorkspaceWriter):
  12. def __init__(self):
  13. super().__init__()
  14. def write(self, stream, nodes, mode=WorkspaceWriter.OutputMode.BinaryMode):
  15. mesh_writer = Application.getInstance().getMeshFileHandler().getWriter("3MFWriter")
  16. if not mesh_writer: # We need to have the 3mf mesh writer, otherwise we can't save the entire workspace
  17. return False
  18. # Indicate that the 3mf mesh writer should not close the archive just yet (we still need to add stuff to it).
  19. mesh_writer.setStoreArchive(True)
  20. mesh_writer.write(stream, nodes, mode)
  21. archive = mesh_writer.getArchive()
  22. if archive is None: # This happens if there was no mesh data to write.
  23. archive = zipfile.ZipFile(stream, "w", compression = zipfile.ZIP_DEFLATED)
  24. global_container_stack = Application.getInstance().getGlobalContainerStack()
  25. # Add global container stack data to the archive.
  26. self._writeContainerToArchive(global_container_stack, archive)
  27. # Also write all containers in the stack to the file
  28. for container in global_container_stack.getContainers():
  29. self._writeContainerToArchive(container, archive)
  30. # Check if the machine has extruders and save all that data as well.
  31. for extruder_stack in ExtruderManager.getInstance().getMachineExtruders(global_container_stack.getId()):
  32. self._writeContainerToArchive(extruder_stack, archive)
  33. for container in extruder_stack.getContainers():
  34. self._writeContainerToArchive(container, archive)
  35. # Write preferences to archive
  36. original_preferences = Preferences.getInstance() #Copy only the preferences that we use to the workspace.
  37. temp_preferences = Preferences()
  38. for preference in {"general/visible_settings", "cura/active_mode", "cura/categories_expanded"}:
  39. temp_preferences.addPreference(preference, None)
  40. temp_preferences.setValue(preference, original_preferences.getValue(preference))
  41. preferences_string = StringIO()
  42. temp_preferences.writeToFile(preferences_string)
  43. preferences_file = zipfile.ZipInfo("Cura/preferences.cfg")
  44. archive.writestr(preferences_file, preferences_string.getvalue())
  45. # Save Cura version
  46. version_file = zipfile.ZipInfo("Cura/version.ini")
  47. version_config_parser = configparser.ConfigParser()
  48. version_config_parser.add_section("versions")
  49. version_config_parser.set("versions", "cura_version", Application.getStaticVersion())
  50. version_file_string = StringIO()
  51. version_config_parser.write(version_file_string)
  52. archive.writestr(version_file, version_file_string.getvalue())
  53. # Close the archive & reset states.
  54. archive.close()
  55. mesh_writer.setStoreArchive(False)
  56. return True
  57. ## Helper function that writes ContainerStacks, InstanceContainers and DefinitionContainers to the archive.
  58. # \param container That follows the \type{ContainerInterface} to archive.
  59. # \param archive The archive to write to.
  60. @staticmethod
  61. def _writeContainerToArchive(container, archive):
  62. if isinstance(container, type(ContainerRegistry.getInstance().getEmptyInstanceContainer())):
  63. return # Empty file, do nothing.
  64. file_suffix = ContainerRegistry.getMimeTypeForContainer(type(container)).preferredSuffix
  65. # Some containers have a base file, which should then be the file to use.
  66. if "base_file" in container.getMetaData():
  67. base_file = container.getMetaDataEntry("base_file")
  68. container = ContainerRegistry.getInstance().findContainers(id = base_file)[0]
  69. file_name = "Cura/%s.%s" % (container.getId(), file_suffix)
  70. if file_name in archive.namelist():
  71. return # File was already saved, no need to do it again. Uranium guarantees unique ID's, so this should hold.
  72. file_in_archive = zipfile.ZipInfo(file_name)
  73. # 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)
  74. file_in_archive.compress_type = zipfile.ZIP_DEFLATED
  75. # Do not include the network authentication keys
  76. ignore_keys = ["network_authentication_id", "network_authentication_key"]
  77. serialized_data = container.serialize(ignored_metadata_keys = ignore_keys)
  78. archive.writestr(file_in_archive, serialized_data)