Ejemplo n.º 1
0
    def write(self, stream, nodes, mode=WorkspaceWriter.OutputMode.BinaryMode):
        application = Application.getInstance()
        machine_manager = application.getMachineManager()

        mesh_writer = application.getMeshFileHandler().getWriter("3MFWriter")

        if not mesh_writer:  # We need to have the 3mf mesh writer, otherwise we can't save the entire workspace
            return False

        # Indicate that the 3mf mesh writer should not close the archive just yet (we still need to add stuff to it).
        mesh_writer.setStoreArchive(True)
        mesh_writer.write(stream, nodes, mode)

        archive = mesh_writer.getArchive()
        if archive is None:  # This happens if there was no mesh data to write.
            archive = zipfile.ZipFile(stream, "w", compression = zipfile.ZIP_DEFLATED)

        global_stack = machine_manager.activeMachine

        # Add global container stack data to the archive.
        self._writeContainerToArchive(global_stack, archive)

        # Also write all containers in the stack to the file
        for container in global_stack.getContainers():
            self._writeContainerToArchive(container, archive)

        # Check if the machine has extruders and save all that data as well.
        for extruder_stack in global_stack.extruders.values():
            self._writeContainerToArchive(extruder_stack, archive)
            for container in extruder_stack.getContainers():
                self._writeContainerToArchive(container, archive)

        # Write preferences to archive
        original_preferences = Preferences.getInstance() #Copy only the preferences that we use to the workspace.
        temp_preferences = Preferences()
        for preference in {"general/visible_settings", "cura/active_mode", "cura/categories_expanded"}:
            temp_preferences.addPreference(preference, None)
            temp_preferences.setValue(preference, original_preferences.getValue(preference))
        preferences_string = StringIO()
        temp_preferences.writeToFile(preferences_string)
        preferences_file = zipfile.ZipInfo("Cura/preferences.cfg")
        archive.writestr(preferences_file, preferences_string.getvalue())

        # Save Cura version
        version_file = zipfile.ZipInfo("Cura/version.ini")
        version_config_parser = configparser.ConfigParser(interpolation = None)
        version_config_parser.add_section("versions")
        version_config_parser.set("versions", "cura_version", application.getVersion())
        version_config_parser.set("versions", "build_type", application.getBuildType())
        version_config_parser.set("versions", "is_debug_mode", str(application.getIsDebugMode()))

        version_file_string = StringIO()
        version_config_parser.write(version_file_string)
        archive.writestr(version_file, version_file_string.getvalue())

        # Close the archive & reset states.
        archive.close()
        mesh_writer.setStoreArchive(False)
        return True
Ejemplo n.º 2
0
def test_setResetValue(new_value):
    preferences = Preferences()
    default_value = "omgzomg"
    preferences.preferenceChanged.emit = MagicMock()
    preferences.addPreference("test/test", default_value)
    assert preferences.preferenceChanged.emit.call_count == 0
    preferences.setValue("test/test", new_value)
    assert preferences.getValue("test/test") == parseValue(new_value)

    if new_value != default_value:
        assert preferences.preferenceChanged.emit.call_count == 1

    preferences.resetPreference("test/test")
    if new_value != default_value:
        assert preferences.preferenceChanged.emit.call_count == 2
    else:
        # The preference never changed. Neither the set or the reset should trigger an emit.
        assert preferences.preferenceChanged.emit.call_count == 0

    assert preferences.getValue("test/test") == default_value
def test_preferenceChanged():
    preferences = Preferences()
    # Set the visible_settings to something silly
    preferences.addPreference("general/visible_settings", "omgzomg")
    visibility_model = SettingVisibilityPresetsModel(preferences)
    visibility_model.activePresetChanged = MagicMock()

    assert visibility_model.activePreset == "custom"  # This should make the model start at "custom
    assert visibility_model.activePresetChanged.emit.call_count == 0


    basic_visibility = visibility_model.getVisibilityPresetById("basic")
    new_visibility_string = ";".join(basic_visibility.settings)
    preferences.setValue("general/visible_settings", new_visibility_string)

    # Fake a signal emit (since we didn't create the application, our own signals are not fired)
    visibility_model._onPreferencesChanged("general/visible_settings")
    # Set the visibility settings to basic
    assert visibility_model.activePreset == "basic"
    assert visibility_model.activePresetChanged.emit.call_count == 1
Ejemplo n.º 4
0
def test_nonExistingSetting():
    preferences = Preferences()
    # Test that attempting to modify a non existing setting in any way doesn't break things.
    preferences.setDefault("nonExistingPreference", "whatever")
    preferences.setValue("nonExistingPreference", "whatever")
    preferences.removePreference("nonExistingPreference")
Ejemplo n.º 5
0
    def write(self, stream, nodes, mode=WorkspaceWriter.OutputMode.BinaryMode):
        application = Application.getInstance()
        machine_manager = application.getMachineManager()

        mesh_writer = application.getMeshFileHandler().getWriter("3MFWriter")

        if not mesh_writer:  # We need to have the 3mf mesh writer, otherwise we can't save the entire workspace
            self.setInformation(
                catalog.i18nc("@error:zip", "3MF Writer plug-in is corrupt."))
            Logger.error(
                "3MF Writer class is unavailable. Can't write workspace.")
            return False

        # Indicate that the 3mf mesh writer should not close the archive just yet (we still need to add stuff to it).
        mesh_writer.setStoreArchive(True)
        mesh_writer.write(stream, nodes, mode)

        archive = mesh_writer.getArchive()
        if archive is None:  # This happens if there was no mesh data to write.
            archive = zipfile.ZipFile(stream,
                                      "w",
                                      compression=zipfile.ZIP_DEFLATED)

        global_stack = machine_manager.activeMachine

        try:
            # Add global container stack data to the archive.
            self._writeContainerToArchive(global_stack, archive)

            # Also write all containers in the stack to the file
            for container in global_stack.getContainers():
                self._writeContainerToArchive(container, archive)

            # Check if the machine has extruders and save all that data as well.
            for extruder_stack in global_stack.extruderList:
                self._writeContainerToArchive(extruder_stack, archive)
                for container in extruder_stack.getContainers():
                    self._writeContainerToArchive(container, archive)
        except PermissionError:
            self.setInformation(
                catalog.i18nc("@error:zip",
                              "No permission to write the workspace here."))
            Logger.error("No permission to write workspace to this stream.")
            return False

        # Write preferences to archive
        original_preferences = Application.getInstance().getPreferences(
        )  #Copy only the preferences that we use to the workspace.
        temp_preferences = Preferences()
        for preference in {
                "general/visible_settings", "cura/active_mode",
                "cura/categories_expanded"
        }:
            temp_preferences.addPreference(preference, None)
            temp_preferences.setValue(
                preference, original_preferences.getValue(preference))
        preferences_string = StringIO()
        temp_preferences.writeToFile(preferences_string)
        preferences_file = zipfile.ZipInfo("Cura/preferences.cfg")
        try:
            archive.writestr(preferences_file, preferences_string.getvalue())

            # Save Cura version
            version_file = zipfile.ZipInfo("Cura/version.ini")
            version_config_parser = configparser.ConfigParser(
                interpolation=None)
            version_config_parser.add_section("versions")
            version_config_parser.set("versions", "cura_version",
                                      application.getVersion())
            version_config_parser.set("versions", "build_type",
                                      application.getBuildType())
            version_config_parser.set("versions", "is_debug_mode",
                                      str(application.getIsDebugMode()))

            version_file_string = StringIO()
            version_config_parser.write(version_file_string)
            archive.writestr(version_file, version_file_string.getvalue())

            self._writePluginMetadataToArchive(archive)

            # Close the archive & reset states.
            archive.close()
        except PermissionError:
            self.setInformation(
                catalog.i18nc("@error:zip",
                              "No permission to write the workspace here."))
            Logger.error("No permission to write workspace to this stream.")
            return False
        mesh_writer.setStoreArchive(False)
        return True