Browse Source

Add the slicemetadata.json file into the makerbot file archive

CURA-12005
Erwan MATHIEU 8 months ago
parent
commit
7ba48bfa97

+ 61 - 1
cura/API/Interface/Settings.py

@@ -1,7 +1,13 @@
 # Copyright (c) 2018 Ultimaker B.V.
 # Cura is released under the terms of the LGPLv3 or higher.
 
-from typing import TYPE_CHECKING
+from dataclasses import asdict
+
+from typing import cast, Dict, TYPE_CHECKING
+
+from UM.Settings.InstanceContainer import InstanceContainer
+from UM.Settings.SettingFunction import SettingFunction
+from cura.Settings.GlobalStack import GlobalStack
 
 if TYPE_CHECKING:
     from cura.CuraApplication import CuraApplication
@@ -47,3 +53,57 @@ class Settings:
         """
 
         return self.application.getSidebarCustomMenuItems()
+
+    def getSliceMetadata(self) -> Dict[str, Dict[str, Dict[str, str]]]:
+        """Get all changed settings and all settings. For each extruder and the global stack"""
+        print_information = self.application.getPrintInformation()
+        machine_manager = self.application.getMachineManager()
+        settings = {
+            "material": {
+                "length": print_information.materialLengths,
+                "weight": print_information.materialWeights,
+                "cost": print_information.materialCosts,
+            },
+            "global": {
+                "changes": {},
+                "all_settings": {},
+            },
+            "quality": asdict(machine_manager.activeQualityDisplayNameMap()),
+        }
+
+        def _retrieveValue(container: InstanceContainer, setting_: str):
+            value_ = container.getProperty(setting_, "value")
+            for _ in range(0, 1024):  # Prevent possibly endless loop by not using a limit.
+                if not isinstance(value_, SettingFunction):
+                    return value_  # Success!
+                value_ = value_(container)
+            return 0  # Fallback value after breaking possibly endless loop.
+
+        global_stack = cast(GlobalStack, self.application.getGlobalContainerStack())
+
+        # Add global user or quality changes
+        global_flattened_changes = InstanceContainer.createMergedInstanceContainer(global_stack.userChanges, global_stack.qualityChanges)
+        for setting in global_flattened_changes.getAllKeys():
+            settings["global"]["changes"][setting] = _retrieveValue(global_flattened_changes, setting)
+
+        # Get global all settings values without user or quality changes
+        for setting in global_stack.getAllKeys():
+            settings["global"]["all_settings"][setting] = _retrieveValue(global_stack, setting)
+
+        for i, extruder in enumerate(global_stack.extruderList):
+            # Add extruder fields to settings dictionary
+            settings[f"extruder_{i}"] = {
+                "changes": {},
+                "all_settings": {},
+            }
+
+            # Add extruder user or quality changes
+            extruder_flattened_changes = InstanceContainer.createMergedInstanceContainer(extruder.userChanges, extruder.qualityChanges)
+            for setting in extruder_flattened_changes.getAllKeys():
+                settings[f"extruder_{i}"]["changes"][setting] = _retrieveValue(extruder_flattened_changes, setting)
+
+            # Get extruder all settings values without user or quality changes
+            for setting in extruder.getAllKeys():
+                settings[f"extruder_{i}"]["all_settings"][setting] = _retrieveValue(extruder, setting)
+
+        return settings

+ 3 - 0
plugins/MakerbotWriter/MakerbotWriter.py

@@ -137,6 +137,9 @@ class MakerbotWriter(MeshWriter):
                 for png_file in png_files:
                     file, data = png_file["file"], png_file["data"]
                     zip_stream.writestr(file, data)
+                api = CuraApplication.getInstance().getCuraAPI()
+                slice_metadata = json.dumps(api.interface.settings.getSliceMetadata(), separators=(", ", ": "), indent=4)
+                zip_stream.writestr("slicemetadata.json", slice_metadata)
         except (IOError, OSError, BadZipFile) as ex:
             Logger.log("e", f"Could not write to (.makerbot) file because: '{ex}'.")
             self.setInformation(catalog.i18nc("@error", "MakerbotWriter could not save to the designated path."))

+ 3 - 55
plugins/UFPWriter/UFPWriter.py

@@ -24,6 +24,7 @@ from UM.Settings.InstanceContainer import InstanceContainer
 from cura.CuraApplication import CuraApplication
 from cura.Settings.GlobalStack import GlobalStack
 from cura.Utils.Threading import call_on_qt_thread
+from cura.API import CuraAPI
 
 from UM.i18n import i18nCatalog
 
@@ -85,7 +86,8 @@ class UFPWriter(MeshWriter):
         try:
             archive.addContentType(extension="json", mime_type="application/json")
             setting_textio = StringIO()
-            json.dump(self._getSliceMetadata(), setting_textio, separators=(", ", ": "), indent=4)
+            api = CuraApplication.getInstance().getCuraAPI()
+            json.dump(api.interface.settings.getSliceMetadata(), setting_textio, separators=(", ", ": "), indent=4)
             steam = archive.getStream(SLICE_METADATA_PATH)
             steam.write(setting_textio.getvalue().encode("UTF-8"))
         except EnvironmentError as e:
@@ -210,57 +212,3 @@ class UFPWriter(MeshWriter):
         return [{"name": item.getName()}
                 for item in DepthFirstIterator(node)
                 if item.getMeshData() is not None and not item.callDecoration("isNonPrintingMesh")]
-
-    def _getSliceMetadata(self) -> Dict[str, Dict[str, Dict[str, str]]]:
-        """Get all changed settings and all settings. For each extruder and the global stack"""
-        print_information = CuraApplication.getInstance().getPrintInformation()
-        machine_manager = CuraApplication.getInstance().getMachineManager()
-        settings = {
-            "material": {
-                "length": print_information.materialLengths,
-                "weight": print_information.materialWeights,
-                "cost": print_information.materialCosts,
-            },
-            "global": {
-                "changes": {},
-                "all_settings": {},
-            },
-            "quality": asdict(machine_manager.activeQualityDisplayNameMap()),
-        }
-
-        def _retrieveValue(container: InstanceContainer, setting_: str):
-            value_ = container.getProperty(setting_, "value")
-            for _ in range(0, 1024):  # Prevent possibly endless loop by not using a limit.
-                if not isinstance(value_, SettingFunction):
-                    return value_  # Success!
-                value_ = value_(container)
-            return 0  # Fallback value after breaking possibly endless loop.
-
-        global_stack = cast(GlobalStack, Application.getInstance().getGlobalContainerStack())
-
-        # Add global user or quality changes
-        global_flattened_changes = InstanceContainer.createMergedInstanceContainer(global_stack.userChanges, global_stack.qualityChanges)
-        for setting in global_flattened_changes.getAllKeys():
-            settings["global"]["changes"][setting] = _retrieveValue(global_flattened_changes, setting)
-
-        # Get global all settings values without user or quality changes
-        for setting in global_stack.getAllKeys():
-            settings["global"]["all_settings"][setting] = _retrieveValue(global_stack, setting)
-
-        for i, extruder in enumerate(global_stack.extruderList):
-            # Add extruder fields to settings dictionary
-            settings[f"extruder_{i}"] = {
-                "changes": {},
-                "all_settings": {},
-            }
-
-            # Add extruder user or quality changes
-            extruder_flattened_changes = InstanceContainer.createMergedInstanceContainer(extruder.userChanges, extruder.qualityChanges)
-            for setting in extruder_flattened_changes.getAllKeys():
-                settings[f"extruder_{i}"]["changes"][setting] = _retrieveValue(extruder_flattened_changes, setting)
-
-            # Get extruder all settings values without user or quality changes
-            for setting in extruder.getAllKeys():
-                settings[f"extruder_{i}"]["all_settings"][setting] = _retrieveValue(extruder, setting)
-
-        return settings