GCodeProfileReader.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. import re #Regular expressions for parsing escape characters in the settings.
  4. import json
  5. from UM.Settings.InstanceContainer import InstanceContainer
  6. from UM.Logger import Logger
  7. from UM.i18n import i18nCatalog
  8. catalog = i18nCatalog("cura")
  9. from cura.ProfileReader import ProfileReader
  10. ## A class that reads profile data from g-code files.
  11. #
  12. # It reads the profile data from g-code files and stores it in a new profile.
  13. # This class currently does not process the rest of the g-code in any way.
  14. class GCodeProfileReader(ProfileReader):
  15. ## The file format version of the serialized g-code.
  16. #
  17. # It can only read settings with the same version as the version it was
  18. # written with. If the file format is changed in a way that breaks reverse
  19. # compatibility, increment this version number!
  20. version = 3
  21. ## Dictionary that defines how characters are escaped when embedded in
  22. # g-code.
  23. #
  24. # Note that the keys of this dictionary are regex strings. The values are
  25. # not.
  26. escape_characters = {
  27. re.escape("\\\\"): "\\", #The escape character.
  28. re.escape("\\n"): "\n", #Newlines. They break off the comment.
  29. re.escape("\\r"): "\r" #Carriage return. Windows users may need this for visualisation in their editors.
  30. }
  31. ## Initialises the g-code reader as a profile reader.
  32. def __init__(self):
  33. super().__init__()
  34. ## Reads a g-code file, loading the profile from it.
  35. #
  36. # \param file_name The name of the file to read the profile from.
  37. # \return The profile that was in the specified file, if any. If the
  38. # specified file was no g-code or contained no parsable profile, \code
  39. # None \endcode is returned.
  40. def read(self, file_name):
  41. if file_name.split(".")[-1] != "gcode":
  42. return None
  43. prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
  44. prefix_length = len(prefix)
  45. # Loading all settings from the file.
  46. # They are all at the end, but Python has no reverse seek any more since Python3.
  47. # TODO: Consider moving settings to the start?
  48. serialized = "" # Will be filled with the serialized profile.
  49. try:
  50. with open(file_name, "r") as f:
  51. for line in f:
  52. if line.startswith(prefix):
  53. # Remove the prefix and the newline from the line and add it to the rest.
  54. serialized += line[prefix_length : -1]
  55. except IOError as e:
  56. Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
  57. return None
  58. serialized = unescapeGcodeComment(serialized)
  59. # serialized data can be invalid JSON
  60. try:
  61. json_data = json.loads(serialized)
  62. except Exception as e:
  63. Logger.log("e", "Could not parse serialized JSON data from GCode %s, error: %s", file_name, e)
  64. return None
  65. profiles = []
  66. global_profile = readQualityProfileFromString(json_data["global_quality"])
  67. # This is a fix for profiles created with 2.3.0 For some reason it added the "extruder" property to the
  68. # global profile.
  69. # The fix is simple and safe, as a global profile should never have the extruder entry.
  70. if global_profile.getMetaDataEntry("extruder", None) is not None:
  71. global_profile.setMetaDataEntry("extruder", None)
  72. profiles.append(global_profile)
  73. for profile_string in json_data.get("extruder_quality", []):
  74. profiles.append(readQualityProfileFromString(profile_string))
  75. return profiles
  76. ## Unescape a string which has been escaped for use in a gcode comment.
  77. #
  78. # \param string The string to unescape.
  79. # \return \type{str} The unscaped string.
  80. def unescapeGcodeComment(string):
  81. # Un-escape the serialized profile.
  82. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
  83. # Perform the replacement with a regular expression.
  84. return pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], string)
  85. ## Read in a profile from a serialized string.
  86. #
  87. # \param profile_string The profile data in serialized form.
  88. # \return \type{Profile} the resulting Profile object or None if it could not be read.
  89. def readQualityProfileFromString(profile_string):
  90. # Create an empty profile - the id and name will be changed by the ContainerRegistry
  91. profile = InstanceContainer("")
  92. try:
  93. profile.deserialize(profile_string)
  94. except Exception as e: # Not a valid g-code file.
  95. Logger.log("e", "Unable to serialise the profile: %s", str(e))
  96. return None
  97. return profile