Add "Create" button to create a new, unique material.

The new custom material is still based on generic_pla, but it gets a new GUID and uses itself as base_file so it is not conjoined to another material.
This commit is contained in:
fieldOfView 2017-04-27 11:19:46 +02:00
parent d0a95426cb
commit 02b978272f
2 changed files with 45 additions and 0 deletions

View file

@ -3,6 +3,7 @@
import os.path
import urllib
import uuid
from typing import Dict, Union
from PyQt5.QtCore import QObject, QUrl, QVariant
@ -694,6 +695,33 @@ class ContainerManager(QObject):
duplicated_container.setDirty(True)
self._container_registry.addContainer(duplicated_container)
@pyqtSlot(result = str)
def createMaterial(self) -> str:
# Ensure all settings are saved.
Application.getInstance().saveSettings()
containers = self._container_registry.findInstanceContainers(id="generic_pla")
if not containers:
Logger.log("d", "Unable to creata a new material by cloning generic_pla, because it doesn't exist.")
return ""
# Create a new ID & container to hold the data.
new_id = self._container_registry.uniqueName("custom_material")
container_type = type(containers[0]) # Could be either a XMLMaterialProfile or a InstanceContainer
duplicated_container = container_type(new_id)
# Instead of duplicating we load the data from the basefile again.
# This ensures that the inheritance goes well and all "cut up" subclasses of the xmlMaterial profile
# are also correctly created.
with open(containers[0].getPath(), encoding="utf-8") as f:
duplicated_container.deserialize(f.read())
duplicated_container.setMetaDataEntry("base_file", new_id)
duplicated_container.setMetaDataEntry("GUID", str(uuid.uuid4()))
duplicated_container.setName(catalog.i18nc("@label", "Custom Material"))
self._container_registry.addContainer(duplicated_container)
## Get the singleton instance for this class.
@classmethod
def getInstance(cls) -> "ContainerManager":

View file

@ -135,6 +135,23 @@ UM.ManagementPage
}
},
Button
{
text: catalog.i18nc("@action:button", "Create")
iconName: "list-add"
onClicked:
{
var material_id = Cura.ContainerManager.createMaterial()
if(material_id == "")
{
return
}
if(Cura.MachineManager.hasMaterials)
{
Cura.MachineManager.setActiveMaterial(material_id)
}
}
},
Button
{
text: catalog.i18nc("@action:button", "Duplicate");
iconName: "list-add";