mirror of
https://git.mirrors.martin98.com/https://github.com/Ultimaker/Cura
synced 2025-08-15 03:55:55 +08:00
Move duplicateMaterial() to MaterialManager
This commit is contained in:
parent
ca785c9df3
commit
7642afb9fe
@ -2,10 +2,12 @@
|
|||||||
# Cura is released under the terms of the LGPLv3 or higher.
|
# Cura is released under the terms of the LGPLv3 or higher.
|
||||||
|
|
||||||
from collections import defaultdict, OrderedDict
|
from collections import defaultdict, OrderedDict
|
||||||
|
import copy
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from PyQt5.Qt import QTimer, QObject, pyqtSignal, pyqtSlot
|
from PyQt5.Qt import QTimer, QObject, pyqtSignal, pyqtSlot
|
||||||
|
|
||||||
|
from UM.Application import Application
|
||||||
from UM.Logger import Logger
|
from UM.Logger import Logger
|
||||||
from UM.Settings import ContainerRegistry
|
from UM.Settings import ContainerRegistry
|
||||||
|
|
||||||
@ -29,6 +31,7 @@ class MaterialManager(QObject):
|
|||||||
|
|
||||||
def __init__(self, container_registry, parent = None):
|
def __init__(self, container_registry, parent = None):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
self._application = Application.getInstance()
|
||||||
self._container_registry = container_registry # type: ContainerRegistry
|
self._container_registry = container_registry # type: ContainerRegistry
|
||||||
|
|
||||||
self._fallback_materials_map = dict() # material_type -> generic material metadata
|
self._fallback_materials_map = dict() # material_type -> generic material metadata
|
||||||
@ -356,3 +359,60 @@ class MaterialManager(QObject):
|
|||||||
|
|
||||||
material_group = self.getMaterialGroup(root_material_id)
|
material_group = self.getMaterialGroup(root_material_id)
|
||||||
material_group.root_material_node.getContainer().setName(name)
|
material_group.root_material_node.getContainer().setName(name)
|
||||||
|
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
@pyqtSlot("QVariant", result = str)
|
||||||
|
def duplicateMaterial(self, material_node, new_base_id = None, new_metadata = None) -> Optional[str]:
|
||||||
|
root_material_id = material_node.metadata["base_file"]
|
||||||
|
|
||||||
|
material_group = self.getMaterialGroup(root_material_id)
|
||||||
|
if not material_group:
|
||||||
|
Logger.log("i", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
|
||||||
|
return None
|
||||||
|
|
||||||
|
base_container = material_group.root_material_node.getContainer()
|
||||||
|
containers_to_copy = []
|
||||||
|
for node in material_group.derived_material_node_list:
|
||||||
|
containers_to_copy.append(node.getContainer())
|
||||||
|
|
||||||
|
# Ensure all settings are saved.
|
||||||
|
self._application.saveSettings()
|
||||||
|
|
||||||
|
# Create a new ID & container to hold the data.
|
||||||
|
new_containers = []
|
||||||
|
if new_base_id is None:
|
||||||
|
new_base_id = self._container_registry.uniqueName(base_container.getId())
|
||||||
|
new_base_container = copy.deepcopy(base_container)
|
||||||
|
new_base_container.getMetaData()["id"] = new_base_id
|
||||||
|
new_base_container.getMetaData()["base_file"] = new_base_id
|
||||||
|
if new_metadata is not None:
|
||||||
|
for key, value in new_metadata.items():
|
||||||
|
new_base_container.getMetaData()[key] = value
|
||||||
|
new_containers.append(new_base_container)
|
||||||
|
|
||||||
|
# Clone all of them.
|
||||||
|
for container_to_copy in containers_to_copy:
|
||||||
|
# Create unique IDs for every clone.
|
||||||
|
new_id = new_base_id
|
||||||
|
if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
|
||||||
|
new_id += "_" + container_to_copy.getMetaDataEntry("definition")
|
||||||
|
if container_to_copy.getMetaDataEntry("variant_name"):
|
||||||
|
variant_name = container_to_copy.getMetaDataEntry("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:
|
||||||
|
for key, value in new_metadata.items():
|
||||||
|
new_container.getMetaData()[key] = value
|
||||||
|
|
||||||
|
new_containers.append(new_container)
|
||||||
|
|
||||||
|
for container_to_add in new_containers:
|
||||||
|
container_to_add.setDirty(True)
|
||||||
|
self._container_registry.addContainer(container_to_add)
|
||||||
|
return new_base_id
|
||||||
|
@ -395,61 +395,6 @@ class ContainerManager(QObject):
|
|||||||
self._container_registry.removeContainer(node.metadata["id"])
|
self._container_registry.removeContainer(node.metadata["id"])
|
||||||
|
|
||||||
|
|
||||||
## Create a duplicate of a material, which has the same GUID and base_file metadata
|
|
||||||
#
|
|
||||||
# \return \type{str} the id of the newly created container.
|
|
||||||
@pyqtSlot("QVariant", result = str)
|
|
||||||
def duplicateMaterial(self, material_node, new_base_id = None, new_metadata = None):
|
|
||||||
root_material_id = material_node.metadata["base_file"]
|
|
||||||
|
|
||||||
material_group = self._material_manager.getMaterialGroup(root_material_id)
|
|
||||||
if not material_group:
|
|
||||||
Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
|
|
||||||
return
|
|
||||||
|
|
||||||
base_container = material_group.root_material_node.getContainer()
|
|
||||||
containers_to_copy = []
|
|
||||||
for node in material_group.derived_material_node_list:
|
|
||||||
containers_to_copy.append(node.getContainer())
|
|
||||||
|
|
||||||
# Ensure all settings are saved.
|
|
||||||
Application.getInstance().saveSettings()
|
|
||||||
|
|
||||||
# Create a new ID & container to hold the data.
|
|
||||||
new_containers = []
|
|
||||||
if new_base_id is None:
|
|
||||||
new_base_id = self._container_registry.uniqueName(base_container.getId())
|
|
||||||
new_base_container = copy.deepcopy(base_container)
|
|
||||||
new_base_container.getMetaData()["id"] = new_base_id
|
|
||||||
new_base_container.getMetaData()["base_file"] = new_base_id
|
|
||||||
if new_metadata is not None:
|
|
||||||
for key, value in new_metadata.items():
|
|
||||||
new_base_container.getMetaData()[key] = value
|
|
||||||
new_containers.append(new_base_container)
|
|
||||||
|
|
||||||
# Clone all of them.
|
|
||||||
for container_to_copy in containers_to_copy:
|
|
||||||
# Create unique IDs for every clone.
|
|
||||||
new_id = new_base_id
|
|
||||||
if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
|
|
||||||
new_id += "_" + container_to_copy.getMetaDataEntry("definition")
|
|
||||||
if container_to_copy.getMetaDataEntry("variant_name"):
|
|
||||||
variant_name = container_to_copy.getMetaDataEntry("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:
|
|
||||||
for key, value in new_metadata.items():
|
|
||||||
new_container.getMetaData()[key] = value
|
|
||||||
|
|
||||||
new_containers.append(new_container)
|
|
||||||
|
|
||||||
for container_to_add in new_containers:
|
|
||||||
container_to_add.setDirty(True)
|
|
||||||
ContainerRegistry.getInstance().addContainer(container_to_add)
|
|
||||||
return new_base_id
|
|
||||||
|
|
||||||
## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
|
## Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
|
||||||
#
|
#
|
||||||
|
@ -13,6 +13,8 @@ import Cura 1.0 as Cura
|
|||||||
Item
|
Item
|
||||||
{
|
{
|
||||||
id: base
|
id: base
|
||||||
|
|
||||||
|
property QtObject materialManager: CuraApplication.getMaterialManager()
|
||||||
property var resetEnabled: false // Keep PreferencesDialog happy
|
property var resetEnabled: false // Keep PreferencesDialog happy
|
||||||
|
|
||||||
UM.I18nCatalog { id: catalog; name: "cura"; }
|
UM.I18nCatalog { id: catalog; name: "cura"; }
|
||||||
@ -89,7 +91,7 @@ Item
|
|||||||
enabled: base.hasCurrentItem
|
enabled: base.hasCurrentItem
|
||||||
onClicked: {
|
onClicked: {
|
||||||
forceActiveFocus();
|
forceActiveFocus();
|
||||||
base.newRootMaterialIdToSwitchTo = Cura.ContainerManager.duplicateMaterial(base.currentItem.container_node);
|
base.newRootMaterialIdToSwitchTo = base.materialManager.duplicateMaterial(base.currentItem.container_node);
|
||||||
base.toActivateNewMaterial = true;
|
base.toActivateNewMaterial = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user