GCodeProfileReader.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 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, NoProfileException
  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 = serialized.strip()
  60. if not serialized:
  61. Logger.log("i", "No custom profile to import from this g-code: %s", file_name)
  62. raise NoProfileException()
  63. # serialized data can be invalid JSON
  64. try:
  65. json_data = json.loads(serialized)
  66. except Exception as e:
  67. Logger.log("e", "Could not parse serialized JSON data from g-code %s, error: %s", file_name, e)
  68. return None
  69. profiles = []
  70. global_profile = readQualityProfileFromString(json_data["global_quality"])
  71. # This is a fix for profiles created with 2.3.0 For some reason it added the "extruder" property to the
  72. # global profile.
  73. # The fix is simple and safe, as a global profile should never have the extruder entry.
  74. if global_profile.getMetaDataEntry("extruder", None) is not None:
  75. global_profile.setMetaDataEntry("extruder", None)
  76. profiles.append(global_profile)
  77. for profile_string in json_data.get("extruder_quality", []):
  78. profiles.append(readQualityProfileFromString(profile_string))
  79. return profiles
  80. ## Unescape a string which has been escaped for use in a gcode comment.
  81. #
  82. # \param string The string to unescape.
  83. # \return \type{str} The unscaped string.
  84. def unescapeGcodeComment(string):
  85. # Un-escape the serialized profile.
  86. pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))
  87. # Perform the replacement with a regular expression.
  88. return pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], string)
  89. ## Read in a profile from a serialized string.
  90. #
  91. # \param profile_string The profile data in serialized form.
  92. # \return \type{Profile} the resulting Profile object or None if it could not be read.
  93. def readQualityProfileFromString(profile_string):
  94. # Create an empty profile - the id and name will be changed by the ContainerRegistry
  95. profile = InstanceContainer("")
  96. try:
  97. profile.deserialize(profile_string)
  98. except Exception as e: # Not a valid g-code file.
  99. Logger.log("e", "Unable to serialise the profile: %s", str(e))
  100. return None
  101. return profile