Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
macdylan committed Dec 13, 2020
1 parent c34bbee commit d06bd28
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
# Installation
1. 从 Cura 的 Help 菜单下打开 Show Configuration Folder,进入 plugins 文件夹
2. 推荐使用 `git clone https://github.com/macdylan/Snapmaker2Plugin.git` 进行安装,或
3. 下载 release 的 zip 包,解压到该文件夹
3. 下载 [release](https://github.com/macdylan/Snapmaker2Plugin/releases) 的 zip 包,解压到该文件夹
4. 重启 Cura

# Usage
- 对模型切片后,将在 Save to File 按钮的位置出现设备选择菜单

![](_snapshots/sendto.png)
![](_snapshots/sendto.png)

- 如果不出现这个菜单,可按照如下方法排查:
⚠️ 如果不出现这个菜单,可按照如下方法排查:
1. 检查电脑的防火墙,是否阻止了 Cura 访问局域网(win10 默认会阻止)
2. 等待 5-10 秒,Cura 会持续查找局域网内所有兼容设备并自动显示
3. 重启 Snapmaker 2 并等待联网,因为它的应答服务可能挂了
Expand All @@ -25,14 +25,15 @@
- 在 Snapmaker 2 触摸屏 WiFi Connection Request 中点击 Yes
- 回到 Cura 点击 Continue 按钮,等待文件发送完成

![](_snapshots/screen_auth.png)

![](_snapshots/sending.png)
![](_snapshots/screen_auth.png)

- 在 Snapmaker 2 触摸屏确认进行打印

![](_snapshots/preview.jpg)
![](_snapshots/preview.jpg)

- 你也可以使用 Save to File,将文件保存为 Snapmaker G-code file(*.gcode) 格式

![](_snapshots/savetofile.png)
![](_snapshots/savetofile.png)

---
<sup>Make Something Wonderful</sup>
7 changes: 3 additions & 4 deletions SM2GCodeWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def _createSnapshot(self, *args):
# must be called from the main thread because of OpenGL
Logger.log("d", "Creating thumbnail image...")
try:
self._snapshot = Snapshot.snapshot(width=300, height=300)
# self._snapshot = snapshot.convertToFormat(QImage.Format_Grayscale8)
self._snapshot = Snapshot.snapshot(width=300, height=300).convertToFormat(QImage.Format_Indexed8)
except Exception:
Logger.logException("w", "Failed to create snapshot image")
self._snapshot = None # Failing to create thumbnail should not fail creation of UFP
Expand Down Expand Up @@ -128,7 +127,7 @@ def write(self, stream, nodes, mode = MeshWriter.OutputMode.TextMode):
thumbnail_buffer = QBuffer()
thumbnail_buffer.open(QBuffer.ReadWrite)
thumbnail_image = self._snapshot
thumbnail_image.save(thumbnail_buffer, "JPEG", 45)
thumbnail_image.save(thumbnail_buffer, "PNG")
base64_bytes = base64.b64encode(thumbnail_buffer.data())
thumbnail_buffer.close()
else:
Expand Down Expand Up @@ -156,7 +155,7 @@ def write(self, stream, nodes, mode = MeshWriter.OutputMode.TextMode):
stream.write(header_buffer[2]) # Filament used
stream.write(header_buffer[3]) # Layer height
stream.write("\n\n;header_type: 3dp\n")
stream.write(";thumbnail: data:image/jpeg;base64,")
stream.write(";thumbnail: data:image/png;base64,")
stream.write(base64_bytes.decode("ascii"))
stream.write("\n")
stream.write(";file_total_lines: %s\n" % model_line_count)
Expand Down
52 changes: 39 additions & 13 deletions SM2OutputDeviceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import socket
import requests
from io import StringIO
from typing import cast

from cura.CuraApplication import CuraApplication
from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
Expand All @@ -11,9 +10,8 @@
from UM.Signal import Signal
from UM.Logger import Logger
from UM.Message import Message
from UM.FileHandler.WriteFileJob import WriteFileJob
from UM.OutputDevice.OutputDevicePlugin import OutputDevicePlugin
from UM.Mesh.MeshWriter import MeshWriter
from UM.PluginRegistry import PluginRegistry

from .SM2GCodeWriter import SM2GCodeWriter

Expand Down Expand Up @@ -109,12 +107,15 @@ def __init__(self, device_id, address, properties={}):
self._api_prefix = ":8080/api/v1"
self._auth_token = ""
self._gcode_stream = StringIO()
self._writing = False

self._setInterface()

self._authentication_state = AuthState.NotAuthenticated
self.authenticationStateChanged.connect(self._onAuthenticationStateChanged)

self.connectionStateChanged.connect(self._onConnectionStateChanged)

self._progress = PrintJobUploadProgressMessage()
self._need_auth = PrintJobNeedAuthMessage(self)

Expand All @@ -125,6 +126,10 @@ def _setInterface(self):
self.setDescription("Send to {}".format(self._id))
self.setConnectionText("Connected to {}".format(self._id))

def _onConnectionStateChanged(self):
if self.connectionState == ConnectionState.Busy:
Message(title="Unable to upload", text="{} is busy".format(self._id)).show()

def _setAuthState(self, state):
self._authentication_state = state
self.authenticationStateChanged.emit()
Expand All @@ -139,17 +144,24 @@ def _onAuthenticationStateChanged(self):
self._need_auth.hide()

def requestWrite(self, nodes, file_name=None, limit_mimetypes=False, file_handler=None, filter_by_machine=False, **kwargs) -> None:
if self._progress.visible:
if self._progress.visible or self._writing:
return

self.writeStarted.emit(self)
self._writing = True

self._gcode_stream = StringIO()
writer = SM2GCodeWriter()
if not writer.write(self._gcode_stream, None):
Logger.log("e", "GCodeWriter failed: %s", writer.getInformation())
return
job = WriteFileJob(SM2GCodeWriter(), self._gcode_stream, nodes, SM2GCodeWriter.OutputMode.TextMode)
job.finished.connect(self._onWriteJobFinished)

message = Message(title="Preparing for upload", progress=-1, lifetime=0, dismissable=False, use_inactivity_timer=False)
message.show()

job.setMessage(message)
job.start()

def _onWriteJobFinished(self, job):
self._writing = False
self._startUpload()

def connect(self) -> str:
Expand All @@ -172,6 +184,15 @@ def check_status(self):
conn = requests.get("http://" + self._address + self._api_prefix + "/status", params={"token": self._auth_token})
Logger.log("d", "check_status: %s", conn.status_code)
if conn.status_code == 200:
resp = conn.json()
status = resp.get("status", "UNKNOWN")
Logger.log("d", "Printer status is %s" % status)
if status == "IDLE":
self.setConnectionState(ConnectionState.Connected)
elif status in ("RUNNING", "PAUSED", "STOPPED"):
self.setConnectionState(ConnectionState.Busy)
else:
self.setConnectionState(ConnectionState.Error)
self._setAuthState(AuthState.Authenticated)

if conn.status_code == 401:
Expand All @@ -184,21 +205,26 @@ def check_status(self):
self._setAuthState(AuthState.NotAuthenticated)

def _startUpload(self):
name = CuraApplication.getInstance().getPrintInformation().jobName.strip()
if name is "":
name = "untitled_print"
file_name = "%s.gcode" % name

self._auth_token = self.connect()
Logger.log("d", "Token: %s", self._auth_token)
if not self._auth_token:
return

self.check_status()

if self.connectionState != ConnectionState.Connected:
return

if self.authenticationState != AuthState.Authenticated:
return

self._progress.show()

name = CuraApplication.getInstance().getPrintInformation().jobName.strip()
if name is "":
name = "untitled_print"
file_name = "%s.gcode" % name

parts = [
self._createFormPart("name=token", self._auth_token.encode()),
self._createFormPart("name=file; filename=\"{}\"".format(file_name), self._gcode_stream.getvalue().encode())
Expand Down
Binary file removed _snapshots/sending.png
Binary file not shown.

0 comments on commit d06bd28

Please sign in to comment.