Skip to content

add G-Code Thumbnail #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Install Python 3.8 by your preferred means, then run the mbotmake file in the ro
## Windows
Download the exe from [here](https://github.com/sckunkle/mbotmake/releases) and drag and drop your gcode onto the executable.

## PrusaSlicer
Change these two settings in PrusaSlicer:
* Runs mbotmake automatically after exporting G-Code.<br><strong>[Print Settings] &rarr; [Output options] &rarr; [Post-processing scripts]:</strong><br>'[path to python] [.../]mbotmake.py -prusa'
* Generates the thumbnails for the Makerbot Replicator Gen5 display.<br><strong>[Printer Settings] &rarr; [General] &rarr; [G-code thumbnails]:</strong><br>
'55x40, 110x80, 320x200'

# PLANNED FEATURES

* Create a Ultimaker Cura plugin
Expand Down
49 changes: 45 additions & 4 deletions mbotmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import copy
import math
import re
import traceback

import base64
from os import getenv

def generateMetajson(temp, vardict):
meta = """
Expand Down Expand Up @@ -50,6 +51,34 @@ def generateCommand(function, metadata, parameters, tags):
])


def generateThumbnails(filename, temp):
tnNames = []
file = open(filename, 'r')
line = file.readline()
if "PrusaSlicer" not in line: # only tested on Prusa Slicer
return tnNames;
while True:
line = file.readline()
if "thumbnail begin" in line:
thumbnailMeta = line.split(' ')
thumbnailSize = thumbnailMeta[3]

line = file.readline()
thumbnail_data = ""
while "thumbnail end" not in line and line:
thumbnail_data = thumbnail_data + line.strip("; \n")
line = file.readline()
decoded_data = base64.b64decode(thumbnail_data)
thumbnailFileName = '{}/thumbnail_' + thumbnailSize + '.png'
tnNames.append(thumbnailFileName)
with open(thumbnailFileName.format(temp), 'wb') as thumbnailFile:
thumbnailFile.write(decoded_data)
if not line:
break;
file.close();
return tnNames


def computeTime(prev, current):
if [current['x'], current['y'], current['z']] == [prev['x'], prev['y'], prev['z']] and current['a'] != prev['a']:
# retraction takes time as well, add it in
Expand Down Expand Up @@ -225,24 +254,36 @@ def createToolpath(filename, temp):
return printersettings


def packageMBotFile(filename, temp):
def packageMBotFile(filename, temp, tnNames):
with zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_DEFLATED) as mbotfile:
for tn in tnNames:
mbotfile.write(tn.format(temp), arcname=tn.strip("{}/"))
mbotfile.write('{}/meta.json'.format(temp), arcname='meta.json')
mbotfile.write('{}/print.jsontoolpath'.format(temp), arcname='print.jsontoolpath')
return


def main(argv):
try:
prusaScript = False
for gcode in argv[1:]:
temp = tempfile.mkdtemp()
output = gcode.replace('.gcode', '.makerbot')
if "-prusa" in gcode: # use "-prusa" as argument in PrusaSlicer Post-Processing-Script
prusaScript = True
continue
if prusaScript:
output = str(getenv('SLIC3R_PP_OUTPUT_NAME')).replace('.gcode', '.makerbot')
else:
output = gcode.replace('.gcode', '.makerbot')
print('Generating toolpath for', output)
vardict = createToolpath(gcode, temp)
print('Generating metadata for', output)
generateMetajson(temp, vardict)
print('Generating thumbnails for', output)
tnNames = generateThumbnails(gcode, temp)
print(len(tnNames), 'Thumbnail(s) generated')
print('Packaging', output)
packageMBotFile(output, temp)
packageMBotFile(output, temp, tnNames)
print(output, 'done!')
except Exception as e:
print()
Expand Down