From 506f2b982075c3a525fbba0ab088cf35771c688c Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Fri, 26 Aug 2022 14:08:03 +0200 Subject: [PATCH] Remove code duplication in createAbstractMachine This also caused a crash when an abstract machine with multiple extruders was selected CURA-8463 --- cura/Machines/Models/MachineListModel.py | 3 +- cura/Settings/CuraStackBuilder.py | 47 ++++++++---------------- cura/Settings/GlobalStack.py | 1 - 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/cura/Machines/Models/MachineListModel.py b/cura/Machines/Models/MachineListModel.py index a758060384..b3e3bd4f71 100644 --- a/cura/Machines/Models/MachineListModel.py +++ b/cura/Machines/Models/MachineListModel.py @@ -81,7 +81,8 @@ class MachineListModel(ListModel): for stack in online_machine_stacks: self.addItem(stack) # Remove this machine from the other stack list - other_machine_stacks.remove(stack) + if stack in other_machine_stacks: + other_machine_stacks.remove(stack) for stack in other_machine_stacks: self.addItem(stack) diff --git a/cura/Settings/CuraStackBuilder.py b/cura/Settings/CuraStackBuilder.py index 5a745f8f0a..813b3f7d2e 100644 --- a/cura/Settings/CuraStackBuilder.py +++ b/cura/Settings/CuraStackBuilder.py @@ -1,7 +1,7 @@ # Copyright (c) 2019 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. -from typing import Optional +from typing import Optional, cast from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from UM.Logger import Logger @@ -275,41 +275,26 @@ class CuraStackBuilder: :return: The new Abstract Machine or None if an error occurred. """ abstract_machine_id = f"{definition_id}_abstract_machine" - from cura.CuraApplication import CuraApplication application = CuraApplication.getInstance() registry = application.getContainerRegistry() - container_tree = ContainerTree.getInstance() - if registry.findContainerStacks(is_abstract_machine = "True", id = abstract_machine_id): - # This abstract machine already exists + abstract_machines = registry.findContainerStacks(id = abstract_machine_id) + if abstract_machines: + return cast(GlobalStack, abstract_machines[0]) + definitions = registry.findDefinitionContainers(id=definition_id) + + name = "" + + if definitions: + name = definitions[0].getName() + stack = cls.createMachine(abstract_machine_id, definition_id) + if not stack: return None - match registry.findDefinitionContainers(type = "machine", id = definition_id): - case []: - # It should not be possible for the definition to be missing since an abstract machine will only - # be created as a result of a machine with definition_id being created. - Logger.error(f"Definition {definition_id} was not found!") - return None - case [machine_definition, *_definitions]: - machine_node = container_tree.machines[machine_definition.getId()] - name = machine_definition.getName() + stack.setName(name) - stack = GlobalStack(abstract_machine_id) - stack.setMetaDataEntry("is_abstract_machine", True) - stack.setMetaDataEntry("is_online", True) - stack.setDefinition(machine_definition) - cls.createUserContainer( - name, - machine_definition, - stack, - application.empty_variant_container, - application.empty_material_container, - machine_node.preferredGlobalQuality().container, - ) + stack.setMetaDataEntry("is_abstract_machine", True) + stack.setMetaDataEntry("is_online", True) - stack.setName(name) - - registry.addContainer(stack) - - return stack + return stack \ No newline at end of file diff --git a/cura/Settings/GlobalStack.py b/cura/Settings/GlobalStack.py index 3c13f236ab..43232da725 100755 --- a/cura/Settings/GlobalStack.py +++ b/cura/Settings/GlobalStack.py @@ -292,7 +292,6 @@ class GlobalStack(CuraContainerStack): for extruder_train in extruder_trains: extruder_position = extruder_train.getMetaDataEntry("position") extruder_check_position.add(extruder_position) - for check_position in range(machine_extruder_count): if str(check_position) not in extruder_check_position: return False