ThreeMFWorkspaceWriter.py 5.8 KB

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