From 924d4cc13b5745492a4145311afedb135566628f Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 16:07:37 +0200 Subject: [PATCH 1/6] Remove unused container_tree variable Contributes to issue CURA-6600. --- plugins/XmlMaterialProfile/XmlMaterialProfile.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index cbdec39054..71a7f8e593 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -226,8 +226,6 @@ class XmlMaterialProfile(InstanceContainer): machine_container_map = {} # type: Dict[str, InstanceContainer] machine_variant_map = {} # type: Dict[str, Dict[str, Any]] - container_tree = ContainerTree.getInstance() - root_material_id = self.getMetaDataEntry("base_file") # if basefile is self.getId, this is a basefile. all_containers = registry.findInstanceContainers(base_file = root_material_id) From 1874a6453da603100633a7aa624c527fc398fdd9 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 16:40:11 +0200 Subject: [PATCH 2/6] Move duplicateMaterial into MaterialManagementModel To move away from the deprecated MaterialManager class. Contributes to issue CURA-6600. --- cura/Machines/MaterialManager.py | 4 +- .../Models/MaterialManagementModel.py | 68 ++++++++++++++++++- .../Preferences/Materials/MaterialsPage.qml | 2 +- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/cura/Machines/MaterialManager.py b/cura/Machines/MaterialManager.py index 5d5938d95d..b055cf7ab4 100644 --- a/cura/Machines/MaterialManager.py +++ b/cura/Machines/MaterialManager.py @@ -298,7 +298,6 @@ class MaterialManager(QObject): # Create a new ID & container to hold the data. new_containers = [] - container_registry = CuraContainerRegistry.getInstance() if new_base_id is None: new_base_id = container_registry.uniqueName(base_container.getId()) new_base_container = copy.deepcopy(base_container) @@ -334,9 +333,10 @@ class MaterialManager(QObject): # if the duplicated material was favorite then the new material should also be added to favorite. if root_material_id in self.getFavorites(): - self.addFavorite(new_base_id) + cura.CuraApplication.CuraApplication.getInstance().getMaterialManagementModel().addFavorite(new_base_id) return new_base_id + # # Creates a duplicate of a material, which has the same GUID and base_file metadata. # Returns the root material ID of the duplicated material if successful. diff --git a/cura/Machines/Models/MaterialManagementModel.py b/cura/Machines/Models/MaterialManagementModel.py index 2c8617073d..55ded501c2 100644 --- a/cura/Machines/Models/MaterialManagementModel.py +++ b/cura/Machines/Models/MaterialManagementModel.py @@ -1,11 +1,13 @@ # Copyright (c) 2019 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +import copy # To duplicate materials. from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page. -from typing import TYPE_CHECKING +from typing import Any, Dict, Optional, TYPE_CHECKING from UM.Logger import Logger +import cura.CuraApplication # Imported like this to prevent circular imports. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find the sets of materials belonging to each other, and currently loaded extruder stacks. if TYPE_CHECKING: @@ -62,4 +64,66 @@ class MaterialManagementModel(QObject): container_registry = CuraContainerRegistry.getInstance() materials_this_base_file = container_registry.findContainersMetadata(base_file = material_node.base_file) for material_metadata in materials_this_base_file: - container_registry.removeContainer(material_metadata["id"]) \ No newline at end of file + container_registry.removeContainer(material_metadata["id"]) + + ## Creates a duplicate of a material with the same GUID and base_file + # metadata. + # \param material_node The node representing the material to duplicate. + # \param new_base_id A new material ID for the base material. The IDs of + # the submaterials will be based off this one. If not provided, a material + # ID will be generated automatically. + # \param new_metadata Metadata for the new material. If not provided, this + # will be duplicated from the original material. + # \return The root material ID of the duplicate material. + @pyqtSlot("QVariant", result = str) + def duplicateMaterial(self, material_node: "MaterialNode", new_base_id: Optional[str] = None, new_metadata: Dict[str, Any] = None) -> Optional[str]: + container_registry = CuraContainerRegistry.getInstance() + + root_materials = container_registry.findContainers(id = material_node.base_file) + if not root_materials: + Logger.log("i", "Unable to duplicate the root material with ID {root_id}, because it doesn't exist.".format(root_id = material_node.base_file)) + return None + root_material = root_materials[0] + + # Ensure that all settings are saved. + cura.CuraApplication.CuraApplication.getInstance().saveSettings() + + # Create a new ID and container to hold the data. + if new_base_id is None: + new_base_id = container_registry.uniqueName(root_material.getId()) + new_root_material = copy.deepcopy(root_material) + new_root_material.getMetaData()["id"] = new_base_id + new_root_material.getMetaData()["base_file"] = new_base_id + if new_metadata is not None: + new_root_material.getMetaData().update(new_metadata) + new_containers = [new_root_material] + + # Clone all submaterials. + for container_to_copy in container_registry.findInstanceContainers(base_file = material_node.base_file): + if container_to_copy.getId() == material_node.base_file: + continue # We already have that one. Skip it. + new_id = new_base_id + definition = container_to_copy.getMetaDataEntry("definition") + if definition != "fdmprinter": + new_id += "_" + definition + variant_name = container_to_copy.getMetaDataEntry("variant_name") + if variant_name: + new_id += "_" + variant_name.replace(" ", "_") + + new_container = copy.deepcopy(container_to_copy) + new_container.getMetaData()["id"] = new_id + new_container.getMetaData()["base_file"] = new_base_id + if new_metadata is not None: + new_container.getMetaData().update(new_metadata) + new_containers.append(new_container) + + for container_to_add in new_containers: + container_to_add.setDirty(True) + container_registry.addContainer(container_to_add) + + # If the duplicated material was favorite then the new material should also be added to the favorites. + # TODO: Move favourites to here. + #if material_node.base_file in self.getFavorites(): + # self.addFavorite(new_base_id) + + return new_base_id \ No newline at end of file diff --git a/resources/qml/Preferences/Materials/MaterialsPage.qml b/resources/qml/Preferences/Materials/MaterialsPage.qml index 4a6ff75f0d..1568e8707f 100644 --- a/resources/qml/Preferences/Materials/MaterialsPage.qml +++ b/resources/qml/Preferences/Materials/MaterialsPage.qml @@ -137,7 +137,7 @@ Item onClicked: { forceActiveFocus(); - base.newRootMaterialIdToSwitchTo = base.materialManager.duplicateMaterial(base.currentItem.container_node); + base.newRootMaterialIdToSwitchTo = base.materialManagementModel.duplicateMaterial(base.currentItem.container_node); base.toActivateNewMaterial = true; } } From 957894b614e22cc3a96baf6a4c300051258cb726 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 16:45:50 +0200 Subject: [PATCH 3/6] Fix duplicating a favourite material The duplicate must also be favourite. Contributes to issue CURA-6600. --- cura/Machines/Models/MaterialManagementModel.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cura/Machines/Models/MaterialManagementModel.py b/cura/Machines/Models/MaterialManagementModel.py index 55ded501c2..301dc12025 100644 --- a/cura/Machines/Models/MaterialManagementModel.py +++ b/cura/Machines/Models/MaterialManagementModel.py @@ -86,7 +86,8 @@ class MaterialManagementModel(QObject): root_material = root_materials[0] # Ensure that all settings are saved. - cura.CuraApplication.CuraApplication.getInstance().saveSettings() + application = cura.CuraApplication.CuraApplication.getInstance() + application.saveSettings() # Create a new ID and container to hold the data. if new_base_id is None: @@ -122,8 +123,9 @@ class MaterialManagementModel(QObject): container_registry.addContainer(container_to_add) # If the duplicated material was favorite then the new material should also be added to the favorites. - # TODO: Move favourites to here. - #if material_node.base_file in self.getFavorites(): - # self.addFavorite(new_base_id) + favorites_set = set(application.getPreferences().getValue("cura/favorite_materials").split(";")) + if material_node.base_file in favorites_set: + favorites_set.add(new_base_id) + application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites_set)) return new_base_id \ No newline at end of file From cb344f9dec5362c334c033be45d4388ef819d60a Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 17:05:46 +0200 Subject: [PATCH 4/6] Fix serialising materials without nozzle profile again Oops. This is simpler anyway. Contributes to issue CURA-6600. --- plugins/XmlMaterialProfile/XmlMaterialProfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index 71a7f8e593..5ec14fe60c 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -245,7 +245,7 @@ class XmlMaterialProfile(InstanceContainer): machine_container_map[definition_id] = container continue - variant_dict = {"variant_type": container.getMetaDataEntry("hardware_type", str(VariantType.NOZZLE)), + variant_dict = {"variant_type": container.getMetaDataEntry("hardware_type", "nozzle"), "material_container": container} machine_variant_map[definition_id][variant_name] = variant_dict From 39523667984e46c2f214c0791bda4f19e0faa105 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 17:08:56 +0200 Subject: [PATCH 5/6] Move createMaterial to MaterialManagementModel and simplify it a bit We can reuse our duplicateMaterial function again but in a simpler way. Also finding the preferred material is simpler with our container tree. However there seems to be a problem with finding the preferred material; it's not finding generic_pla for UM3 and AA0.4 anyway, and then falls back on a random material. This needs to be fixed in the variant node class. Contributes to issue CURA-6600. --- .../Models/MaterialManagementModel.py | 37 ++++++++++++++++++- .../Preferences/Materials/MaterialsPage.qml | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cura/Machines/Models/MaterialManagementModel.py b/cura/Machines/Models/MaterialManagementModel.py index 301dc12025..6ff836cef2 100644 --- a/cura/Machines/Models/MaterialManagementModel.py +++ b/cura/Machines/Models/MaterialManagementModel.py @@ -4,15 +4,20 @@ import copy # To duplicate materials. from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page. from typing import Any, Dict, Optional, TYPE_CHECKING +import uuid # To generate new GUIDs for new materials. +from UM.i18n import i18nCatalog from UM.Logger import Logger import cura.CuraApplication # Imported like this to prevent circular imports. +from cura.Machines.ContainerTree import ContainerTree from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find the sets of materials belonging to each other, and currently loaded extruder stacks. if TYPE_CHECKING: from cura.Machines.MaterialNode import MaterialNode +catalog = i18nCatalog("cura") + ## Proxy class to the materials page in the preferences. # # This class handles the actions in that page, such as creating new materials, @@ -128,4 +133,34 @@ class MaterialManagementModel(QObject): favorites_set.add(new_base_id) application.getPreferences().setValue("cura/favorite_materials", ";".join(favorites_set)) - return new_base_id \ No newline at end of file + return new_base_id + + ## Create a new material by cloning the preferred material for the current + # material diameter and generate a new GUID. + # + # The material type is explicitly left to be the one from the preferred + # material, since this allows the user to still have SOME profiles to work + # with. + # \return The ID of the newly created material. + @pyqtSlot(result = str) + def createMaterial(self) -> str: + # Ensure all settings are saved. + application = cura.CuraApplication.CuraApplication.getInstance() + application.saveSettings() + + # Find the preferred material. + extruder_stack = application.getMachineManager().activeStack + active_variant_name = extruder_stack.variant.getName() + approximate_diameter = str(extruder_stack.approximateMaterialDiameter) + machine_node = ContainerTree.getInstance().machines[application.getGlobalContainerStack().definition.getId()] + preferred_material_node = machine_node.variants[active_variant_name].preferredMaterial(approximate_diameter) + + # Create a new ID & new metadata for the new material. + new_id = CuraContainerRegistry.getInstance().uniqueName("custom_material") + new_metadata = {"name": catalog.i18nc("@label", "Custom Material"), + "brand": catalog.i18nc("@label", "Custom"), + "GUID": str(uuid.uuid4()), + } + + self.duplicateMaterial(preferred_material_node, new_base_id = new_id, new_metadata = new_metadata) + return new_id \ No newline at end of file diff --git a/resources/qml/Preferences/Materials/MaterialsPage.qml b/resources/qml/Preferences/Materials/MaterialsPage.qml index 1568e8707f..6e637a8d1f 100644 --- a/resources/qml/Preferences/Materials/MaterialsPage.qml +++ b/resources/qml/Preferences/Materials/MaterialsPage.qml @@ -122,7 +122,7 @@ Item onClicked: { forceActiveFocus(); - base.newRootMaterialIdToSwitchTo = base.materialManager.createMaterial(); + base.newRootMaterialIdToSwitchTo = base.materialManagementModel.createMaterial(); base.toActivateNewMaterial = true; } } From 9297890d78ec78d522f88e2b65480870a1e6576f Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 27 Aug 2019 17:21:05 +0200 Subject: [PATCH 6/6] Fix typing of approximate diameter and add typing to function for it Seems I forgot to add typing and that's biting my bum right now. Contributes to issue CURA-6600. --- cura/Machines/Models/MaterialManagementModel.py | 2 +- cura/Machines/VariantNode.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cura/Machines/Models/MaterialManagementModel.py b/cura/Machines/Models/MaterialManagementModel.py index 6ff836cef2..90e63e6240 100644 --- a/cura/Machines/Models/MaterialManagementModel.py +++ b/cura/Machines/Models/MaterialManagementModel.py @@ -151,7 +151,7 @@ class MaterialManagementModel(QObject): # Find the preferred material. extruder_stack = application.getMachineManager().activeStack active_variant_name = extruder_stack.variant.getName() - approximate_diameter = str(extruder_stack.approximateMaterialDiameter) + approximate_diameter = int(extruder_stack.approximateMaterialDiameter) machine_node = ContainerTree.getInstance().machines[application.getGlobalContainerStack().definition.getId()] preferred_material_node = machine_node.variants[active_variant_name].preferredMaterial(approximate_diameter) diff --git a/cura/Machines/VariantNode.py b/cura/Machines/VariantNode.py index f5a1e3006e..abcd588c4a 100644 --- a/cura/Machines/VariantNode.py +++ b/cura/Machines/VariantNode.py @@ -79,7 +79,7 @@ class VariantNode(ContainerNode): # material. # \return The node for the preferred material, or any arbitrary material # if there is no match. - def preferredMaterial(self, approximate_diameter) -> MaterialNode: + def preferredMaterial(self, approximate_diameter: int) -> MaterialNode: for base_material, material_node in self.materials.items(): if self.machine.preferred_material in base_material and approximate_diameter == int(material_node.getMetaDataEntry("approximate_diameter")): return material_node