Write active material metadata to ufp when saving.

Add function to fetch package_id using only information from XmlMaterialProfile material container.
The only piece of information associating the material container and the package together is the file_name. To find the package that owns a material we have to search each of the material package paths.

It would be great to find a cleaner solution (preferable one that doesn't require invalidating the cached containers).

CURA-8610
This commit is contained in:
j.delarago 2022-05-30 17:29:59 +02:00
parent 596c24657d
commit 21d59e9349
3 changed files with 69 additions and 19 deletions

View file

@ -1,5 +1,6 @@
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
import os
from typing import Any, cast, Dict, List, Set, Tuple, TYPE_CHECKING, Optional
@ -51,6 +52,25 @@ class CuraPackageManager(PackageManager):
super().initialize()
def getMaterialFilePackageId(self, file_name: str, guid: str) -> str:
"""Get the id of the material package that contains file_name"""
for material_package in [f for f in os.scandir(self._installation_dirs_dict["materials"]) if f.is_dir()]:
package_id = material_package.name
for root, _, file_names in os.walk(material_package.path):
if file_name not in file_names:
#File with the name we are looking for is not in this directory
continue
with open(root + "/" + file_name, encoding="utf-8") as f:
# Make sure the file we found has the same guid as our material
# Parsing this xml would be better but the namespace is needed to search it.
if guid in f.read():
return package_id
continue
def getMachinesUsingPackage(self, package_id: str) -> Tuple[List[Tuple[GlobalStack, str, str]], List[Tuple[GlobalStack, str, str]]]:
"""Returns a list of where the package is used