CreateThumbnail.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import base64
  2. from UM.Logger import Logger
  3. from cura.Snapshot import Snapshot
  4. from PyQt6.QtCore import QByteArray, QIODevice, QBuffer
  5. from ..Script import Script
  6. class CreateThumbnail(Script):
  7. def __init__(self):
  8. super().__init__()
  9. def _createSnapshot(self, width, height):
  10. Logger.log("d", "Creating thumbnail image...")
  11. try:
  12. return Snapshot.snapshot(width, height)
  13. except Exception:
  14. Logger.logException("w", "Failed to create snapshot image")
  15. def _encodeSnapshot(self, snapshot):
  16. Logger.log("d", "Encoding thumbnail image...")
  17. try:
  18. thumbnail_buffer = QBuffer()
  19. thumbnail_buffer.open(QBuffer.OpenModeFlag.ReadWrite)
  20. thumbnail_image = snapshot
  21. thumbnail_image.save(thumbnail_buffer, "PNG")
  22. base64_bytes = base64.b64encode(thumbnail_buffer.data())
  23. base64_message = base64_bytes.decode('ascii')
  24. thumbnail_buffer.close()
  25. return base64_message
  26. except Exception:
  27. Logger.logException("w", "Failed to encode snapshot image")
  28. def _convertSnapshotToGcode(self, encoded_snapshot, width, height, chunk_size=78):
  29. gcode = []
  30. encoded_snapshot_length = len(encoded_snapshot)
  31. gcode.append(";")
  32. gcode.append("; thumbnail begin {} {} {}".format(
  33. width, height, encoded_snapshot_length))
  34. chunks = ["; {}".format(encoded_snapshot[i:i+chunk_size])
  35. for i in range(0, len(encoded_snapshot), chunk_size)]
  36. gcode.extend(chunks)
  37. gcode.append("; thumbnail end")
  38. gcode.append(";")
  39. gcode.append("")
  40. return gcode
  41. def getSettingDataString(self):
  42. return """{
  43. "name": "Create Thumbnail",
  44. "key": "CreateThumbnail",
  45. "metadata": {},
  46. "version": 2,
  47. "settings":
  48. {
  49. "width":
  50. {
  51. "label": "Width",
  52. "description": "Width of the generated thumbnail",
  53. "unit": "px",
  54. "type": "int",
  55. "default_value": 32,
  56. "minimum_value": "0",
  57. "minimum_value_warning": "12",
  58. "maximum_value_warning": "800"
  59. },
  60. "height":
  61. {
  62. "label": "Height",
  63. "description": "Height of the generated thumbnail",
  64. "unit": "px",
  65. "type": "int",
  66. "default_value": 32,
  67. "minimum_value": "0",
  68. "minimum_value_warning": "12",
  69. "maximum_value_warning": "600"
  70. }
  71. }
  72. }"""
  73. def execute(self, data):
  74. width = self.getSettingValueByKey("width")
  75. height = self.getSettingValueByKey("height")
  76. snapshot = self._createSnapshot(width, height)
  77. if snapshot:
  78. encoded_snapshot = self._encodeSnapshot(snapshot)
  79. snapshot_gcode = self._convertSnapshotToGcode(
  80. encoded_snapshot, width, height)
  81. for layer in data:
  82. layer_index = data.index(layer)
  83. lines = data[layer_index].split("\n")
  84. for line in lines:
  85. if line.startswith(";Generated with Cura"):
  86. line_index = lines.index(line)
  87. insert_index = line_index + 1
  88. lines[insert_index:insert_index] = snapshot_gcode
  89. break
  90. final_lines = "\n".join(lines)
  91. data[layer_index] = final_lines
  92. return data