mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-08 07:27:29 -06:00
XML material profile merging is now far less naive.
It's still a work in progress, as it doesn't succeed in all cases just yet. CURA-2108
This commit is contained in:
parent
1fda24c43f
commit
bacac1e42e
1 changed files with 48 additions and 22 deletions
|
@ -58,9 +58,9 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
|
||||||
def setReadOnly(self, read_only):
|
def setReadOnly(self, read_only):
|
||||||
super().setReadOnly(read_only)
|
super().setReadOnly(read_only)
|
||||||
|
|
||||||
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
|
basefile = self.getMetaDataEntry("base_file", self._id) # if basefile is self.id, this is a basefile.
|
||||||
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
||||||
container._read_only = read_only
|
container._read_only = read_only # prevent loop instead of calling setReadOnly
|
||||||
|
|
||||||
## Overridden from InstanceContainer
|
## Overridden from InstanceContainer
|
||||||
def setMetaDataEntry(self, key, value):
|
def setMetaDataEntry(self, key, value):
|
||||||
|
@ -69,7 +69,7 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
|
||||||
|
|
||||||
super().setMetaDataEntry(key, value)
|
super().setMetaDataEntry(key, value)
|
||||||
|
|
||||||
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
|
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is self.id, this is a basefile.
|
||||||
# Update all containers that share GUID and basefile
|
# Update all containers that share GUID and basefile
|
||||||
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
||||||
container.setMetaData(copy.deepcopy(self._metadata))
|
container.setMetaData(copy.deepcopy(self._metadata))
|
||||||
|
@ -95,15 +95,15 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
|
||||||
container.setName(new_name)
|
container.setName(new_name)
|
||||||
|
|
||||||
## Overridden from InstanceContainer
|
## Overridden from InstanceContainer
|
||||||
def setProperty(self, key, property_name, property_value, container = None):
|
# def setProperty(self, key, property_name, property_value, container = None):
|
||||||
if self.isReadOnly():
|
# if self.isReadOnly():
|
||||||
return
|
# return
|
||||||
|
#
|
||||||
super().setProperty(key, property_name, property_value)
|
# super().setProperty(key, property_name, property_value)
|
||||||
|
#
|
||||||
basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is none, this is a basefile.
|
# basefile = self.getMetaDataEntry("base_file", self._id) #if basefile is self.id, this is a basefile.
|
||||||
for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
# for container in UM.Settings.ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
|
||||||
container._dirty = True
|
# container._dirty = True
|
||||||
|
|
||||||
## Overridden from InstanceContainer
|
## Overridden from InstanceContainer
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
|
@ -277,24 +277,50 @@ class XmlMaterialProfile(UM.Settings.InstanceContainer):
|
||||||
self._combineElement(result, second)
|
self._combineElement(result, second)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def _createKey(self, element):
|
||||||
|
key = element.tag.split("}")[-1]
|
||||||
|
if "key" in element.attrib:
|
||||||
|
key += " key:" + element.attrib["key"]
|
||||||
|
if "manufacturer" in element.attrib:
|
||||||
|
key += " manufacturer:" + element.attrib["manufacturer"]
|
||||||
|
if "product" in element.attrib:
|
||||||
|
key += " product:" + element.attrib["product"]
|
||||||
|
if key == "machine":
|
||||||
|
for item in element:
|
||||||
|
if "machine_identifier" in item.tag:
|
||||||
|
key += " " + item.attrib["product"]
|
||||||
|
return key
|
||||||
|
|
||||||
# Recursively merges XML elements. Updates either the text or children if another element is found in first.
|
# Recursively merges XML elements. Updates either the text or children if another element is found in first.
|
||||||
# If it does not exist, copies it from second.
|
# If it does not exist, copies it from second.
|
||||||
def _combineElement(self, first, second):
|
def _combineElement(self, first, second):
|
||||||
# Create a mapping from tag name to element.
|
# Create a mapping from tag name to element.
|
||||||
mapping = {el.tag: el for el in first}
|
|
||||||
for el in second:
|
mapping = {}
|
||||||
if len(el): # Check if element has children.
|
for element in first:
|
||||||
|
key = self._createKey(element)
|
||||||
|
mapping[key] = element
|
||||||
|
for element in second:
|
||||||
|
key = self._createKey(element)
|
||||||
|
if len(element): # Check if element has children.
|
||||||
try:
|
try:
|
||||||
self._combineElement(mapping[el.tag], el) # Multiple elements, handle those.
|
if "setting " in key:
|
||||||
|
# Setting can have points in it. In that case, delete all values and override them.
|
||||||
|
for child in list(mapping[key]):
|
||||||
|
mapping[key].remove(child)
|
||||||
|
for child in element:
|
||||||
|
mapping[key].append(child)
|
||||||
|
else:
|
||||||
|
self._combineElement(mapping[key], element) # Multiple elements, handle those.
|
||||||
except KeyError:
|
except KeyError:
|
||||||
mapping[el.tag] = el
|
mapping[key] = element
|
||||||
first.append(el)
|
first.append(element)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
mapping[el.tag].text = el.text
|
mapping[key].text = element.text
|
||||||
except KeyError: # Not in the mapping, so simply add it
|
except KeyError: # Not in the mapping, so simply add it
|
||||||
mapping[el.tag] = el
|
mapping[key] = element
|
||||||
first.append(el)
|
first.append(element)
|
||||||
|
|
||||||
## Overridden from InstanceContainer
|
## Overridden from InstanceContainer
|
||||||
def deserialize(self, serialized):
|
def deserialize(self, serialized):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue