mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-21 13:47:51 -06:00
Add preview image for Weedo X40
This commit is contained in:
parent
bcd11636d5
commit
9c9f1d0611
1 changed files with 80 additions and 0 deletions
80
plugins/PostProcessingPlugin/scripts/X40Snapshot.py
Normal file
80
plugins/PostProcessingPlugin/scripts/X40Snapshot.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
from UM.Logger import Logger
|
||||
from cura.Snapshot import Snapshot
|
||||
from PyQt5.QtCore import QBuffer
|
||||
|
||||
from ..Script import Script
|
||||
|
||||
|
||||
class X40Snapshot(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 _convertSnapshotToGcode(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, "JPG")
|
||||
|
||||
thumbnail_buffer.seek(0)
|
||||
gcode = []
|
||||
newline = "W221"
|
||||
byte = thumbnail_buffer.read(1)
|
||||
counter = 0
|
||||
while byte != b"":
|
||||
if counter == 0:
|
||||
gcode.append(newline)
|
||||
newline = "W220 "
|
||||
counter = 40
|
||||
newline += byte.hex()
|
||||
counter -= 1
|
||||
|
||||
byte = thumbnail_buffer.read(1)
|
||||
gcode.append(newline)
|
||||
gcode.append("W222")
|
||||
|
||||
thumbnail_buffer.close()
|
||||
return gcode
|
||||
except Exception:
|
||||
Logger.logException("w", "Failed to encode snapshot image")
|
||||
|
||||
def getSettingDataString(self):
|
||||
return """{
|
||||
"name": "Create Thumbnail for Weedo X40",
|
||||
"key": "X40Snapshot",
|
||||
"metadata": {},
|
||||
"version": 2,
|
||||
"settings":
|
||||
{}
|
||||
}"""
|
||||
|
||||
def execute(self, data):
|
||||
width = 180
|
||||
height = 180
|
||||
|
||||
snapshot = self._createSnapshot(width, height)
|
||||
if snapshot:
|
||||
snapshot_gcode = self._convertSnapshotToGcode(snapshot)
|
||||
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue