Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGhostkeeper <rubend@tutanota.com>2021-02-04 17:19:07 +0300
committerGhostkeeper <rubend@tutanota.com>2021-02-04 17:19:07 +0300
commit5138466feac79c18ad509ec717e8cbf5176440dd (patch)
tree4fb7474f5cbabe7dd473ea279255ece4594cd8e6
parentb30bacc9f82040cdeed5a4943e00a8c8b61b746c (diff)
parent4f8c4be34e7adfa53981f24cf953ccd83c38c860 (diff)
Merge branch 'feature/thumbnail' of https://github.com/Gravedigger7789/Cura into Gravedigger7789-feature/thumbnail
-rw-r--r--plugins/PostProcessingPlugin/scripts/CreateThumbnail.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/plugins/PostProcessingPlugin/scripts/CreateThumbnail.py b/plugins/PostProcessingPlugin/scripts/CreateThumbnail.py
new file mode 100644
index 0000000000..0ce6ac6eff
--- /dev/null
+++ b/plugins/PostProcessingPlugin/scripts/CreateThumbnail.py
@@ -0,0 +1,109 @@
+import base64
+
+from UM.Logger import Logger
+from cura.Snapshot import Snapshot
+from PyQt5.QtCore import QByteArray, QIODevice, QBuffer
+
+from ..Script import Script
+
+
+class CreateThumbnail(Script):
+ def __init__(self):
+ super().__init__()
+
+ def _createSnapshot(self, width, height):
+ Logger.log("d", "Creating thumbnail image...")
+ try:
+ return Snapshot.snapshot(width, height)
+ except Exception:
+ Logger.logException("w", "Failed to create snapshot image")
+
+ def _encodeSnapshot(self, snapshot):
+ Logger.log("d", "Encoding thumbnail image...")
+ try:
+ thumbnail_buffer = QBuffer()
+ thumbnail_buffer.open(QBuffer.ReadWrite)
+ thumbnail_image = snapshot
+ thumbnail_image.save(thumbnail_buffer, "PNG")
+ base64_bytes = base64.b64encode(thumbnail_buffer.data())
+ base64_message = base64_bytes.decode('ascii')
+ thumbnail_buffer.close()
+ return base64_message
+ except Exception:
+ Logger.logException("w", "Failed to encode snapshot image")
+
+ def _convertSnapshotToGcode(self, encoded_snapshot, width, height, chunk_size=78):
+ gcode = []
+
+ encoded_snapshot_length = len(encoded_snapshot)
+ gcode.append(";")
+ gcode.append("; thumbnail begin {} {} {}".format(
+ width, height, encoded_snapshot_length))
+
+ chunks = ["; {}".format(encoded_snapshot[i:i+chunk_size])
+ for i in range(0, len(encoded_snapshot), chunk_size)]
+ gcode.extend(chunks)
+
+ gcode.append("; thumbnail end")
+ gcode.append(";")
+ gcode.append("")
+
+ return gcode
+
+ def getSettingDataString(self):
+ return """{
+ "name": "Create Thumbnail",
+ "key": "CreateThumbnail",
+ "metadata": {},
+ "version": 2,
+ "settings":
+ {
+ "width":
+ {
+ "label": "Width",
+ "description": "Width of the generated thumbnail",
+ "unit": "px",
+ "type": "int",
+ "default_value": 32,
+ "minimum_value": "0",
+ "minimum_value_warning": "12",
+ "maximum_value_warning": "800"
+ },
+ "height":
+ {
+ "label": "Height",
+ "description": "Height of the generated thumbnail",
+ "unit": "px",
+ "type": "int",
+ "default_value": 32,
+ "minimum_value": "0",
+ "minimum_value_warning": "12",
+ "maximum_value_warning": "600"
+ }
+ }
+ }"""
+
+ def execute(self, data):
+ width = self.getSettingValueByKey("width")
+ height = self.getSettingValueByKey("height")
+
+ snapshot = self._createSnapshot(width, height)
+ if snapshot:
+ encoded_snapshot = self._encodeSnapshot(snapshot)
+ snapshot_gcode = self._convertSnapshotToGcode(
+ encoded_snapshot, width, height)
+
+ for layer in data:
+ layer_index = data.index(layer)
+ lines = data[layer_index].split("\n")
+ for line in lines:
+ if line.startswith(";Generated with Cura"):
+ line_index = lines.index(line)
+ insert_index = line_index + 1
+ lines[insert_index:insert_index] = snapshot_gcode
+ break
+
+ final_lines = "\n".join(lines)
+ data[layer_index] = final_lines
+
+ return data