diff --git a/plugins/3MFReader/ThreeMFWorkspaceReader.py b/plugins/3MFReader/ThreeMFWorkspaceReader.py index 01ca136bcf..4698d498d2 100755 --- a/plugins/3MFReader/ThreeMFWorkspaceReader.py +++ b/plugins/3MFReader/ThreeMFWorkspaceReader.py @@ -1,6 +1,13 @@ # Copyright (c) 2017 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +from configparser import ConfigParser +import zipfile +import os +import threading + +import xml.etree.ElementTree as ET + from UM.Workspace.WorkspaceReader import WorkspaceReader from UM.Application import Application @@ -14,24 +21,14 @@ from UM.Settings.ContainerRegistry import ContainerRegistry from UM.MimeTypeDatabase import MimeTypeDatabase from UM.Job import Job from UM.Preferences import Preferences -from UM.Util import parseBool -from .WorkspaceDialog import WorkspaceDialog - -import xml.etree.ElementTree as ET from cura.Settings.CuraStackBuilder import CuraStackBuilder -from cura.Settings.ExtruderManager import ExtruderManager from cura.Settings.ExtruderStack import ExtruderStack from cura.Settings.GlobalStack import GlobalStack from cura.Settings.CuraContainerStack import _ContainerIndexes from cura.CuraApplication import CuraApplication -from configparser import ConfigParser -import zipfile -import io -import configparser -import os -import threading +from .WorkspaceDialog import WorkspaceDialog i18n_catalog = i18nCatalog("cura") @@ -67,6 +64,48 @@ def call_on_qt_thread(func): return _call_on_qt_thread_wrapper +class ContainerInfo: + def __init__(self, file_name: str, serialized: str, parser: ConfigParser): + self.file_name = file_name + self.serialized = serialized + self.parser = parser + self.container = None + self.definition_id = None + + +class QualityChangesInfo: + def __init__(self): + self.name = None + self.global_info = None + self.extruder_info_dict = {} + + +class MachineInfo: + def __init__(self): + self.container_id = None + self.name = None + self.definition_id = None + self.quality_type = None + self.custom_quality_name = None + self.quality_changes_info = None + self.variant_info = None + + self.definition_changes_info = None + self.user_changes_info = None + + self.extruder_info_dict = {} + + +class ExtruderInfo: + def __init__(self): + self.position = None + self.variant_info = None + self.root_material_id = None + + self.definition_changes_info = None + self.user_changes_info = None + + ## Base implementation for reading 3MF workspace files. class ThreeMFWorkspaceReader(WorkspaceReader): def __init__(self): @@ -97,13 +136,17 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # In Cura 2.5 and 2.6, the empty profiles used to have those long names self._old_empty_profile_id_dict = {"empty_%s" % k: "empty" for k in ["material", "variant"]} + self._is_same_machine_type = False self._old_new_materials = {} self._materials_to_select = {} + self._machine_info = None def _clearState(self): + self._is_same_machine_type = False self._id_mapping = {} self._old_new_materials = {} self._materials_to_select = {} + self._machine_info = None ## Get a unique name based on the old_id. This is different from directly calling the registry in that it caches results. # This has nothing to do with speed, but with getting consistent new naming for instances & objects. @@ -156,6 +199,8 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # \param file_name # \param show_dialog In case we use preRead() to check if a file is a valid project file, we don't want to show a dialog. def preRead(self, file_name, show_dialog=True, *args, **kwargs): + self._clearState() + self._3mf_mesh_reader = Application.getInstance().getMeshFileHandler().getReaderForFile(file_name) if self._3mf_mesh_reader and self._3mf_mesh_reader.preRead(file_name) == WorkspaceReader.PreReadResult.accepted: pass @@ -163,6 +208,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader): Logger.log("w", "Could not find reader that was able to read the scene data for 3MF workspace") return WorkspaceReader.PreReadResult.failed + self._machine_info = MachineInfo() machine_type = "" variant_type_name = i18n_catalog.i18nc("@label", "Nozzle") @@ -182,23 +228,23 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # # Read definition containers # + machine_definition_id = None machine_definition_container_count = 0 extruder_definition_container_count = 0 definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)] - for each_definition_container_file in definition_container_files: - container_id = self._stripFileToId(each_definition_container_file) + for definition_container_file in definition_container_files: + container_id = self._stripFileToId(definition_container_file) definitions = self._container_registry.findDefinitionContainersMetadata(id = container_id) + serialized = archive.open(definition_container_file).read().decode("utf-8") if not definitions: - definition_container = DefinitionContainer(container_id) - definition_container.deserialize(archive.open(each_definition_container_file).read().decode("utf-8"), file_name = each_definition_container_file) - definition_container = definition_container.getMetaData() - + definition_container = DefinitionContainer.deserializeMetadata(serialized, container_id)[0] else: definition_container = definitions[0] definition_container_type = definition_container.get("type") if definition_container_type == "machine": + machine_definition_id = container_id machine_type = definition_container["name"] variant_type_name = definition_container.get("variants_name", variant_type_name) @@ -207,22 +253,33 @@ class ThreeMFWorkspaceReader(WorkspaceReader): extruder_definition_container_count += 1 else: Logger.log("w", "Unknown definition container type %s for %s", - definition_container_type, each_definition_container_file) + definition_container_type, definition_container_file) Job.yieldThread() if machine_definition_container_count != 1: - return WorkspaceReader.PreReadResult.failed #Not a workspace file but ordinary 3MF. + return WorkspaceReader.PreReadResult.failed # Not a workspace file but ordinary 3MF. material_labels = [] material_conflict = False xml_material_profile = self._getXmlProfileClass() + reverse_material_id_dict = {} if self._material_container_suffix is None: self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).preferredSuffix if xml_material_profile: material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)] for material_container_file in material_container_files: container_id = self._stripFileToId(material_container_file) - material_labels.append(self._getMaterialLabelFromSerialized(archive.open(material_container_file).read().decode("utf-8"))) + + from hashlib import sha1 + hex_container_id = sha1(container_id.encode('utf-8')).hexdigest() + + serialized = archive.open(material_container_file).read().decode("utf-8") + metadata_list = xml_material_profile.deserializeMetadata(serialized, hex_container_id) + reverse_map = {metadata["id"].replace(hex_container_id, container_id): container_id.replace(hex_container_id, container_id) + for metadata in metadata_list} + reverse_material_id_dict.update(reverse_map) + + material_labels.append(self._getMaterialLabelFromSerialized(serialized)) if self._container_registry.findContainersMetadata(id = container_id): #This material already exists. containers_found_dict["material"] = True if not self._container_registry.isReadOnly(container_id): # Only non readonly materials can be in conflict @@ -232,24 +289,38 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # Check if any quality_changes instance container is in conflict. instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)] quality_name = "" - quality_type = "" num_settings_overriden_by_quality_changes = 0 # How many settings are changed by the quality changes - num_settings_overriden_by_definition_changes = 0 # How many settings are changed by the definition changes num_user_settings = 0 quality_changes_conflict = False - definition_changes_conflict = False - for each_instance_container_file in instance_container_files: - container_id = self._stripFileToId(each_instance_container_file) + self._machine_info.quality_changes_info = QualityChangesInfo() + + quality_changes_info_list = [] + instance_container_info_dict = {} # id -> parser + for instance_container_file_name in instance_container_files: + container_id = self._stripFileToId(instance_container_file_name) + + serialized = archive.open(instance_container_file_name).read().decode("utf-8") + serialized = InstanceContainer._updateSerialized(serialized, instance_container_file_name) + parser = ConfigParser(interpolation = None) + parser.read_string(serialized) + container_info = ContainerInfo(instance_container_file_name, serialized, parser) + instance_container_info_dict[container_id] = container_info + instance_container = InstanceContainer(container_id) # Deserialize InstanceContainer by converting read data from bytes to string - instance_container.deserialize(archive.open(each_instance_container_file).read().decode("utf-8"), - file_name = each_instance_container_file) + instance_container.deserialize(serialized, file_name = instance_container_file_name) instance_container_list.append(instance_container) container_type = instance_container.getMetaDataEntry("type") if container_type == "quality_changes": + quality_changes_info_list.append(container_info) + + if not parser.has_option("metadata", "extruder"): + self._machine_info.quality_changes_info.name = parser["general"]["name"] + self._machine_info.quality_changes_info.global_info = container_info + quality_name = instance_container.getName() num_settings_overriden_by_quality_changes += len(instance_container._instances) # Check if quality changes already exists. @@ -259,17 +330,6 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # Check if there really is a conflict by comparing the values if quality_changes[0] != instance_container: quality_changes_conflict = True - elif container_type == "definition_changes": - definition_name = instance_container.getName() - num_settings_overriden_by_definition_changes += len(instance_container._instances) - # Check if definition changes already exists. - definition_changes = self._container_registry.findInstanceContainers(id = container_id) - # Check if there is any difference the loaded settings from the project file and the settings in Cura. - if definition_changes: - containers_found_dict["definition_changes"] = True - # Check if there really is a conflict by comparing the values - if definition_changes[0] != instance_container: - definition_changes_conflict = True elif container_type == "quality": if not quality_name: quality_name = instance_container.getName() @@ -282,6 +342,9 @@ class ThreeMFWorkspaceReader(WorkspaceReader): Job.yieldThread() + if self._machine_info.quality_changes_info.global_info is None: + self._machine_info.quality_changes_info = None + # Load ContainerStack files and ExtruderStack files global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles( file_name, cura_file_names) @@ -290,10 +353,11 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # - the global stack exists but some/all of the extruder stacks DON'T exist # - the global stack DOESN'T exist but some/all of the extruder stacks exist # To simplify this, only check if the global stack exists or not - container_id = self._stripFileToId(global_stack_file) + global_stack_id = self._stripFileToId(global_stack_file) serialized = archive.open(global_stack_file).read().decode("utf-8") machine_name = self._getMachineNameFromSerializedStack(serialized) - stacks = self._container_registry.findContainerStacks(id = container_id) + stacks = self._container_registry.findContainerStacks(name = machine_name, type = "machine") + self._is_same_machine_type = True if stacks: global_stack = stacks[0] containers_found_dict["machine"] = True @@ -305,30 +369,79 @@ class ThreeMFWorkspaceReader(WorkspaceReader): if global_stack.getContainer(index).getId() != container_id: machine_conflict = True break + self._is_same_machine_type = global_stack.definition.getId() == machine_definition_id + + # Get quality type + parser = ConfigParser(interpolation = None) + parser.read_string(serialized) + quality_container_id = parser["containers"][str(_ContainerIndexes.Quality)] + quality_type = instance_container_info_dict[quality_container_id].parser["metadata"]["quality_type"] + + # Get machine info + serialized = archive.open(global_stack_file).read().decode("utf-8") + serialized = GlobalStack._updateSerialized(serialized, global_stack_file) + parser = ConfigParser(interpolation = None) + parser.read_string(serialized) + definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)] + if definition_changes_id not in ("empty", "empty_definition_changes"): + self._machine_info.definition_changes_info = instance_container_info_dict[definition_changes_id] + user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)] + if user_changes_id not in ("empty", "empty_user_changes"): + self._machine_info.user_changes_info = instance_container_info_dict[user_changes_id] + + # Also check variant and material in case it doesn't have extruder stacks + if not extruder_stack_files: + position = "0" + + extruder_info = ExtruderInfo() + extruder_info.position = position + variant_id = parser["containers"][str(_ContainerIndexes.Variant)] + material_id = parser["containers"][str(_ContainerIndexes.Material)] + if variant_id not in ("empty", "empty_variant"): + extruder_info.variant_info = instance_container_info_dict[variant_id] + if material_id not in ("empty", "empty_material"): + root_material_id = reverse_material_id_dict[material_id] + extruder_info.root_material_id = root_material_id + self._machine_info.extruder_info_dict[position] = extruder_info + else: + variant_id = parser["containers"][str(_ContainerIndexes.Variant)] + if variant_id not in ("empty", "empty_variant"): + self._machine_info.variant_info = instance_container_info_dict[variant_id] + Job.yieldThread() # if the global stack is found, we check if there are conflicts in the extruder stacks - if containers_found_dict["machine"] and not machine_conflict: - for extruder_stack_file in extruder_stack_files: - serialized = archive.open(extruder_stack_file).read().decode("utf-8") - parser = configparser.ConfigParser(interpolation = None) - parser.read_string(serialized) + for extruder_stack_file in extruder_stack_files: + serialized = archive.open(extruder_stack_file).read().decode("utf-8") + serialized = ExtruderStack._updateSerialized(serialized, extruder_stack_file) + parser = ConfigParser(interpolation = None) + parser.read_string(serialized) - # The check should be done for the extruder stack that's associated with the existing global stack, - # and those extruder stacks may have different IDs. - # So we check according to the positions + # The check should be done for the extruder stack that's associated with the existing global stack, + # and those extruder stacks may have different IDs. + # So we check according to the positions + position = parser["metadata"]["position"] + variant_id = parser["containers"][str(_ContainerIndexes.Variant)] + material_id = parser["containers"][str(_ContainerIndexes.Material)] - position = str(parser["metadata"]["position"]) + extruder_info = ExtruderInfo() + extruder_info.position = position + if variant_id not in ("empty", "empty_variant"): + extruder_info.variant_info = instance_container_info_dict[variant_id] + if material_id not in ("empty", "empty_material"): + root_material_id = reverse_material_id_dict[material_id] + extruder_info.root_material_id = root_material_id + definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)] + if definition_changes_id not in ("empty", "empty_definition_changes"): + extruder_info.definition_changes_info = instance_container_info_dict[definition_changes_id] + user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)] + if user_changes_id not in ("empty", "empty_user_changes"): + extruder_info.user_changes_info = instance_container_info_dict[user_changes_id] + self._machine_info.extruder_info_dict[position] = extruder_info + + if not machine_conflict and containers_found_dict["machine"]: if position not in global_stack.extruders: - # The extruder position defined in the project doesn't exist in this global stack. - # We can say that it is a machine conflict, but it is very hard to override the machine in this - # case because we need to override the existing extruders and add the non-existing extruders. - # - # HACK: - # To make this simple, we simply say that there is no machine conflict and create a new machine - # by default. - machine_conflict = False - break + continue existing_extruder_stack = global_stack.extruders[position] # check if there are any changes at all in any of the container stacks. @@ -340,6 +453,15 @@ class ThreeMFWorkspaceReader(WorkspaceReader): machine_conflict = True break + if self._machine_info.quality_changes_info is not None: + for quality_changes_info in quality_changes_info_list: + if not quality_changes_info.parser.has_option("metadata", "extruder"): + continue + extruder_definition_id = quality_changes_info.parser["metadata"]["extruder"] + extruder_definition_metadata = self._container_registry.findDefinitionContainersMetadata(id = extruder_definition_id)[0] + position = extruder_definition_metadata["position"] + self._machine_info.quality_changes_info.extruder_info_dict[position] = quality_changes_info + num_visible_settings = 0 try: temp_preferences = Preferences() @@ -369,10 +491,18 @@ class ThreeMFWorkspaceReader(WorkspaceReader): extruders = num_extruders * [""] + self._machine_info.container_id = global_stack_id + self._machine_info.name = machine_name + self._machine_info.definition_id = machine_definition_id + self._machine_info.quality_type = quality_type + self._machine_info.custom_quality_name = quality_name + + if machine_conflict and not self._is_same_machine_type: + machine_conflict = False + # Show the dialog, informing the user what is about to happen. self._dialog.setMachineConflict(machine_conflict) self._dialog.setQualityChangesConflict(quality_changes_conflict) - self._dialog.setDefinitionChangesConflict(definition_changes_conflict) self._dialog.setMaterialConflict(material_conflict) self._dialog.setHasVisibleSettingsField(has_visible_settings_string) self._dialog.setNumVisibleSettings(num_visible_settings) @@ -415,7 +545,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader): ## Overrides an ExtruderStack in the given GlobalStack and returns the new ExtruderStack. def _overrideExtruderStack(self, global_stack, extruder_file_content, extruder_stack_file): # Get extruder position first - extruder_config = configparser.ConfigParser(interpolation = None) + extruder_config = ConfigParser(interpolation = None) extruder_config.read_string(extruder_file_content) if not extruder_config.has_option("metadata", "position"): msg = "Could not find 'metadata/position' in extruder stack file" @@ -464,8 +594,6 @@ class ThreeMFWorkspaceReader(WorkspaceReader): application = CuraApplication.getInstance() material_manager = application.getMaterialManager() - self._clearState() - archive = zipfile.ZipFile(file_name, "r") cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")] @@ -492,52 +620,24 @@ class ThreeMFWorkspaceReader(WorkspaceReader): else: global_preferences.setValue("cura/categories_expanded", categories_expanded) - Application.getInstance().expandedCategoriesChanged.emit() # Notify the GUI of the change + application.expandedCategoriesChanged.emit() # Notify the GUI of the change - self._id_mapping = {} + # If a machine with the same name is of a different type, always create a new one. + if not self._is_same_machine_type or self._resolve_strategies["machine"] != "override": + # We need to create a new machine + machine_name = self._container_registry.uniqueName(self._machine_info.name) - # We don't add containers right away, but wait right until right before the stack serialization. - # We do this so that if something goes wrong, it's easier to clean up. - containers_to_add = [] + global_stack = CuraStackBuilder.createMachine(machine_name, self._machine_info.definition_id) + extruder_stack_dict = global_stack.extruders - global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles(file_name, cura_file_names) + self._container_registry.addContainer(global_stack) + else: + # Find the machine + global_stack = self._container_registry.findContainerStacks(name = self._machine_info.name, type = "machine")[0] + extruder_stacks = self._container_registry.findContainerStacks(machine = global_stack.getId(), + type = "extruder_train") + extruder_stack_dict = {stack.getMetaDataEntry("position"): stack for stack in extruder_stacks} - global_stack = None - extruder_stacks = [] - extruder_stacks_added = [] - container_stacks_added = [] - machine_extruder_count = None - - containers_added = [] - - global_stack_id_original = self._stripFileToId(global_stack_file) - global_stack_id_new = global_stack_id_original - global_stack_name_original = self._getMachineNameFromSerializedStack(archive.open(global_stack_file).read().decode("utf-8")) - global_stack_name_new = global_stack_name_original - global_stack_need_rename = False - - extruder_stack_id_map = {} # new and old ExtruderStack IDs map - if self._resolve_strategies["machine"] == "new": - # We need a new id if the id already exists - if self._container_registry.findContainerStacksMetadata(id = global_stack_id_original): - global_stack_id_new = self.getNewId(global_stack_id_original) - global_stack_need_rename = True - - if self._container_registry.findContainerStacksMetadata(name = global_stack_id_original): - global_stack_name_new = self._container_registry.uniqueName(global_stack_name_original) - - for each_extruder_stack_file in extruder_stack_files: - old_container_id = self._stripFileToId(each_extruder_stack_file) - new_container_id = old_container_id - if self._container_registry.findContainerStacksMetadata(id = old_container_id): - # get a new name for this extruder - new_container_id = self.getNewId(old_container_id) - - extruder_stack_id_map[old_container_id] = new_container_id - - # TODO: For the moment we use pretty naive existence checking. If the ID is the same, we assume in quite a few - # TODO: cases that the container loaded is the same (most notable in materials & definitions). - # TODO: It might be possible that we need to add smarter checking in the future. Logger.log("d", "Workspace loading is checking definitions...") # Get all the definition files & check if they exist. If not, add them. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)] @@ -552,15 +652,14 @@ class ThreeMFWorkspaceReader(WorkspaceReader): Job.yieldThread() Logger.log("d", "Workspace loading is checking materials...") - material_containers = [] # Get all the material files and check if they exist. If not, add them. xml_material_profile = self._getXmlProfileClass() if self._material_container_suffix is None: self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).suffixes[0] if xml_material_profile: - to_deserialize_material = False material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)] for material_container_file in material_container_files: + to_deserialize_material = False container_id = self._stripFileToId(material_container_file) need_new_name = False materials = self._container_registry.findInstanceContainers(id = container_id) @@ -571,7 +670,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader): else: material_container = materials[0] old_material_root_id = material_container.getMetaDataEntry("base_file") - if not self._container_registry.isReadOnly(container_id): # Only create new materials if they are not read only. + if not self._container_registry.isReadOnly(old_material_root_id): # Only create new materials if they are not read only. to_deserialize_material = True if self._resolve_strategies["material"] == "override": @@ -592,544 +691,16 @@ class ThreeMFWorkspaceReader(WorkspaceReader): if need_new_name: new_name = ContainerRegistry.getInstance().uniqueName(material_container.getName()) material_container.setName(new_name) - containers_to_add.append(material_container) + self._container_registry.addContainer(material_container) Job.yieldThread() - Logger.log("d", "Workspace loading is checking instance containers...") - # Get quality_changes and user profiles saved in the workspace - instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)] - user_instance_containers = [] - quality_and_definition_changes_instance_containers = [] - quality_changes_instance_containers = [] - for instance_container_file in instance_container_files: - container_id = self._stripFileToId(instance_container_file) - serialized = archive.open(instance_container_file).read().decode("utf-8") + # Handle quality changes if any + self._processQualityChanges(global_stack) - # HACK! we ignore "quality" and "variant" instance containers! - parser = configparser.ConfigParser(interpolation = None) - parser.read_string(serialized) - if not parser.has_option("metadata", "type"): - Logger.log("w", "Cannot find metadata/type in %s, ignoring it", instance_container_file) - continue - if parser.get("metadata", "type") in self._ignored_instance_container_types: - continue - - instance_container = InstanceContainer(container_id) - - # Deserialize InstanceContainer by converting read data from bytes to string - instance_container.deserialize(serialized, file_name = instance_container_file) - container_type = instance_container.getMetaDataEntry("type") - Job.yieldThread() - - # - # IMPORTANT: - # If an instance container (or maybe other type of container) exists, and user chooses "Create New", - # we need to rename this container and all references to it, and changing those references are VERY - # HARD. - # - if container_type in self._ignored_instance_container_types: - # Ignore certain instance container types - Logger.log("w", "Ignoring instance container [%s] with type [%s]", container_id, container_type) - continue - elif container_type == "user": - # Check if quality changes already exists. - user_containers = self._container_registry.findInstanceContainers(id = container_id) - if not user_containers: - containers_to_add.append(instance_container) - else: - if self._resolve_strategies["machine"] == "override" or self._resolve_strategies["machine"] is None: - instance_container = user_containers[0] - instance_container.deserialize(archive.open(instance_container_file).read().decode("utf-8"), - file_name = instance_container_file) - instance_container.setDirty(True) - elif self._resolve_strategies["machine"] == "new": - # The machine is going to get a spiffy new name, so ensure that the id's of user settings match. - old_extruder_id = instance_container.getMetaDataEntry("extruder", None) - if old_extruder_id: - new_extruder_id = extruder_stack_id_map[old_extruder_id] - new_id = new_extruder_id + "_current_settings" - instance_container.setMetaDataEntry("id", new_id) - instance_container.setName(new_id) - instance_container.setMetaDataEntry("extruder", new_extruder_id) - containers_to_add.append(instance_container) - - machine_id = instance_container.getMetaDataEntry("machine", None) - if machine_id: - new_machine_id = self.getNewId(machine_id) - new_id = new_machine_id + "_current_settings" - instance_container.setMetaDataEntry("id", new_id) - instance_container.setName(new_id) - instance_container.setMetaDataEntry("machine", new_machine_id) - containers_to_add.append(instance_container) - user_instance_containers.append(instance_container) - elif container_type in ("quality_changes", "definition_changes"): - # Check if quality changes already exists. - changes_containers = self._container_registry.findInstanceContainers(id = container_id) - if not changes_containers: - # no existing containers with the same ID, so we can safely add the new one - containers_to_add.append(instance_container) - else: - # we have found existing container with the same ID, so we need to resolve according to the - # selected strategy. - if self._resolve_strategies[container_type] == "override": - instance_container = changes_containers[0] - instance_container.deserialize(archive.open(instance_container_file).read().decode("utf-8"), - file_name = instance_container_file) - instance_container.setDirty(True) - - elif self._resolve_strategies[container_type] == "new": - # TODO: how should we handle the case "new" for quality_changes and definition_changes? - - instance_container.setName(self._container_registry.uniqueName(instance_container.getName())) - new_changes_container_id = self.getNewId(instance_container.getId()) - instance_container.setMetaDataEntry("id", new_changes_container_id) - - # TODO: we don't know the following is correct or not, need to verify - # AND REFACTOR!!! - if self._resolve_strategies["machine"] == "new": - # The machine is going to get a spiffy new name, so ensure that the id's of user settings match. - old_extruder_id = instance_container.getMetaDataEntry("extruder", None) - # Note that in case of a quality_changes extruder means the definition id of the extruder stack - # For the user settings, it means the actual extruder stack id it's assigned to. - if old_extruder_id and old_extruder_id in extruder_stack_id_map: - new_extruder_id = extruder_stack_id_map[old_extruder_id] - instance_container.setMetaDataEntry("extruder", new_extruder_id) - - machine_id = instance_container.getMetaDataEntry("machine", None) - if machine_id: - new_machine_id = self.getNewId(machine_id) - instance_container.setMetaDataEntry("machine", new_machine_id) - - containers_to_add.append(instance_container) - - elif self._resolve_strategies[container_type] is None: - # The ID already exists, but nothing in the values changed, so do nothing. - pass - quality_and_definition_changes_instance_containers.append(instance_container) - if container_type == "quality_changes": - quality_changes_instance_containers.append(instance_container) - - if container_type == "definition_changes": - definition_changes_extruder_count = instance_container.getProperty("machine_extruder_count", "value") - if definition_changes_extruder_count is not None: - machine_extruder_count = definition_changes_extruder_count - - else: - existing_container = self._container_registry.findInstanceContainersMetadata(id = container_id) - if not existing_container: - containers_to_add.append(instance_container) - if global_stack_need_rename: - if instance_container.getMetaDataEntry("machine"): - instance_container.setMetaDataEntry("machine", global_stack_id_new) - - # Add all the containers right before we try to add / serialize the stack - for container in containers_to_add: - self._container_registry.addContainer(container) - container.setDirty(True) - containers_added.append(container) - - # Get the stack(s) saved in the workspace. - Logger.log("d", "Workspace loading is checking stacks containers...") - - # load global stack file - try: - stack = None - - if self._resolve_strategies["machine"] == "override": - container_stacks = self._container_registry.findContainerStacks(id = global_stack_id_original) - stack = container_stacks[0] - - # HACK - # There is a machine, check if it has authentication data. If so, keep that data. - network_authentication_id = stack.getMetaDataEntry("network_authentication_id") - network_authentication_key = stack.getMetaDataEntry("network_authentication_key") - stack.deserialize(archive.open(global_stack_file).read().decode("utf-8"), file_name = global_stack_file) - if network_authentication_id: - stack.addMetaDataEntry("network_authentication_id", network_authentication_id) - if network_authentication_key: - stack.addMetaDataEntry("network_authentication_key", network_authentication_key) - - elif self._resolve_strategies["machine"] == "new": - # create a new global stack - stack = GlobalStack(global_stack_id_new) - # Deserialize stack by converting read data from bytes to string - stack.deserialize(archive.open(global_stack_file).read().decode("utf-8"), - file_name = global_stack_file) - - # Ensure a unique ID and name - stack.setMetaDataEntry("id", global_stack_id_new) - - # Only machines need a new name, stacks may be non-unique - stack.setName(global_stack_name_new) - - container_stacks_added.append(stack) - # self._container_registry.addContainer(stack) - containers_added.append(stack) - else: - Logger.log("e", "Resolve strategy of %s for machine is not supported", self._resolve_strategies["machine"]) - - # Create a new definition_changes container if it was empty - if stack.definitionChanges == self._container_registry.getEmptyInstanceContainer(): - stack.setDefinitionChanges(CuraStackBuilder.createDefinitionChangesContainer(stack, stack.getId() + "_settings")) - global_stack = stack - Job.yieldThread() - except: - Logger.logException("w", "We failed to serialize the stack. Trying to clean up.") - # Something went really wrong. Try to remove any data that we added. - for container in containers_added: - self._container_registry.removeContainer(container.getId()) - return - - # load extruder stack files - has_extruder_stack_files = len(extruder_stack_files) > 0 - empty_quality_container = self._container_registry.findInstanceContainers(id = "empty_quality")[0] - empty_quality_changes_container = self._container_registry.findInstanceContainers(id = "empty_quality_changes")[0] - try: - for extruder_stack_file in extruder_stack_files: - container_id = self._stripFileToId(extruder_stack_file) - extruder_file_content = archive.open(extruder_stack_file, "r").read().decode("utf-8") - - if self._resolve_strategies["machine"] == "override": - # deserialize new extruder stack over the current ones (if any) - stack = self._overrideExtruderStack(global_stack, extruder_file_content, extruder_stack_file) - if stack is None: - continue - - elif self._resolve_strategies["machine"] == "new": - new_id = extruder_stack_id_map[container_id] - stack = ExtruderStack(new_id) - - # HACK: the global stack can have a new name, so we need to make sure that this extruder stack - # references to the new name instead of the old one. Normally, this can be done after - # deserialize() by setting the metadata, but in the case of ExtruderStack, deserialize() - # also does addExtruder() to its machine stack, so we have to make sure that it's pointing - # to the right machine BEFORE deserialization. - extruder_config = configparser.ConfigParser(interpolation = None) - extruder_config.read_string(extruder_file_content) - extruder_config.set("metadata", "machine", global_stack_id_new) - tmp_string_io = io.StringIO() - extruder_config.write(tmp_string_io) - extruder_file_content = tmp_string_io.getvalue() - - stack.deserialize(extruder_file_content, file_name = extruder_stack_file) - - # Ensure a unique ID and name - stack.setMetaDataEntry("id", new_id) - - self._container_registry.addContainer(stack) - extruder_stacks_added.append(stack) - containers_added.append(stack) - else: - Logger.log("w", "Unknown resolve strategy: %s", self._resolve_strategies["machine"]) - - # Create a new definition_changes container if it was empty - if stack.definitionChanges == self._container_registry.getEmptyInstanceContainer(): - stack.setDefinitionChanges(CuraStackBuilder.createDefinitionChangesContainer(stack, stack.getId() + "_settings")) - - if stack.getMetaDataEntry("type") == "extruder_train": - extruder_stacks.append(stack) - - # If not extruder stacks were saved in the project file (pre 3.1) create one manually - # We re-use the container registry's addExtruderStackForSingleExtrusionMachine method for this - if not extruder_stacks: - # If we choose to override a machine but to create a new custom quality profile, the custom quality - # profile is not immediately applied to the global_stack, so this fix for single extrusion machines - # will use the current custom quality profile on the existing machine. The extra optional argument - # in that function is used in this case to specify a new global stack quality_changes container so - # the fix can correctly create and copy over the custom quality settings to the newly created extruder. - new_global_quality_changes = None - if self._resolve_strategies["quality_changes"] == "new" and len(quality_changes_instance_containers) > 0: - new_global_quality_changes = quality_changes_instance_containers[0] - - # Depending if the strategy is to create a new or override, the ids must be or not be unique - stack = self._container_registry.addExtruderStackForSingleExtrusionMachine(global_stack, "fdmextruder", - new_global_quality_changes, - create_new_ids = self._resolve_strategies["machine"] == "new") - if new_global_quality_changes is not None: - quality_changes_instance_containers.append(stack.qualityChanges) - quality_and_definition_changes_instance_containers.append(stack.qualityChanges) - if global_stack.quality.getId() in ("empty", "empty_quality"): - stack.quality = empty_quality_container - if self._resolve_strategies["machine"] == "override": - # in case the extruder is newly created (for a single-extrusion machine), we need to override - # the existing extruder stack. - existing_extruder_stack = global_stack.extruders[stack.getMetaDataEntry("position")] - for idx in range(len(_ContainerIndexes.IndexTypeMap)): - existing_extruder_stack.replaceContainer(idx, stack._containers[idx], postpone_emit = True) - extruder_stacks.append(existing_extruder_stack) - else: - extruder_stacks.append(stack) - - except: - Logger.logException("w", "We failed to serialize the stack. Trying to clean up.") - # Something went really wrong. Try to remove any data that we added. - for container in containers_added: - self._container_registry.removeContainer(container.getId()) - return - - ## In case there is a new machine and once the extruders are created, the global stack is added to the registry, - # otherwise the addContainers function in CuraContainerRegistry will create an extruder stack and then creating - # useless files - if self._resolve_strategies["machine"] == "new": - self._container_registry.addContainer(global_stack) - - # Check quality profiles to make sure that if one stack has the "not supported" quality profile, - # all others should have the same. - # - # This block code tries to fix the following problems in Cura 3.0 and earlier: - # 1. The upgrade script can rename all "Not Supported" quality profiles to "empty_quality", but it cannot fix - # the problem that the global stack the extruder stacks may have different quality profiles. The code - # below loops over all stacks and make sure that if there is one stack with "Not Supported" profile, the - # rest should also use the "Not Supported" profile. - # 2. In earlier versions (at least 2.7 and 3.0), a wrong quality profile could be assigned to a stack. For - # example, a UM3 can have a BB 0.8 variant with "aa04_pla_fast" quality profile enabled. To fix this, - # in the code below we also check the actual available quality profiles for the machine. - # - has_not_supported = False - for stack in [global_stack] + extruder_stacks: - if stack.quality.getId() in ("empty", "empty_quality"): - has_not_supported = True - break - - # We filter out extruder stacks that are not actually used, for example the UM3 and custom FDM printer extruder count setting. - extruder_stacks_in_use = extruder_stacks - if machine_extruder_count is not None: - extruder_stacks_in_use = extruder_stacks[:machine_extruder_count] - - quality_manager = CuraApplication.getInstance()._quality_manager - all_quality_groups = quality_manager.getQualityGroups(global_stack) - available_quality_types = [qt for qt, qg in all_quality_groups.items() if qg.is_available] - if not has_not_supported: - has_not_supported = not available_quality_types - - quality_has_been_changed = False - - if has_not_supported: - for stack in [global_stack] + extruder_stacks_in_use: - stack.replaceContainer(_ContainerIndexes.Quality, empty_quality_container) - stack.replaceContainer(_ContainerIndexes.QualityChanges, empty_quality_changes_container) - quality_has_been_changed = True - - else: - # The machine in the project has non-empty quality and there are usable qualities for this machine. - # We need to check if the current quality_type is still usable for this machine, if not, then the quality - # will be reset to the "preferred quality" if present, otherwise "normal". - if global_stack.quality.getMetaDataEntry("quality_type") not in available_quality_types: - # We are here because the quality_type specified in the project is not supported any more, - # so we need to switch it to the "preferred quality" if present, otherwise "normal". - quality_has_been_changed = True - - # find the preferred quality - preferred_quality_id = global_stack.getMetaDataEntry("preferred_quality", None) - if preferred_quality_id is not None: - definition_id = global_stack.definition.getId() - definition_id = global_stack.definition.getMetaDataEntry("quality_definition", definition_id) - if not parseBool(global_stack.getMetaDataEntry("has_machine_quality", "False")): - definition_id = "fdmprinter" - - containers = self._container_registry.findInstanceContainers(id = preferred_quality_id, - type = "quality", - definition = definition_id) - containers = [c for c in containers if not c.getMetaDataEntry("material", "")] - if containers: - global_stack.quality = containers[0] - global_stack.qualityChanges = empty_quality_changes_container - # also find the quality containers for the extruders - for extruder_stack in extruder_stacks_in_use: - search_criteria = {"id": preferred_quality_id, - "type": "quality", - "definition": definition_id} - if global_stack.getMetaDataEntry("has_machine_materials") and extruder_stack.material.getId() not in ("empty", "empty_material"): - search_criteria["material"] = extruder_stack.material.getId() - containers = self._container_registry.findInstanceContainers(**search_criteria) - if containers: - extruder_stack.quality = containers[0] - extruder_stack.qualityChanges = empty_quality_changes_container - else: - Logger.log("e", "Cannot find preferred quality for extruder [%s].", extruder_stack.getId()) - - else: - # we cannot find the preferred quality. THIS SHOULD NOT HAPPEN - Logger.log("e", "Cannot find the preferred quality for machine [%s]", global_stack.getId()) - else: - # The quality_type specified in the project file is usable, but the quality container itself may not - # be correct. For example, for UM2, the quality container can be "draft" while it should be "um2_draft" - # instead. - # In this code branch, we try to fix those incorrect quality containers. - # - # ***IMPORTANT***: We only do this fix for single-extrusion machines. - # We will first find the correct quality profile for the extruder, then apply the same - # quality profile for the global stack. - # - if len(extruder_stacks) == 1: - extruder_stack = extruder_stacks[0] - - search_criteria = {"type": "quality", "quality_type": global_stack.quality.getMetaDataEntry("quality_type")} - search_criteria["definition"] = global_stack.definition.getId() - if not parseBool(global_stack.getMetaDataEntry("has_machine_quality", "False")): - search_criteria["definition"] = "fdmprinter" - - if global_stack.getMetaDataEntry("has_machine_materials") and extruder_stack.material.getId() not in ("empty", "empty_material"): - search_criteria["material"] = extruder_stack.material.getId() - containers = self._container_registry.findInstanceContainers(**search_criteria) - if containers: - new_quality_container = containers[0] - extruder_stack.quality = new_quality_container - global_stack.quality = new_quality_container - - # Now we are checking if the quality in the extruder stacks is the same as in the global. In other case, - # the quality is set to be the same. - definition_id = global_stack.definition.getId() - definition_id = global_stack.definition.getMetaDataEntry("quality_definition", definition_id) - if not parseBool(global_stack.getMetaDataEntry("has_machine_quality", "False")): - definition_id = "fdmprinter" - - for extruder_stack in extruder_stacks_in_use: - - # If the quality is different in the stacks, then the quality in the global stack is trusted - if extruder_stack.quality.getMetaDataEntry("quality_type") != global_stack.quality.getMetaDataEntry("quality_type"): - search_criteria = {"id": global_stack.quality.getId(), - "type": "quality", - "definition": definition_id} - if global_stack.getMetaDataEntry("has_machine_materials") and extruder_stack.material.getId() not in ("empty", "empty_material"): - search_criteria["material"] = extruder_stack.material.getId() - containers = self._container_registry.findInstanceContainers(**search_criteria) - if containers: - extruder_stack.quality = containers[0] - extruder_stack.qualityChanges = empty_quality_changes_container - else: - Logger.log("e", "Cannot find a suitable quality for extruder [%s].", extruder_stack.getId()) - - quality_has_been_changed = True - - else: - Logger.log("i", "The quality is the same for the global and the extruder stack [%s]", global_stack.quality.getId()) - - # Replacing the old containers if resolve is "new". - # When resolve is "new", some containers will get renamed, so all the other containers that reference to those - # MUST get updated too. - # - if self._resolve_strategies["machine"] == "new": - # A new machine was made, but it was serialized with the wrong user container. Fix that now. - for container in user_instance_containers: - # replacing the container ID for user instance containers for the extruders - extruder_id = container.getMetaDataEntry("extruder", None) - if extruder_id: - for extruder in extruder_stacks: - if extruder.getId() == extruder_id: - extruder.userChanges = container - continue - - # replacing the container ID for user instance containers for the machine - machine_id = container.getMetaDataEntry("machine", None) - if machine_id: - if global_stack.getId() == machine_id: - global_stack.userChanges = container - continue - - changes_container_types = ("quality_changes", "definition_changes") - if quality_has_been_changed: - # DO NOT replace quality_changes if the current quality_type is not supported - changes_container_types = ("definition_changes",) - for changes_container_type in changes_container_types: - if self._resolve_strategies[changes_container_type] == "new": - # Quality changes needs to get a new ID, added to registry and to the right stacks - for each_changes_container in quality_and_definition_changes_instance_containers: - # NOTE: The renaming and giving new IDs are possibly redundant because they are done in the - # instance container loading part. - new_id = each_changes_container.getId() - - # Find the old (current) changes container in the global stack - if changes_container_type == "quality_changes": - old_container = global_stack.qualityChanges - elif changes_container_type == "definition_changes": - old_container = global_stack.definitionChanges - - # sanity checks - # NOTE: The following cases SHOULD NOT happen!!!! - if old_container.getId() in ("empty_quality_changes", "empty_definition_changes", "empty"): - Logger.log("e", "We try to get [%s] from the global stack [%s] but we got None instead!", - changes_container_type, global_stack.getId()) - continue - - # Replace the quality/definition changes container if it's in the GlobalStack - # NOTE: we can get an empty container here, but the IDs will not match, - # so this comparison is fine. - if self._id_mapping.get(old_container.getId()) == new_id: - if changes_container_type == "quality_changes": - global_stack.qualityChanges = each_changes_container - elif changes_container_type == "definition_changes": - global_stack.definitionChanges = each_changes_container - continue - - # Replace the quality/definition changes container if it's in one of the ExtruderStacks - # Only apply the change if we have loaded extruder stacks from the project - if has_extruder_stack_files: - for each_extruder_stack in extruder_stacks: - changes_container = None - if changes_container_type == "quality_changes": - changes_container = each_extruder_stack.qualityChanges - elif changes_container_type == "definition_changes": - changes_container = each_extruder_stack.definitionChanges - - # sanity checks - # NOTE: The following cases SHOULD NOT happen!!!! - if changes_container.getId() in ("empty_quality_changes", "empty_definition_changes", "empty"): - Logger.log("e", "We try to get [%s] from the extruder stack [%s] but we got None instead!", - changes_container_type, each_extruder_stack.getId()) - continue - - # NOTE: we can get an empty container here, but the IDs will not match, - # so this comparison is fine. - if self._id_mapping.get(changes_container.getId()) == new_id: - if changes_container_type == "quality_changes": - each_extruder_stack.qualityChanges = each_changes_container - elif changes_container_type == "definition_changes": - each_extruder_stack.definitionChanges = each_changes_container - - if self._resolve_strategies["material"] == "new": - # the actual material instance container can have an ID such as - # __ - # which cannot be determined immediately, so here we use a HACK to find the right new material - # instance ID: - # - get the old material IDs for all material - # - find the old material with the longest common prefix in ID, that's the old material - # - update the name by replacing the old prefix with the new - # - find the new material container and set it to the stack - old_to_new_material_dict = {} - for each_material in material_containers: - # find the material's old name - for old_id, new_id in self._id_mapping.items(): - if each_material.getId() == new_id: - old_to_new_material_dict[old_id] = each_material - break - - global_stack.quality = empty_quality_container - - # replace old material in global and extruder stacks with new - self._replaceStackMaterialWithNew(global_stack, old_to_new_material_dict) - if extruder_stacks: - for extruder_stack in extruder_stacks: - if extruder_stack.material.getId() in ("empty", "empty_material"): - continue - old_root_material_id = extruder_stack.material.getMetaDataEntry("base_file") - if old_root_material_id in self._old_new_materials: - new_root_material_id = self._old_new_materials[old_root_material_id] - self._materials_to_select[extruder_stack.getMetaDataEntry("position")] = new_root_material_id - - if extruder_stacks: - for stack in extruder_stacks: - ExtruderManager.getInstance().registerExtruder(stack, global_stack.getId()) + # Prepare the machine + self._applyChangesToMachine(global_stack, extruder_stack_dict) Logger.log("d", "Workspace loading is notifying rest of the code of changes...") - - if self._resolve_strategies["machine"] == "new": - for stack in extruder_stacks: - stack.setNextStack(global_stack) - stack.containersChanged.emit(stack.getTop()) - else: - CuraApplication.getInstance().getMachineManager().activeQualityChanged.emit() - # Actually change the active machine. # # This is scheduled for later is because it depends on the Variant/Material/Qualitiy Managers to have the latest @@ -1137,7 +708,8 @@ class ThreeMFWorkspaceReader(WorkspaceReader): # function is running on the main thread (Qt thread), although those "changed" signals have been emitted, but # they won't take effect until this function is done. # To solve this, we schedule _updateActiveMachine() for later so it will have the latest data. - CuraApplication.getInstance().callLater(self._updateActiveMachine, global_stack) + Application.getInstance().setGlobalContainerStack(global_stack) + self._updateActiveMachine(global_stack) # Load all the nodes / meshdata of the workspace nodes = self._3mf_mesh_reader.read(file_name) @@ -1150,85 +722,284 @@ class ThreeMFWorkspaceReader(WorkspaceReader): self.setWorkspaceName(base_file_name) return nodes + def _processQualityChanges(self, global_stack): + if self._machine_info.quality_changes_info is None: + return + + application = CuraApplication.getInstance() + quality_manager = application.getQualityManager() + + # If we have custom profiles, load them + quality_changes_name = self._machine_info.quality_changes_info.name + if self._machine_info.quality_changes_info is not None: + Logger.log("i", "Loading custom profile [%s] from project file", + self._machine_info.quality_changes_info.name) + + # Get the correct extruder definition IDs for quality changes + from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch + machine_definition_id_for_quality = getMachineDefinitionIDForQualitySearch(global_stack) + machine_definition_for_quality = self._container_registry.findDefinitionContainers(id = machine_definition_id_for_quality)[0] + extruder_dict_for_quality = machine_definition_for_quality.getMetaDataEntry("machine_extruder_trains") + + quality_changes_info = self._machine_info.quality_changes_info + quality_changes_quality_type = quality_changes_info.global_info.parser["metadata"]["quality_type"] + + quality_changes_name = quality_changes_info.name + create_new = self._resolve_strategies.get("quality_changes") != "override" + if create_new: + container_info_dict = {None: self._machine_info.quality_changes_info.global_info} + container_info_dict.update(quality_changes_info.extruder_info_dict) + + quality_changes_name = self._container_registry.uniqueName(quality_changes_name) + for position, container_info in container_info_dict.items(): + extruder_definition_id = None + if position is not None: + extruder_definition_id = extruder_dict_for_quality[position] + + container = quality_manager._createQualityChanges(quality_changes_quality_type, + quality_changes_name, + global_stack, extruder_definition_id) + container_info.container = container + container.setDirty(True) + self._container_registry.addContainer(container) + + Logger.log("d", "Created new quality changes container [%s]", container.getId()) + + else: + # Find the existing containers + quality_changes_containers = self._container_registry.findInstanceContainers(name = quality_changes_name, + type = "quality_changes") + for container in quality_changes_containers: + extruder_definition_id = container.getMetaDataEntry("extruder") + if not extruder_definition_id: + quality_changes_info.global_info.container = container + else: + extruder_definition_metadata = self._container_registry.findDefinitionContainersMetadata(id = extruder_definition_id)[0] + position = extruder_definition_metadata["position"] + if position not in quality_changes_info.extruder_info_dict: + quality_changes_info.extruder_info_dict[position] = ContainerInfo(None, None, None) + container_info = quality_changes_info.extruder_info_dict[position] + container_info.container = container + + for position, container_info in quality_changes_info.extruder_info_dict.items(): + container_info.definition_id = extruder_dict_for_quality[position] + + # If there is no quality changes for any extruder, create one. + if not quality_changes_info.extruder_info_dict: + container_info = ContainerInfo(None, None, None) + quality_changes_info.extruder_info_dict["0"] = container_info + extruder_definition_id = extruder_dict_for_quality["0"] + container_info.definition_id = extruder_definition_id + + container = quality_manager._createQualityChanges(quality_changes_quality_type, quality_changes_name, + global_stack, extruder_definition_id) + container_info.container = container + container.setDirty(True) + self._container_registry.addContainer(container) + + Logger.log("d", "Created new quality changes container [%s]", container.getId()) + + # Clear all existing containers + quality_changes_info.global_info.container.clear() + for container_info in quality_changes_info.extruder_info_dict.values(): + container_info.container.clear() + + # Loop over everything and override the existing containers + global_info = quality_changes_info.global_info + global_info.container.clear() # Clear all + for key, value in global_info.parser["values"].items(): + if not machine_definition_for_quality.getProperty(key, "settable_per_extruder"): + global_info.container.setProperty(key, "value", value) + else: + quality_changes_info.extruder_info_dict["0"].container.setProperty(key, "value", value) + + for position, container_info in quality_changes_info.extruder_info_dict.items(): + if container_info.parser is None: + continue + + if container_info.container is None: + extruder_definition_id = extruder_dict_for_quality[position] + container = quality_manager._createQualityChanges(quality_changes_quality_type, quality_changes_name, + global_stack, extruder_definition_id) + container_info.container = container + + for key, value in container_info.parser["values"].items(): + container_info.container.setProperty(key, "value", value) + + self._machine_info.quality_changes_info.name = quality_changes_name + + def _clearStack(self, stack): + application = CuraApplication.getInstance() + + stack.definitionChanges.clear() + stack.variant = application.empty_variant_container + stack.material = application.empty_material_container + stack.quality = application.empty_quality_container + stack.qualityChanges = application.empty_quality_changes_container + stack.userChanges.clear() + + def _applyDefinitionChanges(self, global_stack, extruder_stack_dict): + values_to_set_for_extruders = {} + if self._machine_info.definition_changes_info is not None: + parser = self._machine_info.definition_changes_info.parser + for key, value in parser["values"].items(): + if global_stack.getProperty(key, "settable_per_extruder"): + values_to_set_for_extruders[key] = value + else: + global_stack.definitionChanges.setProperty(key, "value", value) + + for position, extruder_stack in extruder_stack_dict.items(): + if position not in self._machine_info.extruder_info_dict: + continue + + extruder_info = self._machine_info.extruder_info_dict[position] + if extruder_info.definition_changes_info is None: + continue + parser = extruder_info.definition_changes_info.parser + for key, value in values_to_set_for_extruders.items(): + extruder_stack.definitionChanges.setProperty(key, "value", value) + if parser is not None: + for key, value in parser["values"].items(): + extruder_stack.definitionChanges.setProperty(key, "value", value) + + def _applyUserChanges(self, global_stack, extruder_stack_dict): + values_to_set_for_extruder_0 = {} + if self._machine_info.user_changes_info is not None: + parser = self._machine_info.user_changes_info.parser + for key, value in parser["values"].items(): + if global_stack.getProperty(key, "settable_per_extruder"): + values_to_set_for_extruder_0[key] = value + else: + global_stack.userChanges.setProperty(key, "value", value) + + for position, extruder_stack in extruder_stack_dict.items(): + if position not in self._machine_info.extruder_info_dict: + continue + + extruder_info = self._machine_info.extruder_info_dict[position] + if extruder_info.user_changes_info is not None: + parser = self._machine_info.extruder_info_dict[position].user_changes_info.parser + if position == "0": + for key, value in values_to_set_for_extruder_0.items(): + extruder_stack.userChanges.setProperty(key, "value", value) + if parser is not None: + for key, value in parser["values"].items(): + extruder_stack.userChanges.setProperty(key, "value", value) + + def _applyVariants(self, global_stack, extruder_stack_dict): + application = CuraApplication.getInstance() + variant_manager = application.getVariantManager() + + if self._machine_info.variant_info is not None: + parser = self._machine_info.variant_info.parser + variant_name = parser["general"]["name"] + + from cura.Machines.VariantManager import VariantType + variant_type = VariantType.BUILD_PLATE + + node = variant_manager.getVariantNode(global_stack.definition.getId(), variant_name, variant_type) + if node is not None: + global_stack.variant = node.getContainer() + + for position, extruder_stack in extruder_stack_dict.items(): + if position not in self._machine_info.extruder_info_dict: + continue + extruder_info = self._machine_info.extruder_info_dict[position] + if extruder_info.variant_info is None: + continue + parser = extruder_info.variant_info.parser + + variant_name = parser["general"]["name"] + from cura.Machines.VariantManager import VariantType + variant_type = VariantType.NOZZLE + + node = variant_manager.getVariantNode(global_stack.definition.getId(), variant_name, variant_type) + if node is not None: + extruder_stack.variant = node.getContainer() + + def _applyMaterials(self, global_stack, extruder_stack_dict): + application = CuraApplication.getInstance() + material_manager = application.getMaterialManager() + + # Force update lookup tables first + material_manager.initialize() + + for position, extruder_stack in extruder_stack_dict.items(): + if position not in self._machine_info.extruder_info_dict: + continue + extruder_info = self._machine_info.extruder_info_dict[position] + if extruder_info.root_material_id is None: + continue + + root_material_id = extruder_info.root_material_id + root_material_id = self._old_new_materials.get(root_material_id, root_material_id) + + # get material diameter of this extruder + machine_material_diameter = extruder_stack.materialDiameter + material_node = material_manager.getMaterialNode(global_stack.definition.getId(), + extruder_stack.variant.getName(), + machine_material_diameter, + root_material_id) + if material_node is not None: + extruder_stack.material = material_node.getContainer() + + def _applyChangesToMachine(self, global_stack, extruder_stack_dict): + # Clear all first + self._clearStack(global_stack) + for extruder_stack in extruder_stack_dict.values(): + self._clearStack(extruder_stack) + + self._applyDefinitionChanges(global_stack, extruder_stack_dict) + self._applyUserChanges(global_stack, extruder_stack_dict) + self._applyVariants(global_stack, extruder_stack_dict) + self._applyMaterials(global_stack, extruder_stack_dict) + + # prepare the quality to select + self._quality_changes_to_apply = None + self._quality_type_to_apply = None + if self._machine_info.quality_changes_info is not None: + self._quality_changes_to_apply = self._machine_info.quality_changes_info.name + else: + self._quality_type_to_apply = self._machine_info.quality_type + def _updateActiveMachine(self, global_stack): # Actually change the active machine. machine_manager = Application.getInstance().getMachineManager() material_manager = Application.getInstance().getMaterialManager() + quality_manager = Application.getInstance().getQualityManager() - # Switch materials if new materials are created due to conflicts - # We do it here because MaterialManager hasn't been updated in _read() yet. - for position, root_material_id in self._materials_to_select.items(): - extruder_stack = global_stack.extruders[position] - material_diameter = extruder_stack.materialDiameter - material_node = material_manager.getMaterialNode(global_stack.getMetaDataEntry("definition"), - extruder_stack.variant.getName(), - material_diameter, root_material_id) - if material_node is None: - Application.getInstance().callLater(self._updateActiveMachine, global_stack) - return - extruder_stack.material = material_node.getContainer() - Logger.log("d", "Changed extruder [%s] to material [%s]", position, root_material_id) + # Force update the lookup maps first + material_manager.initialize() + quality_manager.initialize() machine_manager.setActiveMachine(global_stack.getId()) + if self._quality_changes_to_apply: + quality_changes_group_dict = quality_manager.getQualityChangesGroups(global_stack) + if self._quality_changes_to_apply not in quality_changes_group_dict: + Logger.log("e", "Could not find quality_changes [%s]", self._quality_changes_to_apply) + return + quality_changes_group = quality_changes_group_dict[self._quality_changes_to_apply] + machine_manager.setQualityChangesGroup(quality_changes_group) + else: + self._quality_type_to_apply = self._quality_type_to_apply.lower() + quality_group_dict = quality_manager.getQualityGroups(global_stack) + if self._quality_type_to_apply in quality_group_dict: + quality_group = quality_group_dict[self._quality_type_to_apply] + else: + Logger.log("i", "Could not find quality type [%s], switch to default", self._quality_type_to_apply) + preferred_quality_type = global_stack.getMetaDataEntry("preferred_quality_type") + quality_group_dict = quality_manager.getQualityGroups(global_stack) + quality_group = quality_group_dict.get(preferred_quality_type) + if quality_group is None: + Logger.log("e", "Could not get preferred quality type [%s]", preferred_quality_type) + + if quality_group is not None: + machine_manager.setQualityGroup(quality_group) + # Notify everything/one that is to notify about changes. global_stack.containersChanged.emit(global_stack.getTop()) - ## HACK: Replaces the material container in the given stack with a newly created material container. - # This function is used when the user chooses to resolve material conflicts by creating new ones. - def _replaceStackMaterialWithNew(self, stack, old_new_material_dict): - # The material containers in the project file are 'parent' material such as "generic_pla", - # but a material container used in a global/extruder stack is a 'child' material, - # such as "generic_pla_ultimaker3_AA_0.4", which can be formalised as the following: - # - # __ - # - # In the project loading, when a user chooses to resolve material conflicts by creating new ones, - # the old 'parent' material ID and the new 'parent' material ID are known, but not the child material IDs. - # In this case, the global stack and the extruder stacks need to use the newly created material, but the - # material containers they use are 'child' material. So, here, we need to find the right 'child' material for - # the stacks. - # - # This hack approach works as follows: - # - No matter there is a child material or not, the actual material we are looking for has the prefix - # "", which is the old material name. For the material in a stack, we know that the new - # material's ID will be "_blabla..", so we just need to replace the old material ID - # with the new one to get the new 'child' material. - # - Because the material containers have IDs such as "m #nn", if we use simple prefix matching, there can - # be a problem in the following scenario: - # - there are two materials in the project file, namely "m #1" and "m #11" - # - the child materials in use are for example: "m #1_um3_aa04", "m #11_um3_aa04" - # - if we only check for a simple prefix match, then "m #11_um3_aa04" will match with "m #1", but they - # are not the same material - # To avoid this, when doing the prefix matching, we use the result with the longest mactching prefix. - - # find the old material ID - old_material_id_in_stack = stack.material.getId() - best_matching_old_material_id = None - best_matching_old_material_prefix_length = -1 - for old_parent_material_id in old_new_material_dict: - if len(old_parent_material_id) < best_matching_old_material_prefix_length: - continue - if len(old_parent_material_id) <= len(old_material_id_in_stack): - if old_parent_material_id == old_material_id_in_stack[0:len(old_parent_material_id)]: - best_matching_old_material_prefix_length = len(old_parent_material_id) - best_matching_old_material_id = old_parent_material_id - - if best_matching_old_material_id is None: - Logger.log("w", "Cannot find any matching old material ID for stack [%s] material [%s]. Something can go wrong", - stack.getId(), old_material_id_in_stack) - return - - # find the new material container - new_material_id = old_new_material_dict[best_matching_old_material_id].getId() + old_material_id_in_stack[len(best_matching_old_material_id):] - new_material_containers = self._container_registry.findInstanceContainers(id = new_material_id, type = "material") - if not new_material_containers: - Logger.log("e", "Cannot find new material container [%s]", new_material_id) - return - - # replace the material in the given stack - stack.material = new_material_containers[0] - def _stripFileToId(self, file): mime_type = MimeTypeDatabase.getMimeTypeForFile(file) file = mime_type.stripExtension(file) @@ -1239,7 +1010,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader): ## Get the list of ID's of all containers in a container stack by partially parsing it's serialized data. def _getContainerIdListFromSerialized(self, serialized): - parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False) + parser = ConfigParser(interpolation=None, empty_lines_in_values=False) parser.read_string(serialized) container_ids = [] @@ -1260,7 +1031,7 @@ class ThreeMFWorkspaceReader(WorkspaceReader): return container_ids def _getMachineNameFromSerializedStack(self, serialized): - parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False) + parser = ConfigParser(interpolation=None, empty_lines_in_values=False) parser.read_string(serialized) return parser["general"].get("name", "") diff --git a/plugins/3MFReader/WorkspaceDialog.py b/plugins/3MFReader/WorkspaceDialog.py index 5b474843d5..bb31afd40b 100644 --- a/plugins/3MFReader/WorkspaceDialog.py +++ b/plugins/3MFReader/WorkspaceDialog.py @@ -52,7 +52,6 @@ class WorkspaceDialog(QObject): machineConflictChanged = pyqtSignal() qualityChangesConflictChanged = pyqtSignal() - definitionChangesConflictChanged = pyqtSignal() materialConflictChanged = pyqtSignal() numVisibleSettingsChanged = pyqtSignal() activeModeChanged = pyqtSignal() @@ -196,10 +195,6 @@ class WorkspaceDialog(QObject): def qualityChangesConflict(self): return self._has_quality_changes_conflict - @pyqtProperty(bool, notify=definitionChangesConflictChanged) - def definitionChangesConflict(self): - return self._has_definition_changes_conflict - @pyqtProperty(bool, notify=materialConflictChanged) def materialConflict(self): return self._has_material_conflict @@ -229,18 +224,11 @@ class WorkspaceDialog(QObject): self._has_quality_changes_conflict = quality_changes_conflict self.qualityChangesConflictChanged.emit() - def setDefinitionChangesConflict(self, definition_changes_conflict): - if self._has_definition_changes_conflict != definition_changes_conflict: - self._has_definition_changes_conflict = definition_changes_conflict - self.definitionChangesConflictChanged.emit() - def getResult(self): if "machine" in self._result and not self._has_machine_conflict: self._result["machine"] = None if "quality_changes" in self._result and not self._has_quality_changes_conflict: self._result["quality_changes"] = None - if "definition_changes" in self._result and not self._has_definition_changes_conflict: - self._result["definition_changes"] = None if "material" in self._result and not self._has_material_conflict: self._result["material"] = None