ThreeMFWorkspaceReader.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Workspace.WorkspaceReader import WorkspaceReader
  4. from UM.Application import Application
  5. from UM.Logger import Logger
  6. from UM.i18n import i18nCatalog
  7. from UM.Settings.ContainerStack import ContainerStack
  8. from UM.Settings.DefinitionContainer import DefinitionContainer
  9. from UM.Settings.InstanceContainer import InstanceContainer
  10. from UM.Settings.ContainerRegistry import ContainerRegistry
  11. from UM.MimeTypeDatabase import MimeTypeDatabase
  12. from UM.Job import Job
  13. from UM.Preferences import Preferences
  14. from .WorkspaceDialog import WorkspaceDialog
  15. import xml.etree.ElementTree as ET
  16. from cura.Settings.CuraStackBuilder import CuraStackBuilder
  17. from cura.Settings.ExtruderManager import ExtruderManager
  18. from cura.Settings.ExtruderStack import ExtruderStack
  19. from cura.Settings.GlobalStack import GlobalStack
  20. from configparser import ConfigParser
  21. import zipfile
  22. import io
  23. import configparser
  24. import os
  25. i18n_catalog = i18nCatalog("cura")
  26. ## Base implementation for reading 3MF workspace files.
  27. class ThreeMFWorkspaceReader(WorkspaceReader):
  28. def __init__(self):
  29. super().__init__()
  30. self._supported_extensions = [".3mf"]
  31. self._dialog = WorkspaceDialog()
  32. self._3mf_mesh_reader = None
  33. self._container_registry = ContainerRegistry.getInstance()
  34. # suffixes registered with the MineTypes don't start with a dot '.'
  35. self._definition_container_suffix = "." + ContainerRegistry.getMimeTypeForContainer(DefinitionContainer).preferredSuffix
  36. self._material_container_suffix = None # We have to wait until all other plugins are loaded before we can set it
  37. self._instance_container_suffix = "." + ContainerRegistry.getMimeTypeForContainer(InstanceContainer).preferredSuffix
  38. self._container_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(ContainerStack).preferredSuffix
  39. self._extruder_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(ExtruderStack).preferredSuffix
  40. self._global_stack_suffix = "." + ContainerRegistry.getMimeTypeForContainer(GlobalStack).preferredSuffix
  41. # Certain instance container types are ignored because we make the assumption that only we make those types
  42. # of containers. They are:
  43. # - quality
  44. # - variant
  45. self._ignored_instance_container_types = {"quality", "variant"}
  46. self._resolve_strategies = {}
  47. self._id_mapping = {}
  48. # In Cura 2.5 and 2.6, the empty profiles used to have those long names
  49. self._old_empty_profile_id_dict = {"empty_%s" % k: "empty" for k in ["material", "variant"]}
  50. ## Get a unique name based on the old_id. This is different from directly calling the registry in that it caches results.
  51. # This has nothing to do with speed, but with getting consistent new naming for instances & objects.
  52. def getNewId(self, old_id):
  53. if old_id not in self._id_mapping:
  54. self._id_mapping[old_id] = self._container_registry.uniqueName(old_id)
  55. return self._id_mapping[old_id]
  56. ## Separates the given file list into a list of GlobalStack files and a list of ExtruderStack files.
  57. #
  58. # In old versions, extruder stack files have the same suffix as container stack files ".stack.cfg".
  59. #
  60. def _determineGlobalAndExtruderStackFiles(self, project_file_name, file_list):
  61. archive = zipfile.ZipFile(project_file_name, "r")
  62. global_stack_file_list = [name for name in file_list if name.endswith(self._global_stack_suffix)]
  63. extruder_stack_file_list = [name for name in file_list if name.endswith(self._extruder_stack_suffix)]
  64. # separate container stack files and extruder stack files
  65. files_to_determine = [name for name in file_list if name.endswith(self._container_stack_suffix)]
  66. for file_name in files_to_determine:
  67. # FIXME: HACK!
  68. # We need to know the type of the stack file, but we can only know it if we deserialize it.
  69. # The default ContainerStack.deserialize() will connect signals, which is not desired in this case.
  70. # Since we know that the stack files are INI files, so we directly use the ConfigParser to parse them.
  71. serialized = archive.open(file_name).read().decode("utf-8")
  72. stack_config = ConfigParser()
  73. stack_config.read_string(serialized)
  74. # sanity check
  75. if not stack_config.has_option("metadata", "type"):
  76. Logger.log("e", "%s in %s doesn't seem to be valid stack file", file_name, project_file_name)
  77. continue
  78. stack_type = stack_config.get("metadata", "type")
  79. if stack_type == "extruder_train":
  80. extruder_stack_file_list.append(file_name)
  81. elif stack_type == "machine":
  82. global_stack_file_list.append(file_name)
  83. else:
  84. Logger.log("w", "Unknown container stack type '%s' from %s in %s",
  85. stack_type, file_name, project_file_name)
  86. if len(global_stack_file_list) != 1:
  87. raise RuntimeError("More than one global stack file found: [%s]" % str(global_stack_file_list))
  88. return global_stack_file_list[0], extruder_stack_file_list
  89. ## read some info so we can make decisions
  90. # \param file_name
  91. # \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.
  92. def preRead(self, file_name, show_dialog=True, *args, **kwargs):
  93. self._3mf_mesh_reader = Application.getInstance().getMeshFileHandler().getReaderForFile(file_name)
  94. if self._3mf_mesh_reader and self._3mf_mesh_reader.preRead(file_name) == WorkspaceReader.PreReadResult.accepted:
  95. pass
  96. else:
  97. Logger.log("w", "Could not find reader that was able to read the scene data for 3MF workspace")
  98. return WorkspaceReader.PreReadResult.failed
  99. machine_name = ""
  100. machine_type = ""
  101. variant_type_name = i18n_catalog.i18nc("@label", "Nozzle")
  102. # Check if there are any conflicts, so we can ask the user.
  103. archive = zipfile.ZipFile(file_name, "r")
  104. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  105. # A few lists of containers in this project files.
  106. # When loading the global stack file, it may be associated with those containers, which may or may not be
  107. # in Cura already, so we need to provide them as alternative search lists.
  108. definition_container_list = []
  109. instance_container_list = []
  110. material_container_list = []
  111. resolve_strategy_keys = ["machine", "material", "quality_changes"]
  112. self._resolve_strategies = {k: None for k in resolve_strategy_keys}
  113. containers_found_dict = {k: False for k in resolve_strategy_keys}
  114. #
  115. # Read definition containers
  116. #
  117. machine_definition_container_count = 0
  118. extruder_definition_container_count = 0
  119. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  120. for each_definition_container_file in definition_container_files:
  121. container_id = self._stripFileToId(each_definition_container_file)
  122. definitions = self._container_registry.findDefinitionContainers(id=container_id)
  123. if not definitions:
  124. definition_container = DefinitionContainer(container_id)
  125. definition_container.deserialize(archive.open(each_definition_container_file).read().decode("utf-8"))
  126. else:
  127. definition_container = definitions[0]
  128. definition_container_list.append(definition_container)
  129. definition_container_type = definition_container.getMetaDataEntry("type")
  130. if definition_container_type == "machine":
  131. machine_type = definition_container.getName()
  132. variant_type_name = definition_container.getMetaDataEntry("variants_name", variant_type_name)
  133. machine_definition_container_count += 1
  134. elif definition_container_type == "extruder":
  135. extruder_definition_container_count += 1
  136. else:
  137. Logger.log("w", "Unknown definition container type %s for %s",
  138. definition_container_type, each_definition_container_file)
  139. Job.yieldThread()
  140. # sanity check
  141. if machine_definition_container_count != 1:
  142. msg = "Expecting one machine definition container but got %s" % machine_definition_container_count
  143. Logger.log("e", msg)
  144. raise RuntimeError(msg)
  145. material_labels = []
  146. material_conflict = False
  147. xml_material_profile = self._getXmlProfileClass()
  148. if self._material_container_suffix is None:
  149. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).preferredSuffix
  150. if xml_material_profile:
  151. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  152. for material_container_file in material_container_files:
  153. container_id = self._stripFileToId(material_container_file)
  154. materials = self._container_registry.findInstanceContainers(id=container_id)
  155. material_labels.append(self._getMaterialLabelFromSerialized(archive.open(material_container_file).read().decode("utf-8")))
  156. if materials:
  157. containers_found_dict["material"] = True
  158. if not materials[0].isReadOnly(): # Only non readonly materials can be in conflict
  159. material_conflict = True
  160. Job.yieldThread()
  161. # Check if any quality_changes instance container is in conflict.
  162. instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)]
  163. quality_name = ""
  164. quality_type = ""
  165. num_settings_overriden_by_quality_changes = 0 # How many settings are changed by the quality changes
  166. num_settings_overriden_by_definition_changes = 0 # How many settings are changed by the definition changes
  167. num_user_settings = 0
  168. quality_changes_conflict = False
  169. definition_changes_conflict = False
  170. for each_instance_container_file in instance_container_files:
  171. container_id = self._stripFileToId(each_instance_container_file)
  172. instance_container = InstanceContainer(container_id)
  173. # Deserialize InstanceContainer by converting read data from bytes to string
  174. instance_container.deserialize(archive.open(each_instance_container_file).read().decode("utf-8"))
  175. instance_container_list.append(instance_container)
  176. container_type = instance_container.getMetaDataEntry("type")
  177. if container_type == "quality_changes":
  178. quality_name = instance_container.getName()
  179. num_settings_overriden_by_quality_changes += len(instance_container._instances)
  180. # Check if quality changes already exists.
  181. quality_changes = self._container_registry.findInstanceContainers(id = container_id)
  182. if quality_changes:
  183. containers_found_dict["quality_changes"] = True
  184. # Check if there really is a conflict by comparing the values
  185. if quality_changes[0] != instance_container:
  186. quality_changes_conflict = True
  187. elif container_type == "definition_changes":
  188. definition_name = instance_container.getName()
  189. num_settings_overriden_by_definition_changes += len(instance_container._instances)
  190. # Check if definition changes already exists.
  191. definition_changes = self._container_registry.findInstanceContainers(id = container_id)
  192. # Check if there is any difference the loaded settings from the project file and the settings in Cura.
  193. if definition_changes:
  194. containers_found_dict["definition_changes"] = True
  195. # Check if there really is a conflict by comparing the values
  196. if definition_changes[0] != instance_container:
  197. definition_changes_conflict = True
  198. elif container_type == "quality":
  199. if not quality_name:
  200. quality_name = instance_container.getName()
  201. elif container_type == "user":
  202. num_user_settings += len(instance_container._instances)
  203. elif container_type in self._ignored_instance_container_types:
  204. # Ignore certain instance container types
  205. Logger.log("w", "Ignoring instance container [%s] with type [%s]", container_id, container_type)
  206. continue
  207. Job.yieldThread()
  208. # Load ContainerStack files and ExtruderStack files
  209. global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles(
  210. file_name, cura_file_names)
  211. machine_conflict = False
  212. # Because there can be cases as follows:
  213. # - the global stack exists but some/all of the extruder stacks DON'T exist
  214. # - the global stack DOESN'T exist but some/all of the extruder stacks exist
  215. # To simplify this, only check if the global stack exists or not
  216. container_id = self._stripFileToId(global_stack_file)
  217. serialized = archive.open(global_stack_file).read().decode("utf-8")
  218. machine_name = self._getMachineNameFromSerializedStack(serialized)
  219. stacks = self._container_registry.findContainerStacks(id = container_id)
  220. if stacks:
  221. global_stack = stacks[0]
  222. containers_found_dict["machine"] = True
  223. # Check if there are any changes at all in any of the container stacks.
  224. id_list = self._getContainerIdListFromSerialized(serialized)
  225. for index, container_id in enumerate(id_list):
  226. # take into account the old empty container IDs
  227. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  228. if global_stack.getContainer(index).getId() != container_id:
  229. machine_conflict = True
  230. break
  231. Job.yieldThread()
  232. # if the global stack is found, we check if there are conflicts in the extruder stacks
  233. if containers_found_dict["machine"] and not machine_conflict:
  234. for extruder_stack_file in extruder_stack_files:
  235. container_id = self._stripFileToId(extruder_stack_file)
  236. serialized = archive.open(extruder_stack_file).read().decode("utf-8")
  237. parser = configparser.ConfigParser()
  238. parser.read_string(serialized)
  239. # The check should be done for the extruder stack that's associated with the existing global stack,
  240. # and those extruder stacks may have different IDs.
  241. # So we check according to the positions
  242. position = str(parser["metadata"]["position"])
  243. if position not in global_stack.extruders:
  244. # The extruder position defined in the project doesn't exist in this global stack.
  245. # We can say that it is a machine conflict, but it is very hard to override the machine in this
  246. # case because we need to override the existing extruders and add the non-existing extruders.
  247. #
  248. # HACK:
  249. # To make this simple, we simply say that there is no machine conflict and create a new machine
  250. # by default.
  251. machine_conflict = False
  252. break
  253. existing_extruder_stack = global_stack.extruders[position]
  254. # check if there are any changes at all in any of the container stacks.
  255. id_list = self._getContainerIdListFromSerialized(serialized)
  256. for index, container_id in enumerate(id_list):
  257. # take into account the old empty container IDs
  258. container_id = self._old_empty_profile_id_dict.get(container_id, container_id)
  259. if existing_extruder_stack.getContainer(index).getId() != container_id:
  260. machine_conflict = True
  261. break
  262. num_visible_settings = 0
  263. has_visible_settings_string = False
  264. try:
  265. temp_preferences = Preferences()
  266. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  267. temp_preferences.deserialize(serialized)
  268. visible_settings_string = temp_preferences.getValue("general/visible_settings")
  269. has_visible_settings_string = visible_settings_string is not None
  270. if visible_settings_string is not None:
  271. num_visible_settings = len(visible_settings_string.split(";"))
  272. active_mode = temp_preferences.getValue("cura/active_mode")
  273. if not active_mode:
  274. active_mode = Preferences.getInstance().getValue("cura/active_mode")
  275. except KeyError:
  276. # If there is no preferences file, it's not a workspace, so notify user of failure.
  277. Logger.log("w", "File %s is not a valid workspace.", file_name)
  278. return WorkspaceReader.PreReadResult.failed
  279. # In case we use preRead() to check if a file is a valid project file, we don't want to show a dialog.
  280. if not show_dialog:
  281. return WorkspaceReader.PreReadResult.accepted
  282. # prepare data for the dialog
  283. num_extruders = extruder_definition_container_count
  284. if num_extruders == 0:
  285. num_extruders = 1 # No extruder stacks found, which means there is one extruder
  286. extruders = num_extruders * [""]
  287. # Show the dialog, informing the user what is about to happen.
  288. self._dialog.setMachineConflict(machine_conflict)
  289. self._dialog.setQualityChangesConflict(quality_changes_conflict)
  290. self._dialog.setDefinitionChangesConflict(definition_changes_conflict)
  291. self._dialog.setMaterialConflict(material_conflict)
  292. self._dialog.setHasVisibleSettingsField(has_visible_settings_string)
  293. self._dialog.setNumVisibleSettings(num_visible_settings)
  294. self._dialog.setQualityName(quality_name)
  295. self._dialog.setQualityType(quality_type)
  296. self._dialog.setNumSettingsOverridenByQualityChanges(num_settings_overriden_by_quality_changes)
  297. self._dialog.setNumUserSettings(num_user_settings)
  298. self._dialog.setActiveMode(active_mode)
  299. self._dialog.setMachineName(machine_name)
  300. self._dialog.setMaterialLabels(material_labels)
  301. self._dialog.setMachineType(machine_type)
  302. self._dialog.setExtruders(extruders)
  303. self._dialog.setVariantType(variant_type_name)
  304. self._dialog.setHasObjectsOnPlate(Application.getInstance().platformActivity)
  305. self._dialog.show()
  306. # Block until the dialog is closed.
  307. self._dialog.waitForClose()
  308. if self._dialog.getResult() == {}:
  309. return WorkspaceReader.PreReadResult.cancelled
  310. self._resolve_strategies = self._dialog.getResult()
  311. #
  312. # There can be 3 resolve strategies coming from the dialog:
  313. # - new: create a new container
  314. # - override: override the existing container
  315. # - None: There is no conflict, which means containers with the same IDs may or may not be there already.
  316. # If there is an existing container, there is no conflict between them, and default to "override"
  317. # If there is no existing container, default to "new"
  318. #
  319. # Default values
  320. for key, strategy in self._resolve_strategies.items():
  321. if key not in containers_found_dict or strategy is not None:
  322. continue
  323. self._resolve_strategies[key] = "override" if containers_found_dict[key] else "new"
  324. return WorkspaceReader.PreReadResult.accepted
  325. ## Overrides an ExtruderStack in the given GlobalStack and returns the new ExtruderStack.
  326. def _overrideExtruderStack(self, global_stack, extruder_file_content):
  327. # Get extruder position first
  328. extruder_config = configparser.ConfigParser()
  329. extruder_config.read_string(extruder_file_content)
  330. if not extruder_config.has_option("metadata", "position"):
  331. msg = "Could not find 'metadata/position' in extruder stack file"
  332. Logger.log("e", "Could not find 'metadata/position' in extruder stack file")
  333. raise RuntimeError(msg)
  334. extruder_position = extruder_config.get("metadata", "position")
  335. try:
  336. extruder_stack = global_stack.extruders[extruder_position]
  337. except KeyError:
  338. Logger.log("w", "Could not find the matching extruder stack to override for position %s", extruder_position)
  339. return None
  340. # Override the given extruder stack
  341. extruder_stack.deserialize(extruder_file_content)
  342. # return the new ExtruderStack
  343. return extruder_stack
  344. ## Read the project file
  345. # Add all the definitions / materials / quality changes that do not exist yet. Then it loads
  346. # all the stacks into the container registry. In some cases it will reuse the container for the global stack.
  347. # It handles old style project files containing .stack.cfg as well as new style project files
  348. # containing global.cfg / extruder.cfg
  349. #
  350. # \param file_name
  351. def read(self, file_name):
  352. archive = zipfile.ZipFile(file_name, "r")
  353. cura_file_names = [name for name in archive.namelist() if name.startswith("Cura/")]
  354. # Create a shadow copy of the preferences (we don't want all of the preferences, but we do want to re-use its
  355. # parsing code.
  356. temp_preferences = Preferences()
  357. serialized = archive.open("Cura/preferences.cfg").read().decode("utf-8")
  358. temp_preferences.deserialize(serialized)
  359. # Copy a number of settings from the temp preferences to the global
  360. global_preferences = Preferences.getInstance()
  361. visible_settings = temp_preferences.getValue("general/visible_settings")
  362. if visible_settings is None:
  363. Logger.log("w", "Workspace did not contain visible settings. Leaving visibility unchanged")
  364. else:
  365. global_preferences.setValue("general/visible_settings", visible_settings)
  366. categories_expanded = temp_preferences.getValue("cura/categories_expanded")
  367. if categories_expanded is None:
  368. Logger.log("w", "Workspace did not contain expanded categories. Leaving them unchanged")
  369. else:
  370. global_preferences.setValue("cura/categories_expanded", categories_expanded)
  371. Application.getInstance().expandedCategoriesChanged.emit() # Notify the GUI of the change
  372. self._id_mapping = {}
  373. # We don't add containers right away, but wait right until right before the stack serialization.
  374. # We do this so that if something goes wrong, it's easier to clean up.
  375. containers_to_add = []
  376. global_stack_file, extruder_stack_files = self._determineGlobalAndExtruderStackFiles(file_name, cura_file_names)
  377. global_stack = None
  378. extruder_stacks = []
  379. extruder_stacks_added = []
  380. container_stacks_added = []
  381. containers_added = []
  382. global_stack_id_original = self._stripFileToId(global_stack_file)
  383. global_stack_id_new = global_stack_id_original
  384. global_stack_name_original = self._getMachineNameFromSerializedStack(archive.open(global_stack_file).read().decode("utf-8"))
  385. global_stack_name_new = global_stack_name_original
  386. global_stack_need_rename = False
  387. extruder_stack_id_map = {} # new and old ExtruderStack IDs map
  388. if self._resolve_strategies["machine"] == "new":
  389. # We need a new id if the id already exists
  390. if self._container_registry.findContainerStacks(id = global_stack_id_original):
  391. global_stack_id_new = self.getNewId(global_stack_id_original)
  392. global_stack_need_rename = True
  393. global_stack_name_new = self._container_registry.uniqueName(global_stack_name_original)
  394. for each_extruder_stack_file in extruder_stack_files:
  395. old_container_id = self._stripFileToId(each_extruder_stack_file)
  396. new_container_id = old_container_id
  397. if self._container_registry.findContainerStacks(id = old_container_id):
  398. # get a new name for this extruder
  399. new_container_id = self.getNewId(old_container_id)
  400. extruder_stack_id_map[old_container_id] = new_container_id
  401. # TODO: For the moment we use pretty naive existence checking. If the ID is the same, we assume in quite a few
  402. # TODO: cases that the container loaded is the same (most notable in materials & definitions).
  403. # TODO: It might be possible that we need to add smarter checking in the future.
  404. Logger.log("d", "Workspace loading is checking definitions...")
  405. # Get all the definition files & check if they exist. If not, add them.
  406. definition_container_files = [name for name in cura_file_names if name.endswith(self._definition_container_suffix)]
  407. for definition_container_file in definition_container_files:
  408. container_id = self._stripFileToId(definition_container_file)
  409. definitions = self._container_registry.findDefinitionContainers(id = container_id)
  410. if not definitions:
  411. definition_container = DefinitionContainer(container_id)
  412. definition_container.deserialize(archive.open(definition_container_file).read().decode("utf-8"))
  413. self._container_registry.addContainer(definition_container)
  414. Job.yieldThread()
  415. Logger.log("d", "Workspace loading is checking materials...")
  416. material_containers = []
  417. # Get all the material files and check if they exist. If not, add them.
  418. xml_material_profile = self._getXmlProfileClass()
  419. if self._material_container_suffix is None:
  420. self._material_container_suffix = ContainerRegistry.getMimeTypeForContainer(xml_material_profile).suffixes[0]
  421. if xml_material_profile:
  422. material_container_files = [name for name in cura_file_names if name.endswith(self._material_container_suffix)]
  423. for material_container_file in material_container_files:
  424. container_id = self._stripFileToId(material_container_file)
  425. materials = self._container_registry.findInstanceContainers(id = container_id)
  426. if not materials:
  427. material_container = xml_material_profile(container_id)
  428. material_container.deserialize(archive.open(material_container_file).read().decode("utf-8"))
  429. containers_to_add.append(material_container)
  430. else:
  431. material_container = materials[0]
  432. if not material_container.isReadOnly(): # Only create new materials if they are not read only.
  433. if self._resolve_strategies["material"] == "override":
  434. material_container.deserialize(archive.open(material_container_file).read().decode("utf-8"))
  435. elif self._resolve_strategies["material"] == "new":
  436. # Note that we *must* deserialize it with a new ID, as multiple containers will be
  437. # auto created & added.
  438. material_container = xml_material_profile(self.getNewId(container_id))
  439. material_container.deserialize(archive.open(material_container_file).read().decode("utf-8"))
  440. containers_to_add.append(material_container)
  441. material_containers.append(material_container)
  442. Job.yieldThread()
  443. Logger.log("d", "Workspace loading is checking instance containers...")
  444. # Get quality_changes and user profiles saved in the workspace
  445. instance_container_files = [name for name in cura_file_names if name.endswith(self._instance_container_suffix)]
  446. user_instance_containers = []
  447. quality_and_definition_changes_instance_containers = []
  448. for instance_container_file in instance_container_files:
  449. container_id = self._stripFileToId(instance_container_file)
  450. serialized = archive.open(instance_container_file).read().decode("utf-8")
  451. # HACK! we ignore "quality" and "variant" instance containers!
  452. parser = configparser.ConfigParser()
  453. parser.read_string(serialized)
  454. if not parser.has_option("metadata", "type"):
  455. Logger.log("w", "Cannot find metadata/type in %s, ignoring it", instance_container_file)
  456. continue
  457. if parser.get("metadata", "type") in self._ignored_instance_container_types:
  458. continue
  459. instance_container = InstanceContainer(container_id)
  460. # Deserialize InstanceContainer by converting read data from bytes to string
  461. instance_container.deserialize(serialized)
  462. container_type = instance_container.getMetaDataEntry("type")
  463. Job.yieldThread()
  464. #
  465. # IMPORTANT:
  466. # If an instance container (or maybe other type of container) exists, and user chooses "Create New",
  467. # we need to rename this container and all references to it, and changing those references are VERY
  468. # HARD.
  469. #
  470. if container_type in self._ignored_instance_container_types:
  471. # Ignore certain instance container types
  472. Logger.log("w", "Ignoring instance container [%s] with type [%s]", container_id, container_type)
  473. continue
  474. elif container_type == "user":
  475. # Check if quality changes already exists.
  476. user_containers = self._container_registry.findInstanceContainers(id = container_id)
  477. if not user_containers:
  478. containers_to_add.append(instance_container)
  479. else:
  480. if self._resolve_strategies["machine"] == "override" or self._resolve_strategies["machine"] is None:
  481. instance_container = user_containers[0]
  482. instance_container.deserialize(archive.open(instance_container_file).read().decode("utf-8"))
  483. instance_container.setDirty(True)
  484. elif self._resolve_strategies["machine"] == "new":
  485. # The machine is going to get a spiffy new name, so ensure that the id's of user settings match.
  486. old_extruder_id = instance_container.getMetaDataEntry("extruder", None)
  487. if old_extruder_id:
  488. new_extruder_id = extruder_stack_id_map[old_extruder_id]
  489. new_id = new_extruder_id + "_current_settings"
  490. instance_container._id = new_id
  491. instance_container.setName(new_id)
  492. instance_container.setMetaDataEntry("extruder", new_extruder_id)
  493. containers_to_add.append(instance_container)
  494. machine_id = instance_container.getMetaDataEntry("machine", None)
  495. if machine_id:
  496. new_machine_id = self.getNewId(machine_id)
  497. new_id = new_machine_id + "_current_settings"
  498. instance_container._id = new_id
  499. instance_container.setName(new_id)
  500. instance_container.setMetaDataEntry("machine", new_machine_id)
  501. containers_to_add.append(instance_container)
  502. user_instance_containers.append(instance_container)
  503. elif container_type in ("quality_changes", "definition_changes"):
  504. # Check if quality changes already exists.
  505. changes_containers = self._container_registry.findInstanceContainers(id = container_id)
  506. if not changes_containers:
  507. # no existing containers with the same ID, so we can safely add the new one
  508. containers_to_add.append(instance_container)
  509. else:
  510. # we have found existing container with the same ID, so we need to resolve according to the
  511. # selected strategy.
  512. if self._resolve_strategies[container_type] == "override":
  513. instance_container = changes_containers[0]
  514. instance_container.deserialize(archive.open(instance_container_file).read().decode("utf-8"))
  515. instance_container.setDirty(True)
  516. elif self._resolve_strategies[container_type] == "new":
  517. # TODO: how should we handle the case "new" for quality_changes and definition_changes?
  518. instance_container.setName(self._container_registry.uniqueName(instance_container.getName()))
  519. new_changes_container_id = self.getNewId(instance_container.getId())
  520. instance_container._id = new_changes_container_id
  521. # TODO: we don't know the following is correct or not, need to verify
  522. # AND REFACTOR!!!
  523. if self._resolve_strategies["machine"] == "new":
  524. # The machine is going to get a spiffy new name, so ensure that the id's of user settings match.
  525. old_extruder_id = instance_container.getMetaDataEntry("extruder", None)
  526. # Note that in case of a quality_changes extruder means the definition id of the extruder stack
  527. # For the user settings, it means the actual extruder stack id it's assigned to.
  528. if old_extruder_id and old_extruder_id in extruder_stack_id_map:
  529. new_extruder_id = extruder_stack_id_map[old_extruder_id]
  530. instance_container.setMetaDataEntry("extruder", new_extruder_id)
  531. machine_id = instance_container.getMetaDataEntry("machine", None)
  532. if machine_id:
  533. new_machine_id = self.getNewId(machine_id)
  534. instance_container.setMetaDataEntry("machine", new_machine_id)
  535. containers_to_add.append(instance_container)
  536. elif self._resolve_strategies[container_type] is None:
  537. # The ID already exists, but nothing in the values changed, so do nothing.
  538. pass
  539. quality_and_definition_changes_instance_containers.append(instance_container)
  540. else:
  541. existing_container = self._container_registry.findInstanceContainers(id = container_id)
  542. if not existing_container:
  543. containers_to_add.append(instance_container)
  544. if global_stack_need_rename:
  545. if instance_container.getMetaDataEntry("machine"):
  546. instance_container.setMetaDataEntry("machine", global_stack_id_new)
  547. # Add all the containers right before we try to add / serialize the stack
  548. for container in containers_to_add:
  549. self._container_registry.addContainer(container)
  550. container.setDirty(True)
  551. containers_added.append(container)
  552. # Get the stack(s) saved in the workspace.
  553. Logger.log("d", "Workspace loading is checking stacks containers...")
  554. # --
  555. # load global stack file
  556. try:
  557. if self._resolve_strategies["machine"] == "override":
  558. container_stacks = self._container_registry.findContainerStacks(id = global_stack_id_original)
  559. stack = container_stacks[0]
  560. # HACK
  561. # There is a machine, check if it has authentication data. If so, keep that data.
  562. network_authentication_id = container_stacks[0].getMetaDataEntry("network_authentication_id")
  563. network_authentication_key = container_stacks[0].getMetaDataEntry("network_authentication_key")
  564. container_stacks[0].deserialize(archive.open(global_stack_file).read().decode("utf-8"))
  565. if network_authentication_id:
  566. container_stacks[0].addMetaDataEntry("network_authentication_id", network_authentication_id)
  567. if network_authentication_key:
  568. container_stacks[0].addMetaDataEntry("network_authentication_key", network_authentication_key)
  569. elif self._resolve_strategies["machine"] == "new":
  570. # create a new global stack
  571. stack = GlobalStack(global_stack_id_new)
  572. # Deserialize stack by converting read data from bytes to string
  573. stack.deserialize(archive.open(global_stack_file).read().decode("utf-8"))
  574. # Ensure a unique ID and name
  575. stack._id = global_stack_id_new
  576. # Extruder stacks are "bound" to a machine. If we add the machine as a new one, the id of the
  577. # bound machine also needs to change.
  578. if stack.getMetaDataEntry("machine", None):
  579. stack.setMetaDataEntry("machine", global_stack_id_new)
  580. # Only machines need a new name, stacks may be non-unique
  581. stack.setName(global_stack_name_new)
  582. container_stacks_added.append(stack)
  583. self._container_registry.addContainer(stack)
  584. containers_added.append(stack)
  585. else:
  586. Logger.log("e", "Resolve strategy of %s for machine is not supported",
  587. self._resolve_strategies["machine"])
  588. # Create a new definition_changes container if it was empty
  589. if stack.definitionChanges == self._container_registry.getEmptyInstanceContainer():
  590. stack.setDefinitionChanges(CuraStackBuilder.createDefinitionChangesContainer(stack, stack._id + "_settings"))
  591. global_stack = stack
  592. Job.yieldThread()
  593. except:
  594. Logger.logException("w", "We failed to serialize the stack. Trying to clean up.")
  595. # Something went really wrong. Try to remove any data that we added.
  596. for container in containers_added:
  597. self._container_registry.removeContainer(container.getId())
  598. return
  599. #
  600. # Use the number of extruders from the global stack instead of the number of extruder stacks this project file
  601. # contains. The Custom FDM Printer can have multiple extruders, but the actual number of extruders in used is
  602. # defined in the global stack.
  603. # Because for single-extrusion machines, there won't be an extruder stack, so relying on the the extruder count
  604. # in the global stack can avoid problems in those cases.
  605. #
  606. extruder_count_from_global_stack = global_stack.getProperty("machine_extruder_count", "value")
  607. # --
  608. # load extruder stack files
  609. try:
  610. for extruder_stack_file in extruder_stack_files:
  611. container_id = self._stripFileToId(extruder_stack_file)
  612. extruder_file_content = archive.open(extruder_stack_file, "r").read().decode("utf-8")
  613. if self._resolve_strategies["machine"] == "override":
  614. if global_stack.getProperty("machine_extruder_count", "value") > 1:
  615. # deserialize new extruder stack over the current ones (if any)
  616. stack = self._overrideExtruderStack(global_stack, extruder_file_content)
  617. if stack is None:
  618. continue
  619. elif self._resolve_strategies["machine"] == "new":
  620. new_id = extruder_stack_id_map[container_id]
  621. stack = ExtruderStack(new_id)
  622. # HACK: the global stack can have a new name, so we need to make sure that this extruder stack
  623. # references to the new name instead of the old one. Normally, this can be done after
  624. # deserialize() by setting the metadata, but in the case of ExtruderStack, deserialize()
  625. # also does addExtruder() to its machine stack, so we have to make sure that it's pointing
  626. # to the right machine BEFORE deserialization.
  627. extruder_config = configparser.ConfigParser()
  628. extruder_config.read_string(extruder_file_content)
  629. extruder_config.set("metadata", "machine", global_stack_id_new)
  630. tmp_string_io = io.StringIO()
  631. extruder_config.write(tmp_string_io)
  632. extruder_file_content = tmp_string_io.getvalue()
  633. stack.deserialize(extruder_file_content)
  634. # Ensure a unique ID and name
  635. stack._id = new_id
  636. self._container_registry.addContainer(stack)
  637. extruder_stacks_added.append(stack)
  638. containers_added.append(stack)
  639. else:
  640. Logger.log("w", "Unknown resolve strategy: %s", self._resolve_strategies["machine"])
  641. # Create a new definition_changes container if it was empty
  642. if stack.definitionChanges == self._container_registry.getEmptyInstanceContainer():
  643. stack.setDefinitionChanges(CuraStackBuilder.createDefinitionChangesContainer(stack, stack._id + "_settings"))
  644. if global_stack.getProperty("machine_extruder_count", "value") > 1:
  645. extruder_stacks.append(stack)
  646. except:
  647. Logger.logException("w", "We failed to serialize the stack. Trying to clean up.")
  648. # Something went really wrong. Try to remove any data that we added.
  649. for container in containers_added:
  650. self._container_registry.removeContainer(container.getId())
  651. return
  652. #
  653. # Replacing the old containers if resolve is "new".
  654. # When resolve is "new", some containers will get renamed, so all the other containers that reference to those
  655. # MUST get updated too.
  656. #
  657. if self._resolve_strategies["machine"] == "new":
  658. # A new machine was made, but it was serialized with the wrong user container. Fix that now.
  659. for container in user_instance_containers:
  660. # replacing the container ID for user instance containers for the extruders
  661. extruder_id = container.getMetaDataEntry("extruder", None)
  662. if extruder_id:
  663. for extruder in extruder_stacks:
  664. if extruder.getId() == extruder_id:
  665. extruder.userChanges = container
  666. continue
  667. # replacing the container ID for user instance containers for the machine
  668. machine_id = container.getMetaDataEntry("machine", None)
  669. if machine_id:
  670. if global_stack.getId() == machine_id:
  671. global_stack.userChanges = container
  672. continue
  673. for changes_container_type in ("quality_changes", "definition_changes"):
  674. if self._resolve_strategies[changes_container_type] == "new":
  675. # Quality changes needs to get a new ID, added to registry and to the right stacks
  676. for each_changes_container in quality_and_definition_changes_instance_containers:
  677. # NOTE: The renaming and giving new IDs are possibly redundant because they are done in the
  678. # instance container loading part.
  679. new_id = each_changes_container.getId()
  680. # Find the old (current) changes container in the global stack
  681. if changes_container_type == "quality_changes":
  682. old_container = global_stack.qualityChanges
  683. elif changes_container_type == "definition_changes":
  684. old_container = global_stack.definitionChanges
  685. # sanity checks
  686. # NOTE: The following cases SHOULD NOT happen!!!!
  687. if not old_container:
  688. Logger.log("e", "We try to get [%s] from the global stack [%s] but we got None instead!",
  689. changes_container_type, global_stack.getId())
  690. # Replace the quality/definition changes container if it's in the GlobalStack
  691. # NOTE: we can get an empty container here, but the IDs will not match,
  692. # so this comparison is fine.
  693. if self._id_mapping.get(old_container.getId()) == new_id:
  694. if changes_container_type == "quality_changes":
  695. global_stack.qualityChanges = each_changes_container
  696. elif changes_container_type == "definition_changes":
  697. global_stack.definitionChanges = each_changes_container
  698. continue
  699. # Replace the quality/definition changes container if it's in one of the ExtruderStacks
  700. for each_extruder_stack in extruder_stacks:
  701. changes_container = None
  702. if changes_container_type == "quality_changes":
  703. changes_container = each_extruder_stack.qualityChanges
  704. elif changes_container_type == "definition_changes":
  705. changes_container = each_extruder_stack.definitionChanges
  706. # sanity checks
  707. # NOTE: The following cases SHOULD NOT happen!!!!
  708. if not changes_container:
  709. Logger.log("e", "We try to get [%s] from the extruder stack [%s] but we got None instead!",
  710. changes_container_type, each_extruder_stack.getId())
  711. # NOTE: we can get an empty container here, but the IDs will not match,
  712. # so this comparison is fine.
  713. if self._id_mapping.get(changes_container.getId()) == new_id:
  714. if changes_container_type == "quality_changes":
  715. each_extruder_stack.qualityChanges = each_changes_container
  716. elif changes_container_type == "definition_changes":
  717. each_extruder_stack.definitionChanges = each_changes_container
  718. if self._resolve_strategies["material"] == "new":
  719. # the actual material instance container can have an ID such as
  720. # <material>_<machine>_<variant>
  721. # which cannot be determined immediately, so here we use a HACK to find the right new material
  722. # instance ID:
  723. # - get the old material IDs for all material
  724. # - find the old material with the longest common prefix in ID, that's the old material
  725. # - update the name by replacing the old prefix with the new
  726. # - find the new material container and set it to the stack
  727. old_to_new_material_dict = {}
  728. for each_material in material_containers:
  729. # find the material's old name
  730. for old_id, new_id in self._id_mapping.items():
  731. if each_material.getId() == new_id:
  732. old_to_new_material_dict[old_id] = each_material
  733. break
  734. # replace old material in global and extruder stacks with new
  735. self._replaceStackMaterialWithNew(global_stack, old_to_new_material_dict)
  736. if extruder_stacks:
  737. for each_extruder_stack in extruder_stacks:
  738. self._replaceStackMaterialWithNew(each_extruder_stack, old_to_new_material_dict)
  739. if extruder_stacks:
  740. for stack in extruder_stacks:
  741. ExtruderManager.getInstance().registerExtruder(stack, global_stack.getId())
  742. Logger.log("d", "Workspace loading is notifying rest of the code of changes...")
  743. if self._resolve_strategies["machine"] == "new":
  744. for stack in extruder_stacks:
  745. stack.setNextStack(global_stack)
  746. stack.containersChanged.emit(stack.getTop())
  747. # Actually change the active machine.
  748. Application.getInstance().setGlobalContainerStack(global_stack)
  749. # Notify everything/one that is to notify about changes.
  750. global_stack.containersChanged.emit(global_stack.getTop())
  751. # Load all the nodes / meshdata of the workspace
  752. nodes = self._3mf_mesh_reader.read(file_name)
  753. if nodes is None:
  754. nodes = []
  755. base_file_name = os.path.basename(file_name)
  756. if base_file_name.endswith(".curaproject.3mf"):
  757. base_file_name = base_file_name[:base_file_name.rfind(".curaproject.3mf")]
  758. Application.getInstance().projectFileLoaded.emit(base_file_name)
  759. return nodes
  760. ## HACK: Replaces the material container in the given stack with a newly created material container.
  761. # This function is used when the user chooses to resolve material conflicts by creating new ones.
  762. def _replaceStackMaterialWithNew(self, stack, old_new_material_dict):
  763. # The material containers in the project file are 'parent' material such as "generic_pla",
  764. # but a material container used in a global/extruder stack is a 'child' material,
  765. # such as "generic_pla_ultimaker3_AA_0.4", which can be formalised as the following:
  766. #
  767. # <material_name>_<machine_name>_<variant_name>
  768. #
  769. # In the project loading, when a user chooses to resolve material conflicts by creating new ones,
  770. # the old 'parent' material ID and the new 'parent' material ID are known, but not the child material IDs.
  771. # In this case, the global stack and the extruder stacks need to use the newly created material, but the
  772. # material containers they use are 'child' material. So, here, we need to find the right 'child' material for
  773. # the stacks.
  774. #
  775. # This hack approach works as follows:
  776. # - No matter there is a child material or not, the actual material we are looking for has the prefix
  777. # "<material_name>", which is the old material name. For the material in a stack, we know that the new
  778. # material's ID will be "<new_material_name>_blabla..", so we just need to replace the old material ID
  779. # with the new one to get the new 'child' material.
  780. # - Because the material containers have IDs such as "m #nn", if we use simple prefix matching, there can
  781. # be a problem in the following scenario:
  782. # - there are two materials in the project file, namely "m #1" and "m #11"
  783. # - the child materials in use are for example: "m #1_um3_aa04", "m #11_um3_aa04"
  784. # - if we only check for a simple prefix match, then "m #11_um3_aa04" will match with "m #1", but they
  785. # are not the same material
  786. # To avoid this, when doing the prefix matching, we use the result with the longest mactching prefix.
  787. # find the old material ID
  788. old_material_id_in_stack = stack.material.getId()
  789. best_matching_old_material_id = None
  790. best_matching_old_meterial_prefix_length = -1
  791. for old_parent_material_id in old_new_material_dict:
  792. if len(old_parent_material_id) < best_matching_old_meterial_prefix_length:
  793. continue
  794. if len(old_parent_material_id) <= len(old_material_id_in_stack):
  795. if old_parent_material_id == old_material_id_in_stack[0:len(old_parent_material_id)]:
  796. best_matching_old_meterial_prefix_length = len(old_parent_material_id)
  797. best_matching_old_material_id = old_parent_material_id
  798. if best_matching_old_material_id is None:
  799. Logger.log("w", "Cannot find any matching old material ID for stack [%s] material [%s]. Something can go wrong",
  800. stack.getId(), old_material_id_in_stack)
  801. return
  802. # find the new material container
  803. new_material_id = old_new_material_dict[best_matching_old_material_id].getId() + old_material_id_in_stack[len(best_matching_old_material_id):]
  804. new_material_containers = self._container_registry.findInstanceContainers(id = new_material_id, type = "material")
  805. if not new_material_containers:
  806. Logger.log("e", "Cannot find new material container [%s]", new_material_id)
  807. return
  808. # replace the material in the given stack
  809. stack.material = new_material_containers[0]
  810. def _stripFileToId(self, file):
  811. mime_type = MimeTypeDatabase.getMimeTypeForFile(file)
  812. file = mime_type.stripExtension(file)
  813. return file.replace("Cura/", "")
  814. def _getXmlProfileClass(self):
  815. return self._container_registry.getContainerForMimeType(MimeTypeDatabase.getMimeType("application/x-ultimaker-material-profile"))
  816. ## Get the list of ID's of all containers in a container stack by partially parsing it's serialized data.
  817. def _getContainerIdListFromSerialized(self, serialized):
  818. parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False)
  819. parser.read_string(serialized)
  820. container_ids = []
  821. if "containers" in parser:
  822. for index, container_id in parser.items("containers"):
  823. container_ids.append(container_id)
  824. elif parser.has_option("general", "containers"):
  825. container_string = parser["general"].get("containers", "")
  826. container_list = container_string.split(",")
  827. container_ids = [container_id for container_id in container_list if container_id != ""]
  828. # HACK: there used to be 6 containers numbering from 0 to 5 in a stack,
  829. # now we have 7: index 5 becomes "definition_changes"
  830. if len(container_ids) == 6:
  831. # Hack; We used to not save the definition changes. Fix this.
  832. container_ids.insert(5, "empty")
  833. return container_ids
  834. def _getMachineNameFromSerializedStack(self, serialized):
  835. parser = configparser.ConfigParser(interpolation=None, empty_lines_in_values=False)
  836. parser.read_string(serialized)
  837. return parser["general"].get("name", "")
  838. def _getMaterialLabelFromSerialized(self, serialized):
  839. data = ET.fromstring(serialized)
  840. metadata = data.iterfind("./um:metadata/um:name/um:label", {"um": "http://www.ultimaker.com/material"})
  841. for entry in metadata:
  842. return entry.text