GCodeWriter.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.Mesh.MeshWriter import MeshWriter
  4. from UM.Logger import Logger
  5. from UM.Application import Application
  6. from UM.Settings.InstanceContainer import InstanceContainer #To create a complete setting profile to store in the g-code.
  7. import re #For escaping characters in the settings.
  8. ## Writes g-code to a file.
  9. #
  10. # While this poses as a mesh writer, what this really does is take the g-code
  11. # in the entire scene and write it to an output device. Since the g-code of a
  12. # single mesh isn't separable from the rest what with rafts and travel moves
  13. # and all, it doesn't make sense to write just a single mesh.
  14. #
  15. # So this plug-in takes the g-code that is stored in the root of the scene
  16. # node tree, adds a bit of extra information about the profiles and writes
  17. # that to the output device.
  18. class GCodeWriter(MeshWriter):
  19. ## The file format version of the serialised g-code.
  20. #
  21. # It can only read settings with the same version as the version it was
  22. # written with. If the file format is changed in a way that breaks reverse
  23. # compatibility, increment this version number!
  24. version = 2
  25. ## Dictionary that defines how characters are escaped when embedded in
  26. # g-code.
  27. #
  28. # Note that the keys of this dictionary are regex strings. The values are
  29. # not.
  30. escape_characters = {
  31. re.escape("\\"): "\\\\", # The escape character.
  32. re.escape("\n"): "\\n", # Newlines. They break off the comment.
  33. re.escape("\r"): "\\r" # Carriage return. Windows users may need this for visualisation in their editors.
  34. }
  35. def __init__(self):
  36. super().__init__()
  37. def write(self, stream, node, mode = MeshWriter.OutputMode.TextMode):
  38. if mode != MeshWriter.OutputMode.TextMode:
  39. Logger.log("e", "GCode Writer does not support non-text mode.")
  40. return False
  41. scene = Application.getInstance().getController().getScene()
  42. gcode_list = getattr(scene, "gcode_list")
  43. if gcode_list:
  44. for gcode in gcode_list:
  45. stream.write(gcode)
  46. # Serialise the current container stack and put it at the end of the file.
  47. settings = self._serialiseSettings(Application.getInstance().getGlobalContainerStack())
  48. stream.write(settings)
  49. return True
  50. return False
  51. ## Serialises a container stack to prepare it for writing at the end of the
  52. # g-code.
  53. #
  54. # The settings are serialised, and special characters (including newline)
  55. # are escaped.
  56. #
  57. # \param settings A container stack to serialise.
  58. # \return A serialised string of the settings.
  59. def _serialiseSettings(self, settings):
  60. prefix = ";SETTING_" + str(GCodeWriter.version) + " " # The prefix to put before each line.
  61. prefix_length = len(prefix)
  62. global_stack = Application.getInstance().getGlobalContainerStack()
  63. container_with_profile = global_stack.findContainer({"type": "quality"})
  64. serialized = container_with_profile.serialize()
  65. # Escape characters that have a special meaning in g-code comments.
  66. pattern = re.compile("|".join(GCodeWriter.escape_characters.keys()))
  67. # Perform the replacement with a regular expression.
  68. serialized = pattern.sub(lambda m: GCodeWriter.escape_characters[re.escape(m.group(0))], serialized)
  69. # Introduce line breaks so that each comment is no longer than 80 characters. Prepend each line with the prefix.
  70. result = ""
  71. # Lines have 80 characters, so the payload of each line is 80 - prefix.
  72. for pos in range(0, len(serialized), 80 - prefix_length):
  73. result += prefix + serialized[pos : pos + 80 - prefix_length] + "\n"
  74. serialized = result
  75. return serialized