ThreeMFWorkspaceReader.py 54 KB

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