XmlMaterialProfile.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import copy
  4. import io
  5. import json #To parse the product-to-id mapping file.
  6. import os.path #To find the product-to-id mapping.
  7. import sys
  8. from typing import Any, Dict, List, Optional
  9. import xml.etree.ElementTree as ET
  10. from UM.Resources import Resources
  11. from UM.Logger import Logger
  12. from cura.CuraApplication import CuraApplication
  13. import UM.Dictionary
  14. from UM.Settings.InstanceContainer import InstanceContainer
  15. from UM.Settings.ContainerRegistry import ContainerRegistry
  16. ## Handles serializing and deserializing material containers from an XML file
  17. class XmlMaterialProfile(InstanceContainer):
  18. CurrentFdmMaterialVersion = "1.3"
  19. Version = 1
  20. def __init__(self, container_id, *args, **kwargs):
  21. super().__init__(container_id, *args, **kwargs)
  22. self._inherited_files = []
  23. ## Translates the version number in the XML files to the setting_version
  24. # metadata entry.
  25. #
  26. # Since the two may increment independently we need a way to say which
  27. # versions of the XML specification are compatible with our setting data
  28. # version numbers.
  29. #
  30. # \param xml_version: The version number found in an XML file.
  31. # \return The corresponding setting_version.
  32. @classmethod
  33. def xmlVersionToSettingVersion(cls, xml_version: str) -> int:
  34. if xml_version == "1.3":
  35. return 3
  36. return 0 #Older than 1.3.
  37. def getInheritedFiles(self):
  38. return self._inherited_files
  39. ## Overridden from InstanceContainer
  40. def setReadOnly(self, read_only):
  41. super().setReadOnly(read_only)
  42. basefile = self.getMetaDataEntry("base_file", self.getId()) # if basefile is self.getId, this is a basefile.
  43. for container in ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
  44. container._read_only = read_only # prevent loop instead of calling setReadOnly
  45. ## Overridden from InstanceContainer
  46. # set the meta data for all machine / variant combinations
  47. def setMetaDataEntry(self, key, value):
  48. if self.isReadOnly():
  49. return
  50. super().setMetaDataEntry(key, value)
  51. basefile = self.getMetaDataEntry("base_file", self.getId()) #if basefile is self.getId, this is a basefile.
  52. # Update all containers that share basefile
  53. for container in ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile):
  54. if container.getMetaDataEntry(key, None) != value: # Prevent recursion
  55. container.setMetaDataEntry(key, value)
  56. ## Overridden from InstanceContainer, similar to setMetaDataEntry.
  57. # without this function the setName would only set the name of the specific nozzle / material / machine combination container
  58. # The function is a bit tricky. It will not set the name of all containers if it has the correct name itself.
  59. def setName(self, new_name):
  60. if self.isReadOnly():
  61. return
  62. # Not only is this faster, it also prevents a major loop that causes a stack overflow.
  63. if self.getName() == new_name:
  64. return
  65. super().setName(new_name)
  66. basefile = self.getMetaDataEntry("base_file", self.getId()) # if basefile is self.getId, this is a basefile.
  67. # Update the basefile as well, this is actually what we're trying to do
  68. # Update all containers that share GUID and basefile
  69. containers = ContainerRegistry.getInstance().findInstanceContainers(base_file = basefile)
  70. for container in containers:
  71. container.setName(new_name)
  72. ## Overridden from InstanceContainer, to set dirty to base file as well.
  73. def setDirty(self, dirty):
  74. super().setDirty(dirty)
  75. base_file = self.getMetaDataEntry("base_file", None)
  76. if base_file is not None and base_file != self.getId():
  77. containers = ContainerRegistry.getInstance().findContainers(id=base_file)
  78. if containers:
  79. base_container = containers[0]
  80. if not base_container.isReadOnly():
  81. base_container.setDirty(dirty)
  82. ## Overridden from InstanceContainer
  83. # base file: common settings + supported machines
  84. # machine / variant combination: only changes for itself.
  85. def serialize(self, ignored_metadata_keys: Optional[set] = None):
  86. registry = ContainerRegistry.getInstance()
  87. base_file = self.getMetaDataEntry("base_file", "")
  88. if base_file and self.getId() != base_file:
  89. # Since we create an instance of XmlMaterialProfile for each machine and nozzle in the profile,
  90. # we should only serialize the "base" material definition, since that can then take care of
  91. # serializing the machine/nozzle specific profiles.
  92. raise NotImplementedError("Ignoring serializing non-root XML materials, the data is contained in the base material")
  93. builder = ET.TreeBuilder()
  94. root = builder.start("fdmmaterial",
  95. {"xmlns": "http://www.ultimaker.com/material",
  96. "version": self.CurrentFdmMaterialVersion})
  97. ## Begin Metadata Block
  98. builder.start("metadata")
  99. metadata = copy.deepcopy(self.getMetaData())
  100. # setting_version is derived from the "version" tag in the schema, so don't serialize it into a file
  101. if ignored_metadata_keys is None:
  102. ignored_metadata_keys = set()
  103. ignored_metadata_keys |= {"setting_version"}
  104. # remove the keys that we want to ignore in the metadata
  105. for key in ignored_metadata_keys:
  106. if key in metadata:
  107. del metadata[key]
  108. properties = metadata.pop("properties", {})
  109. # Metadata properties that should not be serialized.
  110. metadata.pop("status", "")
  111. metadata.pop("variant", "")
  112. metadata.pop("type", "")
  113. metadata.pop("base_file", "")
  114. metadata.pop("approximate_diameter", "")
  115. metadata.pop("id", "")
  116. metadata.pop("container_type", "")
  117. ## Begin Name Block
  118. builder.start("name")
  119. builder.start("brand")
  120. builder.data(metadata.pop("brand", ""))
  121. builder.end("brand")
  122. builder.start("material")
  123. builder.data(metadata.pop("material", ""))
  124. builder.end("material")
  125. builder.start("color")
  126. builder.data(metadata.pop("color_name", ""))
  127. builder.end("color")
  128. builder.start("label")
  129. builder.data(self.getName())
  130. builder.end("label")
  131. builder.end("name")
  132. ## End Name Block
  133. for key, value in metadata.items():
  134. builder.start(key)
  135. if value is not None: #Nones get handled well by the builder.
  136. #Otherwise the builder always expects a string.
  137. #Deserialize expects the stringified version.
  138. value = str(value)
  139. builder.data(value)
  140. builder.end(key)
  141. builder.end("metadata")
  142. ## End Metadata Block
  143. ## Begin Properties Block
  144. builder.start("properties")
  145. for key, value in properties.items():
  146. builder.start(key)
  147. builder.data(value)
  148. builder.end(key)
  149. builder.end("properties")
  150. ## End Properties Block
  151. ## Begin Settings Block
  152. builder.start("settings")
  153. if self.getDefinition().getId() == "fdmprinter":
  154. for instance in self.findInstances():
  155. self._addSettingElement(builder, instance)
  156. machine_container_map = {}
  157. machine_nozzle_map = {}
  158. all_containers = registry.findInstanceContainers(GUID = self.getMetaDataEntry("GUID"), base_file = self.getId())
  159. for container in all_containers:
  160. definition_id = container.getDefinition().getId()
  161. if definition_id == "fdmprinter":
  162. continue
  163. if definition_id not in machine_container_map:
  164. machine_container_map[definition_id] = container
  165. if definition_id not in machine_nozzle_map:
  166. machine_nozzle_map[definition_id] = {}
  167. variant = container.getMetaDataEntry("variant")
  168. if variant:
  169. machine_nozzle_map[definition_id][variant] = container
  170. continue
  171. machine_container_map[definition_id] = container
  172. # Map machine human-readable names to IDs
  173. product_id_map = self.getProductIdMap()
  174. for definition_id, container in machine_container_map.items():
  175. definition = container.getDefinition()
  176. try:
  177. product = UM.Dictionary.findKey(product_id_map, definition_id)
  178. except ValueError:
  179. # An unknown product id; export it anyway
  180. product = definition_id
  181. builder.start("machine")
  182. builder.start("machine_identifier", {
  183. "manufacturer": container.getMetaDataEntry("machine_manufacturer", definition.getMetaDataEntry("manufacturer", "Unknown")),
  184. "product": product
  185. })
  186. builder.end("machine_identifier")
  187. for instance in container.findInstances():
  188. if self.getDefinition().getId() == "fdmprinter" and self.getInstance(instance.definition.key) and self.getProperty(instance.definition.key, "value") == instance.value:
  189. # If the settings match that of the base profile, just skip since we inherit the base profile.
  190. continue
  191. self._addSettingElement(builder, instance)
  192. # Find all hotend sub-profiles corresponding to this material and machine and add them to this profile.
  193. for hotend_id, hotend in machine_nozzle_map[definition_id].items():
  194. variant_containers = registry.findInstanceContainersMetadata(id = hotend.getMetaDataEntry("variant"))
  195. if not variant_containers:
  196. continue
  197. builder.start("hotend", {"id": variant_containers[0]["id"]})
  198. # Compatible is a special case, as it's added as a meta data entry (instead of an instance).
  199. compatible = hotend.getMetaDataEntry("compatible")
  200. if compatible is not None:
  201. builder.start("setting", {"key": "hardware compatible"})
  202. if compatible:
  203. builder.data("yes")
  204. else:
  205. builder.data("no")
  206. builder.end("setting")
  207. for instance in hotend.findInstances():
  208. if container.getInstance(instance.definition.key) and container.getProperty(instance.definition.key, "value") == instance.value:
  209. # If the settings match that of the machine profile, just skip since we inherit the machine profile.
  210. continue
  211. self._addSettingElement(builder, instance)
  212. builder.end("hotend")
  213. builder.end("machine")
  214. builder.end("settings")
  215. ## End Settings Block
  216. builder.end("fdmmaterial")
  217. root = builder.close()
  218. _indent(root)
  219. stream = io.BytesIO()
  220. tree = ET.ElementTree(root)
  221. # this makes sure that the XML header states encoding="utf-8"
  222. tree.write(stream, encoding="utf-8", xml_declaration=True)
  223. return stream.getvalue().decode('utf-8')
  224. # Recursively resolve loading inherited files
  225. def _resolveInheritance(self, file_name):
  226. xml = self._loadFile(file_name)
  227. inherits = xml.find("./um:inherits", self.__namespaces)
  228. if inherits is not None:
  229. inherited = self._resolveInheritance(inherits.text)
  230. xml = self._mergeXML(inherited, xml)
  231. return xml
  232. def _loadFile(self, file_name):
  233. path = Resources.getPath(CuraApplication.getInstance().ResourceTypes.MaterialInstanceContainer, file_name + ".xml.fdm_material")
  234. with open(path, encoding="utf-8") as f:
  235. contents = f.read()
  236. self._inherited_files.append(path)
  237. return ET.fromstring(contents)
  238. # The XML material profile can have specific settings for machines.
  239. # Some machines share profiles, so they are only created once.
  240. # This function duplicates those elements so that each machine tag only has one identifier.
  241. def _expandMachinesXML(self, element):
  242. settings_element = element.find("./um:settings", self.__namespaces)
  243. machines = settings_element.iterfind("./um:machine", self.__namespaces)
  244. machines_to_add = []
  245. machines_to_remove = []
  246. for machine in machines:
  247. identifiers = list(machine.iterfind("./um:machine_identifier", self.__namespaces))
  248. has_multiple_identifiers = len(identifiers) > 1
  249. if has_multiple_identifiers:
  250. # Multiple identifiers found. We need to create a new machine element and copy all it's settings there.
  251. for identifier in identifiers:
  252. new_machine = copy.deepcopy(machine)
  253. # Create list of identifiers that need to be removed from the copied element.
  254. other_identifiers = [self._createKey(other_identifier) for other_identifier in identifiers if other_identifier is not identifier]
  255. # As we can only remove by exact object reference, we need to look through the identifiers of copied machine.
  256. new_machine_identifiers = list(new_machine.iterfind("./um:machine_identifier", self.__namespaces))
  257. for new_machine_identifier in new_machine_identifiers:
  258. key = self._createKey(new_machine_identifier)
  259. # Key was in identifiers to remove, so this element needs to be purged
  260. if key in other_identifiers:
  261. new_machine.remove(new_machine_identifier)
  262. machines_to_add.append(new_machine)
  263. machines_to_remove.append(machine)
  264. else:
  265. pass # Machine only has one identifier. Nothing to do.
  266. # Remove & add all required machines.
  267. for machine_to_remove in machines_to_remove:
  268. settings_element.remove(machine_to_remove)
  269. for machine_to_add in machines_to_add:
  270. settings_element.append(machine_to_add)
  271. return element
  272. def _mergeXML(self, first, second):
  273. result = copy.deepcopy(first)
  274. self._combineElement(self._expandMachinesXML(result), self._expandMachinesXML(second))
  275. return result
  276. def _createKey(self, element):
  277. key = element.tag.split("}")[-1]
  278. if "key" in element.attrib:
  279. key += " key:" + element.attrib["key"]
  280. if "manufacturer" in element.attrib:
  281. key += " manufacturer:" + element.attrib["manufacturer"]
  282. if "product" in element.attrib:
  283. key += " product:" + element.attrib["product"]
  284. if key == "machine":
  285. for item in element:
  286. if "machine_identifier" in item.tag:
  287. key += " " + item.attrib["product"]
  288. return key
  289. # Recursively merges XML elements. Updates either the text or children if another element is found in first.
  290. # If it does not exist, copies it from second.
  291. def _combineElement(self, first, second):
  292. # Create a mapping from tag name to element.
  293. mapping = {}
  294. for element in first:
  295. key = self._createKey(element)
  296. mapping[key] = element
  297. for element in second:
  298. key = self._createKey(element)
  299. if len(element): # Check if element has children.
  300. try:
  301. if "setting" in element.tag and not "settings" in element.tag:
  302. # Setting can have points in it. In that case, delete all values and override them.
  303. for child in list(mapping[key]):
  304. mapping[key].remove(child)
  305. for child in element:
  306. mapping[key].append(child)
  307. else:
  308. self._combineElement(mapping[key], element) # Multiple elements, handle those.
  309. except KeyError:
  310. mapping[key] = element
  311. first.append(element)
  312. else:
  313. try:
  314. mapping[key].text = element.text
  315. except KeyError: # Not in the mapping, so simply add it
  316. mapping[key] = element
  317. first.append(element)
  318. def clearData(self):
  319. self._metadata = {
  320. "id": self.getId(),
  321. "name": ""
  322. }
  323. self._definition = None
  324. self._instances = {}
  325. self._read_only = False
  326. self._dirty = False
  327. self._path = ""
  328. @classmethod
  329. def getConfigurationTypeFromSerialized(cls, serialized: str) -> Optional[str]:
  330. return "materials"
  331. @classmethod
  332. def getVersionFromSerialized(cls, serialized: str) -> Optional[int]:
  333. data = ET.fromstring(serialized)
  334. version = 1
  335. # get setting version
  336. if "version" in data.attrib:
  337. setting_version = cls.xmlVersionToSettingVersion(data.attrib["version"])
  338. else:
  339. setting_version = cls.xmlVersionToSettingVersion("1.2")
  340. return version * 1000000 + setting_version
  341. ## Overridden from InstanceContainer
  342. def deserialize(self, serialized):
  343. containers_to_add = []
  344. # update the serialized data first
  345. from UM.Settings.Interfaces import ContainerInterface
  346. serialized = ContainerInterface.deserialize(self, serialized)
  347. try:
  348. data = ET.fromstring(serialized)
  349. except:
  350. Logger.logException("e", "An exception occurred while parsing the material profile")
  351. return
  352. # Reset previous metadata
  353. old_id = self.getId()
  354. self.clearData() # Ensure any previous data is gone.
  355. meta_data = {}
  356. meta_data["type"] = "material"
  357. meta_data["base_file"] = self.getId()
  358. meta_data["status"] = "unknown" # TODO: Add material verification
  359. meta_data["id"] = old_id
  360. meta_data["container_type"] = XmlMaterialProfile
  361. common_setting_values = {}
  362. inherits = data.find("./um:inherits", self.__namespaces)
  363. if inherits is not None:
  364. inherited = self._resolveInheritance(inherits.text)
  365. data = self._mergeXML(inherited, data)
  366. # set setting_version in metadata
  367. if "version" in data.attrib:
  368. meta_data["setting_version"] = self.xmlVersionToSettingVersion(data.attrib["version"])
  369. else:
  370. meta_data["setting_version"] = self.xmlVersionToSettingVersion("1.2") #1.2 and lower didn't have that version number there yet.
  371. metadata = data.iterfind("./um:metadata/*", self.__namespaces)
  372. for entry in metadata:
  373. tag_name = _tag_without_namespace(entry)
  374. if tag_name == "name":
  375. brand = entry.find("./um:brand", self.__namespaces)
  376. material = entry.find("./um:material", self.__namespaces)
  377. color = entry.find("./um:color", self.__namespaces)
  378. label = entry.find("./um:label", self.__namespaces)
  379. if label is not None:
  380. self.setName(label.text)
  381. else:
  382. self.setName(self._profile_name(material.text, color.text))
  383. meta_data["brand"] = brand.text
  384. meta_data["material"] = material.text
  385. meta_data["color_name"] = color.text
  386. continue
  387. # setting_version is derived from the "version" tag in the schema earlier, so don't set it here
  388. if tag_name == "setting_version":
  389. continue
  390. meta_data[tag_name] = entry.text
  391. if tag_name in self.__material_metadata_setting_map:
  392. common_setting_values[self.__material_metadata_setting_map[tag_name]] = entry.text
  393. if "description" not in meta_data:
  394. meta_data["description"] = ""
  395. if "adhesion_info" not in meta_data:
  396. meta_data["adhesion_info"] = ""
  397. property_values = {}
  398. properties = data.iterfind("./um:properties/*", self.__namespaces)
  399. for entry in properties:
  400. tag_name = _tag_without_namespace(entry)
  401. property_values[tag_name] = entry.text
  402. if tag_name in self.__material_properties_setting_map:
  403. common_setting_values[self.__material_properties_setting_map[tag_name]] = entry.text
  404. meta_data["approximate_diameter"] = str(round(float(property_values.get("diameter", 2.85)))) # In mm
  405. meta_data["properties"] = property_values
  406. meta_data["definition"] = "fdmprinter"
  407. self.setDefinition("fdmprinter")
  408. common_compatibility = True
  409. settings = data.iterfind("./um:settings/um:setting", self.__namespaces)
  410. for entry in settings:
  411. key = entry.get("key")
  412. if key in self.__material_settings_setting_map:
  413. common_setting_values[self.__material_settings_setting_map[key]] = entry.text
  414. elif key in self.__unmapped_settings:
  415. if key == "hardware compatible":
  416. common_compatibility = self._parseCompatibleValue(entry.text)
  417. else:
  418. Logger.log("d", "Unsupported material setting %s", key)
  419. self._cached_values = common_setting_values # from InstanceContainer ancestor
  420. meta_data["compatible"] = common_compatibility
  421. self.setMetaData(meta_data)
  422. self._dirty = False
  423. # Map machine human-readable names to IDs
  424. product_id_map = self.getProductIdMap()
  425. machines = data.iterfind("./um:settings/um:machine", self.__namespaces)
  426. for machine in machines:
  427. machine_compatibility = common_compatibility
  428. machine_setting_values = {}
  429. settings = machine.iterfind("./um:setting", self.__namespaces)
  430. for entry in settings:
  431. key = entry.get("key")
  432. if key in self.__material_settings_setting_map:
  433. machine_setting_values[self.__material_settings_setting_map[key]] = entry.text
  434. elif key in self.__unmapped_settings:
  435. if key == "hardware compatible":
  436. machine_compatibility = self._parseCompatibleValue(entry.text)
  437. else:
  438. Logger.log("d", "Unsupported material setting %s", key)
  439. cached_machine_setting_properties = common_setting_values.copy()
  440. cached_machine_setting_properties.update(machine_setting_values)
  441. identifiers = machine.iterfind("./um:machine_identifier", self.__namespaces)
  442. for identifier in identifiers:
  443. machine_id = product_id_map.get(identifier.get("product"), None)
  444. if machine_id is None:
  445. # Lets try again with some naive heuristics.
  446. machine_id = identifier.get("product").replace(" ", "").lower()
  447. definitions = ContainerRegistry.getInstance().findDefinitionContainers(id = machine_id)
  448. if not definitions:
  449. Logger.log("w", "No definition found for machine ID %s", machine_id)
  450. continue
  451. definition = definitions[0]
  452. machine_manufacturer = identifier.get("manufacturer", definition.getMetaDataEntry("manufacturer", "Unknown")) #If the XML material doesn't specify a manufacturer, use the one in the actual printer definition.
  453. if machine_compatibility:
  454. new_material_id = self.getId() + "_" + machine_id
  455. # The child or derived material container may already exist. This can happen when a material in a
  456. # project file and the a material in Cura have the same ID.
  457. # In the case if a derived material already exists, override that material container because if
  458. # the data in the parent material has been changed, the derived ones should be updated too.
  459. found_materials = ContainerRegistry.getInstance().findInstanceContainers(id = new_material_id)
  460. is_new_material = False
  461. if found_materials:
  462. new_material = found_materials[0]
  463. else:
  464. new_material = XmlMaterialProfile(new_material_id)
  465. is_new_material = True
  466. new_material.setMetaData(copy.deepcopy(self.getMetaData()))
  467. new_material.setDefinition(machine_id)
  468. # Don't use setMetadata, as that overrides it for all materials with same base file
  469. new_material.getMetaData()["compatible"] = machine_compatibility
  470. new_material.getMetaData()["machine_manufacturer"] = machine_manufacturer
  471. new_material.setCachedValues(cached_machine_setting_properties)
  472. new_material._dirty = False
  473. if is_new_material:
  474. containers_to_add.append(new_material)
  475. hotends = machine.iterfind("./um:hotend", self.__namespaces)
  476. for hotend in hotends:
  477. hotend_id = hotend.get("id")
  478. if hotend_id is None:
  479. continue
  480. variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = hotend_id)
  481. if not variant_containers:
  482. # It is not really properly defined what "ID" is so also search for variants by name.
  483. variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(definition = definition.getId(), name = hotend_id)
  484. if not variant_containers:
  485. continue
  486. hotend_compatibility = machine_compatibility
  487. hotend_setting_values = {}
  488. settings = hotend.iterfind("./um:setting", self.__namespaces)
  489. for entry in settings:
  490. key = entry.get("key")
  491. if key in self.__material_settings_setting_map:
  492. hotend_setting_values[self.__material_settings_setting_map[key]] = entry.text
  493. elif key in self.__unmapped_settings:
  494. if key == "hardware compatible":
  495. hotend_compatibility = self._parseCompatibleValue(entry.text)
  496. else:
  497. Logger.log("d", "Unsupported material setting %s", key)
  498. new_hotend_id = self.getId() + "_" + machine_id + "_" + hotend_id.replace(" ", "_")
  499. # Same as machine compatibility, keep the derived material containers consistent with the parent
  500. # material
  501. found_materials = ContainerRegistry.getInstance().findInstanceContainers(id = new_hotend_id)
  502. is_new_material = False
  503. if found_materials:
  504. new_hotend_material = found_materials[0]
  505. else:
  506. new_hotend_material = XmlMaterialProfile(new_hotend_id)
  507. is_new_material = True
  508. # Update the private directly, as we want to prevent the lookup that is done when using setName
  509. new_hotend_material.setName(self.getName())
  510. new_hotend_material.setMetaData(copy.deepcopy(self.getMetaData()))
  511. new_hotend_material.setDefinition(machine_id)
  512. new_hotend_material.addMetaDataEntry("variant", variant_containers[0]["id"])
  513. # Don't use setMetadata, as that overrides it for all materials with same base file
  514. new_hotend_material.getMetaData()["compatible"] = hotend_compatibility
  515. new_hotend_material.getMetaData()["machine_manufacturer"] = machine_manufacturer
  516. cached_hotend_setting_properties = cached_machine_setting_properties.copy()
  517. cached_hotend_setting_properties.update(hotend_setting_values)
  518. new_hotend_material.setCachedValues(cached_hotend_setting_properties)
  519. new_hotend_material._dirty = False
  520. if is_new_material:
  521. containers_to_add.append(new_hotend_material)
  522. for container_to_add in containers_to_add:
  523. ContainerRegistry.getInstance().addContainer(container_to_add)
  524. @classmethod
  525. def deserializeMetadata(cls, serialized: str, container_id: str) -> List[Dict[str, Any]]:
  526. result_metadata = [] #All the metadata that we found except the base (because the base is returned).
  527. #Update the serialized data to the latest version.
  528. serialized = cls._updateSerialized(serialized)
  529. base_metadata = {
  530. "type": "material",
  531. "status": "unknown", #TODO: Add material verification.
  532. "container_type": XmlMaterialProfile,
  533. "id": container_id
  534. }
  535. try:
  536. data = ET.fromstring(serialized)
  537. except:
  538. Logger.logException("e", "An exception occurred while parsing the material profile")
  539. return []
  540. #TODO: Implement the <inherits> tag. It's unused at the moment though.
  541. if "version" in data.attrib:
  542. base_metadata["setting_version"] = cls.xmlVersionToSettingVersion(data.attrib["version"])
  543. else:
  544. base_metadata["setting_version"] = cls.xmlVersionToSettingVersion("1.2") #1.2 and lower didn't have that version number there yet.
  545. for entry in data.iterfind("./um:metadata/*", cls.__namespaces):
  546. tag_name = _tag_without_namespace(entry)
  547. if tag_name == "name":
  548. brand = entry.find("./um:brand", cls.__namespaces)
  549. material = entry.find("./um:material", cls.__namespaces)
  550. color = entry.find("./um:color", cls.__namespaces)
  551. label = entry.find("./um:label", cls.__namespaces)
  552. if label is not None:
  553. base_metadata["name"] = label.text
  554. else:
  555. base_metadata["name"] = cls._profile_name(material.text, color.text)
  556. base_metadata["brand"] = brand.text
  557. base_metadata["material"] = material.text
  558. base_metadata["color_name"] = color.text
  559. continue
  560. #Setting_version is derived from the "version" tag in the schema earlier, so don't set it here.
  561. if tag_name == "setting_version":
  562. continue
  563. base_metadata[tag_name] = entry.text
  564. if "description" not in base_metadata:
  565. base_metadata["description"] = ""
  566. if "adhesion_info" not in base_metadata:
  567. base_metadata["adhesion_info"] = ""
  568. property_values = {}
  569. properties = data.iterfind("./um:properties/*", cls.__namespaces)
  570. for entry in properties:
  571. tag_name = _tag_without_namespace(entry)
  572. property_values[tag_name] = entry.text
  573. base_metadata["approximate_diameter"] = str(round(float(property_values.get("diameter", 2.85)))) # In mm
  574. base_metadata["properties"] = property_values
  575. base_metadata["definition"] = "fdmprinter"
  576. compatible_entries = data.iterfind("./um:settings/um:setting[@key='hardware compatible']", cls.__namespaces)
  577. try:
  578. common_compatibility = cls._parseCompatibleValue(next(compatible_entries).text)
  579. except StopIteration: #No 'hardware compatible' setting.
  580. common_compatibility = True
  581. base_metadata["compatible"] = common_compatibility
  582. result_metadata.append(base_metadata)
  583. # Map machine human-readable names to IDs
  584. product_id_map = cls.getProductIdMap()
  585. for machine in data.iterfind("./um:settings/um:machine", cls.__namespaces):
  586. machine_compatibility = common_compatibility
  587. for entry in machine.iterfind("./um:setting", cls.__namespaces):
  588. key = entry.get("key")
  589. if key == "hardware compatible":
  590. machine_compatibility = cls._parseCompatibleValue(entry.text)
  591. for identifier in machine.iterfind("./um:machine_identifier", cls.__namespaces):
  592. machine_id = product_id_map.get(identifier.get("product"), None)
  593. if machine_id is None:
  594. # Lets try again with some naive heuristics.
  595. machine_id = identifier.get("product").replace(" ", "").lower()
  596. definition_metadata = ContainerRegistry.getInstance().findDefinitionContainersMetadata(id = machine_id)
  597. if not definition_metadata:
  598. Logger.log("w", "No definition found for machine ID %s", machine_id)
  599. continue
  600. definition_metadata = definition_metadata[0]
  601. machine_manufacturer = identifier.get("manufacturer", definition_metadata.get("manufacturer", "Unknown")) #If the XML material doesn't specify a manufacturer, use the one in the actual printer definition.
  602. if machine_compatibility:
  603. new_material_id = container_id + "_" + machine_id
  604. # The child or derived material container may already exist. This can happen when a material in a
  605. # project file and the a material in Cura have the same ID.
  606. # In the case if a derived material already exists, override that material container because if
  607. # the data in the parent material has been changed, the derived ones should be updated too.
  608. found_materials = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = new_material_id)
  609. if found_materials:
  610. new_material_metadata = found_materials[0]
  611. else:
  612. new_material_metadata = {}
  613. new_material_metadata.update(base_metadata)
  614. new_material_metadata["id"] = new_material_id
  615. new_material_metadata["compatible"] = machine_compatibility
  616. new_material_metadata["machine_manufacturer"] = machine_manufacturer
  617. if len(found_materials) == 0: #This is a new material.
  618. result_metadata.append(new_material_metadata)
  619. for hotend in machine.iterfind("./um:hotend", cls.__namespaces):
  620. hotend_id = hotend.get("id")
  621. if hotend_id is None:
  622. continue
  623. variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = hotend_id)
  624. if not variant_containers:
  625. # It is not really properly defined what "ID" is so also search for variants by name.
  626. variant_containers = ContainerRegistry.getInstance().findInstanceContainersMetadata(definition = machine_id, name = hotend_id)
  627. if not variant_containers:
  628. continue
  629. hotend_compatibility = machine_compatibility
  630. for entry in hotend.iterfind("./um:setting", cls.__namespaces):
  631. key = entry.get("key")
  632. if key == "hardware compatible":
  633. hotend_compatibility = cls._parseCompatibleValue(entry.text)
  634. new_hotend_id = container_id + "_" + machine_id + "_" + hotend_id.replace(" ", "_")
  635. # Same as machine compatibility, keep the derived material containers consistent with the parent
  636. # material
  637. found_materials = ContainerRegistry.getInstance().findInstanceContainersMetadata(id = new_hotend_id)
  638. if found_materials:
  639. new_hotend_material_metadata = found_materials[0]
  640. else:
  641. new_hotend_material_metadata = {}
  642. new_hotend_material_metadata["name"] = base_metadata["name"]
  643. new_hotend_material_metadata.update(base_metadata)
  644. new_hotend_material_metadata["variant"] = variant_containers[0]["id"]
  645. new_hotend_material_metadata["compatible"] = hotend_compatibility
  646. new_hotend_material_metadata["machine_manufacturer"] = machine_manufacturer
  647. new_hotend_material_metadata["id"] = new_hotend_id
  648. if len(found_materials) == 0:
  649. result_metadata.append(new_hotend_material_metadata)
  650. return result_metadata
  651. def _addSettingElement(self, builder, instance):
  652. try:
  653. key = UM.Dictionary.findKey(self.__material_settings_setting_map, instance.definition.key)
  654. except ValueError:
  655. return
  656. builder.start("setting", { "key": key })
  657. builder.data(str(instance.value))
  658. builder.end("setting")
  659. @classmethod
  660. def _profile_name(cls, material_name, color_name):
  661. if color_name != "Generic":
  662. return "%s %s" % (color_name, material_name)
  663. else:
  664. return material_name
  665. ## Gets a mapping from product names in the XML files to their definition
  666. # IDs.
  667. #
  668. # This loads the mapping from a file.
  669. @classmethod
  670. def getProductIdMap(cls) -> Dict[str, str]:
  671. product_to_id_file = os.path.join(os.path.dirname(sys.modules[cls.__module__].__file__), "product_to_id.json")
  672. with open(product_to_id_file) as f:
  673. return json.load(f)
  674. ## Parse the value of the "material compatible" property.
  675. @classmethod
  676. def _parseCompatibleValue(cls, value: str):
  677. return value in {"yes", "unknown"}
  678. # Map XML file setting names to internal names
  679. __material_settings_setting_map = {
  680. "print temperature": "default_material_print_temperature",
  681. "heated bed temperature": "material_bed_temperature",
  682. "standby temperature": "material_standby_temperature",
  683. "processing temperature graph": "material_flow_temp_graph",
  684. "print cooling": "cool_fan_speed",
  685. "retraction amount": "retraction_amount",
  686. "retraction speed": "retraction_speed",
  687. "adhesion tendency": "material_adhesion_tendency",
  688. "surface energy": "material_surface_energy"
  689. }
  690. __unmapped_settings = [
  691. "hardware compatible"
  692. ]
  693. __material_properties_setting_map = {
  694. "diameter": "material_diameter"
  695. }
  696. __material_metadata_setting_map = {
  697. "GUID": "material_guid"
  698. }
  699. # Map of recognised namespaces with a proper prefix.
  700. __namespaces = {
  701. "um": "http://www.ultimaker.com/material"
  702. }
  703. ## Helper function for pretty-printing XML because ETree is stupid
  704. def _indent(elem, level = 0):
  705. i = "\n" + level * " "
  706. if len(elem):
  707. if not elem.text or not elem.text.strip():
  708. elem.text = i + " "
  709. if not elem.tail or not elem.tail.strip():
  710. elem.tail = i
  711. for elem in elem:
  712. _indent(elem, level + 1)
  713. if not elem.tail or not elem.tail.strip():
  714. elem.tail = i
  715. else:
  716. if level and (not elem.tail or not elem.tail.strip()):
  717. elem.tail = i
  718. # The namespace is prepended to the tag name but between {}.
  719. # We are only interested in the actual tag name, so discard everything
  720. # before the last }
  721. def _tag_without_namespace(element):
  722. return element.tag[element.tag.rfind("}") + 1:]