Do not overwrite existing metadata with in material deserializeMetadata()

CURA-5056
This commit is contained in:
Lipu Fei 2018-03-14 10:32:53 +01:00
parent dc427488a2
commit 86afd6f5ff
2 changed files with 15 additions and 29 deletions

View file

@ -265,13 +265,9 @@ class ThreeMFWorkspaceReader(WorkspaceReader):
for material_container_file in material_container_files: for material_container_file in material_container_files:
container_id = self._stripFileToId(material_container_file) container_id = self._stripFileToId(material_container_file)
from hashlib import sha1
hex_container_id = sha1(container_id.encode('utf-8')).hexdigest()
serialized = archive.open(material_container_file).read().decode("utf-8") serialized = archive.open(material_container_file).read().decode("utf-8")
metadata_list = xml_material_profile.deserializeMetadata(serialized, hex_container_id) metadata_list = xml_material_profile.deserializeMetadata(serialized, container_id)
reverse_map = {metadata["id"].replace(hex_container_id, container_id): container_id.replace(hex_container_id, container_id) reverse_map = {metadata["id"]: container_id for metadata in metadata_list}
for metadata in metadata_list}
reverse_material_id_dict.update(reverse_map) reverse_material_id_dict.update(reverse_map)
material_labels.append(self._getMaterialLabelFromSerialized(serialized)) material_labels.append(self._getMaterialLabelFromSerialized(serialized))

View file

@ -838,14 +838,10 @@ class XmlMaterialProfile(InstanceContainer):
if machine_compatibility: if machine_compatibility:
new_material_id = container_id + "_" + machine_id new_material_id = container_id + "_" + machine_id
# The child or derived material container may already exist. This can happen when a material in a # Do not look for existing container/container metadata with the same ID although they may exist.
# project file and the a material in Cura have the same ID. # In project loading and perhaps some other places, we only want to get information (metadata)
# In the case if a derived material already exists, override that material container because if # from a file without changing the current state of the system. If we overwrite the existing
# the data in the parent material has been changed, the derived ones should be updated too. # metadata here, deserializeMetadata() will not be safe for retrieving information.
found_materials = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = new_material_id)
if found_materials:
new_material_metadata = found_materials[0]
else:
new_material_metadata = {} new_material_metadata = {}
new_material_metadata.update(base_metadata) new_material_metadata.update(base_metadata)
@ -854,7 +850,6 @@ class XmlMaterialProfile(InstanceContainer):
new_material_metadata["machine_manufacturer"] = machine_manufacturer new_material_metadata["machine_manufacturer"] = machine_manufacturer
new_material_metadata["definition"] = machine_id new_material_metadata["definition"] = machine_id
if len(found_materials) == 0: #This is a new material.
result_metadata.append(new_material_metadata) result_metadata.append(new_material_metadata)
buildplates = machine.iterfind("./um:buildplate", cls.__namespaces) buildplates = machine.iterfind("./um:buildplate", cls.__namespaces)
@ -866,12 +861,12 @@ class XmlMaterialProfile(InstanceContainer):
if buildplate_id is None: if buildplate_id is None:
continue continue
variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = buildplate_id) variant_metadata = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = buildplate_id)
if not variant_containers: if not variant_metadata:
# It is not really properly defined what "ID" is so also search for variants by name. # It is not really properly defined what "ID" is so also search for variants by name.
variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(definition = machine_id, name = buildplate_id) variant_metadata = ContainerRegistry.getInstance().findInstanceContainersMetadata(definition = machine_id, name = buildplate_id)
if not variant_containers: if not variant_metadata:
continue continue
settings = buildplate.iterfind("./um:setting", cls.__namespaces) settings = buildplate.iterfind("./um:setting", cls.__namespaces)
@ -900,11 +895,7 @@ class XmlMaterialProfile(InstanceContainer):
new_hotend_specific_material_id = container_id + "_" + machine_id + "_" + hotend_name.replace(" ", "_") new_hotend_specific_material_id = container_id + "_" + machine_id + "_" + hotend_name.replace(" ", "_")
# Same as machine compatibility, keep the derived material containers consistent with the parent material # Same as above, do not overwrite existing metadata.
found_materials = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = new_hotend_specific_material_id)
if found_materials:
new_hotend_material_metadata = found_materials[0]
else:
new_hotend_material_metadata = {} new_hotend_material_metadata = {}
new_hotend_material_metadata.update(base_metadata) new_hotend_material_metadata.update(base_metadata)
@ -917,7 +908,6 @@ class XmlMaterialProfile(InstanceContainer):
new_hotend_material_metadata["buildplate_compatible"] = buildplate_map["buildplate_compatible"] new_hotend_material_metadata["buildplate_compatible"] = buildplate_map["buildplate_compatible"]
new_hotend_material_metadata["buildplate_recommended"] = buildplate_map["buildplate_recommended"] new_hotend_material_metadata["buildplate_recommended"] = buildplate_map["buildplate_recommended"]
if len(found_materials) == 0:
result_metadata.append(new_hotend_material_metadata) result_metadata.append(new_hotend_material_metadata)
# there is only one ID for a machine. Once we have reached here, it means we have already found # there is only one ID for a machine. Once we have reached here, it means we have already found