ThreeMFWorkspaceReader.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. # Copyright (c) 2018 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 threading
  7. from typing import List, Tuple
  8. import xml.etree.ElementTree as ET
  9. from UM.Workspace.WorkspaceReader import WorkspaceReader
  10. from UM.Application import Application
  11. from UM.Logger import Logger
  12. from UM.i18n import i18nCatalog
  13. from UM.Signal import postponeSignals, CompressTechnique
  14. from UM.Settings.ContainerStack import ContainerStack
  15. from UM.Settings.DefinitionContainer import DefinitionContainer
  16. from UM.Settings.InstanceContainer import InstanceContainer
  17. from UM.Settings.ContainerRegistry import ContainerRegistry
  18. from UM.MimeTypeDatabase import MimeTypeDatabase
  19. from UM.Job import Job
  20. from UM.Preferences import Preferences
  21. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  22. from cura.Settings.ExtruderStack import ExtruderStack
  23. from cura.Settings.GlobalStack import GlobalStack
  24. from cura.Settings.CuraContainerStack import _ContainerIndexes
  25. from cura.CuraApplication import CuraApplication
  26. from cura.Utils.Threading import call_on_qt_thread
  27. from .WorkspaceDialog import WorkspaceDialog
  28. i18n_catalog = i18nCatalog("cura")
  29. class ContainerInfo:
  30. def __init__(self, file_name: str, serialized: str, parser: ConfigParser):
  31. self.file_name = file_name
  32. self.serialized = serialized
  33. self.parser = parser
  34. self.container = None
  35. self.definition_id = None
  36. class QualityChangesInfo:
  37. def __init__(self):
  38. self.name = None
  39. self.global_info = None
  40. self.extruder_info_dict = {}
  41. class MachineInfo:
  42. def __init__(self):
  43. self.container_id = None
  44. self.name = None
  45. self.definition_id = None
  46. self.quality_type = None
  47. self.custom_quality_name = None
  48. self.quality_changes_info = None
  49. self.variant_info = None
  50. self.definition_changes_info = None
  51. self.user_changes_info = None
  52. self.extruder_info_dict = {}
  53. class ExtruderInfo:
  54. def __init__(self):
  55. self.position = None
  56. self.enabled = True
  57. self.variant_info = None
  58. self.root_material_id = None
  59. self.definition_changes_info = None
  60. self.user_changes_info = None
  61. ## Base implementation for reading 3MF workspace files.
  62. class ThreeMFWorkspaceReader(WorkspaceReader):
  63. def __init__(self):
  64. super().__init__()
  65. self._supported_extensions = [".3mf"]
  66. self._dialog = WorkspaceDialog()
  67. self._3mf_mesh_reader = None
  68. self._container_registry = ContainerRegistry.getInstance()
  69. # suffixes registered with the MineTypes don't start with a dot '.'
  70. self._definition_container_suffix = "." + ContainerRegistry.getMimeTypeForContainer(DefinitionContainer).preferredSuffix
  71. self._material_container_suffix = None # We have to wait until all other plugins are loaded before we can set it
  72. self._instance_container_suffix = "." + ContainerRegistry.getMimeTypeForContainer(InstanceContainer).preferredSuffix
  73. self._container_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(ContainerStack).preferredSuffix
  74. self._extruder_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(ExtruderStack).preferredSuffix
  75. self._global_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(GlobalStack).preferredSuffix
  76. # Certain instance container types are ignored because we make the assumption that only we make those types
  77. # of containers. They are:
  78. # - quality
  79. # - variant
  80. self._ignored_instance_container_types = {"quality", "variant"}
  81. self._resolve_strategies = {}
  82. self._id_mapping = {}
  83. # In Cura 2.5 and 2.6, the empty profiles used to have those long names
  84. self._old_empty_profile_id_dict = {"empty_%s" % k: "empty" for k in ["material", "variant"]}
  85. self._is_same_machine_type = False
  86. self._old_new_materials = {}
  87. self._materials_to_select = {}
  88. self._machine_info = None
  89. def _clearState(self):
  90. self._is_same_machine_type = False
  91. self._id_mapping = {}
  92. self._old_new_materials = {}
  93. self._materials_to_select = {}
  94. self._machine_info = None
  95. ## Get a unique name based on the old_id. This is different from directly calling the registry in that it caches results.
  96. # This has nothing to do with speed, but with getting consistent new naming for instances & objects.
  97. def getNewId(self, old_id):
  98. if old_id not in self._id_mapping:
  99. self._id_mapping[old_id] = self._container_registry.uniqueName(old_id)
  100. return self._id_mapping[old_id]
  101. ## Separates the given file list into a list of GlobalStack files and a list of ExtruderStack files.
  102. #
  103. # In old versions, extruder stack files have the same suffix as container stack files ".stack.cfg".
  104. #
  105. def _determineGlobalAndExtruderStackFiles(self, project_file_name: str, file_list: List[str]) -> Tuple[str, List[str]]:
  106. archive = zipfile.ZipFile(project_file_name, "r")
  107. global_stack_file_list = [name for name in file_list if name.endswith(self._global_stack_suffix)]
  108. extruder_stack_file_list = [name for name in file_list if name.endswith(self._extruder_stack_suffix)]
  109. # separate container stack files and extruder stack files
  110. files_to_determine = [name for name in file_list if name.endswith(self._container_stack_suffix)]
  111. for file_name in files_to_determine:
  112. # FIXME: HACK!
  113. # We need to know the type of the stack file, but we can only know it if we deserialize it.
  114. # The default ContainerStack.deserialize() will connect signals, which is not desired in this case.
  115. # Since we know that the stack files are INI files, so we directly use the ConfigParser to parse them.
  116. serialized = archive.open(file_name).read().decode("utf-8")
  117. stack_config = ConfigParser(interpolation = None)
  118. stack_config.read_string(serialized)
  119. # sanity check
  120. if not stack_config.has_option("metadata", "type"):
  121. Logger.log("e", "%s in %s doesn't seem to be valid stack file", file_name, project_file_name)
  122. continue
  123. stack_type = stack_config.get("metadata", "type")
  124. if stack_type == "extruder_train":
  125. extruder_stack_file_list.append(file_name)
  126. elif stack_type == "machine":
  127. global_stack_file_list.append(file_name)
  128. else:
  129. Logger.log("w", "Unknown container stack type '%s' from %s in %s",
  130. stack_type, file_name, project_file_name)
  131. if len(global_stack_file_list) > 1:
  132. Logger.log("e", "More than one global stack file found: [{file_list}]".format(file_list = global_stack_file_list))
  133. #But we can recover by just getting the first global stack file.
  134. if len(global_stack_file_list) == 0:
  135. Logger.log("e", "No global stack file found!")
  136. raise FileNotFoundError("No global stack file found!")
  137. return global_stack_file_list[0], extruder_stack_file_list
  138. ## read some info so we can make decisions
  139. # \param file_name
  140. # \param show_dialog In case we use preRead() to check if a file is a valid project file, we don't want to show a dialog.
  141. def preRead(self, file_name, show_dialog=True, *args, **kwargs):
  142. self._clearState()
  143. self._3mf_mesh_reader = Application.getInstance().getMeshFileHandler().getReaderForFile(file_name)
  144. if self._3mf_mesh_reader and self._3mf_mesh_reader.preRead(file_name) == WorkspaceReader.PreReadResult.accepted:
  145. pass
  146. else:
  147. Logger.log("w", "Could not find reader that was able to read the scene data for 3MF workspace")
  148. return WorkspaceReader.PreReadResult.failed
  149. self._machine_info = MachineInfo()
  150. machine_type = ""
  151. variant_type_name = i18n_catalog.i18nc("@label", "Nozzle")
  152. # Check if there are any conflicts, so we can ask the user.
  153. archive = zipfile.ZipFile(file_name, "r")
  154. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  155. resolve_strategy_keys = ["machine", "material", "quality_changes"]
  156. self._resolve_strategies = {k: None for k in resolve_strategy_keys}
  157. containers_found_dict = {k: False for k in resolve_strategy_keys}
  158. #
  159. # Read definition containers
  160. #
  161. machine_definition_id = None
  162. machine_definition_container_count = 0
  163. extruder_definition_container_count = 0
  164. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  165. for definition_container_file in definition_container_files:
  166. container_id = self._stripFileToId(definition_container_file)
  167. definitions = self._container_registry.findDefinitionContainersMetadata(id = container_id)
  168. serialized = archive.open(definition_container_file).read().decode("utf-8")
  169. if not definitions:
  170. definition_container = DefinitionContainer.deserializeMetadata(serialized, container_id)[0]
  171. else:
  172. definition_container = definitions[0]
  173. definition_container_type = definition_container.get("type")
  174. if definition_container_type == "machine":
  175. machine_definition_id = container_id
  176. machine_type = definition_container["name"]
  177. variant_type_name = definition_container.get("variants_name", variant_type_name)
  178. machine_definition_container_count += 1
  179. elif definition_container_type == "extruder":
  180. extruder_definition_container_count += 1
  181. else:
  182. Logger.log("w", "Unknown definition container type %s for %s",
  183. definition_container_type, definition_container_file)
  184. Job.yieldThread()
  185. if machine_definition_container_count != 1:
  186. return WorkspaceReader.PreReadResult.failed # Not a workspace file but ordinary 3MF.
  187. material_labels = []
  188. material_conflict = False
  189. xml_material_profile = self._getXmlProfileClass()
  190. reverse_material_id_dict = {}
  191. if self._material_container_suffix is None:
  192. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).preferredSuffix
  193. if xml_material_profile:
  194. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  195. for material_container_file in material_container_files:
  196. container_id = self._stripFileToId(material_container_file)
  197. serialized = archive.open(material_container_file).read().decode("utf-8")
  198. metadata_list = xml_material_profile.deserializeMetadata(serialized, container_id)
  199. reverse_map = {metadata["id"]: container_id for metadata in metadata_list}
  200. reverse_material_id_dict.update(reverse_map)
  201. material_labels.append(self._getMaterialLabelFromSerialized(serialized))
  202. if self._container_registry.findContainersMetadata(id = container_id): #This material already exists.
  203. containers_found_dict["material"] = True
  204. if not self._container_registry.isReadOnly(container_id): # Only non readonly materials can be in conflict
  205. material_conflict = True
  206. Job.yieldThread()
  207. # Check if any quality_changes instance container is in conflict.
  208. instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)]
  209. quality_name = ""
  210. custom_quality_name = ""
  211. num_settings_overriden_by_quality_changes = 0 # How many settings are changed by the quality changes
  212. num_user_settings = 0
  213. quality_changes_conflict = False
  214. self._machine_info.quality_changes_info = QualityChangesInfo()
  215. quality_changes_info_list = []
  216. instance_container_info_dict = {} # id -> parser
  217. for instance_container_file_name in instance_container_files:
  218. container_id = self._stripFileToId(instance_container_file_name)
  219. serialized = archive.open(instance_container_file_name).read().decode("utf-8")
  220. # Qualities and variants don't have upgrades, so don't upgrade them
  221. parser = ConfigParser(interpolation = None)
  222. parser.read_string(serialized)
  223. container_type = parser["metadata"]["type"]
  224. if container_type not in ("quality", "variant"):
  225. serialized = InstanceContainer._updateSerialized(serialized, instance_container_file_name)
  226. parser = ConfigParser(interpolation = None)
  227. parser.read_string(serialized)
  228. container_info = ContainerInfo(instance_container_file_name, serialized, parser)
  229. instance_container_info_dict[container_id] = container_info
  230. container_type = parser["metadata"]["type"]
  231. if container_type == "quality_changes":
  232. quality_changes_info_list.append(container_info)
  233. if not parser.has_option("metadata", "position"):
  234. self._machine_info.quality_changes_info.name = parser["general"]["name"]
  235. self._machine_info.quality_changes_info.global_info = container_info
  236. else:
  237. position = parser["metadata"]["position"]
  238. self._machine_info.quality_changes_info.extruder_info_dict[position] = container_info
  239. custom_quality_name = parser["general"]["name"]
  240. values = parser["values"] if parser.has_section("values") else dict()
  241. num_settings_overriden_by_quality_changes += len(values)
  242. # Check if quality changes already exists.
  243. quality_changes = self._container_registry.findInstanceContainers(id = container_id)
  244. if quality_changes:
  245. containers_found_dict["quality_changes"] = True
  246. # Check if there really is a conflict by comparing the values
  247. instance_container = InstanceContainer(container_id)
  248. instance_container.deserialize(serialized, file_name = instance_container_file_name)
  249. if quality_changes[0] != instance_container:
  250. quality_changes_conflict = True
  251. elif container_type == "quality":
  252. if not quality_name:
  253. quality_name = parser["general"]["name"]
  254. elif container_type == "user":
  255. num_user_settings += len(parser["values"])
  256. elif container_type in self._ignored_instance_container_types:
  257. # Ignore certain instance container types
  258. Logger.log("w", "Ignoring instance container [%s] with type [%s]", container_id, container_type)
  259. continue
  260. Job.yieldThread()
  261. if self._machine_info.quality_changes_info.global_info is None:
  262. self._machine_info.quality_changes_info = None
  263. # Load ContainerStack files and ExtruderStack files
  264. try:
  265. global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles(
  266. file_name, cura_file_names)
  267. except FileNotFoundError:
  268. return WorkspaceReader.PreReadResult.failed
  269. machine_conflict = False
  270. # Because there can be cases as follows:
  271. # - the global stack exists but some/all of the extruder stacks DON'T exist
  272. # - the global stack DOESN'T exist but some/all of the extruder stacks exist
  273. # To simplify this, only check if the global stack exists or not
  274. global_stack_id = self._stripFileToId(global_stack_file)
  275. serialized = archive.open(global_stack_file).read().decode("utf-8")
  276. machine_name = self._getMachineNameFromSerializedStack(serialized)
  277. stacks = self._container_registry.findContainerStacks(name = machine_name, type = "machine")
  278. self._is_same_machine_type = True
  279. existing_global_stack = None
  280. if stacks:
  281. global_stack = stacks[0]
  282. existing_global_stack = global_stack
  283. containers_found_dict["machine"] = True
  284. # Check if there are any changes at all in any of the container stacks.
  285. id_list = self._getContainerIdListFromSerialized(serialized)
  286. for index, container_id in enumerate(id_list):
  287. # take into account the old empty container IDs
  288. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  289. if global_stack.getContainer(index).getId() != container_id:
  290. machine_conflict = True
  291. break
  292. self._is_same_machine_type = global_stack.definition.getId() == machine_definition_id
  293. # Get quality type
  294. parser = ConfigParser(interpolation = None)
  295. parser.read_string(serialized)
  296. quality_container_id = parser["containers"][str(_ContainerIndexes.Quality)]
  297. quality_type = "empty_quality"
  298. if quality_container_id not in ("empty", "empty_quality"):
  299. quality_type = instance_container_info_dict[quality_container_id].parser["metadata"]["quality_type"]
  300. # Get machine info
  301. serialized = archive.open(global_stack_file).read().decode("utf-8")
  302. serialized = GlobalStack._updateSerialized(serialized, global_stack_file)
  303. parser = ConfigParser(interpolation = None)
  304. parser.read_string(serialized)
  305. definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)]
  306. if definition_changes_id not in ("empty", "empty_definition_changes"):
  307. self._machine_info.definition_changes_info = instance_container_info_dict[definition_changes_id]
  308. user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)]
  309. if user_changes_id not in ("empty", "empty_user_changes"):
  310. self._machine_info.user_changes_info = instance_container_info_dict[user_changes_id]
  311. # Also check variant and material in case it doesn't have extruder stacks
  312. if not extruder_stack_files:
  313. position = "0"
  314. extruder_info = ExtruderInfo()
  315. extruder_info.position = position
  316. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  317. material_id = parser["containers"][str(_ContainerIndexes.Material)]
  318. if variant_id not in ("empty", "empty_variant"):
  319. extruder_info.variant_info = instance_container_info_dict[variant_id]
  320. if material_id not in ("empty", "empty_material"):
  321. root_material_id = reverse_material_id_dict[material_id]
  322. extruder_info.root_material_id = root_material_id
  323. self._machine_info.extruder_info_dict[position] = extruder_info
  324. else:
  325. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  326. if variant_id not in ("empty", "empty_variant"):
  327. self._machine_info.variant_info = instance_container_info_dict[variant_id]
  328. Job.yieldThread()
  329. # if the global stack is found, we check if there are conflicts in the extruder stacks
  330. for extruder_stack_file in extruder_stack_files:
  331. serialized = archive.open(extruder_stack_file).read().decode("utf-8")
  332. serialized = ExtruderStack._updateSerialized(serialized, extruder_stack_file)
  333. parser = ConfigParser(interpolation = None)
  334. parser.read_string(serialized)
  335. # The check should be done for the extruder stack that's associated with the existing global stack,
  336. # and those extruder stacks may have different IDs.
  337. # So we check according to the positions
  338. position = parser["metadata"]["position"]
  339. variant_id = parser["containers"][str(_ContainerIndexes.Variant)]
  340. material_id = parser["containers"][str(_ContainerIndexes.Material)]
  341. extruder_info = ExtruderInfo()
  342. extruder_info.position = position
  343. if parser.has_option("metadata", "enabled"):
  344. extruder_info.enabled = parser["metadata"]["enabled"]
  345. if variant_id not in ("empty", "empty_variant"):
  346. extruder_info.variant_info = instance_container_info_dict[variant_id]
  347. if material_id not in ("empty", "empty_material"):
  348. root_material_id = reverse_material_id_dict[material_id]
  349. extruder_info.root_material_id = root_material_id
  350. definition_changes_id = parser["containers"][str(_ContainerIndexes.DefinitionChanges)]
  351. if definition_changes_id not in ("empty", "empty_definition_changes"):
  352. extruder_info.definition_changes_info = instance_container_info_dict[definition_changes_id]
  353. user_changes_id = parser["containers"][str(_ContainerIndexes.UserChanges)]
  354. if user_changes_id not in ("empty", "empty_user_changes"):
  355. extruder_info.user_changes_info = instance_container_info_dict[user_changes_id]
  356. self._machine_info.extruder_info_dict[position] = extruder_info
  357. if not machine_conflict and containers_found_dict["machine"]:
  358. if position not in global_stack.extruders:
  359. continue
  360. existing_extruder_stack = global_stack.extruders[position]
  361. # check if there are any changes at all in any of the container stacks.
  362. id_list = self._getContainerIdListFromSerialized(serialized)
  363. for index, container_id in enumerate(id_list):
  364. # take into account the old empty container IDs
  365. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  366. if existing_extruder_stack.getContainer(index).getId() != container_id:
  367. machine_conflict = True
  368. break
  369. num_visible_settings = 0
  370. try:
  371. temp_preferences = Preferences()
  372. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  373. temp_preferences.deserialize(serialized)
  374. visible_settings_string = temp_preferences.getValue("general/visible_settings")
  375. has_visible_settings_string = visible_settings_string is not None
  376. if visible_settings_string is not None:
  377. num_visible_settings = len(visible_settings_string.split(";"))
  378. active_mode = temp_preferences.getValue("cura/active_mode")
  379. if not active_mode:
  380. active_mode = Preferences.getInstance().getValue("cura/active_mode")
  381. except KeyError:
  382. # If there is no preferences file, it's not a workspace, so notify user of failure.
  383. Logger.log("w", "File %s is not a valid workspace.", file_name)
  384. return WorkspaceReader.PreReadResult.failed
  385. # In case we use preRead() to check if a file is a valid project file, we don't want to show a dialog.
  386. if not show_dialog:
  387. return WorkspaceReader.PreReadResult.accepted
  388. # prepare data for the dialog
  389. num_extruders = extruder_definition_container_count
  390. if num_extruders == 0:
  391. num_extruders = 1 # No extruder stacks found, which means there is one extruder
  392. extruders = num_extruders * [""]
  393. quality_name = custom_quality_name if custom_quality_name else quality_name
  394. self._machine_info.container_id = global_stack_id
  395. self._machine_info.name = machine_name
  396. self._machine_info.definition_id = machine_definition_id
  397. self._machine_info.quality_type = quality_type
  398. self._machine_info.custom_quality_name = quality_name
  399. if machine_conflict and not self._is_same_machine_type:
  400. machine_conflict = False
  401. is_printer_group = False
  402. if machine_conflict:
  403. group_name = existing_global_stack.getMetaDataEntry("connect_group_name")
  404. if group_name is not None:
  405. is_printer_group = True
  406. machine_name = group_name
  407. # Show the dialog, informing the user what is about to happen.
  408. self._dialog.setMachineConflict(machine_conflict)
  409. self._dialog.setIsPrinterGroup(is_printer_group)
  410. self._dialog.setQualityChangesConflict(quality_changes_conflict)
  411. self._dialog.setMaterialConflict(material_conflict)
  412. self._dialog.setHasVisibleSettingsField(has_visible_settings_string)
  413. self._dialog.setNumVisibleSettings(num_visible_settings)
  414. self._dialog.setQualityName(quality_name)
  415. self._dialog.setQualityType(quality_type)
  416. self._dialog.setNumSettingsOverridenByQualityChanges(num_settings_overriden_by_quality_changes)
  417. self._dialog.setNumUserSettings(num_user_settings)
  418. self._dialog.setActiveMode(active_mode)
  419. self._dialog.setMachineName(machine_name)
  420. self._dialog.setMaterialLabels(material_labels)
  421. self._dialog.setMachineType(machine_type)
  422. self._dialog.setExtruders(extruders)
  423. self._dialog.setVariantType(variant_type_name)
  424. self._dialog.setHasObjectsOnPlate(Application.getInstance().platformActivity)
  425. self._dialog.show()
  426. # Block until the dialog is closed.
  427. self._dialog.waitForClose()
  428. if self._dialog.getResult() == {}:
  429. return WorkspaceReader.PreReadResult.cancelled
  430. self._resolve_strategies = self._dialog.getResult()
  431. #
  432. # There can be 3 resolve strategies coming from the dialog:
  433. # - new: create a new container
  434. # - override: override the existing container
  435. # - None: There is no conflict, which means containers with the same IDs may or may not be there already.
  436. # If there is an existing container, there is no conflict between them, and default to "override"
  437. # If there is no existing container, default to "new"
  438. #
  439. # Default values
  440. for key, strategy in self._resolve_strategies.items():
  441. if key not in containers_found_dict or strategy is not None:
  442. continue
  443. self._resolve_strategies[key] = "override" if containers_found_dict[key] else "new"
  444. return WorkspaceReader.PreReadResult.accepted
  445. ## Read the project file
  446. # Add all the definitions / materials / quality changes that do not exist yet. Then it loads
  447. # all the stacks into the container registry. In some cases it will reuse the container for the global stack.
  448. # It handles old style project files containing .stack.cfg as well as new style project files
  449. # containing global.cfg / extruder.cfg
  450. #
  451. # \param file_name
  452. @call_on_qt_thread
  453. def read(self, file_name):
  454. container_registry = ContainerRegistry.getInstance()
  455. signals = [container_registry.containerAdded,
  456. container_registry.containerRemoved,
  457. container_registry.containerMetaDataChanged]
  458. #
  459. # We now have different managers updating their lookup tables upon container changes. It is critical to make
  460. # sure that the managers have a complete set of data when they update.
  461. #
  462. # In project loading, lots of the container-related signals are loosely emitted, which can create timing gaps
  463. # for incomplete data update or other kinds of issues to happen.
  464. #
  465. # To avoid this, we postpone all signals so they don't get emitted immediately. But, please also be aware that,
  466. # because of this, do not expect to have the latest data in the lookup tables in project loading.
  467. #
  468. with postponeSignals(*signals, compress = CompressTechnique.NoCompression):
  469. return self._read(file_name)
  470. def _read(self, file_name):
  471. application = CuraApplication.getInstance()
  472. material_manager = application.getMaterialManager()
  473. archive = zipfile.ZipFile(file_name, "r")
  474. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  475. # Create a shadow copy of the preferences (we don't want all of the preferences, but we do want to re-use its
  476. # parsing code.
  477. temp_preferences = Preferences()
  478. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  479. temp_preferences.deserialize(serialized)
  480. # Copy a number of settings from the temp preferences to the global
  481. global_preferences = Preferences.getInstance()
  482. visible_settings = temp_preferences.getValue("general/visible_settings")
  483. if visible_settings is None:
  484. Logger.log("w", "Workspace did not contain visible settings. Leaving visibility unchanged")
  485. else:
  486. global_preferences.setValue("general/visible_settings", visible_settings)
  487. global_preferences.setValue("cura/active_setting_visibility_preset", "custom")
  488. categories_expanded = temp_preferences.getValue("cura/categories_expanded")
  489. if categories_expanded is None:
  490. Logger.log("w", "Workspace did not contain expanded categories. Leaving them unchanged")
  491. else:
  492. global_preferences.setValue("cura/categories_expanded", categories_expanded)
  493. application.expandedCategoriesChanged.emit() # Notify the GUI of the change
  494. # If a machine with the same name is of a different type, always create a new one.
  495. if not self._is_same_machine_type or self._resolve_strategies["machine"] != "override":
  496. # We need to create a new machine
  497. machine_name = self._container_registry.uniqueName(self._machine_info.name)
  498. global_stack = CuraStackBuilder.createMachine(machine_name, self._machine_info.definition_id)
  499. extruder_stack_dict = global_stack.extruders
  500. self._container_registry.addContainer(global_stack)
  501. else:
  502. # Find the machine
  503. global_stack = self._container_registry.findContainerStacks(name = self._machine_info.name, type = "machine")[0]
  504. extruder_stacks = self._container_registry.findContainerStacks(machine = global_stack.getId(),
  505. type = "extruder_train")
  506. extruder_stack_dict = {stack.getMetaDataEntry("position"): stack for stack in extruder_stacks}
  507. Logger.log("d", "Workspace loading is checking definitions...")
  508. # Get all the definition files & check if they exist. If not, add them.
  509. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  510. for definition_container_file in definition_container_files:
  511. container_id = self._stripFileToId(definition_container_file)
  512. definitions = self._container_registry.findDefinitionContainersMetadata(id = container_id)
  513. if not definitions:
  514. definition_container = DefinitionContainer(container_id)
  515. definition_container.deserialize(archive.open(definition_container_file).read().decode("utf-8"),
  516. file_name = definition_container_file)
  517. self._container_registry.addContainer(definition_container)
  518. Job.yieldThread()
  519. Logger.log("d", "Workspace loading is checking materials...")
  520. # Get all the material files and check if they exist. If not, add them.
  521. xml_material_profile = self._getXmlProfileClass()
  522. if self._material_container_suffix is None:
  523. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).suffixes[0]
  524. if xml_material_profile:
  525. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  526. for material_container_file in material_container_files:
  527. to_deserialize_material = False
  528. container_id = self._stripFileToId(material_container_file)
  529. need_new_name = False
  530. materials = self._container_registry.findInstanceContainers(id = container_id)
  531. if not materials:
  532. # No material found, deserialize this material later and add it
  533. to_deserialize_material = True
  534. else:
  535. material_container = materials[0]
  536. old_material_root_id = material_container.getMetaDataEntry("base_file")
  537. if not self._container_registry.isReadOnly(old_material_root_id): # Only create new materials if they are not read only.
  538. to_deserialize_material = True
  539. if self._resolve_strategies["material"] == "override":
  540. # Remove the old materials and then deserialize the one from the project
  541. root_material_id = material_container.getMetaDataEntry("base_file")
  542. material_manager.removeMaterialByRootId(root_material_id)
  543. elif self._resolve_strategies["material"] == "new":
  544. # Note that we *must* deserialize it with a new ID, as multiple containers will be
  545. # auto created & added.
  546. container_id = self.getNewId(container_id)
  547. self._old_new_materials[old_material_root_id] = container_id
  548. need_new_name = True
  549. if to_deserialize_material:
  550. material_container = xml_material_profile(container_id)
  551. material_container.deserialize(archive.open(material_container_file).read().decode("utf-8"),
  552. file_name = container_id + "." + self._material_container_suffix)
  553. if need_new_name:
  554. new_name = ContainerRegistry.getInstance().uniqueName(material_container.getName())
  555. material_container.setName(new_name)
  556. material_container.setDirty(True)
  557. self._container_registry.addContainer(material_container)
  558. Job.yieldThread()
  559. # Handle quality changes if any
  560. self._processQualityChanges(global_stack)
  561. # Prepare the machine
  562. self._applyChangesToMachine(global_stack, extruder_stack_dict)
  563. Logger.log("d", "Workspace loading is notifying rest of the code of changes...")
  564. # Actually change the active machine.
  565. #
  566. # This is scheduled for later is because it depends on the Variant/Material/Qualitiy Managers to have the latest
  567. # data, but those managers will only update upon a container/container metadata changed signal. Because this
  568. # function is running on the main thread (Qt thread), although those "changed" signals have been emitted, but
  569. # they won't take effect until this function is done.
  570. # To solve this, we schedule _updateActiveMachine() for later so it will have the latest data.
  571. self._updateActiveMachine(global_stack)
  572. # Load all the nodes / meshdata of the workspace
  573. nodes = self._3mf_mesh_reader.read(file_name)
  574. if nodes is None:
  575. nodes = []
  576. base_file_name = os.path.basename(file_name)
  577. if base_file_name.endswith(".curaproject.3mf"):
  578. base_file_name = base_file_name[:base_file_name.rfind(".curaproject.3mf")]
  579. self.setWorkspaceName(base_file_name)
  580. return nodes
  581. def _processQualityChanges(self, global_stack):
  582. if self._machine_info.quality_changes_info is None:
  583. return
  584. application = CuraApplication.getInstance()
  585. quality_manager = application.getQualityManager()
  586. # If we have custom profiles, load them
  587. quality_changes_name = self._machine_info.quality_changes_info.name
  588. if self._machine_info.quality_changes_info is not None:
  589. Logger.log("i", "Loading custom profile [%s] from project file",
  590. self._machine_info.quality_changes_info.name)
  591. # Get the correct extruder definition IDs for quality changes
  592. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch
  593. machine_definition_id_for_quality = getMachineDefinitionIDForQualitySearch(global_stack.definition)
  594. machine_definition_for_quality = self._container_registry.findDefinitionContainers(id = machine_definition_id_for_quality)[0]
  595. quality_changes_info = self._machine_info.quality_changes_info
  596. quality_changes_quality_type = quality_changes_info.global_info.parser["metadata"]["quality_type"]
  597. quality_changes_name = quality_changes_info.name
  598. create_new = self._resolve_strategies.get("quality_changes") != "override"
  599. if create_new:
  600. container_info_dict = {None: self._machine_info.quality_changes_info.global_info}
  601. container_info_dict.update(quality_changes_info.extruder_info_dict)
  602. quality_changes_name = self._container_registry.uniqueName(quality_changes_name)
  603. for position, container_info in container_info_dict.items():
  604. extruder_stack = None
  605. if position is not None:
  606. extruder_stack = global_stack.extruders[position]
  607. container = quality_manager._createQualityChanges(quality_changes_quality_type,
  608. quality_changes_name,
  609. global_stack, extruder_stack)
  610. container_info.container = container
  611. container.setDirty(True)
  612. self._container_registry.addContainer(container)
  613. Logger.log("d", "Created new quality changes container [%s]", container.getId())
  614. else:
  615. # Find the existing containers
  616. quality_changes_containers = self._container_registry.findInstanceContainers(name = quality_changes_name,
  617. type = "quality_changes")
  618. for container in quality_changes_containers:
  619. extruder_position = container.getMetaDataEntry("position")
  620. if extruder_position is None:
  621. quality_changes_info.global_info.container = container
  622. else:
  623. if extruder_position not in quality_changes_info.extruder_info_dict:
  624. quality_changes_info.extruder_info_dict[extruder_position] = ContainerInfo(None, None, None)
  625. container_info = quality_changes_info.extruder_info_dict[extruder_position]
  626. container_info.container = container
  627. # If there is no quality changes for any extruder, create one.
  628. if not quality_changes_info.extruder_info_dict:
  629. container_info = ContainerInfo(None, None, None)
  630. quality_changes_info.extruder_info_dict["0"] = container_info
  631. extruder_stack = global_stack.extruders["0"]
  632. container = quality_manager._createQualityChanges(quality_changes_quality_type, quality_changes_name,
  633. global_stack, extruder_stack)
  634. container_info.container = container
  635. container.setDirty(True)
  636. self._container_registry.addContainer(container)
  637. Logger.log("d", "Created new quality changes container [%s]", container.getId())
  638. # Clear all existing containers
  639. quality_changes_info.global_info.container.clear()
  640. for container_info in quality_changes_info.extruder_info_dict.values():
  641. container_info.container.clear()
  642. # Loop over everything and override the existing containers
  643. global_info = quality_changes_info.global_info
  644. global_info.container.clear() # Clear all
  645. for key, value in global_info.parser["values"].items():
  646. if not machine_definition_for_quality.getProperty(key, "settable_per_extruder"):
  647. global_info.container.setProperty(key, "value", value)
  648. else:
  649. quality_changes_info.extruder_info_dict["0"].container.setProperty(key, "value", value)
  650. for position, container_info in quality_changes_info.extruder_info_dict.items():
  651. if container_info.parser is None:
  652. continue
  653. if container_info.container is None:
  654. extruder_stack = global_stack.extruders[position]
  655. container = quality_manager._createQualityChanges(quality_changes_quality_type, quality_changes_name,
  656. global_stack, extruder_stack)
  657. container_info.container = container
  658. for key, value in container_info.parser["values"].items():
  659. container_info.container.setProperty(key, "value", value)
  660. self._machine_info.quality_changes_info.name = quality_changes_name
  661. def _clearStack(self, stack):
  662. application = CuraApplication.getInstance()
  663. stack.definitionChanges.clear()
  664. stack.variant = application.empty_variant_container
  665. stack.material = application.empty_material_container
  666. stack.quality = application.empty_quality_container
  667. stack.qualityChanges = application.empty_quality_changes_container
  668. stack.userChanges.clear()
  669. def _applyDefinitionChanges(self, global_stack, extruder_stack_dict):
  670. values_to_set_for_extruders = {}
  671. if self._machine_info.definition_changes_info is not None:
  672. parser = self._machine_info.definition_changes_info.parser
  673. for key, value in parser["values"].items():
  674. if global_stack.getProperty(key, "settable_per_extruder"):
  675. values_to_set_for_extruders[key] = value
  676. else:
  677. global_stack.definitionChanges.setProperty(key, "value", value)
  678. for position, extruder_stack in extruder_stack_dict.items():
  679. if position not in self._machine_info.extruder_info_dict:
  680. continue
  681. extruder_info = self._machine_info.extruder_info_dict[position]
  682. if extruder_info.definition_changes_info is None:
  683. continue
  684. parser = extruder_info.definition_changes_info.parser
  685. for key, value in values_to_set_for_extruders.items():
  686. extruder_stack.definitionChanges.setProperty(key, "value", value)
  687. if parser is not None:
  688. for key, value in parser["values"].items():
  689. extruder_stack.definitionChanges.setProperty(key, "value", value)
  690. def _applyUserChanges(self, global_stack, extruder_stack_dict):
  691. values_to_set_for_extruder_0 = {}
  692. if self._machine_info.user_changes_info is not None:
  693. parser = self._machine_info.user_changes_info.parser
  694. for key, value in parser["values"].items():
  695. if global_stack.getProperty(key, "settable_per_extruder"):
  696. values_to_set_for_extruder_0[key] = value
  697. else:
  698. global_stack.userChanges.setProperty(key, "value", value)
  699. for position, extruder_stack in extruder_stack_dict.items():
  700. if position not in self._machine_info.extruder_info_dict:
  701. continue
  702. extruder_info = self._machine_info.extruder_info_dict[position]
  703. if extruder_info.user_changes_info is not None:
  704. parser = self._machine_info.extruder_info_dict[position].user_changes_info.parser
  705. if position == "0":
  706. for key, value in values_to_set_for_extruder_0.items():
  707. extruder_stack.userChanges.setProperty(key, "value", value)
  708. if parser is not None:
  709. for key, value in parser["values"].items():
  710. extruder_stack.userChanges.setProperty(key, "value", value)
  711. def _applyVariants(self, global_stack, extruder_stack_dict):
  712. application = CuraApplication.getInstance()
  713. variant_manager = application.getVariantManager()
  714. if self._machine_info.variant_info is not None:
  715. parser = self._machine_info.variant_info.parser
  716. variant_name = parser["general"]["name"]
  717. from cura.Machines.VariantManager import VariantType
  718. variant_type = VariantType.BUILD_PLATE
  719. node = variant_manager.getVariantNode(global_stack.definition.getId(), variant_name, variant_type)
  720. if node is not None and node.getContainer() is not None:
  721. global_stack.variant = node.getContainer()
  722. for position, extruder_stack in extruder_stack_dict.items():
  723. if position not in self._machine_info.extruder_info_dict:
  724. continue
  725. extruder_info = self._machine_info.extruder_info_dict[position]
  726. if extruder_info.variant_info is None:
  727. continue
  728. parser = extruder_info.variant_info.parser
  729. variant_name = parser["general"]["name"]
  730. from cura.Machines.VariantManager import VariantType
  731. variant_type = VariantType.NOZZLE
  732. node = variant_manager.getVariantNode(global_stack.definition.getId(), variant_name, variant_type)
  733. if node is not None and node.getContainer() is not None:
  734. extruder_stack.variant = node.getContainer()
  735. def _applyMaterials(self, global_stack, extruder_stack_dict):
  736. application = CuraApplication.getInstance()
  737. material_manager = application.getMaterialManager()
  738. # Force update lookup tables first
  739. material_manager.initialize()
  740. for position, extruder_stack in extruder_stack_dict.items():
  741. if position not in self._machine_info.extruder_info_dict:
  742. continue
  743. extruder_info = self._machine_info.extruder_info_dict[position]
  744. if extruder_info.root_material_id is None:
  745. continue
  746. root_material_id = extruder_info.root_material_id
  747. root_material_id = self._old_new_materials.get(root_material_id, root_material_id)
  748. # get material diameter of this extruder
  749. machine_material_diameter = extruder_stack.materialDiameter
  750. material_node = material_manager.getMaterialNode(global_stack.definition.getId(),
  751. extruder_stack.variant.getName(),
  752. machine_material_diameter,
  753. root_material_id)
  754. if material_node is not None and material_node.getContainer() is not None:
  755. extruder_stack.material = material_node.getContainer()
  756. def _applyChangesToMachine(self, global_stack, extruder_stack_dict):
  757. # Clear all first
  758. self._clearStack(global_stack)
  759. for extruder_stack in extruder_stack_dict.values():
  760. self._clearStack(extruder_stack)
  761. self._applyDefinitionChanges(global_stack, extruder_stack_dict)
  762. self._applyUserChanges(global_stack, extruder_stack_dict)
  763. self._applyVariants(global_stack, extruder_stack_dict)
  764. self._applyMaterials(global_stack, extruder_stack_dict)
  765. # prepare the quality to select
  766. self._quality_changes_to_apply = None
  767. self._quality_type_to_apply = None
  768. if self._machine_info.quality_changes_info is not None:
  769. self._quality_changes_to_apply = self._machine_info.quality_changes_info.name
  770. else:
  771. self._quality_type_to_apply = self._machine_info.quality_type
  772. # Set enabled/disabled for extruders
  773. for position, extruder_stack in extruder_stack_dict.items():
  774. extruder_info = self._machine_info.extruder_info_dict.get(position)
  775. if not extruder_info:
  776. continue
  777. if "enabled" not in extruder_stack.getMetaData():
  778. extruder_stack.addMetaDataEntry("enabled", "True")
  779. extruder_stack.setMetaDataEntry("enabled", str(extruder_info.enabled))
  780. def _updateActiveMachine(self, global_stack):
  781. # Actually change the active machine.
  782. machine_manager = Application.getInstance().getMachineManager()
  783. material_manager = Application.getInstance().getMaterialManager()
  784. quality_manager = Application.getInstance().getQualityManager()
  785. # Force update the lookup maps first
  786. material_manager.initialize()
  787. quality_manager.initialize()
  788. machine_manager.setActiveMachine(global_stack.getId())
  789. if self._quality_changes_to_apply:
  790. quality_changes_group_dict = quality_manager.getQualityChangesGroups(global_stack)
  791. if self._quality_changes_to_apply not in quality_changes_group_dict:
  792. Logger.log("e", "Could not find quality_changes [%s]", self._quality_changes_to_apply)
  793. return
  794. quality_changes_group = quality_changes_group_dict[self._quality_changes_to_apply]
  795. machine_manager.setQualityChangesGroup(quality_changes_group, no_dialog = True)
  796. else:
  797. self._quality_type_to_apply = self._quality_type_to_apply.lower()
  798. quality_group_dict = quality_manager.getQualityGroups(global_stack)
  799. if self._quality_type_to_apply in quality_group_dict:
  800. quality_group = quality_group_dict[self._quality_type_to_apply]
  801. else:
  802. Logger.log("i", "Could not find quality type [%s], switch to default", self._quality_type_to_apply)
  803. preferred_quality_type = global_stack.getMetaDataEntry("preferred_quality_type")
  804. quality_group_dict = quality_manager.getQualityGroups(global_stack)
  805. quality_group = quality_group_dict.get(preferred_quality_type)
  806. if quality_group is None:
  807. Logger.log("e", "Could not get preferred quality type [%s]", preferred_quality_type)
  808. if quality_group is not None:
  809. machine_manager.setQualityGroup(quality_group, no_dialog = True)
  810. # Notify everything/one that is to notify about changes.
  811. global_stack.containersChanged.emit(global_stack.getTop())
  812. def _stripFileToId(self, file):
  813. mime_type = MimeTypeDatabase.getMimeTypeForFile(file)
  814. file = mime_type.stripExtension(file)
  815. return file.replace("Cura/", "")
  816. def _getXmlProfileClass(self):
  817. return self._container_registry.getContainerForMimeType(MimeTypeDatabase.getMimeType("application/x-ultimaker-material-profile"))
  818. ## Get the list of ID's of all containers in a container stack by partially parsing it's serialized data.
  819. def _getContainerIdListFromSerialized(self, serialized):
  820. parser = ConfigParser(interpolation=None, empty_lines_in_values=False)
  821. parser.read_string(serialized)
  822. container_ids = []
  823. if "containers" in parser:
  824. for index, container_id in parser.items("containers"):
  825. container_ids.append(container_id)
  826. elif parser.has_option("general", "containers"):
  827. container_string = parser["general"].get("containers", "")
  828. container_list = container_string.split(",")
  829. container_ids = [container_id for container_id in container_list if container_id != ""]
  830. # HACK: there used to be 6 containers numbering from 0 to 5 in a stack,
  831. # now we have 7: index 5 becomes "definition_changes"
  832. if len(container_ids) == 6:
  833. # Hack; We used to not save the definition changes. Fix this.
  834. container_ids.insert(5, "empty")
  835. return container_ids
  836. def _getMachineNameFromSerializedStack(self, serialized):
  837. parser = ConfigParser(interpolation=None, empty_lines_in_values=False)
  838. parser.read_string(serialized)
  839. return parser["general"].get("name", "")
  840. def _getMaterialLabelFromSerialized(self, serialized):
  841. data = ET.fromstring(serialized)
  842. metadata = data.iterfind("./um:metadata/um:name/um:label", {"um": "http://www.ultimaker.com/material"})
  843. for entry in metadata:
  844. return entry.text