GCodeWriter.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import re # For escaping characters in the settings.
  4. import json
  5. import copy
  6. from UM.Mesh.MeshWriter import MeshWriter
  7. from UM.Logger import Logger
  8. from UM.Application import Application
  9. from UM.Settings.InstanceContainer import InstanceContainer
  10. from cura.Machines.ContainerTree import ContainerTree
  11. from UM.i18n import i18nCatalog
  12. catalog = i18nCatalog("cura")
  13. class GCodeWriter(MeshWriter):
  14. """Writes g-code to a file.
  15. While this poses as a mesh writer, what this really does is take the g-code
  16. in the entire scene and write it to an output device. Since the g-code of a
  17. single mesh isn't separable from the rest what with rafts and travel moves
  18. and all, it doesn't make sense to write just a single mesh.
  19. So this plug-in takes the g-code that is stored in the root of the scene
  20. node tree, adds a bit of extra information about the profiles and writes
  21. that to the output device.
  22. """
  23. version = 3
  24. """The file format version of the serialised g-code.
  25. It can only read settings with the same version as the version it was
  26. written with. If the file format is changed in a way that breaks reverse
  27. compatibility, increment this version number!
  28. """
  29. escape_characters = {
  30. re.escape("\\"): "\\\\", # The escape character.
  31. re.escape("\n"): "\\n", # Newlines. They break off the comment.
  32. re.escape("\r"): "\\r" # Carriage return. Windows users may need this for visualisation in their editors.
  33. }
  34. """Dictionary that defines how characters are escaped when embedded in
  35. g-code.
  36. Note that the keys of this dictionary are regex strings. The values are
  37. not.
  38. """
  39. _setting_keyword = ";SETTING_"
  40. def __init__(self):
  41. super().__init__(add_to_recent_files = False)
  42. self._application = Application.getInstance()
  43. def write(self, stream, nodes, mode = MeshWriter.OutputMode.TextMode):
  44. """Writes the g-code for the entire scene to a stream.
  45. Note that even though the function accepts a collection of nodes, the
  46. entire scene is always written to the file since it is not possible to
  47. separate the g-code for just specific nodes.
  48. :param stream: The stream to write the g-code to.
  49. :param nodes: This is ignored.
  50. :param mode: Additional information on how to format the g-code in the
  51. file. This must always be text mode.
  52. """
  53. if mode != MeshWriter.OutputMode.TextMode:
  54. Logger.log("e", "GCodeWriter does not support non-text mode.")
  55. self.setInformation(catalog.i18nc("@error:not supported", "GCodeWriter does not support non-text mode."))
  56. return False
  57. active_build_plate = Application.getInstance().getMultiBuildPlateModel().activeBuildPlate
  58. scene = Application.getInstance().getController().getScene()
  59. if not hasattr(scene, "gcode_dict"):
  60. self.setInformation(catalog.i18nc("@warning:status", "Please prepare G-code before exporting."))
  61. return False
  62. gcode_dict = getattr(scene, "gcode_dict")
  63. gcode_list = gcode_dict.get(active_build_plate, None)
  64. if gcode_list is not None:
  65. has_settings = False
  66. for gcode in gcode_list:
  67. if gcode[:len(self._setting_keyword)] == self._setting_keyword:
  68. has_settings = True
  69. stream.write(gcode)
  70. # Serialise the current container stack and put it at the end of the file.
  71. if not has_settings:
  72. settings = self._serialiseSettings(Application.getInstance().getGlobalContainerStack())
  73. stream.write(settings)
  74. return True
  75. self.setInformation(catalog.i18nc("@warning:status", "Please prepare G-code before exporting."))
  76. return False
  77. def _createFlattenedContainerInstance(self, instance_container1, instance_container2):
  78. """Create a new container with container 2 as base and container 1 written over it."""
  79. flat_container = InstanceContainer(instance_container2.getName())
  80. # The metadata includes id, name and definition
  81. flat_container.setMetaData(copy.deepcopy(instance_container2.getMetaData()))
  82. if instance_container1.getDefinition():
  83. flat_container.setDefinition(instance_container1.getDefinition().getId())
  84. for key in instance_container2.getAllKeys():
  85. flat_container.setProperty(key, "value", instance_container2.getProperty(key, "value"))
  86. for key in instance_container1.getAllKeys():
  87. flat_container.setProperty(key, "value", instance_container1.getProperty(key, "value"))
  88. return flat_container
  89. def _serialiseSettings(self, stack):
  90. """Serialises a container stack to prepare it for writing at the end of the g-code.
  91. The settings are serialised, and special characters (including newline)
  92. are escaped.
  93. :param stack: A container stack to serialise.
  94. :return: A serialised string of the settings.
  95. """
  96. container_registry = self._application.getContainerRegistry()
  97. prefix = self._setting_keyword + str(GCodeWriter.version) + " " # The prefix to put before each line.
  98. prefix_length = len(prefix)
  99. quality_type = stack.quality.getMetaDataEntry("quality_type")
  100. container_with_profile = stack.qualityChanges
  101. machine_definition_id_for_quality = ContainerTree.getInstance().machines[stack.definition.getId()].quality_definition
  102. if container_with_profile.getId() == "empty_quality_changes":
  103. # If the global quality changes is empty, create a new one
  104. quality_name = container_registry.uniqueName(stack.quality.getName())
  105. quality_id = container_registry.uniqueName((stack.definition.getId() + "_" + quality_name).lower().replace(" ", "_"))
  106. container_with_profile = InstanceContainer(quality_id)
  107. container_with_profile.setName(quality_name)
  108. container_with_profile.setMetaDataEntry("type", "quality_changes")
  109. container_with_profile.setMetaDataEntry("quality_type", quality_type)
  110. if stack.getMetaDataEntry("position") is not None: # For extruder stacks, the quality changes should include an intent category.
  111. container_with_profile.setMetaDataEntry("intent_category", stack.intent.getMetaDataEntry("intent_category", "default"))
  112. container_with_profile.setDefinition(machine_definition_id_for_quality)
  113. container_with_profile.setMetaDataEntry("setting_version", stack.quality.getMetaDataEntry("setting_version"))
  114. flat_global_container = self._createFlattenedContainerInstance(stack.userChanges, container_with_profile)
  115. # If the quality changes is not set, we need to set type manually
  116. if flat_global_container.getMetaDataEntry("type", None) is None:
  117. flat_global_container.setMetaDataEntry("type", "quality_changes")
  118. # Ensure that quality_type is set. (Can happen if we have empty quality changes).
  119. if flat_global_container.getMetaDataEntry("quality_type", None) is None:
  120. flat_global_container.setMetaDataEntry("quality_type", stack.quality.getMetaDataEntry("quality_type", "normal"))
  121. # Get the machine definition ID for quality profiles
  122. flat_global_container.setMetaDataEntry("definition", machine_definition_id_for_quality)
  123. serialized = flat_global_container.serialize()
  124. data = {"global_quality": serialized}
  125. all_setting_keys = flat_global_container.getAllKeys()
  126. for extruder in stack.extruderList:
  127. extruder_quality = extruder.qualityChanges
  128. if extruder_quality.getId() == "empty_quality_changes":
  129. # Same story, if quality changes is empty, create a new one
  130. quality_name = container_registry.uniqueName(stack.quality.getName())
  131. quality_id = container_registry.uniqueName((stack.definition.getId() + "_" + quality_name).lower().replace(" ", "_"))
  132. extruder_quality = InstanceContainer(quality_id)
  133. extruder_quality.setName(quality_name)
  134. extruder_quality.setMetaDataEntry("type", "quality_changes")
  135. extruder_quality.setMetaDataEntry("quality_type", quality_type)
  136. extruder_quality.setDefinition(machine_definition_id_for_quality)
  137. extruder_quality.setMetaDataEntry("setting_version", stack.quality.getMetaDataEntry("setting_version"))
  138. flat_extruder_quality = self._createFlattenedContainerInstance(extruder.userChanges, extruder_quality)
  139. # If the quality changes is not set, we need to set type manually
  140. if flat_extruder_quality.getMetaDataEntry("type", None) is None:
  141. flat_extruder_quality.setMetaDataEntry("type", "quality_changes")
  142. # Ensure that extruder is set. (Can happen if we have empty quality changes).
  143. if flat_extruder_quality.getMetaDataEntry("position", None) is None:
  144. flat_extruder_quality.setMetaDataEntry("position", extruder.getMetaDataEntry("position"))
  145. # Ensure that quality_type is set. (Can happen if we have empty quality changes).
  146. if flat_extruder_quality.getMetaDataEntry("quality_type", None) is None:
  147. flat_extruder_quality.setMetaDataEntry("quality_type", extruder.quality.getMetaDataEntry("quality_type", "normal"))
  148. # Change the default definition
  149. flat_extruder_quality.setMetaDataEntry("definition", machine_definition_id_for_quality)
  150. extruder_serialized = flat_extruder_quality.serialize()
  151. data.setdefault("extruder_quality", []).append(extruder_serialized)
  152. all_setting_keys.update(flat_extruder_quality.getAllKeys())
  153. # Check if there is any profiles
  154. if not all_setting_keys:
  155. Logger.log("i", "No custom settings found, not writing settings to g-code.")
  156. return ""
  157. json_string = json.dumps(data)
  158. # Escape characters that have a special meaning in g-code comments.
  159. pattern = re.compile("|".join(GCodeWriter.escape_characters.keys()))
  160. # Perform the replacement with a regular expression.
  161. escaped_string = pattern.sub(lambda m: GCodeWriter.escape_characters[re.escape(m.group(0))], json_string)
  162. # Introduce line breaks so that each comment is no longer than 80 characters. Prepend each line with the prefix.
  163. result = ""
  164. # Lines have 80 characters, so the payload of each line is 80 - prefix.
  165. for pos in range(0, len(escaped_string), 80 - prefix_length):
  166. result += prefix + escaped_string[pos: pos + 80 - prefix_length] + "\n"
  167. return result