From e29cc5e699d70476936f652710964935d9d92025 Mon Sep 17 00:00:00 2001 From: Arjen Hiemstra Date: Wed, 25 May 2016 11:26:19 +0200 Subject: [PATCH] Create machine-specific material containers for machine specific overrides in XML material files --- .../XmlMaterialProfile/XmlMaterialProfile.py | 67 ++++++++++++++++--- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/plugins/XmlMaterialProfile/XmlMaterialProfile.py b/plugins/XmlMaterialProfile/XmlMaterialProfile.py index 5566301a09..50c39238fd 100644 --- a/plugins/XmlMaterialProfile/XmlMaterialProfile.py +++ b/plugins/XmlMaterialProfile/XmlMaterialProfile.py @@ -2,10 +2,16 @@ # Cura is released under the terms of the AGPLv3 or higher. import math +import copy import xml.etree.ElementTree as ET +from UM.Logger import Logger + import UM.Settings +# The namespace is prepended to the tag name but between {}. +# We are only interested in the actual tag name, so discard everything +# before the last } def _tag_without_namespace(element): return element.tag[element.tag.rfind("}") + 1:] @@ -17,7 +23,6 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer): raise NotImplementedError("Writing material profiles has not yet been implemented") def deserialize(self, serialized): - print("deserialize material profile") data = ET.fromstring(serialized) self.addMetaDataEntry("type", "material") @@ -27,9 +32,7 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer): metadata = data.iterfind("./um:metadata/*", self.__namespaces) for entry in metadata: - # The namespace is prepended to the tag name but between {}. - # We are only interested in the actual tag name. - tag_name = entry.tag[entry.tag.rfind("}") + 1:] + tag_name = _tag_without_namespace(entry) if tag_name == "name": brand = entry.find("./um:brand", self.__namespaces) @@ -47,7 +50,7 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer): property_values = {} properties = data.iterfind("./um:properties/*", self.__namespaces) for entry in properties: - tag_name = entry.tag[entry.tag.rfind("}") + 1:] + tag_name = _tag_without_namespace(entry) property_values[tag_name] = entry.text diameter = float(property_values.get("diameter", 2.85)) # In mm @@ -66,12 +69,50 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer): self.addMetaDataEntry("properties", property_values) + global_setting_values = {} settings = data.iterfind("./um:settings/um:setting", self.__namespaces) for entry in settings: - tag_name = _tag_without_namespace(entry) + key = entry.get("key") + if key in self.__material_property_setting_map: + self.setProperty(self.__material_property_setting_map[key], "value", entry.text, self._definition) + global_setting_values[key] = entry.text + + machines = data.iterfind("./um:settings/um:machine", self.__namespaces) + for machine in machines: + machine_setting_values = {} + settings = machine.iterfind("./um:setting", self.__namespaces) + for entry in settings: + key = entry.get("key") + if key in self.__material_property_setting_map: + machine_setting_values[self.__material_property_setting_map[key]] = entry.text + + identifiers = machine.iterfind("./um:machine_identifier", self.__namespaces) + for identifier in identifiers: + machine_id = self.__product_id_map.get(identifier.get("product"), None) + if machine_id is None: + Logger.log("w", "Cannot create material for unknown machine %s", machine_id) + continue + + definitions = UM.Settings.ContainerRegistry.getInstance().findDefinitionContainers(id = machine_id) + if not definitions: + Logger.log("w", "No definition found for machine ID %s", machine_id) + continue + + new_material = XmlMaterialProfile(self.id + "_" + machine_id) + new_material.setName(self.getName()) + new_material.setMetaData(self.getMetaData()) + new_material.setDefinition(definitions[0]) + + for key, value in global_setting_values.items(): + new_material.setProperty(key, "value", value, definitions[0]) + + for key, value in machine_setting_values.items(): + new_material.setProperty(key, "value", value, definitions[0]) + + new_material._dirty = False + + UM.Settings.ContainerRegistry.getInstance().addContainer(new_material) - if tag_name in self.__material_property_setting_map: - self.setProperty(self.__material_property_setting_map[tag_name], "value", entry.text) __material_property_setting_map = { "print temperature": "material_print_temperature", @@ -79,6 +120,16 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer): "standby temperature": "material_standby_temperature", } + __product_id_map = { + "Ultimaker2": "ultimaker2", + "Ultimaker2+": "ultimaker2_plus", + "Ultimaker2go": "ultimaker2_go", + "Ultimaker2extended": "ultimaker2_extended", + "Ultimaker2extended+": "ultimaker2_extended_plus", + "Ultimaker Original": "ultimaker_original", + "Ultimaker Original+": "ultimaker_original_plus" + } + __namespaces = { "um": "http://www.ultimaker.com/material" }