ThreeMFWorkspaceReader.py 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from configparser import ConfigParser
  4. import zipfile
  5. import os
  6. import json
  7. import re
  8. from typing import cast, Dict, List, Optional, Tuple, Any, Set
  9. import xml.etree.ElementTree as ET
  10. from UM.Math.AxisAlignedBox import AxisAlignedBox
  11. from UM.Math.Vector import Vector
  12. from UM.Util import parseBool
  13. from UM.Workspace.WorkspaceReader import WorkspaceReader
  14. from UM.Application import Application
  15. from UM.Logger import Logger
  16. from UM.Message import Message
  17. from UM.i18n import i18nCatalog
  18. from UM.Settings.ContainerFormatError import ContainerFormatError
  19. from UM.Settings.ContainerStack import ContainerStack
  20. from UM.Settings.DefinitionContainer import DefinitionContainer
  21. from UM.Settings.InstanceContainer import InstanceContainer
  22. from UM.Settings.ContainerRegistry import ContainerRegistry
  23. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
  24. from UM.Job import Job
  25. from UM.Preferences import Preferences
  26. from cura.CuraPackageManager import CuraPackageManager
  27. from cura.Machines.ContainerTree import ContainerTree
  28. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  29. from cura.Settings.ExtruderManager import ExtruderManager
  30. from cura.Settings.ExtruderStack import ExtruderStack
  31. from cura.Settings.GlobalStack import GlobalStack
  32. from cura.Settings.IntentManager import IntentManager
  33. from cura.Settings.CuraContainerStack import _ContainerIndexes
  34. from cura.CuraApplication import CuraApplication
  35. from cura.Utils.Threading import call_on_qt_thread
  36. from PyQt6.QtCore import QCoreApplication
  37. from .WorkspaceDialog import WorkspaceDialog
  38. i18n_catalog = i18nCatalog("cura")
  39. _ignored_machine_network_metadata: Set[str] = {
  40. "um_cloud_cluster_id",
  41. "um_network_key",
  42. "um_linked_to_account",
  43. "host_guid",
  44. "removal_warning",
  45. "group_name",
  46. "group_size",
  47. "connection_type",
  48. "capabilities",
  49. "octoprint_api_key",
  50. "is_abstract_machine"
  51. }
  52. USER_SETTINGS_PATH = "Cura/user-settings.json"
  53. class ContainerInfo:
  54. def __init__(self, file_name: Optional[str], serialized: Optional[str], parser: Optional[ConfigParser]) -> None:
  55. self.file_name = file_name
  56. self.serialized = serialized
  57. self.parser = parser
  58. self.container = None
  59. self.definition_id = None
  60. class QualityChangesInfo:
  61. def __init__(self) -> None:
  62. self.name: Optional[str] = None
  63. self.global_info = None
  64. self.extruder_info_dict: Dict[str, ContainerInfo] = {}
  65. class MachineInfo:
  66. def __init__(self) -> None:
  67. self.container_id: Optional[str] = None
  68. self.name: Optional[str] = None
  69. self.definition_id: Optional[str] = None
  70. self.metadata_dict: Dict[str, str] = {}
  71. self.quality_type: Optional[str] = None
  72. self.intent_category: Optional[str] = None
  73. self.custom_quality_name: Optional[str] = None
  74. self.quality_changes_info: Optional[QualityChangesInfo] = None
  75. self.variant_info: Optional[ContainerInfo] = None
  76. self.definition_changes_info: Optional[ContainerInfo] = None
  77. self.user_changes_info: Optional[ContainerInfo] = None
  78. self.extruder_info_dict: Dict[str, str] = {}
  79. class ExtruderInfo:
  80. def __init__(self) -> None:
  81. self.position = None
  82. self.enabled = True
  83. self.variant_info: Optional[ContainerInfo] = None
  84. self.root_material_id: Optional[str] = None
  85. self.definition_changes_info: Optional[ContainerInfo] = None
  86. self.user_changes_info: Optional[ContainerInfo] = None
  87. self.intent_info: Optional[ContainerInfo] = None
  88. class ThreeMFWorkspaceReader(WorkspaceReader):
  89. """Base implementation for reading 3MF workspace files."""
  90. def __init__(self) -> None:
  91. super().__init__()
  92. self._supported_extensions = [".3mf"]
  93. self._dialog = WorkspaceDialog()
  94. self._3mf_mesh_reader = None
  95. self._is_ucp = None
  96. self._container_registry = ContainerRegistry.getInstance()
  97. # suffixes registered with the MimeTypes don't start with a dot '.'
  98. self._definition_container_suffix = "." + cast(MimeType, ContainerRegistry.getMimeTypeForContainer(DefinitionContainer)).preferredSuffix
  99. self._material_container_suffix = None # We have to wait until all other plugins are loaded before we can set it
  100. self._instance_container_suffix = "." + cast(MimeType, ContainerRegistry.getMimeTypeForContainer(InstanceContainer)).preferredSuffix
  101. self._container_stack_suffix = "." + cast(MimeType, ContainerRegistry.getMimeTypeForContainer(ContainerStack)).preferredSuffix
  102. self._extruder_stack_suffix = "." + cast(MimeType, ContainerRegistry.getMimeTypeForContainer(ExtruderStack)).preferredSuffix
  103. self._global_stack_suffix = "." + cast(MimeType, ContainerRegistry.getMimeTypeForContainer(GlobalStack)).preferredSuffix
  104. # Certain instance container types are ignored because we make the assumption that only we make those types
  105. # of containers. They are:
  106. # - quality
  107. # - variant
  108. self._ignored_instance_container_types = {"quality", "variant"}
  109. self._resolve_strategies: Dict[str, str] = {}
  110. self._id_mapping: Dict[str, str] = {}
  111. # In Cura 2.5 and 2.6, the empty profiles used to have those long names
  112. self._old_empty_profile_id_dict = {"empty_%s" % k: "empty" for k in ["material", "variant"]}
  113. self._old_new_materials: Dict[str, str] = {}
  114. self._machine_info = None
  115. self._user_settings: Dict[str, Dict[str, Any]] = {}
  116. def _clearState(self):
  117. self._id_mapping = {}
  118. self._old_new_materials = {}
  119. self._machine_info = None
  120. self._user_settings = {}
  121. def clearOpenAsUcp(self):
  122. self._is_ucp = None
  123. def getNewId(self, old_id: str):
  124. """Get a unique name based on the old_id. This is different from directly calling the registry in that it caches results.
  125. This has nothing to do with speed, but with getting consistent new naming for instances & objects.
  126. """
  127. if old_id not in self._id_mapping:
  128. self._id_mapping[old_id] = self._container_registry.uniqueName(old_id)
  129. return self._id_mapping[old_id]
  130. def _determineGlobalAndExtruderStackFiles(self, project_file_name: str, file_list: List[str]) -> Tuple[str, List[str]]:
  131. """Separates the given file list into a list of GlobalStack files and a list of ExtruderStack files.
  132. In old versions, extruder stack files have the same suffix as container stack files ".stack.cfg".
  133. """
  134. archive = zipfile.ZipFile(project_file_name, "r")
  135. global_stack_file_list = [name for name in file_list if name.endswith(self._global_stack_suffix)]
  136. extruder_stack_file_list = [name for name in file_list if name.endswith(self._extruder_stack_suffix)]
  137. # separate container stack files and extruder stack files
  138. files_to_determine = [name for name in file_list if name.endswith(self._container_stack_suffix)]
  139. for file_name in files_to_determine:
  140. # FIXME: HACK!
  141. # We need to know the type of the stack file, but we can only know it if we deserialize it.
  142. # The default ContainerStack.deserialize() will connect signals, which is not desired in this case.
  143. # Since we know that the stack files are INI files, so we directly use the ConfigParser to parse them.
  144. serialized = archive.open(file_name).read().decode("utf-8")
  145. stack_config = ConfigParser(interpolation = None)
  146. stack_config.read_string(serialized)
  147. # sanity check
  148. if not stack_config.has_option("metadata", "type"):
  149. Logger.log("e", "%s in %s doesn't seem to be valid stack file", file_name, project_file_name)
  150. continue
  151. stack_type = stack_config.get("metadata", "type")
  152. if stack_type == "extruder_train":
  153. extruder_stack_file_list.append(file_name)
  154. elif stack_type == "machine":
  155. global_stack_file_list.append(file_name)
  156. else:
  157. Logger.log("w", "Unknown container stack type '%s' from %s in %s",
  158. stack_type, file_name, project_file_name)
  159. if len(global_stack_file_list) > 1:
  160. Logger.log("e", "More than one global stack file found: [{file_list}]".format(file_list = global_stack_file_list))
  161. #But we can recover by just getting the first global stack file.
  162. if len(global_stack_file_list) == 0:
  163. Logger.log("e", "No global stack file found!")
  164. raise FileNotFoundError("No global stack file found!")
  165. return global_stack_file_list[0], extruder_stack_file_list
  166. def _isProjectUcp(self, file_name) -> bool:
  167. if self._is_ucp == None:
  168. archive = zipfile.ZipFile(file_name, "r")
  169. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  170. self._is_ucp =True if USER_SETTINGS_PATH in cura_file_names else False
  171. def getIsProjectUcp(self) -> bool:
  172. return self._is_ucp
  173. def preRead(self, file_name, show_dialog=True, *args, **kwargs):
  174. """Read some info so we can make decisions
  175. :param file_name:
  176. :param show_dialog: In case we use preRead() to check if a file is a valid project file,
  177. we don't want to show a dialog.
  178. """
  179. self._clearState()
  180. self._isProjectUcp(file_name)
  181. self._3mf_mesh_reader = Application.getInstance().getMeshFileHandler().getReaderForFile(file_name)
  182. if self._3mf_mesh_reader and self._3mf_mesh_reader.preRead(file_name) == WorkspaceReader.PreReadResult.accepted:
  183. pass
  184. else:
  185. Logger.log("w", "Could not find reader that was able to read the scene data for 3MF workspace")
  186. return WorkspaceReader.PreReadResult.failed
  187. self._machine_info = MachineInfo()
  188. machine_type = ""
  189. variant_type_name = i18n_catalog.i18nc("@label", "Nozzle")
  190. # Check if there are any conflicts, so we can ask the user.
  191. archive = zipfile.ZipFile(file_name, "r")
  192. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  193. resolve_strategy_keys = ["machine", "material", "quality_changes"]
  194. self._resolve_strategies = {k: None for k in resolve_strategy_keys}
  195. containers_found_dict = {k: False for k in resolve_strategy_keys}
  196. # Check whether the file is a UCP, which changes some import options
  197. is_ucp = USER_SETTINGS_PATH in cura_file_names
  198. #
  199. # Read definition containers
  200. #
  201. machine_definition_id = None
  202. updatable_machines = None if self._is_ucp else []
  203. machine_definition_container_count = 0
  204. extruder_definition_container_count = 0
  205. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  206. for definition_container_file in definition_container_files:
  207. container_id = self._stripFileToId(definition_container_file)
  208. definitions = self._container_registry.findDefinitionContainersMetadata(id = container_id)
  209. serialized = archive.open(definition_container_file).read().decode("utf-8")
  210. if not definitions:
  211. definition_container = DefinitionContainer.deserializeMetadata(serialized, container_id)[0]
  212. else:
  213. definition_container = definitions[0]
  214. definition_container_type = definition_container.get("type")
  215. if definition_container_type == "machine":
  216. machine_definition_id = container_id
  217. machine_definition_containers = self._container_registry.findDefinitionContainers(id = machine_definition_id)
  218. if machine_definition_containers and updatable_machines is not None:
  219. updatable_machines = [machine for machine in self._container_registry.findContainerStacks(type = "machine") if machine.definition == machine_definition_containers[0]]
  220. machine_type = definition_container["name"]
  221. variant_type_name = definition_container.get("variants_name", variant_type_name)
  222. machine_definition_container_count += 1
  223. elif definition_container_type == "extruder":
  224. extruder_definition_container_count += 1
  225. else:
  226. Logger.log("w", "Unknown definition container type %s for %s",
  227. definition_container_type, definition_container_file)
  228. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  229. Job.yieldThread()
  230. if machine_definition_container_count != 1:
  231. return WorkspaceReader.PreReadResult.failed # Not a workspace file but ordinary 3MF.
  232. material_ids_to_names_map = {}
  233. material_conflict = False
  234. xml_material_profile = self._getXmlProfileClass()
  235. reverse_material_id_dict = {}
  236. if self._material_container_suffix is None:
  237. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).preferredSuffix
  238. if xml_material_profile:
  239. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  240. for material_container_file in material_container_files:
  241. container_id = self._stripFileToId(material_container_file)
  242. serialized = archive.open(material_container_file).read().decode("utf-8")
  243. metadata_list = xml_material_profile.deserializeMetadata(serialized, container_id)
  244. reverse_map = {metadata["id"]: container_id for metadata in metadata_list}
  245. reverse_material_id_dict.update(reverse_map)
  246. material_ids_to_names_map[container_id] = self._getMaterialLabelFromSerialized(serialized)
  247. if self._container_registry.findContainersMetadata(id = container_id): #This material already exists.
  248. containers_found_dict["material"] = True
  249. if not self._container_registry.isReadOnly(container_id): # Only non readonly materials can be in conflict
  250. material_conflict = True
  251. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  252. Job.yieldThread()
  253. # Check if any quality_changes instance container is in conflict.
  254. instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)]
  255. quality_name = ""
  256. custom_quality_name = ""
  257. intent_name = ""
  258. intent_category = ""
  259. num_settings_overridden_by_quality_changes = 0 # How many settings are changed by the quality changes
  260. num_user_settings = 0
  261. quality_changes_conflict = False
  262. self._machine_info.quality_changes_info = QualityChangesInfo()
  263. quality_changes_info_list = []
  264. instance_container_info_dict = {} # id -> parser
  265. for instance_container_file_name in instance_container_files:
  266. container_id = self._stripFileToId(instance_container_file_name)
  267. serialized = archive.open(instance_container_file_name).read().decode("utf-8")
  268. # Qualities and variants don't have upgrades, so don't upgrade them
  269. parser = ConfigParser(interpolation = None, comment_prefixes = ())
  270. parser.read_string(serialized)
  271. container_type = parser["metadata"]["type"]
  272. if container_type not in ("quality", "variant"):
  273. serialized = InstanceContainer._updateSerialized(serialized, instance_container_file_name)
  274. parser = ConfigParser(interpolation = None, comment_prefixes = ())
  275. parser.read_string(serialized)
  276. container_info = ContainerInfo(instance_container_file_name, serialized, parser)
  277. instance_container_info_dict[container_id] = container_info
  278. container_type = parser["metadata"]["type"]
  279. if container_type == "quality_changes":
  280. quality_changes_info_list.append(container_info)
  281. if not parser.has_option("metadata", "position"):
  282. self._machine_info.quality_changes_info.name = parser["general"]["name"]
  283. self._machine_info.quality_changes_info.global_info = container_info
  284. else:
  285. position = parser["metadata"]["position"]
  286. self._machine_info.quality_changes_info.extruder_info_dict[position] = container_info
  287. custom_quality_name = parser["general"]["name"]
  288. values = parser["values"] if parser.has_section("values") else dict()
  289. num_settings_overridden_by_quality_changes += len(values)
  290. # Check if quality changes already exists.
  291. quality_changes = self._container_registry.findInstanceContainers(name = custom_quality_name,
  292. type = "quality_changes")
  293. if quality_changes:
  294. containers_found_dict["quality_changes"] = True
  295. # Check if there really is a conflict by comparing the values
  296. instance_container = InstanceContainer(container_id)
  297. try:
  298. instance_container.deserialize(serialized, file_name = instance_container_file_name)
  299. except ContainerFormatError:
  300. Logger.logException("e", "Failed to deserialize InstanceContainer %s from project file %s",
  301. instance_container_file_name, file_name)
  302. return ThreeMFWorkspaceReader.PreReadResult.failed
  303. if quality_changes[0] != instance_container:
  304. quality_changes_conflict = True
  305. elif container_type == "quality":
  306. if not quality_name:
  307. quality_name = parser["general"]["name"]
  308. elif container_type == "intent":
  309. if not intent_name:
  310. intent_name = parser["general"]["name"]
  311. intent_category = parser["metadata"]["intent_category"]
  312. elif container_type == "user":
  313. num_user_settings += len(parser["values"])
  314. elif container_type in self._ignored_instance_container_types:
  315. # Ignore certain instance container types
  316. Logger.log("w", "Ignoring instance container [%s] with type [%s]", container_id, container_type)
  317. continue
  318. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  319. Job.yieldThread()
  320. if self._machine_info.quality_changes_info.global_info is None:
  321. self._machine_info.quality_changes_info = None
  322. # Load ContainerStack files and ExtruderStack files
  323. try:
  324. global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles(
  325. file_name, cura_file_names)
  326. except FileNotFoundError:
  327. return WorkspaceReader.PreReadResult.failed
  328. machine_conflict = False
  329. # Because there can be cases as follows:
  330. # - the global stack exists but some/all of the extruder stacks DON'T exist
  331. # - the global stack DOESN'T exist but some/all of the extruder stacks exist
  332. # To simplify this, only check if the global stack exists or not
  333. global_stack_id = self._stripFileToId(global_stack_file)
  334. serialized = archive.open(global_stack_file).read().decode("utf-8")
  335. serialized = GlobalStack._updateSerialized(serialized, global_stack_file)
  336. machine_name = self._getMachineNameFromSerializedStack(serialized)
  337. self._machine_info.metadata_dict = self._getMetaDataDictFromSerializedStack(serialized)
  338. # Check if the definition has been changed (this usually happens due to an upgrade)
  339. id_list = self._getContainerIdListFromSerialized(serialized)
  340. if id_list[7] != machine_definition_id:
  341. machine_definition_id = id_list[7]
  342. stacks = self._container_registry.findContainerStacks(name = machine_name, type = "machine")
  343. existing_global_stack = None
  344. global_stack = None
  345. if stacks:
  346. global_stack = stacks[0]
  347. existing_global_stack = global_stack
  348. containers_found_dict["machine"] = True
  349. # Check if there are any changes at all in any of the container stacks.
  350. for index, container_id in enumerate(id_list):
  351. # take into account the old empty container IDs
  352. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  353. if global_stack.getContainer(index).getId() != container_id:
  354. machine_conflict = True
  355. break
  356. if updatable_machines and not containers_found_dict["machine"]:
  357. containers_found_dict["machine"] = True
  358. # Get quality type
  359. parser = ConfigParser(interpolation = None)
  360. parser.read_string(serialized)
  361. quality_container_id = parser["containers"][str(_ContainerIndexes.Quality)]
  362. quality_type = "empty_quality"
  363. if quality_container_id not in ("empty", "empty_quality"):
  364. if quality_container_id in instance_container_info_dict:
  365. quality_type = instance_container_info_dict[quality_container_id].parser["metadata"]["quality_type"]
  366. else: # If a version upgrade changed the quality profile in the stack, we'll need to look for it in the built-in profiles instead of the workspace.
  367. quality_matches = ContainerRegistry.getInstance().findContainersMetadata(id = quality_container_id)
  368. if quality_matches: # If there's no profile with this ID, leave it empty_quality.
  369. quality_type = quality_matches[0]["quality_type"]
  370. # Get machine info
  371. serialized = archive.open(global_stack_file).read().decode("utf-8")
  372. serialized = GlobalStack._updateSerialized(serialized, global_stack_file)
  373. parser = ConfigParser(interpolation = None)
  374. parser.read_string(serialized)
  375. definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)]
  376. if definition_changes_id not in ("empty", "empty_definition_changes"):
  377. self._machine_info.definition_changes_info = instance_container_info_dict[definition_changes_id]
  378. user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)]
  379. if user_changes_id not in ("empty", "empty_user_changes"):
  380. self._machine_info.user_changes_info = instance_container_info_dict[user_changes_id]
  381. # Also check variant and material in case it doesn't have extruder stacks
  382. if not extruder_stack_files:
  383. position = "0"
  384. extruder_info = ExtruderInfo()
  385. extruder_info.position = position
  386. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  387. material_id = parser["containers"][str(_ContainerIndexes.Material)]
  388. if variant_id not in ("empty", "empty_variant"):
  389. extruder_info.variant_info = instance_container_info_dict[variant_id]
  390. if material_id not in ("empty", "empty_material"):
  391. root_material_id = reverse_material_id_dict[material_id]
  392. extruder_info.root_material_id = root_material_id
  393. self._machine_info.extruder_info_dict[position] = extruder_info
  394. else:
  395. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  396. if variant_id not in ("empty", "empty_variant"):
  397. self._machine_info.variant_info = instance_container_info_dict[variant_id]
  398. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  399. Job.yieldThread()
  400. materials_in_extruders_dict = {} # Which material is in which extruder
  401. # If the global stack is found, we check if there are conflicts in the extruder stacks
  402. for extruder_stack_file in extruder_stack_files:
  403. serialized = archive.open(extruder_stack_file).read().decode("utf-8")
  404. not_upgraded_parser = ConfigParser(interpolation=None)
  405. not_upgraded_parser.read_string(serialized)
  406. serialized = ExtruderStack._updateSerialized(serialized, extruder_stack_file)
  407. parser = ConfigParser(interpolation=None)
  408. parser.read_string(serialized)
  409. # The check should be done for the extruder stack that's associated with the existing global stack,
  410. # and those extruder stacks may have different IDs.
  411. # So we check according to the positions
  412. position = parser["metadata"]["position"]
  413. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  414. material_id = parser["containers"][str(_ContainerIndexes.Material)]
  415. extruder_info = ExtruderInfo()
  416. extruder_info.position = position
  417. if parser.has_option("metadata", "enabled"):
  418. extruder_info.enabled = parser["metadata"]["enabled"]
  419. if variant_id not in ("empty", "empty_variant"):
  420. if variant_id in instance_container_info_dict:
  421. extruder_info.variant_info = instance_container_info_dict[variant_id]
  422. if material_id not in ("empty", "empty_material"):
  423. root_material_id = reverse_material_id_dict[material_id]
  424. extruder_info.root_material_id = root_material_id
  425. materials_in_extruders_dict[position] = material_ids_to_names_map[reverse_material_id_dict[material_id]]
  426. definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)]
  427. if definition_changes_id not in ("empty", "empty_definition_changes"):
  428. extruder_info.definition_changes_info = instance_container_info_dict[definition_changes_id]
  429. user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)]
  430. if user_changes_id not in ("empty", "empty_user_changes"):
  431. extruder_info.user_changes_info = instance_container_info_dict[user_changes_id]
  432. self._machine_info.extruder_info_dict[position] = extruder_info
  433. intent_container_id = parser["containers"][str(_ContainerIndexes.Intent)]
  434. intent_id = parser["containers"][str(_ContainerIndexes.Intent)]
  435. if intent_id not in ("empty", "empty_intent"):
  436. if intent_container_id in instance_container_info_dict:
  437. extruder_info.intent_info = instance_container_info_dict[intent_id]
  438. else:
  439. # It can happen that an intent has been renamed. In that case, we should still use the old
  440. # name, since we used that to generate the instance_container_info_dict keys.
  441. extruder_info.intent_info = instance_container_info_dict[not_upgraded_parser["containers"][str(_ContainerIndexes.Intent)]]
  442. if not machine_conflict and containers_found_dict["machine"] and global_stack:
  443. if int(position) >= len(global_stack.extruderList):
  444. continue
  445. existing_extruder_stack = global_stack.extruderList[int(position)]
  446. # Check if there are any changes at all in any of the container stacks.
  447. id_list = self._getContainerIdListFromSerialized(serialized)
  448. for index, container_id in enumerate(id_list):
  449. # Take into account the old empty container IDs
  450. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  451. if existing_extruder_stack.getContainer(index).getId() != container_id:
  452. machine_conflict = True
  453. break
  454. # Now we know which material is in which extruder. Let's use that to sort the material_labels according to
  455. # their extruder position
  456. material_labels = [material_name for pos, material_name in sorted(materials_in_extruders_dict.items())]
  457. machine_extruder_count = self._getMachineExtruderCount()
  458. if machine_extruder_count:
  459. material_labels = material_labels[:machine_extruder_count]
  460. num_visible_settings = 0
  461. try:
  462. temp_preferences = Preferences()
  463. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  464. temp_preferences.deserialize(serialized)
  465. visible_settings_string = temp_preferences.getValue("general/visible_settings")
  466. has_visible_settings_string = visible_settings_string is not None
  467. if visible_settings_string is not None:
  468. num_visible_settings = len(visible_settings_string.split(";"))
  469. active_mode = temp_preferences.getValue("cura/active_mode")
  470. if not active_mode:
  471. active_mode = Application.getInstance().getPreferences().getValue("cura/active_mode")
  472. except KeyError:
  473. # If there is no preferences file, it's not a workspace, so notify user of failure.
  474. Logger.log("w", "File %s is not a valid workspace.", file_name)
  475. return WorkspaceReader.PreReadResult.failed
  476. # Check if the machine definition exists. If not, indicate failure because we do not import definition files.
  477. def_results = self._container_registry.findDefinitionContainersMetadata(id = machine_definition_id)
  478. if not def_results:
  479. message = Message(i18n_catalog.i18nc("@info:status Don't translate the XML tags <filename> or <message>!",
  480. "Project file <filename>{0}</filename> contains an unknown machine type"
  481. " <message>{1}</message>. Cannot import the machine."
  482. " Models will be imported instead.", file_name, machine_definition_id),
  483. title = i18n_catalog.i18nc("@info:title", "Open Project File"),
  484. message_type = Message.MessageType.WARNING)
  485. message.show()
  486. Logger.log("i", "Could unknown machine definition %s in project file %s, cannot import it.",
  487. self._machine_info.definition_id, file_name)
  488. return WorkspaceReader.PreReadResult.failed
  489. # In case we use preRead() to check if a file is a valid project file, we don't want to show a dialog.
  490. if not show_dialog:
  491. return WorkspaceReader.PreReadResult.accepted
  492. # prepare data for the dialog
  493. num_extruders = extruder_definition_container_count
  494. if num_extruders == 0:
  495. num_extruders = 1 # No extruder stacks found, which means there is one extruder
  496. extruders = num_extruders * [""]
  497. quality_name = custom_quality_name if custom_quality_name else quality_name
  498. self._machine_info.container_id = global_stack_id
  499. self._machine_info.name = machine_name
  500. self._machine_info.definition_id = machine_definition_id
  501. self._machine_info.quality_type = quality_type
  502. self._machine_info.custom_quality_name = quality_name
  503. self._machine_info.intent_category = intent_category
  504. is_printer_group = False
  505. if machine_conflict:
  506. group_name = existing_global_stack.getMetaDataEntry("group_name")
  507. if group_name is not None:
  508. is_printer_group = True
  509. machine_name = group_name
  510. # Getting missing required package ids
  511. package_metadata = self._parse_packages_metadata(archive)
  512. missing_package_metadata = self._filter_missing_package_metadata(package_metadata)
  513. # Load the user specifically exported settings
  514. self._dialog.exportedSettingModel.clear()
  515. self._dialog.setCurrentMachineName("")
  516. if self._is_ucp:
  517. try:
  518. self._user_settings = json.loads(archive.open("Cura/user-settings.json").read().decode("utf-8"))
  519. any_extruder_stack = ExtruderManager.getInstance().getExtruderStack(0)
  520. actual_global_stack = CuraApplication.getInstance().getGlobalContainerStack()
  521. self._dialog.setCurrentMachineName(actual_global_stack.id)
  522. for stack_name, settings in self._user_settings.items():
  523. if stack_name == 'global':
  524. self._dialog.exportedSettingModel.addSettingsFromStack(actual_global_stack, i18n_catalog.i18nc("@label", "Global"), settings)
  525. else:
  526. extruder_match = re.fullmatch('extruder_([0-9]+)', stack_name)
  527. if extruder_match is not None:
  528. extruder_nr = int(extruder_match.group(1))
  529. self._dialog.exportedSettingModel.addSettingsFromStack(any_extruder_stack,
  530. i18n_catalog.i18nc("@label",
  531. "Extruder {0}", extruder_nr + 1),
  532. settings)
  533. except KeyError as e:
  534. # If there is no user settings file, it's not a UCP, so notify user of failure.
  535. Logger.log("w", "File %s is not a valid UCP.", file_name)
  536. message = Message(
  537. i18n_catalog.i18nc("@info:error Don't translate the XML tags <filename> or <message>!",
  538. "Project file <filename>{0}</filename> is corrupt: <message>{1}</message>.",
  539. file_name, str(e)),
  540. title=i18n_catalog.i18nc("@info:title", "Can't Open Project File"),
  541. message_type=Message.MessageType.ERROR)
  542. message.show()
  543. return WorkspaceReader.PreReadResult.failed
  544. # Show the dialog, informing the user what is about to happen.
  545. self._dialog.setMachineConflict(machine_conflict)
  546. self._dialog.setIsPrinterGroup(is_printer_group)
  547. self._dialog.setQualityChangesConflict(quality_changes_conflict)
  548. self._dialog.setMaterialConflict(material_conflict)
  549. self._dialog.setHasVisibleSettingsField(has_visible_settings_string)
  550. self._dialog.setNumVisibleSettings(num_visible_settings)
  551. self._dialog.setQualityName(quality_name)
  552. self._dialog.setQualityType(quality_type)
  553. self._dialog.setIntentName(intent_category)
  554. self._dialog.setNumSettingsOverriddenByQualityChanges(num_settings_overridden_by_quality_changes)
  555. self._dialog.setNumUserSettings(num_user_settings)
  556. self._dialog.setActiveMode(active_mode)
  557. self._dialog.setUpdatableMachines(updatable_machines)
  558. self._dialog.setMaterialLabels(material_labels)
  559. self._dialog.setMachineType(machine_type)
  560. self._dialog.setExtruders(extruders)
  561. self._dialog.setVariantType(variant_type_name)
  562. self._dialog.setHasObjectsOnPlate(Application.getInstance().platformActivity)
  563. self._dialog.setMissingPackagesMetadata(missing_package_metadata)
  564. self._dialog.setAllowCreatemachine(not self._is_ucp)
  565. self._dialog.setIsUcp(self._is_ucp)
  566. self._dialog.show()
  567. # Choosing the initially selected printer in MachineSelector
  568. is_networked_machine = False
  569. is_abstract_machine = False
  570. if global_stack and isinstance(global_stack, GlobalStack) and not self._is_ucp:
  571. # The machine included in the project file exists locally already, no need to change selected printers.
  572. is_networked_machine = global_stack.hasNetworkedConnection()
  573. is_abstract_machine = parseBool(existing_global_stack.getMetaDataEntry("is_abstract_machine", False))
  574. self._dialog.setMachineToOverride(global_stack.getId())
  575. self._dialog.setResolveStrategy("machine", "override")
  576. elif self._dialog.updatableMachinesModel.count > 0:
  577. # The machine included in the project file does not exist. There is another machine of the same type.
  578. # This will always default to an abstract machine first.
  579. machine = self._dialog.updatableMachinesModel.getItem(self._dialog.currentMachinePositionIndex)
  580. machine_name = machine["name"]
  581. is_networked_machine = machine["isNetworked"]
  582. is_abstract_machine = machine["isAbstractMachine"]
  583. self._dialog.setMachineToOverride(machine["id"])
  584. self._dialog.setResolveStrategy("machine", "override")
  585. else:
  586. # The machine included in the project file does not exist. There are no other printers of the same type. Default to "Create New".
  587. machine_name = i18n_catalog.i18nc("@button", "Create new")
  588. is_networked_machine = False
  589. is_abstract_machine = False
  590. self._dialog.setMachineToOverride(None)
  591. self._dialog.setResolveStrategy("machine", "new")
  592. self._dialog.setIsNetworkedMachine(is_networked_machine)
  593. self._dialog.setIsAbstractMachine(is_abstract_machine)
  594. self._dialog.setMachineName(machine_name)
  595. self._dialog.updateCompatibleMachine()
  596. # Block until the dialog is closed.
  597. self._dialog.waitForClose()
  598. if self._dialog.getResult() == {}:
  599. return WorkspaceReader.PreReadResult.cancelled
  600. self._resolve_strategies = self._dialog.getResult()
  601. #
  602. # There can be 3 resolve strategies coming from the dialog:
  603. # - new: create a new container
  604. # - override: override the existing container
  605. # - None: There is no conflict, which means containers with the same IDs may or may not be there already.
  606. # If there is an existing container, there is no conflict between them, and default to "override"
  607. # If there is no existing container, default to "new"
  608. #
  609. # Default values
  610. for key, strategy in self._resolve_strategies.items():
  611. if key not in containers_found_dict or strategy is not None:
  612. continue
  613. self._resolve_strategies[key] = "override" if containers_found_dict[key] else "new"
  614. return WorkspaceReader.PreReadResult.accepted
  615. @call_on_qt_thread
  616. def read(self, file_name):
  617. """Read the project file
  618. Add all the definitions / materials / quality changes that do not exist yet. Then it loads
  619. all the stacks into the container registry. In some cases it will reuse the container for the global stack.
  620. It handles old style project files containing .stack.cfg as well as new style project files
  621. containing global.cfg / extruder.cfg
  622. :param file_name:
  623. """
  624. application = CuraApplication.getInstance()
  625. try:
  626. archive = zipfile.ZipFile(file_name, "r")
  627. except EnvironmentError as e:
  628. message = Message(i18n_catalog.i18nc("@info:error Don't translate the XML tags <filename> or <message>!",
  629. "Project file <filename>{0}</filename> is suddenly inaccessible: <message>{1}</message>.", file_name, str(e)),
  630. title = i18n_catalog.i18nc("@info:title", "Can't Open Project File"),
  631. message_type = Message.MessageType.ERROR)
  632. message.show()
  633. self.setWorkspaceName("")
  634. return [], {}
  635. except zipfile.BadZipFile as e:
  636. message = Message(i18n_catalog.i18nc("@info:error Don't translate the XML tags <filename> or <message>!",
  637. "Project file <filename>{0}</filename> is corrupt: <message>{1}</message>.", file_name, str(e)),
  638. title = i18n_catalog.i18nc("@info:title", "Can't Open Project File"),
  639. message_type = Message.MessageType.ERROR)
  640. message.show()
  641. self.setWorkspaceName("")
  642. return [], {}
  643. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  644. # Create a shadow copy of the preferences (We don't want all of the preferences, but we do want to re-use its
  645. # parsing code.
  646. temp_preferences = Preferences()
  647. try:
  648. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  649. except KeyError as e:
  650. # If there is no preferences file, it's not a workspace, so notify user of failure.
  651. Logger.log("w", "File %s is not a valid workspace.", file_name)
  652. message = Message(i18n_catalog.i18nc("@info:error Don't translate the XML tags <filename> or <message>!",
  653. "Project file <filename>{0}</filename> is corrupt: <message>{1}</message>.",
  654. file_name, str(e)),
  655. title=i18n_catalog.i18nc("@info:title", "Can't Open Project File"),
  656. message_type=Message.MessageType.ERROR)
  657. message.show()
  658. self.setWorkspaceName("")
  659. return [], {}
  660. temp_preferences.deserialize(serialized)
  661. # Copy a number of settings from the temp preferences to the global
  662. global_preferences = application.getInstance().getPreferences()
  663. visible_settings = temp_preferences.getValue("general/visible_settings")
  664. if visible_settings is None:
  665. Logger.log("w", "Workspace did not contain visible settings. Leaving visibility unchanged")
  666. else:
  667. global_preferences.setValue("general/visible_settings", visible_settings)
  668. global_preferences.setValue("cura/active_setting_visibility_preset", "custom")
  669. categories_expanded = temp_preferences.getValue("cura/categories_expanded")
  670. if categories_expanded is None:
  671. Logger.log("w", "Workspace did not contain expanded categories. Leaving them unchanged")
  672. else:
  673. global_preferences.setValue("cura/categories_expanded", categories_expanded)
  674. application.expandedCategoriesChanged.emit() # Notify the GUI of the change
  675. # If there are no machines of the same type, create a new machine.
  676. if self._resolve_strategies["machine"] != "override" or self._dialog.updatableMachinesModel.count == 0:
  677. # We need to create a new machine
  678. machine_name = self._container_registry.uniqueName(self._machine_info.name)
  679. # Printers with modifiable number of extruders (such as CFFF) will specify a machine_extruder_count in their
  680. # quality_changes file. If that's the case, take the extruder count into account when creating the machine
  681. # or else the extruderList will return only the first extruder, leading to missing non-global settings in
  682. # the other extruders.
  683. machine_extruder_count: Optional[int] = self._getMachineExtruderCount()
  684. global_stack = CuraStackBuilder.createMachine(machine_name, self._machine_info.definition_id, machine_extruder_count)
  685. if global_stack: # Only switch if creating the machine was successful.
  686. extruder_stack_dict = {str(position): extruder for position, extruder in enumerate(global_stack.extruderList)}
  687. self._container_registry.addContainer(global_stack)
  688. else:
  689. # Find the machine which will be overridden
  690. global_stacks = self._container_registry.findContainerStacks(id = self._dialog.getMachineToOverride(), type = "machine")
  691. if not global_stacks:
  692. message = Message(i18n_catalog.i18nc("@info:error Don't translate the XML tag <filename>!",
  693. "Project file <filename>{0}</filename> is made using profiles that are unknown to this version of UltiMaker Cura.", file_name),
  694. message_type = Message.MessageType.ERROR)
  695. message.show()
  696. self.setWorkspaceName("")
  697. return [], {}
  698. global_stack = global_stacks[0]
  699. extruder_stacks = self._container_registry.findContainerStacks(machine = global_stack.getId(),
  700. type = "extruder_train")
  701. extruder_stack_dict = {stack.getMetaDataEntry("position"): stack for stack in extruder_stacks}
  702. # Make sure that those extruders have the global stack as the next stack or later some value evaluation
  703. # will fail.
  704. for stack in extruder_stacks:
  705. stack.setNextStack(global_stack, connect_signals = False)
  706. if not self._is_ucp:
  707. Logger.log("d", "Workspace loading is checking definitions...")
  708. # Get all the definition files & check if they exist. If not, add them.
  709. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  710. for definition_container_file in definition_container_files:
  711. container_id = self._stripFileToId(definition_container_file)
  712. definitions = self._container_registry.findDefinitionContainersMetadata(id = container_id)
  713. if not definitions:
  714. definition_container = DefinitionContainer(container_id)
  715. try:
  716. definition_container.deserialize(archive.open(definition_container_file).read().decode("utf-8"),
  717. file_name = definition_container_file)
  718. except ContainerFormatError:
  719. # We cannot just skip the definition file because everything else later will just break if the
  720. # machine definition cannot be found.
  721. Logger.logException("e", "Failed to deserialize definition file %s in project file %s",
  722. definition_container_file, file_name)
  723. definition_container = self._container_registry.findDefinitionContainers(id = "fdmprinter")[0] #Fall back to defaults.
  724. self._container_registry.addContainer(definition_container)
  725. Job.yieldThread()
  726. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  727. Logger.log("d", "Workspace loading is checking materials...")
  728. # Get all the material files and check if they exist. If not, add them.
  729. xml_material_profile = self._getXmlProfileClass()
  730. if self._material_container_suffix is None:
  731. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).suffixes[0]
  732. if xml_material_profile:
  733. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  734. for material_container_file in material_container_files:
  735. to_deserialize_material = False
  736. container_id = self._stripFileToId(material_container_file)
  737. need_new_name = False
  738. materials = self._container_registry.findInstanceContainers(id = container_id)
  739. if not materials:
  740. # No material found, deserialize this material later and add it
  741. to_deserialize_material = True
  742. else:
  743. material_container = materials[0]
  744. old_material_root_id = material_container.getMetaDataEntry("base_file")
  745. if old_material_root_id is not None and not self._container_registry.isReadOnly(old_material_root_id): # Only create new materials if they are not read only.
  746. to_deserialize_material = True
  747. if self._resolve_strategies["material"] == "override":
  748. # Remove the old materials and then deserialize the one from the project
  749. root_material_id = material_container.getMetaDataEntry("base_file")
  750. application.getContainerRegistry().removeContainer(root_material_id)
  751. elif self._resolve_strategies["material"] == "new":
  752. # Note that we *must* deserialize it with a new ID, as multiple containers will be
  753. # auto created & added.
  754. container_id = self.getNewId(container_id)
  755. self._old_new_materials[old_material_root_id] = container_id
  756. need_new_name = True
  757. if to_deserialize_material:
  758. material_container = xml_material_profile(container_id)
  759. try:
  760. material_container.deserialize(archive.open(material_container_file).read().decode("utf-8"),
  761. file_name = container_id + "." + self._material_container_suffix)
  762. except ContainerFormatError:
  763. Logger.logException("e", "Failed to deserialize material file %s in project file %s",
  764. material_container_file, file_name)
  765. continue
  766. if need_new_name:
  767. new_name = ContainerRegistry.getInstance().uniqueName(material_container.getName())
  768. material_container.setName(new_name)
  769. material_container.setDirty(True)
  770. self._container_registry.addContainer(material_container)
  771. Job.yieldThread()
  772. QCoreApplication.processEvents() # Ensure that the GUI does not freeze.
  773. if global_stack:
  774. if not self._is_ucp:
  775. # Handle quality changes if any
  776. self._processQualityChanges(global_stack)
  777. # Prepare the machine
  778. self._applyChangesToMachine(global_stack, extruder_stack_dict)
  779. Logger.log("d", "Workspace loading is notifying rest of the code of changes...")
  780. # Actually change the active machine.
  781. #
  782. # This is scheduled for later is because it depends on the Variant/Material/Qualitiy Managers to have the latest
  783. # data, but those managers will only update upon a container/container metadata changed signal. Because this
  784. # function is running on the main thread (Qt thread), although those "changed" signals have been emitted, but
  785. # they won't take effect until this function is done.
  786. # To solve this, we schedule _updateActiveMachine() for later so it will have the latest data.
  787. self._updateActiveMachine(global_stack)
  788. if self._is_ucp:
  789. # Now we have switched, apply the user settings
  790. self._applyUserSettings(global_stack, extruder_stack_dict, self._user_settings)
  791. # Load all the nodes / mesh data of the workspace
  792. nodes = self._3mf_mesh_reader.read(file_name)
  793. if nodes is None:
  794. nodes = []
  795. if self._is_ucp:
  796. # We might be on a different printer than the one this project was made on.
  797. # The offset to the printers' center isn't saved; instead, try to just fit everything on the buildplate.
  798. full_extents = None
  799. for node in nodes:
  800. extents = node.getMeshData().getExtents() if node.getMeshData() else None
  801. if extents is not None:
  802. pos = node.getPosition()
  803. node_box = AxisAlignedBox(extents.minimum + pos, extents.maximum + pos)
  804. if full_extents is None:
  805. full_extents = node_box
  806. else:
  807. full_extents = full_extents + node_box
  808. if full_extents and full_extents.isValid():
  809. for node in nodes:
  810. pos = node.getPosition()
  811. node.setPosition(Vector(pos.x - full_extents.center.x, pos.y, pos.z - full_extents.center.z))
  812. base_file_name = os.path.basename(file_name)
  813. self.setWorkspaceName(base_file_name)
  814. self._is_ucp = None
  815. return nodes, self._loadMetadata(file_name)
  816. @staticmethod
  817. def _loadMetadata(file_name: str) -> Dict[str, Dict[str, Any]]:
  818. result: Dict[str, Dict[str, Any]] = dict()
  819. try:
  820. archive = zipfile.ZipFile(file_name, "r")
  821. except zipfile.BadZipFile:
  822. Logger.logException("w", "Unable to retrieve metadata from {fname}: 3MF archive is corrupt.".format(fname = file_name))
  823. return result
  824. except EnvironmentError as e:
  825. Logger.logException("w", "Unable to retrieve metadata from {fname}: File is inaccessible. Error: {err}".format(fname = file_name, err = str(e)))
  826. return result
  827. metadata_files = [name for name in archive.namelist() if name.endswith("plugin_metadata.json")]
  828. for metadata_file in metadata_files:
  829. try:
  830. plugin_id = metadata_file.split("/")[0]
  831. result[plugin_id] = json.loads(archive.open("%s/plugin_metadata.json" % plugin_id).read().decode("utf-8"))
  832. except Exception:
  833. Logger.logException("w", "Unable to retrieve metadata for %s", metadata_file)
  834. return result
  835. def _processQualityChanges(self, global_stack):
  836. if self._machine_info.quality_changes_info is None:
  837. return
  838. # If we have custom profiles, load them
  839. quality_changes_name = self._machine_info.quality_changes_info.name
  840. if self._machine_info.quality_changes_info is not None:
  841. Logger.log("i", "Loading custom profile [%s] from project file",
  842. self._machine_info.quality_changes_info.name)
  843. # Get the correct extruder definition IDs for quality changes
  844. machine_definition_id_for_quality = ContainerTree.getInstance().machines[global_stack.definition.getId()].quality_definition
  845. machine_definition_for_quality = self._container_registry.findDefinitionContainers(id = machine_definition_id_for_quality)[0]
  846. quality_changes_info = self._machine_info.quality_changes_info
  847. quality_changes_quality_type = quality_changes_info.global_info.parser["metadata"]["quality_type"]
  848. # quality changes container may not be present for every extruder. Prepopulate the dict with default values.
  849. quality_changes_intent_category_per_extruder = {position: "default" for position in self._machine_info.extruder_info_dict}
  850. for position, info in quality_changes_info.extruder_info_dict.items():
  851. quality_changes_intent_category_per_extruder[position] = info.parser["metadata"].get("intent_category", "default")
  852. quality_changes_name = quality_changes_info.name
  853. create_new = self._resolve_strategies.get("quality_changes") != "override"
  854. if create_new:
  855. container_info_dict = {None: self._machine_info.quality_changes_info.global_info}
  856. container_info_dict.update(quality_changes_info.extruder_info_dict)
  857. quality_changes_name = self._container_registry.uniqueName(quality_changes_name)
  858. for position, container_info in container_info_dict.items():
  859. extruder_stack = None
  860. intent_category: Optional[str] = None
  861. if position is not None:
  862. try:
  863. extruder_stack = global_stack.extruderList[int(position)]
  864. except IndexError:
  865. continue
  866. intent_category = quality_changes_intent_category_per_extruder[position]
  867. container = self._createNewQualityChanges(quality_changes_quality_type, intent_category, quality_changes_name, global_stack, extruder_stack)
  868. container_info.container = container
  869. self._container_registry.addContainer(container)
  870. Logger.log("d", "Created new quality changes container [%s]", container.getId())
  871. else:
  872. # Find the existing containers
  873. quality_changes_containers = self._container_registry.findInstanceContainers(name = quality_changes_name,
  874. type = "quality_changes")
  875. for container in quality_changes_containers:
  876. extruder_position = container.getMetaDataEntry("position")
  877. if extruder_position is None:
  878. quality_changes_info.global_info.container = container
  879. else:
  880. if extruder_position not in quality_changes_info.extruder_info_dict:
  881. quality_changes_info.extruder_info_dict[extruder_position] = ContainerInfo(None, None, None)
  882. container_info = quality_changes_info.extruder_info_dict[extruder_position]
  883. container_info.container = container
  884. # If there is no quality changes for any extruder, create one.
  885. if not quality_changes_info.extruder_info_dict:
  886. container_info = ContainerInfo(None, None, None)
  887. quality_changes_info.extruder_info_dict["0"] = container_info
  888. # If the global stack we're "targeting" has never been active, but was updated from Cura 3.4,
  889. # it might not have its extruders set properly.
  890. if len(global_stack.extruderList) == 0:
  891. ExtruderManager.getInstance().fixSingleExtrusionMachineExtruderDefinition(global_stack)
  892. try:
  893. extruder_stack = global_stack.extruderList[0]
  894. except IndexError:
  895. extruder_stack = None
  896. intent_category = quality_changes_intent_category_per_extruder["0"]
  897. container = self._createNewQualityChanges(quality_changes_quality_type, intent_category, quality_changes_name, global_stack, extruder_stack)
  898. container_info.container = container
  899. self._container_registry.addContainer(container)
  900. Logger.log("d", "Created new quality changes container [%s]", container.getId())
  901. # Clear all existing containers
  902. quality_changes_info.global_info.container.clear()
  903. for container_info in quality_changes_info.extruder_info_dict.values():
  904. if container_info.container:
  905. container_info.container.clear()
  906. # Loop over everything and override the existing containers
  907. global_info = quality_changes_info.global_info
  908. global_info.container.clear() # Clear all
  909. for key, value in global_info.parser["values"].items():
  910. if not machine_definition_for_quality.getProperty(key, "settable_per_extruder"):
  911. global_info.container.setProperty(key, "value", value)
  912. else:
  913. quality_changes_info.extruder_info_dict["0"].container.setProperty(key, "value", value)
  914. for position, container_info in quality_changes_info.extruder_info_dict.items():
  915. if container_info.parser is None:
  916. continue
  917. if container_info.container is None:
  918. try:
  919. extruder_stack = global_stack.extruderList[int(position)]
  920. except IndexError:
  921. continue
  922. intent_category = quality_changes_intent_category_per_extruder[position]
  923. container = self._createNewQualityChanges(quality_changes_quality_type, intent_category, quality_changes_name, global_stack, extruder_stack)
  924. container_info.container = container
  925. self._container_registry.addContainer(container)
  926. for key, value in container_info.parser["values"].items():
  927. container_info.container.setProperty(key, "value", value)
  928. self._machine_info.quality_changes_info.name = quality_changes_name
  929. def _getMachineExtruderCount(self) -> Optional[int]:
  930. """
  931. Extracts the machine extruder count from the definition_changes file of the printer. If it is not specified in
  932. the file, None is returned instead.
  933. :return: The count of the machine's extruders
  934. """
  935. machine_extruder_count = None
  936. if self._machine_info \
  937. and self._machine_info.definition_changes_info \
  938. and "values" in self._machine_info.definition_changes_info.parser \
  939. and "machine_extruder_count" in self._machine_info.definition_changes_info.parser["values"]:
  940. try:
  941. # Theoretically, if the machine_extruder_count is a setting formula (e.g. "=3"), this will produce a
  942. # value error and the project file loading will load the settings in the first extruder only.
  943. # This is not expected to happen though, since all machine definitions define the machine_extruder_count
  944. # as an integer.
  945. machine_extruder_count = int(self._machine_info.definition_changes_info.parser["values"]["machine_extruder_count"])
  946. except ValueError:
  947. Logger.log("w", "'machine_extruder_count' in file '{file_name}' is not a number."
  948. .format(file_name = self._machine_info.definition_changes_info.file_name))
  949. return machine_extruder_count
  950. def _createNewQualityChanges(self, quality_type: str, intent_category: Optional[str], name: str, global_stack: GlobalStack, extruder_stack: Optional[ExtruderStack]) -> InstanceContainer:
  951. """Helper class to create a new quality changes profile.
  952. This will then later be filled with the appropriate data.
  953. :param quality_type: The quality type of the new profile.
  954. :param intent_category: The intent category of the new profile.
  955. :param name: The name for the profile. This will later be made unique so
  956. it doesn't need to be unique yet.
  957. :param global_stack: The global stack showing the configuration that the
  958. profile should be created for.
  959. :param extruder_stack: The extruder stack showing the configuration that
  960. the profile should be created for. If this is None, it will be created
  961. for the global stack.
  962. """
  963. container_registry = CuraApplication.getInstance().getContainerRegistry()
  964. base_id = global_stack.definition.getId() if extruder_stack is None else extruder_stack.getId()
  965. new_id = base_id + "_" + name
  966. new_id = new_id.lower().replace(" ", "_")
  967. new_id = container_registry.uniqueName(new_id)
  968. # Create a new quality_changes container for the quality.
  969. quality_changes = InstanceContainer(new_id)
  970. quality_changes.setName(name)
  971. quality_changes.setMetaDataEntry("type", "quality_changes")
  972. quality_changes.setMetaDataEntry("quality_type", quality_type)
  973. if intent_category is not None:
  974. quality_changes.setMetaDataEntry("intent_category", intent_category)
  975. # If we are creating a container for an extruder, ensure we add that to the container.
  976. if extruder_stack is not None:
  977. quality_changes.setMetaDataEntry("position", extruder_stack.getMetaDataEntry("position"))
  978. # If the machine specifies qualities should be filtered, ensure we match the current criteria.
  979. machine_definition_id = ContainerTree.getInstance().machines[global_stack.definition.getId()].quality_definition
  980. quality_changes.setDefinition(machine_definition_id)
  981. quality_changes.setMetaDataEntry("setting_version", CuraApplication.getInstance().SettingVersion)
  982. quality_changes.setDirty(True)
  983. return quality_changes
  984. @staticmethod
  985. def _clearStack(stack):
  986. application = CuraApplication.getInstance()
  987. stack.definitionChanges.clear()
  988. stack.variant = application.empty_variant_container
  989. stack.material = application.empty_material_container
  990. stack.quality = application.empty_quality_container
  991. stack.qualityChanges = application.empty_quality_changes_container
  992. stack.userChanges.clear()
  993. def _applyDefinitionChanges(self, global_stack, extruder_stack_dict):
  994. values_to_set_for_extruders = {}
  995. if self._machine_info.definition_changes_info is not None:
  996. parser = self._machine_info.definition_changes_info.parser
  997. for key, value in parser["values"].items():
  998. if global_stack.getProperty(key, "settable_per_extruder"):
  999. values_to_set_for_extruders[key] = value
  1000. else:
  1001. if not self._settingIsFromMissingPackage(key, value):
  1002. global_stack.definitionChanges.setProperty(key, "value", value)
  1003. for position, extruder_stack in extruder_stack_dict.items():
  1004. if position not in self._machine_info.extruder_info_dict:
  1005. continue
  1006. extruder_info = self._machine_info.extruder_info_dict[position]
  1007. if extruder_info.definition_changes_info is None:
  1008. continue
  1009. parser = extruder_info.definition_changes_info.parser
  1010. for key, value in values_to_set_for_extruders.items():
  1011. extruder_stack.definitionChanges.setProperty(key, "value", value)
  1012. if parser is not None:
  1013. for key, value in parser["values"].items():
  1014. if not self._settingIsFromMissingPackage(key, value):
  1015. extruder_stack.definitionChanges.setProperty(key, "value", value)
  1016. def _applyUserChanges(self, global_stack, extruder_stack_dict):
  1017. values_to_set_for_extruder_0 = {}
  1018. if self._machine_info.user_changes_info is not None:
  1019. parser = self._machine_info.user_changes_info.parser
  1020. for key, value in parser["values"].items():
  1021. if global_stack.getProperty(key, "settable_per_extruder"):
  1022. values_to_set_for_extruder_0[key] = value
  1023. else:
  1024. if not self._settingIsFromMissingPackage(key, value):
  1025. global_stack.userChanges.setProperty(key, "value", value)
  1026. for position, extruder_stack in extruder_stack_dict.items():
  1027. if position not in self._machine_info.extruder_info_dict:
  1028. continue
  1029. extruder_info = self._machine_info.extruder_info_dict[position]
  1030. if extruder_info.user_changes_info is not None:
  1031. parser = self._machine_info.extruder_info_dict[position].user_changes_info.parser
  1032. if position == "0":
  1033. for key, value in values_to_set_for_extruder_0.items():
  1034. extruder_stack.userChanges.setProperty(key, "value", value)
  1035. if parser is not None:
  1036. for key, value in parser["values"].items():
  1037. if not self._settingIsFromMissingPackage(key, value):
  1038. extruder_stack.userChanges.setProperty(key, "value", value)
  1039. def _applyVariants(self, global_stack, extruder_stack_dict):
  1040. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  1041. # Take the global variant from the machine info if available.
  1042. if self._machine_info.variant_info is not None:
  1043. variant_name = self._machine_info.variant_info.parser["general"]["name"]
  1044. if variant_name in machine_node.variants:
  1045. global_stack.variant = machine_node.variants[variant_name].container
  1046. else:
  1047. Logger.log("w", "Could not find global variant '{0}'.".format(variant_name))
  1048. for position, extruder_stack in extruder_stack_dict.items():
  1049. if position not in self._machine_info.extruder_info_dict:
  1050. continue
  1051. extruder_info = self._machine_info.extruder_info_dict[position]
  1052. if extruder_info.variant_info is None:
  1053. # If there is no variant_info, try to use the default variant. Otherwise, any available variant.
  1054. node = machine_node.variants.get(machine_node.preferred_variant_name, next(iter(machine_node.variants.values())))
  1055. else:
  1056. variant_name = extruder_info.variant_info.parser["general"]["name"]
  1057. node = ContainerTree.getInstance().machines[global_stack.definition.getId()].variants.get(variant_name, next(iter(machine_node.variants.values())))
  1058. extruder_stack.variant = node.container
  1059. def _applyMaterials(self, global_stack, extruder_stack_dict):
  1060. machine_node = ContainerTree.getInstance().machines[global_stack.definition.getId()]
  1061. for position, extruder_stack in extruder_stack_dict.items():
  1062. if position not in self._machine_info.extruder_info_dict:
  1063. continue
  1064. extruder_info = self._machine_info.extruder_info_dict[position]
  1065. if extruder_info.root_material_id is None:
  1066. continue
  1067. root_material_id = extruder_info.root_material_id
  1068. root_material_id = self._old_new_materials.get(root_material_id, root_material_id)
  1069. available_materials = machine_node.variants[extruder_stack.variant.getName()].materials
  1070. if root_material_id not in available_materials:
  1071. continue
  1072. material_node = available_materials[root_material_id]
  1073. extruder_stack.material = material_node.container
  1074. def _clearMachineSettings(self, global_stack, extruder_stack_dict):
  1075. self._clearStack(global_stack)
  1076. for extruder_stack in extruder_stack_dict.values():
  1077. self._clearStack(extruder_stack)
  1078. self._quality_changes_to_apply = None
  1079. self._quality_type_to_apply = None
  1080. self._intent_category_to_apply = None
  1081. self._user_settings_to_apply = None
  1082. def _applyUserSettings(self, global_stack, extruder_stack_dict, user_settings):
  1083. for stack_name, settings in user_settings.items():
  1084. if stack_name == 'global':
  1085. ThreeMFWorkspaceReader._applyUserSettingsOnStack(global_stack, settings)
  1086. else:
  1087. extruder_match = re.fullmatch('extruder_([0-9]+)', stack_name)
  1088. if extruder_match is not None:
  1089. extruder_nr = extruder_match.group(1)
  1090. if extruder_nr in extruder_stack_dict:
  1091. ThreeMFWorkspaceReader._applyUserSettingsOnStack(extruder_stack_dict[extruder_nr], settings)
  1092. @staticmethod
  1093. def _applyUserSettingsOnStack(stack, user_settings):
  1094. user_settings_container = stack.userChanges
  1095. for setting_to_import, setting_value in user_settings.items():
  1096. user_settings_container.setProperty(setting_to_import, 'value', setting_value)
  1097. def _applyChangesToMachine(self, global_stack, extruder_stack_dict):
  1098. # Clear all first
  1099. self._clearMachineSettings(global_stack, extruder_stack_dict)
  1100. self._applyDefinitionChanges(global_stack, extruder_stack_dict)
  1101. self._applyUserChanges(global_stack, extruder_stack_dict)
  1102. self._applyVariants(global_stack, extruder_stack_dict)
  1103. self._applyMaterials(global_stack, extruder_stack_dict)
  1104. # prepare the quality to select
  1105. if self._machine_info.quality_changes_info is not None:
  1106. self._quality_changes_to_apply = self._machine_info.quality_changes_info.name
  1107. else:
  1108. self._quality_type_to_apply = self._machine_info.quality_type
  1109. self._intent_category_to_apply = self._machine_info.intent_category
  1110. # Set enabled/disabled for extruders
  1111. for position, extruder_stack in extruder_stack_dict.items():
  1112. extruder_info = self._machine_info.extruder_info_dict.get(position)
  1113. if not extruder_info:
  1114. continue
  1115. if "enabled" not in extruder_stack.getMetaData():
  1116. extruder_stack.setMetaDataEntry("enabled", "True")
  1117. extruder_stack.setMetaDataEntry("enabled", str(extruder_info.enabled))
  1118. # Set metadata fields that are missing from the global stack
  1119. for key, value in self._machine_info.metadata_dict.items():
  1120. if key not in _ignored_machine_network_metadata:
  1121. global_stack.setMetaDataEntry(key, value)
  1122. def _settingIsFromMissingPackage(self, key, value):
  1123. # Check if the key and value pair is from the missing package
  1124. for package in self._dialog.missingPackages:
  1125. if value.startswith("PLUGIN::"):
  1126. if (package['id'] + "@" + package['package_version']) in value:
  1127. Logger.log("w", f"Ignoring {key} value {value} from missing package")
  1128. return True
  1129. return False
  1130. def _updateActiveMachine(self, global_stack):
  1131. # Actually change the active machine.
  1132. machine_manager = Application.getInstance().getMachineManager()
  1133. container_tree = ContainerTree.getInstance()
  1134. machine_manager.setActiveMachine(global_stack.getId())
  1135. # Set metadata fields that are missing from the global stack
  1136. if not self._is_ucp:
  1137. for key, value in self._machine_info.metadata_dict.items():
  1138. if key not in global_stack.getMetaData() and key not in _ignored_machine_network_metadata:
  1139. global_stack.setMetaDataEntry(key, value)
  1140. if self._quality_changes_to_apply !=None:
  1141. quality_changes_group_list = container_tree.getCurrentQualityChangesGroups()
  1142. quality_changes_group = next((qcg for qcg in quality_changes_group_list if qcg.name == self._quality_changes_to_apply), None)
  1143. if not quality_changes_group:
  1144. Logger.log("e", "Could not find quality_changes [%s]", self._quality_changes_to_apply)
  1145. return
  1146. machine_manager.setQualityChangesGroup(quality_changes_group, no_dialog = True)
  1147. else:
  1148. self._quality_type_to_apply = self._quality_type_to_apply.lower() if self._quality_type_to_apply else None
  1149. quality_group_dict = container_tree.getCurrentQualityGroups()
  1150. if self._quality_type_to_apply in quality_group_dict:
  1151. quality_group = quality_group_dict[self._quality_type_to_apply]
  1152. else:
  1153. Logger.log("i", "Could not find quality type [%s], switch to default", self._quality_type_to_apply)
  1154. preferred_quality_type = global_stack.getMetaDataEntry("preferred_quality_type")
  1155. quality_group = quality_group_dict.get(preferred_quality_type)
  1156. if quality_group is None:
  1157. Logger.log("e", "Could not get preferred quality type [%s]", preferred_quality_type)
  1158. if quality_group is not None:
  1159. machine_manager.setQualityGroup(quality_group, no_dialog = True)
  1160. # Also apply intent if available
  1161. available_intent_category_list = IntentManager.getInstance().currentAvailableIntentCategories()
  1162. if self._intent_category_to_apply is not None and self._intent_category_to_apply in available_intent_category_list:
  1163. machine_manager.setIntentByCategory(self._intent_category_to_apply)
  1164. else:
  1165. # if no intent is provided, reset to the default (balanced) intent
  1166. machine_manager.resetIntents()
  1167. # Notify everything/one that is to notify about changes.
  1168. global_stack.containersChanged.emit(global_stack.getTop())
  1169. @staticmethod
  1170. def _stripFileToId(file):
  1171. mime_type = MimeTypeDatabase.getMimeTypeForFile(file)
  1172. file = mime_type.stripExtension(file)
  1173. return file.replace("Cura/", "")
  1174. def _getXmlProfileClass(self):
  1175. return self._container_registry.getContainerForMimeType(MimeTypeDatabase.getMimeType("application/x-ultimaker-material-profile"))
  1176. @staticmethod
  1177. def _getContainerIdListFromSerialized(serialized):
  1178. """Get the list of ID's of all containers in a container stack by partially parsing it's serialized data."""
  1179. parser = ConfigParser(interpolation = None, empty_lines_in_values = False)
  1180. parser.read_string(serialized)
  1181. container_ids = []
  1182. if "containers" in parser:
  1183. for index, container_id in parser.items("containers"):
  1184. container_ids.append(container_id)
  1185. elif parser.has_option("general", "containers"):
  1186. container_string = parser["general"].get("containers", "")
  1187. container_list = container_string.split(",")
  1188. container_ids = [container_id for container_id in container_list if container_id != ""]
  1189. # HACK: there used to be 6 containers numbering from 0 to 5 in a stack,
  1190. # now we have 7: index 5 becomes "definition_changes"
  1191. if len(container_ids) == 6:
  1192. # Hack; We used to not save the definition changes. Fix this.
  1193. container_ids.insert(5, "empty")
  1194. return container_ids
  1195. @staticmethod
  1196. def _getMachineNameFromSerializedStack(serialized):
  1197. parser = ConfigParser(interpolation = None, empty_lines_in_values = False)
  1198. parser.read_string(serialized)
  1199. return parser["general"].get("name", "")
  1200. @staticmethod
  1201. def _getMetaDataDictFromSerializedStack(serialized: str) -> Dict[str, str]:
  1202. parser = ConfigParser(interpolation = None, empty_lines_in_values = False)
  1203. parser.read_string(serialized)
  1204. return dict(parser["metadata"])
  1205. @staticmethod
  1206. def _getMaterialLabelFromSerialized(serialized):
  1207. data = ET.fromstring(serialized)
  1208. metadata = data.iterfind("./um:metadata/um:name/um:label", {"um": "http://www.ultimaker.com/material"})
  1209. for entry in metadata:
  1210. return entry.text
  1211. @staticmethod
  1212. def _parse_packages_metadata(archive: zipfile.ZipFile) -> List[Dict[str, str]]:
  1213. try:
  1214. package_metadata = json.loads(archive.open("Cura/packages.json").read().decode("utf-8"))
  1215. return package_metadata["packages"]
  1216. except KeyError:
  1217. Logger.warning("No package metadata was found in .3mf file.")
  1218. except Exception:
  1219. Logger.error("Failed to load packages metadata from .3mf file.")
  1220. return []
  1221. @staticmethod
  1222. def _filter_missing_package_metadata(package_metadata: List[Dict[str, str]]) -> List[Dict[str, str]]:
  1223. """Filters out installed packages from package_metadata"""
  1224. missing_packages = []
  1225. package_manager = cast(CuraPackageManager, CuraApplication.getInstance().getPackageManager())
  1226. for package in package_metadata:
  1227. package_id = package["id"]
  1228. if not package_manager.isPackageInstalled(package_id):
  1229. missing_packages.append(package)
  1230. return missing_packages