Also move printer name mapping to new FormatMaps utility.

done as part of CURA-12005
This commit is contained in:
Remco Burema 2024-08-01 14:33:24 +02:00
parent f81a3f0e11
commit ddf4f4c6fe
3 changed files with 31 additions and 26 deletions

View File

@ -1,6 +1,11 @@
# Copyright (c) 2024 UltiMaker # Copyright (c) 2024 UltiMaker
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
from UM.Resources import Resources
import json
from typing import Dict, List, Optional
class FormatMaps: class FormatMaps:
PRINTER_TYPE_NAME = { PRINTER_TYPE_NAME = {
@ -38,3 +43,25 @@ class FormatMaps:
"tpu": {"name": "TPU 95A", "guid": "19baa6a9-94ff-478b-b4a1-8157b74358d2"}, "tpu": {"name": "TPU 95A", "guid": "19baa6a9-94ff-478b-b4a1-8157b74358d2"},
"im-pla": {"name": "Tough", "guid": "de031137-a8ca-4a72-bd1b-17bb964033ad"} "im-pla": {"name": "Tough", "guid": "de031137-a8ca-4a72-bd1b-17bb964033ad"}
} }
__product_to_id_map: Optional[Dict[str, List[str]]] = None
@classmethod
def getProductIdMap(cls) -> Dict[str, List[str]]:
"""Gets a mapping from product names in the XML files to their definition IDs.
This loads the mapping from a file.
"""
if cls.__product_to_id_map is not None:
return cls.__product_to_id_map
product_to_id_file = Resources.getPath(Resources.Texts, "product_to_id.json")
with open(product_to_id_file, encoding = "utf-8") as f:
contents = ""
for line in f:
contents += line if "#" not in line else "".join([line.replace("#", str(n)) for n in range(1, 12)])
cls.__product_to_id_map = json.loads(contents)
cls.__product_to_id_map = {key: [value] for key, value in cls.__product_to_id_map.items()}
#This also loads "Ultimaker S5" -> "ultimaker_s5" even though that is not strictly necessary with the default to change spaces into underscores.
#However it is not always loaded with that default; this mapping is also used in serialize() without that default.
return cls.__product_to_id_map

View File

@ -17,6 +17,7 @@ from UM.Settings.ContainerRegistry import ContainerRegistry
from UM.ConfigurationErrorMessage import ConfigurationErrorMessage from UM.ConfigurationErrorMessage import ConfigurationErrorMessage
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.FormatMaps import FormatMaps
from cura.Machines.VariantType import VariantType from cura.Machines.VariantType import VariantType
try: try:
@ -249,7 +250,7 @@ class XmlMaterialProfile(InstanceContainer):
machine_variant_map[definition_id][variant_name] = variant_dict machine_variant_map[definition_id][variant_name] = variant_dict
# Map machine human-readable names to IDs # Map machine human-readable names to IDs
product_id_map = self.getProductIdMap() product_id_map = FormatMaps.getProductIdMap()
for definition_id, container in machine_container_map.items(): for definition_id, container in machine_container_map.items():
definition_id = container.getMetaDataEntry("definition") definition_id = container.getMetaDataEntry("definition")
@ -647,7 +648,7 @@ class XmlMaterialProfile(InstanceContainer):
self._dirty = False self._dirty = False
# Map machine human-readable names to IDs # Map machine human-readable names to IDs
product_id_map = self.getProductIdMap() product_id_map = FormatMaps.getProductIdMap()
machines = data.iterfind("./um:settings/um:machine", self.__namespaces) machines = data.iterfind("./um:settings/um:machine", self.__namespaces)
for machine in machines: for machine in machines:
@ -923,7 +924,7 @@ class XmlMaterialProfile(InstanceContainer):
result_metadata.append(base_metadata) result_metadata.append(base_metadata)
# Map machine human-readable names to IDs # Map machine human-readable names to IDs
product_id_map = cls.getProductIdMap() product_id_map = FormatMaps.getProductIdMap()
for machine in data.iterfind("./um:settings/um:machine", cls.__namespaces): for machine in data.iterfind("./um:settings/um:machine", cls.__namespaces):
machine_compatibility = common_compatibility machine_compatibility = common_compatibility
@ -1128,29 +1129,6 @@ class XmlMaterialProfile(InstanceContainer):
id_list = list(id_list) id_list = list(id_list)
return id_list return id_list
__product_to_id_map: Optional[Dict[str, List[str]]] = None
@classmethod
def getProductIdMap(cls) -> Dict[str, List[str]]:
"""Gets a mapping from product names in the XML files to their definition IDs.
This loads the mapping from a file.
"""
if cls.__product_to_id_map is not None:
return cls.__product_to_id_map
plugin_path = cast(str, PluginRegistry.getInstance().getPluginPath("XmlMaterialProfile"))
product_to_id_file = os.path.join(plugin_path, "product_to_id.json")
with open(product_to_id_file, encoding = "utf-8") as f:
contents = ""
for line in f:
contents += line if "#" not in line else "".join([line.replace("#", str(n)) for n in range(1, 12)])
cls.__product_to_id_map = json.loads(contents)
cls.__product_to_id_map = {key: [value] for key, value in cls.__product_to_id_map.items()}
#This also loads "Ultimaker S5" -> "ultimaker_s5" even though that is not strictly necessary with the default to change spaces into underscores.
#However it is not always loaded with that default; this mapping is also used in serialize() without that default.
return cls.__product_to_id_map
@staticmethod @staticmethod
def _parseCompatibleValue(value: str): def _parseCompatibleValue(value: str):
"""Parse the value of the "material compatible" property.""" """Parse the value of the "material compatible" property."""