Use MaterialManager in SendMaterialJob

CURA-6035
This commit is contained in:
Lipu Fei 2018-12-20 10:45:38 +01:00
parent d93e19d530
commit 68372aff60

View file

@ -2,17 +2,14 @@
# Cura is released under the terms of the LGPLv3 or higher. # Cura is released under the terms of the LGPLv3 or higher.
import json import json
import os import os
import urllib.parse
from typing import Dict, TYPE_CHECKING, Set from typing import Dict, TYPE_CHECKING, Set
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
from UM.Application import Application
from UM.Job import Job from UM.Job import Job
from UM.Logger import Logger from UM.Logger import Logger
from UM.MimeTypeDatabase import MimeTypeDatabase
from UM.Resources import Resources
from cura.CuraApplication import CuraApplication from cura.CuraApplication import CuraApplication
# Absolute imports don't work in plugins # Absolute imports don't work in plugins
from .Models import ClusterMaterial, LocalMaterial from .Models import ClusterMaterial, LocalMaterial
@ -91,21 +88,22 @@ class SendMaterialJob(Job):
# #
# \param materials_to_send A set with id's of materials that must be sent. # \param materials_to_send A set with id's of materials that must be sent.
def _sendMaterials(self, materials_to_send: Set[str]) -> None: def _sendMaterials(self, materials_to_send: Set[str]) -> None:
file_paths = Resources.getAllResourcesOfType(CuraApplication.ResourceTypes.MaterialInstanceContainer) container_registry = CuraApplication.getInstance().getContainerRegistry()
material_manager = CuraApplication.getInstance().getMaterialManager()
material_group_dict = material_manager.getAllMaterialGroups()
# Find all local material files and send them if needed. for root_material_id in material_group_dict:
for file_path in file_paths: if root_material_id not in materials_to_send:
try: # If the material does not have to be sent we skip it.
mime_type = MimeTypeDatabase.getMimeTypeForFile(file_path) continue
except MimeTypeDatabase.MimeTypeNotFoundError:
file_path = container_registry.getContainerFilePathById(root_material_id)
if not file_path:
Logger.log("e", "Cannot get file path for material container [%s]", root_material_id)
continue continue
file_name = os.path.basename(file_path) file_name = os.path.basename(file_path)
material_id = urllib.parse.unquote_plus(mime_type.stripExtension(file_name)).lower() self._sendMaterialFile(file_path, file_name, root_material_id)
if material_id not in materials_to_send:
# If the material does not have to be sent we skip it.
continue
self._sendMaterialFile(file_path, file_name, material_id)
## Send a single material file to the printer. ## Send a single material file to the printer.
# #
@ -115,7 +113,6 @@ class SendMaterialJob(Job):
# \param file_name The name of the material file. # \param file_name The name of the material file.
# \param material_id The ID of the material in the file. # \param material_id The ID of the material in the file.
def _sendMaterialFile(self, file_path: str, file_name: str, material_id: str) -> None: def _sendMaterialFile(self, file_path: str, file_name: str, material_id: str) -> None:
parts = [] parts = []
# Add the material file. # Add the material file.
@ -170,28 +167,31 @@ class SendMaterialJob(Job):
# \return a dictionary of LocalMaterial objects by GUID # \return a dictionary of LocalMaterial objects by GUID
def _getLocalMaterials(self) -> Dict[str, LocalMaterial]: def _getLocalMaterials(self) -> Dict[str, LocalMaterial]:
result = {} # type: Dict[str, LocalMaterial] result = {} # type: Dict[str, LocalMaterial]
container_registry = Application.getInstance().getContainerRegistry() material_manager = CuraApplication.getInstance().getMaterialManager()
material_containers = container_registry.findContainersMetadata(type = "material")
material_group_dict = material_manager.getAllMaterialGroups()
# Find the latest version of all material containers in the registry. # Find the latest version of all material containers in the registry.
for material in material_containers: for root_material_id, material_group in material_group_dict.items():
material_metadata = material_group.root_material_node.getMetadata()
try: try:
# material version must be an int # material version must be an int
material["version"] = int(material["version"]) material_metadata["version"] = int(material_metadata["version"])
# Create a new local material # Create a new local material
local_material = LocalMaterial(**material) local_material = LocalMaterial(**material_metadata)
local_material.id = material["base_file"].lower() # Don't compare each profile, only base materials. local_material.id = root_material_id
if local_material.GUID not in result or \ if local_material.GUID not in result or \
local_material.version > result.get(local_material.GUID).version: local_material.version > result.get(local_material.GUID).version:
result[local_material.GUID] = local_material result[local_material.GUID] = local_material
except KeyError: except KeyError:
Logger.logException("w", "Local material {} has missing values.".format(material["id"])) Logger.logException("w", "Local material {} has missing values.".format(material_metadata["id"]))
except ValueError: except ValueError:
Logger.logException("w", "Local material {} has invalid values.".format(material["id"])) Logger.logException("w", "Local material {} has invalid values.".format(material_metadata["id"]))
except TypeError: except TypeError:
Logger.logException("w", "Local material {} has invalid values.".format(material["id"])) Logger.logException("w", "Local material {} has invalid values.".format(material_metadata["id"]))
return result return result